Skip to content
Browse files

First cut at socketserver for localhost assertion generation

  • Loading branch information...
1 parent 5593b14 commit d36bb79645d655b4bfb930af22ebc0ce55ccc259 mhanson@gmail.com committed Oct 26, 2011
Showing with 123 additions and 0 deletions.
  1. +3 −0 addons/jetpack/lib/main.js
  2. +120 −0 addons/jetpack/lib/socketserver.js
View
3 addons/jetpack/lib/main.js
@@ -407,5 +407,8 @@ function shutdown(why) {
// Let's go!
startup(addon.data.url);
+const socketserver = require("socketserver");
+socketserver.startServer();
+
// Hook up unloaders
unload.when(shutdown);
View
120 addons/jetpack/lib/socketserver.js
@@ -0,0 +1,120 @@
+const {Cc, Ci} = require("chrome");
+var pageWorkers = require("page-worker");
+
+var gIdServer;
+const PORT_RANGE_START = 7350;
+const PORT_RANGE_END = 7400;
+
+function IdentityServer() {
+
+ return this;
+}
+
+IdentityServer.prototype = {
+ start: function() {
+ if (this.serverSocket) return;
+
+ this.serverSocket = Cc["@mozilla.org/network/server-socket;1"].createInstance(Ci.nsIServerSocket);
+
+ var port = PORT_RANGE_START;
+ while (port < PORT_RANGE_END)
+ {
+ try {
+ this.serverSocket.init(port, true, 5);
+ console.log("Started identity server on port " + port);
+ break;
+ } catch (e) {
+ port++;
+ if (port == PORT_RANGE_END)
+ {
+ console.log("Unable to start identity server on port " + port + "; checking next available port");
+ }
+ else
+ {
+ console.log("Unable to start identity server on any port. Terminating.");
+ this.serverSocket = null;
+ return;
+ }
+ break;
+ }
+ }
+
+ this.serverSocket.asyncListen(this);
+ },
+
+ stop: function() {
+ if (this.serverSocket) this.serverSocket.stop();
+ this.serverSocket = null;
+ },
+
+ onSocketAccepted: function(aSocket, aTransport)
+ {
+ try {
+ console.log("ID server accepted connection from " + aTransport.port);
+ var inStreamNative = aTransport.openInputStream(aTransport.OPEN_BLOCKING, 0, 0); // XXX blocking, blech.
+ var inStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
+ inStream.init(inStreamNative);
+
+ var buffer = "";
+
+ try {
+ while (true)
+ {
+ var chunk = inStream.read(1024);
+ buffer = buffer + chunk;
+ if (chunk == null || buffer.indexOf("\r\n\r\n") >= 0) {
+ this.handleRequest(buffer, aTransport);
+ break;
+ }
+ }
+ } catch (e) {
+ console.log("ID server I/O error while reading request:" + e);
+ }
+ inStream.close();
+ } catch (e) {
+ console.log(e);
+ }
+ },
+
+ onStopListening: function(aSocket, aStatus)
+ {
+ console.log("ID server stop listening; " + aStatus);
+ },
+
+ handleRequest: function(request, aTransport)
+ {
+ var outStream = aTransport.openOutputStream(aTransport.OPEN_BLOCKING, 0, 0); // XXX blocking, blech.
+
+ var id = request.split("\r")[0];
+ // TODO sanitize ID through regex
+ try {
+ pageWorkers.Page({
+ contentURL: "https://browserid.org",
+ contentScript: "unsafeWindow.BrowserID.User.getAssertion(\"" + id + "\", " +
+ "function(res) {self.postMessage({status:\"ok\", assertion:res});}, " +
+ "function(err) {self.postMessage({status:\"error\"});})",
+ contentScriptWhen: "end",
+ onMessage: function(message) {
+ var out = JSON.stringify(message);
+ outStream.write(out, out.length);
+ outStream.close();
+ }
+ });
+ } catch (e) {
+ var out = "error: " + e;
+ outStream.write(out, out.length);
+ outStream.close();
+ }
+ }
+}
+
+function startServer() {
+ gIdServer = new IdentityServer();
+ gIdServer.start();
+}
+function stopServer() {
+
+}
+
+exports.startServer = startServer;
+exports.stopServer = stopServer;

0 comments on commit d36bb79

Please sign in to comment.
Something went wrong with that request. Please try again.