diff --git a/lib/c2s/server.js b/lib/c2s/server.js index efb06ca..d3265f6 100644 --- a/lib/c2s/server.js +++ b/lib/c2s/server.js @@ -37,7 +37,9 @@ function C2SServer(options) { /* And now start listening to connections on the * port provided as an option. */ - this._setupAutoStart() + if (this.options.autostart !== false) + this.listen() + // TODO add reconnect-ability // Load TLS key material this._setupTls() @@ -57,17 +59,31 @@ C2SServer.prototype._setupTls = function() { this.credentials = crypto.createCredentials(details) } -C2SServer.prototype._setupAutoStart = function() { - if (this.options.autostart === false) return - var port = this.options.port || this.C2S_PORT - var bindAddress = this.options.bindAddress || '::' +C2SServer.prototype.listen = function (port, bindAddress, callback) { + if (typeof port === 'function') { + callback = port + port = bindAddress = null + } else if (typeof bindAddress === 'function') { + callback = bindAddress + bindAddress = null + } + if (callback) + this.once('online', callback) + port = port || this.options.port || this.C2S_PORT + bindAddress = bindAddress || this.options.bindAddress || '::' this.c2sPort = net.createServer(function(inStream) { this.acceptConnection(inStream) - }.bind(this)).listen(port, bindAddress) - this.c2sPort.on('close', this.emit.bind(this, 'close')) - this.c2sPort.on('error', this.emit.bind(this, 'error')) - this.c2sPort.on('end', this.emit.bind(this, 'end')) + }.bind(this)).listen(port, bindAddress, this.emit.bind(this, 'online')) + this._addConnectionListener() +} + +C2SServer.prototype._addConnectionListener = function (con) { + con = con || this.c2sPort + con.on('close', this.emit.bind(this, 'close')) + con.on('error', this.emit.bind(this, 'error')) + con.on('end', this.emit.bind(this, 'end')) } + /** * Called upon TCP connection from client. * If you want to use your own C2SStream class just overwrite server.C2SStream*/ diff --git a/lib/c2s/stream.js b/lib/c2s/stream.js index 036bdd6..b1487a2 100644 --- a/lib/c2s/stream.js +++ b/lib/c2s/stream.js @@ -49,11 +49,6 @@ util.inherits(C2SStream, Connection) C2SStream.prototype._addConnectionListeners = function(con) { con = con || this.connection - con.on('streamStart', function (streamAttrs) { - this.serverdomain = streamAttrs.to - con.streamAttrs.from = streamAttrs.to - this.startStream() - }.bind(this)) con.on('stanza', this.onStanza.bind(this)) con.on('drain', this.emit.bind(this, 'drain')) con.on('data', this.emit.bind(this, 'data')) @@ -63,6 +58,11 @@ C2SStream.prototype._addConnectionListeners = function(con) { con.on('connect', this.emit.bind(this, 'connect')) con.on('reconnect', this.emit.bind(this, 'reconnect')) con.on('disconnect', this.emit.bind(this, 'disconnect')) + con.on('streamStart', function (streamAttrs) { + this.serverdomain = streamAttrs.to + con.streamAttrs.from = streamAttrs.to + this.startStream() + }.bind(this)) } C2SStream.prototype.send = function (stanza) { @@ -150,7 +150,6 @@ C2SStream.prototype.onStanza = function (stanza) { ) this.send(toSend) this.connection.setSecure(this.server.credentials, true) - // FIXME: stop/startParser after 'secure' } else if (stanza.is('auth', NS_XMPP_SASL) || stanza.is('response', NS_XMPP_SASL)) { this.onAuth(stanza) } else if (stanza.is('iq') && stanza.getChild('query', NS_REGISTER)) { diff --git a/lib/server.js b/lib/server.js index 19e61c7..4cae5e3 100644 --- a/lib/server.js +++ b/lib/server.js @@ -190,7 +190,6 @@ exports.OutgoingServer = function(srcDomain, destDomain, credentials) { }) this.listen({socket: SRV.connect({ - connection: this, services: ['_xmpp-server._tcp', '_jabber._tcp'], domain: destDomain, defaultPort: 5269 diff --git a/test/sasl-test.js b/test/sasl-test.js index d3c44ec..7d29233 100644 --- a/test/sasl-test.js +++ b/test/sasl-test.js @@ -89,11 +89,11 @@ function startServer(mechanism) { return c2s } -function registerHandler(cl) { +function createClient(opts) { - cl.on( - 'stanza', - function(stanza) { + var cl = new Client(opts) + + cl.on('stanza', function(stanza) { if (stanza.is('message') && // Important: never reply to errors! (stanza.attrs.type !== 'error')) { @@ -108,6 +108,8 @@ function registerHandler(cl) { } } ) + + return cl } describe('SASL', function() { @@ -120,19 +122,16 @@ describe('SASL', function() { }) after(function(done) { - c2s.shutdown() - done() + c2s.shutdown(done) }) it('should accept plain authentication', function(done) { - var cl = new Client({ + var cl = createClient({ jid: user.jid, password: user.password, preferred: Plain.id }) - registerHandler(cl) - cl.on('online', function() { done() }) @@ -144,13 +143,11 @@ describe('SASL', function() { }) it('should not accept plain authentication', function(done) { - var cl = new Client({ + var cl = createClient({ jid: user.jid, password: 'secretsecret' }) - registerHandler(cl) - cl.on('online', function() { done('user is not valid') }) @@ -171,8 +168,7 @@ describe('SASL', function() { }) after(function(done) { - c2s.shutdown() - done() + c2s.shutdown(done) }) /* @@ -181,15 +177,13 @@ describe('SASL', function() { */ it('should accept google authentication', function(done) { /*jshint camelcase: false */ - var gtalk = new Client({ + var gtalk = createClient({ jid: 'me@gmail.com', oauth2_token: 'xxxx.xxxxxxxxxxx', // from OAuth2 oauth2_auth: 'http://www.google.com/talk/protocol/auth', host: 'localhost' }) - registerHandler(gtalk) - gtalk.on('online', function() { done() }) @@ -210,20 +204,17 @@ describe('SASL', function() { }) after(function(done) { - c2s.shutdown() - done() + c2s.shutdown(done) }) it('should accept digest md5 authentication', function(done) { - var cl = new Client({ + var cl = createClient({ jid: user.jid, password: user.password, preferred: 'DIGEST-MD5' }) - registerHandler(cl) - cl.on('online', function() { done() }) @@ -292,18 +283,15 @@ describe('SASL', function() { }) after(function(done) { - c2s.shutdown() - done() + c2s.shutdown(done) }) it('should accept anonymous authentication', function(done) { - var cl = new Client({ + var cl = createClient({ jid: '@localhost', preferred: Anonymous.id }) - registerHandler(cl) - cl.on('online', function(online) { online.jid.local.length.should.equal(32) online.jid.resource.length.should.equal(16)