[tor-commits] [meek/master] Factor out some code for setting a custom proxy per-request.
dcf at torproject.org
dcf at torproject.org
Wed May 28 07:03:21 UTC 2014
commit 0567884cb38b7b514abce8bc63370517d83b0682
Author: David Fifield <david at bamsoftware.com>
Date: Sat May 24 19:15:32 2014 -0700
Factor out some code for setting a custom proxy per-request.
---
firefox/components/main.js | 42 ++++++++++++++++++++++++++++++++++++++----
1 file changed, 38 insertions(+), 4 deletions(-)
diff --git a/firefox/components/main.js b/firefox/components/main.js
index bef833f..e1284f0 100644
--- a/firefox/components/main.js
+++ b/firefox/components/main.js
@@ -97,6 +97,16 @@ MeekHTTPHelper.prototype = {
MeekHTTPHelper.LOCAL_READ_TIMEOUT = 2.0;
MeekHTTPHelper.LOCAL_WRITE_TIMEOUT = 2.0;
+// https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIProtocolProxyService
+MeekHTTPHelper.proxyProtocolService = Components.classes["@mozilla.org/network/protocol-proxy-service;1"]
+ .getService(Components.interfaces.nsIProtocolProxyService);
+
+// https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIIOService
+MeekHTTPHelper.ioService = Components.classes["@mozilla.org/network/io-service;1"]
+ .getService(Components.interfaces.nsIIOService);
+MeekHTTPHelper.httpProtocolHandler = MeekHTTPHelper.ioService.getProtocolHandler("http")
+ .QueryInterface(Components.interfaces.nsIHttpProtocolHandler);
+
// Set the transport to time out at the given absolute deadline.
MeekHTTPHelper.refreshDeadline = function(transport, deadline) {
var timeout;
@@ -116,6 +126,20 @@ MeekHTTPHelper.lookupStatus = function(status) {
return null;
};
+// Return an nsIProxyInfo according to the given specification. Returns null on
+// error.
+// https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIProxyInfo
+// The specification may look like:
+// undefined
+MeekHTTPHelper.buildProxyInfo = function(spec) {
+ // https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIProxyInfo#Constants
+ if (spec === undefined) {
+ // "direct"; i.e., no proxy. This is the default.
+ return MeekHTTPHelper.proxyProtocolService.newProxyInfo("direct", "", 0, 0, 0xffffffff, null);
+ }
+ return null;
+};
+
// LocalConnectionHandler handles each new client connection received on the
// socket opened by MeekHTTPHelper. It reads a JSON request, makes the request
// on the Internet, and writes the result back to the socket. Error handling
@@ -138,10 +162,20 @@ MeekHTTPHelper.LocalConnectionHandler.prototype = {
this.transport.close(0);
return;
}
- // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIIOService
- var ioService = Components.classes["@mozilla.org/network/io-service;1"]
- .getService(Components.interfaces.nsIIOService);
- this.channel = ioService.newChannel(req.url, null, null)
+
+ // Check what proxy to use, if any.
+ // dump("using proxy " + JSON.stringify(req.proxy) + "\n");
+ var proxyInfo = MeekHTTPHelper.buildProxyInfo(req.proxy);
+ if (proxyInfo === null) {
+ dump("can't create nsIProxyInfo from " + JSON.stringify(req.proxy) + "\n");
+ this.transport.close(0);
+ return;
+ }
+
+ // Construct an HTTP channel with the given nsIProxyInfo.
+ // https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIHttpChannel
+ var uri = MeekHTTPHelper.ioService.newURI(req.url, null, null);
+ this.channel = MeekHTTPHelper.httpProtocolHandler.newProxiedChannel(uri, proxyInfo, 0, null)
.QueryInterface(Components.interfaces.nsIHttpChannel);
if (req.header !== undefined) {
for (var key in req.header) {
More information about the tor-commits
mailing list