Skip to content

Commit

Permalink
Merge pull request #146 from revmischa/master
Browse files Browse the repository at this point in the history
Add option to specify bind address when connecting
  • Loading branch information
Chris Nehren committed Jan 8, 2015
2 parents 3a9f21f + ff13632 commit dc7d262
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Client
userName: 'nodebot',
realName: 'nodeJS IRC client',
port: 6667,
localAddress: null,
debug: false,
showErrors: false,
autoRejoin: true,
Expand All @@ -40,6 +41,8 @@ Client
If you set `selfSigned` to true SSL accepts certificates from a non trusted CA.
If you set `certExpired` to true, the bot connects even if the ssl cert has expired.

`localAddress` is the address to bind to when connecting.

`floodProtection` queues all your messages and slowly unpacks it to make sure
that we won't get kicked out because for Excess Flood. You can also use
`Client.activateFloodProtection()` to activate flood protection after
Expand Down
28 changes: 23 additions & 5 deletions lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function Client(server, nick, opt) {
userName: 'nodebot',
realName: 'nodeJS IRC client',
port: 6667,
localAddress: null,
debug: false,
showErrors: false,
autoRejoin: true,
Expand Down Expand Up @@ -631,14 +632,31 @@ Client.prototype.connect = function(retryCount, callback) { // {{{
}
var self = this;
self.chans = {};

// socket opts
var connectionOpts = {
host: self.opt.server,
port: self.opt.port
};

// local address to bind to
if (self.opt.localAddress)
connectionOpts.localAddress = self.opt.localAddress;

// try to connect to the server
if (self.opt.secure) {
var creds = self.opt.secure;
if (typeof self.opt.secure !== 'object') {
creds = {rejectUnauthorized: !self.opt.selfSigned};
creds.rejectUnauthorized = !self.opt.selfSigned;

if (typeof self.opt.secure == 'object') {
// copy "secure" opts to options passed to connect()
for (var f in self.opt.secure) {
if (creds.hasOwnProperty(f))
connectionOpts[f] = creds[f];
}
}

self.conn = tls.connect(self.opt.port, self.opt.server, creds, function() {
self.conn = tls.connect(connectionOpts, function() {
// callback called only after successful socket connection
self.conn.connected = true;
if (self.conn.authorized ||
Expand Down Expand Up @@ -668,8 +686,8 @@ Client.prototype.connect = function(retryCount, callback) { // {{{
util.log(self.conn.authorizationError);
}
});
}else {
self.conn = net.createConnection(self.opt.port, self.opt.server);
} else {
self.conn = net.createConnection(connectionOpts);
}
self.conn.requestedDisconnect = false;
self.conn.setTimeout(0);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"contributors": [
"Fionn Kelleher <me@fionn.co>",
"xAndy <xandy@hackerspace-bamberg.de>"
"xAndy <xandy@hackerspace-bamberg.de>",
"Mischa Spiegelmock <revmischa@cpan.org>"
],
"repository": {
"type": "git",
Expand Down

0 comments on commit dc7d262

Please sign in to comment.