[Pkg-javascript-commits] [ltx] 124/469: router & server: cleaner approach

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:01:12 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository ltx.

commit 021506916330aa38db44b0ca9feed2fdef929875
Author: Astro <astro at spaceboyz.net>
Date:   Thu Sep 9 05:10:48 2010 +0200

    router & server: cleaner approach
---
 lib/xmpp/router.js |  21 +++----
 lib/xmpp/server.js | 162 ++++++++++++++++++++++++++---------------------------
 2 files changed, 90 insertions(+), 93 deletions(-)

diff --git a/lib/xmpp/router.js b/lib/xmpp/router.js
index c5dc329..8fac1dc 100644
--- a/lib/xmpp/router.js
+++ b/lib/xmpp/router.js
@@ -12,7 +12,7 @@ var NS_XMPP_STANZAS = 'urn:ietf:params:xml:ns:xmpp-stanzas';
 
 
 dbgStream = function(tag, stream) {
-    stream.on('data', function(data) {
+    stream.socket.on('data', function(data) {
 	console.log(tag + ' in: ' + data);
     });
     stream.on('error', function(e) {
@@ -111,8 +111,9 @@ DomainContext.prototype.getOutStream = function(destDomain) {
 	return this.s2sOut[destDomain];
     } else {
 	console.log("OUTGOING to " + destDomain);
+	var credentials = this.router.credentials[this.domain];
 	// Setup a new outgoing connection
-	var outStream = Server.makeOutgoingServer(this.domain, destDomain);
+	var outStream = new Server.OutgoingServer(this.domain, destDomain, credentials);
 	this.s2sOut[destDomain] = outStream;
 	dbgStream('outgoing', outStream);
 
@@ -159,6 +160,7 @@ DomainContext.prototype.getOutStream = function(destDomain) {
 		onStanza = function(stanza) {
 		    console.log({external:{domain:destDomain,stanza:stanza.toString()}});
 		    if (stanza.is('success', NS_XMPP_SASL)) {
+			outStream.stopParser();
 			outStream.startStream();
 			outStream.removeListener('stanza', onStanza);
 			var onStream;
@@ -416,12 +418,12 @@ Router.prototype.loadCredentials = function(domain, keyPath, certPath) {
 							  cert: cert });
 };
 
-Router.prototype.acceptConnection = function(inStream) {
+Router.prototype.acceptConnection = function(socket) {
     var self = this;
 
-    dbgStream('incoming', inStream);
+    var inStream = new Server.IncomingServer(socket, this.credentials);
     this.setupStream(inStream);
-    Server.makeIncomingServer(inStream);
+    dbgStream('incoming', inStream);
 
     // incoming server wants to verify an outgoing connection of ours
     inStream.addListener('dialbackVerify', function(from, to, id, key) {
@@ -452,12 +454,11 @@ Router.prototype.acceptConnection = function(inStream) {
 };
 
 Router.prototype.setupStream = function(stream) {
-    stream.credentials = this.credentials;
     stream.maxStanzaSize = this.maxStanzaSize;
-    StreamShaper.attach(stream, this.rateLimit);
-    stream.setKeepAlive(true, this.keepAlive);
-    IdleTimeout.attach(stream, this.streamTimeout);
-    stream.addListener('timeout', function() {
+    StreamShaper.attach(stream.socket, this.rateLimit);
+    stream.socket.setKeepAlive(true, this.keepAlive);
+    IdleTimeout.attach(stream.socket, this.streamTimeout);
+    stream.socket.addListener('timeout', function() {
 			   stream.error('connection-timeout');
 		       });
 };
diff --git a/lib/xmpp/server.js b/lib/xmpp/server.js
index 1f59619..68ba2e1 100644
--- a/lib/xmpp/server.js
+++ b/lib/xmpp/server.js
@@ -1,10 +1,11 @@
 var dns = require('dns');
 var Connection = require('./connection');
 var xml = require('./xml');
+var sys = require('sys');
+var SRV = require('./srv');
 
 var NS_SERVER = 'jabber:server';
 var NS_DIALBACK = 'jabber:server:dialback';
-var NS_XMPP_STREAMS = 'urn:ietf:params:xml:ns:xmpp-streams';
 var NS_XMPP_SASL = 'urn:ietf:params:xml:ns:xmpp-sasl';
 
 /**
@@ -14,11 +15,15 @@ var NS_XMPP_SASL = 'urn:ietf:params:xml:ns:xmpp-sasl';
  * (3) dialbackVerified(from, to, id, isValid)
  * (4) dialbackResult(from, to, isValid)
  */
-function initServer(self) {
-    self.xmlns = NS_SERVER;
-    self.xmppVersion = '1.0';
+function Server(socket) {
+    var self = this;
+    Connection.Connection.call(this, socket);
 
-    self.addListener('rawStanza', function(stanza) {
+    this.xmlns = NS_SERVER;
+    this.xmppVersion = '1.0';
+
+console.log({Server:this});
+    this.addListener('rawStanza', function(stanza) {
 	var key = stanza.getText();
 
 	if (stanza.is('result', NS_DIALBACK)) {
@@ -55,6 +60,23 @@ function initServer(self) {
 	    self.emit('stanza', stanza);
     });
 }
+sys.inherits(Server, Connection.Connection);
+
+Server.prototype.startStream = function() {
+    Connection.Connection.prototype.startStream.call(this);
+
+    var tag = "<stream:stream xmlns='" + this.xmlns +
+	"' xmlns:stream='" + Connection.NS_STREAM +
+	"' xmlns:db='" + NS_DIALBACK + "'";
+    if (this.destDomain)
+	tag += " to='" + this.destDomain + "'";
+    if (this.streamId)
+	tag += " id='" + this.streamId + "'";
+    if (this.xmppVersion)
+	tag += " version='" + this.xmppVersion + "'";
+    tag += ">";
+    this.send(tag);
+};
 
 exports.dialbackKey = function(from, to, key) {
     return new xml.Element('db:result', { to: to,
@@ -79,98 +101,69 @@ exports.dialbackResult = function(from, to, isValid) {
 					  type: isValid ? 'valid' : 'invalid' });
 };
 
-exports.makeIncomingServer = function(self) {
-    var credentials;
-    Connection.makeConnection(self);
+exports.IncomingServer = function(stream, credentials) {
+    var self = this;
+    Server.call(this, stream);
 
-    initServer(self);
-    self.startStream();
-    self.streamId = generateId();
+    this.startParser();
+    this.streamId = generateId();
 
-    self.addListener('streamStart', function(streamAttrs) {
-	// TLS cert & key for this domain
-	if (streamAttrs.to && self.credentials[streamAttrs.to])
-	    credentials = self.credentials[streamAttrs.to];
+    this.addListener('streamStart', function(streamAttrs) {
+console.log({streamAttrs:streamAttrs,credentials:credentials});
+	if (streamAttrs.to &&
+	    credentials &&
+	    credentials.hasOwnProperty(streamAttrs.to))
+	    // TLS cert & key for this domain
+	    self.credentials = credentials[streamAttrs.to];
 	// No credentials means we cannot <starttls/> on the server
 	// side. Unfortunately this is required for XMPP 1.0.
-	if (!credentials)
+	if (!self.credentials)
 	    delete self.xmppVersion;
 
-	var tag = "<stream:stream xmlns='" + self.xmlns +
-	    "' xmlns:stream='" + Connection.NS_STREAM +
-	    "' xmlns:db='" + NS_DIALBACK +
-	    "' id='" + self.streamId + "'";
-	if (self.xmppVersion)
-	    tag += " version='" + self.xmppVersion + "'";
-	tag += ">";
-	if (self.xmppVersion == '1.0') {
-	    tag += "<stream:features>";
-	    if (credentials && !self.secureEstablished)
-		tag += "<starttls xmlns='" + Connection.NS_XMPP_TLS + "'/>";
-	    tag += "</stream:features>";
+	self.startStream();
+    });
+    this.addListener('rawStanza', function(stanza) {
+	if (stanza.is('starttls', Connection.NS_XMPP_TLS)) {
+	    self.send(new xml.Element('proceed', { xmlns: Connection.NS_XMPP_TLS }));
+	    self.setSecure();
 	}
-	self.send(tag);
     });
-    self.addListener('rawStanza', function(stanza) {
-			 if (stanza.is('starttls', Connection.NS_XMPP_TLS)) {
-			     self.send(new xml.Element('proceed', { xmlns: Connection.NS_XMPP_TLS }));
-			     self.stopParser();
-			     console.log("setSecure...");
-			     self.setSecure(credentials);
-			     self.addListener('secure', function() {
-						  console.log("secure!!!");
-						  self.startParser();
-					      });
-			 }
-		     });
 
     return self;
 };
+sys.inherits(exports.IncomingServer, Server);
 
-function dnsLookup(domain, cb) {
-    // TODO: improve SRV lookups
-    dns.resolveSrv('_xmpp-server._tcp.' + domain, function(error, data) {
-	if (data && data[0])
-	    cb(data[0].name, data[0].port);
-	else
-	    dns.resolveSrv('_jabber._tcp.' + domain, function(error, data) {
-		if (data && data[0])
-		    cb(data[0].name, data[0].port);
-		else
-		    cb(domain, 5269);
-	    });
-    });
-}
-
-exports.makeOutgoingServer = function(srcDomain, destDomain) {
-    var self = new Connection.Connection();
-    initServer(self);
-    self.startStream = function() {
-	Connection.Connection.prototype.startStream.call(self);
+exports.IncomingServer.prototype.startStream = function() {
+    Server.prototype.startStream.call(this);
 
-	// For outgoing, we only need our own cert & key
-	self.credentials = self.credentials && self.credentials[srcDomain];
-	// No credentials means we cannot <starttls/> on the server
-	// side. Unfortunately this is required for XMPP 1.0.
-	if (!self.credentials)
-	    delete self.xmppVersion;
+    if (this.xmppVersion == '1.0') {
+	this.send("<stream:features>");
+	if (this.credentials && !this.socket.secureEstablished)
+	    this.send("<starttls xmlns='" + Connection.NS_XMPP_TLS + "'/>");
+	this.send("</stream:features>");
+    }
+};
 
-	var tag = "<stream:stream xmlns='" + self.xmlns +
-	    "' xmlns:stream='" + Connection.NS_STREAM +
-	    "' xmlns:db='" + NS_DIALBACK +
-	    "' to='" + destDomain + "'";
-	if (self.xmppVersion)
-	    tag += " version='" + self.xmppVersion + "'";
-	tag += ">";
-	self.send(tag);
-    };
-
-    dnsLookup(destDomain, function(host, port) {
-	self.connect(port, host);
-	self.addListener('connect', self.startStream);
+exports.OutgoingServer = function(srcDomain, destDomain, credentials) {
+    var self = this;
+    Server.call(this);
+
+    this.destDomain = destDomain;
+    // For outgoing, we only need our own cert & key
+    this.credentials = credentials;
+    // No credentials means we cannot <starttls/> on the server
+    // side. Unfortunately this is required for XMPP 1.0.
+console.log({outCreds:self.credentials});
+    if (!this.credentials)
+	delete this.xmppVersion;
+
+    this.socket.addListener('connect', function() {
+	self.startStream();
     });
-
-    self.addListener('streamStart', function(attrs) {
+    this.socket.addListener('secure', function() {
+	self.startStream();
+    });
+    this.addListener('streamStart', function(attrs) {
 			 if (attrs.version !== "1.0")
 			     // Don't wait for <stream:features/>
 			     self.emit('auth', 'dialback');
@@ -192,8 +185,11 @@ exports.makeOutgoingServer = function(srcDomain, destDomain) {
 	}
     });
 
-    return self;
+    SRV.connect(this.socket, ['_xmpp-server._tcp',
+			      '_jabber._tcp'],
+		destDomain, 5269);
 };
+sys.inherits(exports.OutgoingServer, Server);
 
 function generateId() {
     var r = new Buffer(16);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git



More information about the Pkg-javascript-commits mailing list