-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binding specific ip to irc connection #141
base: master
Are you sure you want to change the base?
Changes from all commits
aca1ec7
df9a396
1ae3976
80ded75
370fd7a
a96c3cc
26bf44f
6de4913
232084d
b282546
3736169
80afa9a
360449a
adce523
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/thoj/go-ircevent" | ||
"crypto/tls" | ||
"fmt" | ||
|
||
irc "github.com/kofany/go-ircevent" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefix is not required. |
||
) | ||
|
||
const channel = "#go-eventirc-test"; | ||
const channel = "#go-eventirc-test" | ||
const serverssl = "irc.freenode.net:7000" | ||
|
||
func main() { | ||
ircnick1 := "blatiblat" | ||
irccon := irc.IRC(ircnick1, "IRCTestSSL") | ||
irccon.VerboseCallbackHandler = true | ||
irccon.Debug = true | ||
irccon.UseTLS = true | ||
irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true} | ||
irccon.AddCallback("001", func(e *irc.Event) { irccon.Join(channel) }) | ||
irccon.AddCallback("366", func(e *irc.Event) { }) | ||
err := irccon.Connect(serverssl) | ||
ircnick1 := "blatiblat" | ||
irccon := irc.IRC(ircnick1, "IRCTestSSL") | ||
irccon.VerboseCallbackHandler = true | ||
irccon.Debug = true | ||
irccon.UseTLS = true | ||
irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true} | ||
irccon.AddCallback("001", func(e *irc.Event) { irccon.Join(channel) }) | ||
irccon.AddCallback("366", func(e *irc.Event) {}) | ||
err := irccon.Connect(serverssl) | ||
if err != nil { | ||
fmt.Printf("Err %s", err ) | ||
fmt.Printf("Err %s", err) | ||
return | ||
} | ||
irccon.Loop() | ||
irccon.Loop() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/thoj/go-ircevent | ||
module github.com/kofany/go-ircevent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not change the module name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I change as I use fork in my project |
||
|
||
go 1.12 | ||
go 1.23.2 | ||
|
||
require ( | ||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q= | ||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= | ||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= | ||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= | ||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
// irc.go - corrected version | ||
// Copyright 2009 Thomas Jager <[email protected]> All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
/* | ||
This package provides an event based IRC client library. It allows to | ||
This package provides an event-based IRC client library. It allows you to | ||
register callbacks for the events you need to handle. Its features | ||
include handling standard CTCP, reconnecting on errors and detecting | ||
stones servers. | ||
include handling standard CTCP, reconnecting on errors, and detecting | ||
stone servers. | ||
Details of the IRC protocol can be found in the following RFCs: | ||
https://tools.ietf.org/html/rfc1459 | ||
https://tools.ietf.org/html/rfc2810 | ||
|
@@ -36,7 +37,7 @@ import ( | |
) | ||
|
||
const ( | ||
VERSION = "go-ircevent v2.1" | ||
VERSION = "go-ircevent v2.1+myip" | ||
) | ||
|
||
const CAP_TIMEOUT = time.Second * 15 | ||
|
@@ -65,7 +66,7 @@ func (irc *Connection) readLoop() { | |
|
||
msg, err := br.ReadString('\n') | ||
|
||
// We got past our blocking read, so bin timeout | ||
// We got past our blocking read, so clear timeout | ||
if irc.socket != nil { | ||
var zero time.Time | ||
irc.socket.SetReadDeadline(zero) | ||
|
@@ -103,13 +104,13 @@ func unescapeTagValue(value string) string { | |
return value | ||
} | ||
|
||
//Parse raw irc messages | ||
// Parse raw IRC messages | ||
func parseToEvent(msg string) (*Event, error) { | ||
msg = strings.TrimSuffix(msg, "\n") //Remove \r\n | ||
msg = strings.TrimSuffix(msg, "\n") // Remove \r\n | ||
msg = strings.TrimSuffix(msg, "\r") | ||
event := &Event{Raw: msg} | ||
if len(msg) < 5 { | ||
return nil, errors.New("Malformed msg from server") | ||
return nil, errors.New("malformed msg from server") | ||
} | ||
|
||
if msg[0] == '@' { | ||
|
@@ -125,25 +126,24 @@ func parseToEvent(msg string) (*Event, error) { | |
event.Tags[parts[0]] = unescapeTagValue(parts[1]) | ||
} | ||
} | ||
msg = msg[i+1 : len(msg)] | ||
msg = msg[i+1:] | ||
} else { | ||
return nil, errors.New("Malformed msg from server") | ||
return nil, errors.New("malformed msg from server") | ||
} | ||
} | ||
|
||
if msg[0] == ':' { | ||
if i := strings.Index(msg, " "); i > -1 { | ||
event.Source = msg[1:i] | ||
msg = msg[i+1 : len(msg)] | ||
|
||
msg = msg[i+1:] | ||
} else { | ||
return nil, errors.New("Malformed msg from server") | ||
return nil, errors.New("malformed msg from server") | ||
} | ||
|
||
if i, j := strings.Index(event.Source, "!"), strings.Index(event.Source, "@"); i > -1 && j > -1 && i < j { | ||
event.Nick = event.Source[0:i] | ||
event.User = event.Source[i+1 : j] | ||
event.Host = event.Source[j+1 : len(event.Source)] | ||
event.Host = event.Source[j+1:] | ||
} | ||
} | ||
|
||
|
@@ -176,12 +176,12 @@ func (irc *Connection) writeLoop() { | |
irc.Log.Printf("--> %s\n", strings.TrimSpace(b)) | ||
} | ||
|
||
// Set a write deadline based on the time out | ||
// Set a write deadline based on the timeout | ||
irc.socket.SetWriteDeadline(time.Now().Add(irc.Timeout)) | ||
|
||
_, err := w.Write([]byte(b)) | ||
|
||
// Past blocking write, bin timeout | ||
// Clear the write deadline | ||
var zero time.Time | ||
irc.socket.SetWriteDeadline(zero) | ||
|
||
|
@@ -202,16 +202,16 @@ func (irc *Connection) pingLoop() { | |
for { | ||
select { | ||
case <-ticker.C: | ||
//Ping if we haven't received anything from the server within the keep alive period | ||
// Ping if we haven't received anything from the server within the keep-alive period | ||
irc.lastMessageMutex.Lock() | ||
if time.Since(irc.lastMessage) >= irc.KeepAlive { | ||
irc.SendRawf("PING %d", time.Now().UnixNano()) | ||
} | ||
irc.lastMessageMutex.Unlock() | ||
case <-ticker2.C: | ||
//Ping at the ping frequency | ||
// Ping at the ping frequency | ||
irc.SendRawf("PING %d", time.Now().UnixNano()) | ||
//Try to recapture nickname if it's not as configured. | ||
// Try to recapture nickname if it's not as configured. | ||
irc.Lock() | ||
if irc.nick != irc.nickcurrent { | ||
irc.nickcurrent = irc.nick | ||
|
@@ -288,7 +288,7 @@ func (irc *Connection) Notice(target, message string) { | |
irc.pwrite <- fmt.Sprintf("NOTICE %s :%s\r\n", target, message) | ||
} | ||
|
||
// Send a formated notification to a nickname. | ||
// Send a formatted notification to a nickname. | ||
// RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.4.2 | ||
func (irc *Connection) Noticef(target, format string, a ...interface{}) { | ||
irc.Notice(target, fmt.Sprintf(format, a...)) | ||
|
@@ -311,7 +311,7 @@ func (irc *Connection) Privmsg(target, message string) { | |
irc.pwrite <- fmt.Sprintf("PRIVMSG %s :%s\r\n", target, message) | ||
} | ||
|
||
// Send formated string to specified target (channel or nickname). | ||
// Send formatted string to specified target (channel or nickname). | ||
func (irc *Connection) Privmsgf(target, format string, a ...interface{}) { | ||
irc.Privmsg(target, fmt.Sprintf(format, a...)) | ||
} | ||
|
@@ -344,7 +344,7 @@ func (irc *Connection) SendRaw(message string) { | |
irc.pwrite <- message + "\r\n" | ||
} | ||
|
||
// Send raw formated string. | ||
// Send raw formatted string. | ||
func (irc *Connection) SendRawf(format string, a ...interface{}) { | ||
irc.SendRaw(fmt.Sprintf(format, a...)) | ||
} | ||
|
@@ -397,6 +397,7 @@ func (irc *Connection) Connected() bool { | |
// stops all goroutines and then closes the socket. | ||
func (irc *Connection) Disconnect() { | ||
irc.Lock() | ||
irc.fullyConnected = false | ||
defer irc.Unlock() | ||
|
||
if irc.end != nil { | ||
|
@@ -419,6 +420,9 @@ func (irc *Connection) Disconnect() { | |
|
||
// Reconnect to a server using the current connection. | ||
func (irc *Connection) Reconnect() error { | ||
irc.Lock() | ||
irc.fullyConnected = false | ||
irc.Unlock() | ||
irc.end = make(chan struct{}) | ||
return irc.Connect(irc.Server) | ||
} | ||
|
@@ -428,10 +432,10 @@ func (irc *Connection) Reconnect() error { | |
// RFC 1459 details: https://tools.ietf.org/html/rfc1459#section-4.1 | ||
func (irc *Connection) Connect(server string) error { | ||
irc.Server = server | ||
// mark Server as stopped since there can be an error during connect | ||
// Mark Server as stopped since there can be an error during connect | ||
irc.stopped = true | ||
|
||
// make sure everything is ready for connection | ||
// Make sure everything is ready for connection | ||
if len(irc.Server) == 0 { | ||
return errors.New("empty 'server'") | ||
} | ||
|
@@ -462,7 +466,19 @@ func (irc *Connection) Connect(server string) error { | |
return errors.New("empty 'user'") | ||
} | ||
|
||
dialer := proxy.FromEnvironmentUsing(&net.Dialer{Timeout: irc.Timeout}) | ||
var localAddr net.Addr | ||
if irc.localIP != "" { | ||
localAddr = &net.TCPAddr{ | ||
IP: net.ParseIP(irc.localIP), | ||
Port: 0, | ||
} | ||
} | ||
|
||
dialer := proxy.FromEnvironmentUsing(&net.Dialer{ | ||
LocalAddr: localAddr, | ||
Timeout: irc.Timeout, | ||
}) | ||
|
||
irc.socket, err = dialer.Dial("tcp", irc.Server) | ||
if err != nil { | ||
return err | ||
|
@@ -596,7 +612,7 @@ func (irc *Connection) negotiateCaps() error { | |
remaining_caps-- | ||
} | ||
|
||
irc.pwrite <- fmt.Sprintf("CAP END\r\n") | ||
irc.pwrite <- "CAP END\r\n" | ||
|
||
return nil | ||
} | ||
|
@@ -605,7 +621,7 @@ func (irc *Connection) negotiateCaps() error { | |
// The nickname is later used to address the user. Returns nil if nick | ||
// or user are empty. | ||
func IRC(nick, user string) *Connection { | ||
// catch invalid values | ||
// Catch invalid values | ||
if len(nick) == 0 { | ||
return nil | ||
} | ||
|
@@ -614,18 +630,25 @@ func IRC(nick, user string) *Connection { | |
} | ||
|
||
irc := &Connection{ | ||
nick: nick, | ||
nickcurrent: nick, | ||
user: user, | ||
Log: log.New(os.Stdout, "", log.LstdFlags), | ||
end: make(chan struct{}), | ||
Version: VERSION, | ||
KeepAlive: 4 * time.Minute, | ||
Timeout: 1 * time.Minute, | ||
PingFreq: 15 * time.Minute, | ||
SASLMech: "PLAIN", | ||
QuitMessage: "", | ||
nick: nick, | ||
nickcurrent: nick, | ||
user: user, | ||
Log: log.New(os.Stdout, "", log.LstdFlags), | ||
end: make(chan struct{}), | ||
Version: VERSION, | ||
KeepAlive: 4 * time.Minute, | ||
Timeout: 1 * time.Minute, | ||
PingFreq: 15 * time.Minute, | ||
SASLMech: "PLAIN", | ||
QuitMessage: "", | ||
fullyConnected: false, // Initialize to false | ||
} | ||
irc.setupCallbacks() | ||
return irc | ||
} | ||
|
||
// SetLocalIP sets the local IP address to bind when connecting. | ||
// This allows the client to specify which local interface/IP to use. | ||
func (irc *Connection) SetLocalIP(ip string) { | ||
irc.localIP = ip | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefix is not required.