[tor-commits] [meek/master] Wait for an incoming connection before making the request.

dcf at torproject.org dcf at torproject.org
Wed Apr 9 05:56:56 UTC 2014


commit 6fdc8521b13eb3736473d6f17c09a61350d872dc
Author: David Fifield <david at bamsoftware.com>
Date:   Sat Mar 15 10:07:25 2014 -0700

    Wait for an incoming connection before making the request.
---
 firefox/components/main.js |   97 +++++++++++++++++++++++++++-----------------
 1 file changed, 59 insertions(+), 38 deletions(-)

diff --git a/firefox/components/main.js b/firefox/components/main.js
index 24c2abd..9db0e7e 100644
--- a/firefox/components/main.js
+++ b/firefox/components/main.js
@@ -1,6 +1,3 @@
-const FRONT_URL = "https://www.google.com/";
-const HOST = "meek-reflect.appspot.com";
-
 // https://developer.mozilla.org/en-US/docs/How_to_Build_an_XPCOM_Component_in_Javascript#Using_XPCOMUtils
 // https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/XPCOMUtils.jsm
 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
@@ -8,56 +5,80 @@ Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 function MeekHTTPHelper() {
     this.wrappedJSObject = this;
 
+    const LOCAL_PORT = 7000;
+
     // Create a "direct" nsIProxyInfo that bypasses the default proxy.
     // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIProtocolProxyService
     var pps = Components.classes["@mozilla.org/network/protocol-proxy-service;1"]
         .getService(Components.interfaces.nsIProtocolProxyService);
     // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIProxyInfo
-    var proxy = pps.newProxyInfo("direct", "", 0, 0, 0xffffffff, null);
+    this.directProxyInfo = pps.newProxyInfo("direct", "", 0, 0, 0xffffffff, null);
 
     // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIIOService
-    var ioService = Components.classes["@mozilla.org/network/io-service;1"]
+    this.ioService = Components.classes["@mozilla.org/network/io-service;1"]
         .getService(Components.interfaces.nsIIOService);
-    var httpProtocolHandler = ioService.getProtocolHandler("http")
+    this.httpProtocolHandler = this.ioService.getProtocolHandler("http")
         .QueryInterface(Components.interfaces.nsIHttpProtocolHandler);
-    var uri = ioService.newURI(FRONT_URL, null, null);
-    // Construct an HTTP channel with the proxy bypass.
-    // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIHttpChannel
-    var channel = httpProtocolHandler.newProxiedChannel(uri, proxy, 0, null)
-        .QueryInterface(Components.interfaces.nsIHttpChannel);
-    // Set the host we really want.
-    channel.setRequestHeader("Host", HOST, false);
-    channel.redirectionLimit = 0;
-    // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIUploadChannel
-    // channel.requestMethod = "POST";
-
-    // https://developer.mozilla.org/en-US/docs/Creating_Sandboxed_HTTP_Connections
-    function StreamListener() {
-        this.onStartRequest = function(aRequest, aContext) {
-            dump("onStartRequest\n");
-        };
-        this.onStopRequest = function(aRequest, aContext, aStatus) {
-            dump("onStopRequest\n");
-        };
-        this.onDataAvailable = function(aRequest, aContext, aStream, aSourceOffset, aLength) {
-            dump("onDataAvailable\n");
-            var a = new Uint8Array(aLength);
-            var input = Components.classes["@mozilla.org/binaryinputstream;1"]
-                .createInstance(Components.interfaces.nsIBinaryInputStream);
-            input.setInputStream(aStream);
-            input.readByteArray(aLength, a);
-            dump(aLength + ":" + a + "\n");
-        };
-    }
-
-    var listener = new StreamListener();
-    channel.asyncOpen(listener, null);
+
+    // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIServerSocket
+    var serverSocket = Components.classes["@mozilla.org/network/server-socket;1"]
+        .createInstance(Components.interfaces.nsIServerSocket);
+    serverSocket.init(LOCAL_PORT, true, -1);
+    serverSocket.asyncListen(this);
 }
 
 MeekHTTPHelper.prototype = {
     classDescription: "meek HTTP helper component",
     classID: Components.ID("{e7bc2b9c-f454-49f3-a19f-14848a4d871d}"),
     contractID: "@bamsoftware.com/meek-http-helper;1",
+
+    // nsIServerSocketListener implementation.
+    onSocketAccepted: function(aServ, aTransport) {
+        dump("onSocketAccepted host " + aTransport.host + "\n");
+
+        const FRONT_URL = "https://www.google.com/";
+        const HOST = "meek-reflect.appspot.com";
+
+        var uri = this.ioService.newURI(FRONT_URL, null, null);
+        // Construct an HTTP channel with the proxy bypass.
+        // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIHttpChannel
+        var channel = this.httpProtocolHandler.newProxiedChannel(uri, this.directProxyInfo, 0, null)
+            .QueryInterface(Components.interfaces.nsIHttpChannel);
+        // Set the host we really want.
+        channel.setRequestHeader("Host", HOST, false);
+        channel.redirectionLimit = 0;
+        // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIUploadChannel
+        // channel.requestMethod = "POST";
+
+        channel.asyncOpen(new this.httpResponseStreamListener(), null);
+    },
+    onStopListening: function(aServ, aStatus) {
+        dump("onStopListening status " + aStatus + "\n");
+    },
+};
+
+
+// https://developer.mozilla.org/en-US/docs/Creating_Sandboxed_HTTP_Connections
+MeekHTTPHelper.prototype.httpResponseStreamListener = function() {
+}
+MeekHTTPHelper.prototype.httpResponseStreamListener.prototype = {
+    // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIRequestObserver
+    onStartRequest: function(aRequest, aContext) {
+        dump("onStartRequest\n");
+    },
+    onStopRequest: function(aRequest, aContext, aStatus) {
+        dump("onStopRequest\n");
+    },
+    // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIStreamListener
+    onDataAvailable: function(aRequest, aContext, aStream, aSourceOffset, aLength) {
+        dump("onDataAvailable\n");
+        var a = new Uint8Array(aLength);
+        var input = Components.classes["@mozilla.org/binaryinputstream;1"]
+            .createInstance(Components.interfaces.nsIBinaryInputStream);
+        input.setInputStream(aStream);
+        input.readByteArray(aLength, a);
+        dump(aLength + ":" + a + "\n");
+    },
 };
 
 var NSGetFactory = XPCOMUtils.generateNSGetFactory([MeekHTTPHelper]);





More information about the tor-commits mailing list