[or-cvs] [torbutton/master] Initial tor protocol component implmentation.

mikeperry at seul.org mikeperry at seul.org
Wed Sep 23 23:49:08 UTC 2009


Author: Kory Kork <kory at korykirk.com>
Date: Sat, 13 Jun 2009 20:21:38 +0000
Subject: Initial tor protocol component implmentation.
Commit: ad6d8182afb73ad3feeadf01892e377ab87a6a39

---
 src/components/tor-protocol.js          |  139 +++++++++++++++++++++++++++++++
 src/defaults/preferences/preferences.js |    2 +
 2 files changed, 141 insertions(+), 0 deletions(-)
 create mode 100644 src/components/tor-protocol.js

diff --git a/src/components/tor-protocol.js b/src/components/tor-protocol.js
new file mode 100644
index 0000000..1fdb354
--- /dev/null
+++ b/src/components/tor-protocol.js
@@ -0,0 +1,139 @@
+g// Test protocol related
+const kSCHEME = "tor";
+const kPROTOCOL_NAME = "tor";
+const kPROTOCOL_CONTRACTID = "@mozilla.org/network/protocol;1?name=" + kSCHEME;
+const kPROTOCOL_CID = Components.ID("52183e20-4d4b-11de-8a39-0800200c9a66");
+
+// Mozilla defined
+const kSIMPLEURI_CONTRACTID = "@mozilla.org/network/simple-uri;1";
+const kIOSERVICE_CONTRACTID = "@mozilla.org/network/io-service;1";
+const nsISupports = Components.interfaces.nsISupports;
+const nsIIOService = Components.interfaces.nsIIOService;
+const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler;
+const nsIURI = Components.interfaces.nsIURI;
+
+function Protocol()
+{
+}
+
+Protocol.prototype =
+{
+  QueryInterface: function(iid)
+  {
+    if (!iid.equals(nsIProtocolHandler) &&
+        !iid.equals(nsISupports))
+      throw Components.results.NS_ERROR_NO_INTERFACE;
+    return this;
+  },
+
+  scheme: kSCHEME,
+  defaultPort: -1,
+  protocolFlags: nsIProtocolHandler.URI_NORELATIVE |
+                 nsIProtocolHandler.URI_NOAUTH,
+  
+  allowPort: function(port, scheme)
+  {
+    return false;
+  },
+
+  newURI: function(spec, charset, baseURI)
+  {
+    const nsIStandardURL = Components.interfaces.nsIStandardURL;
+    var uri = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(nsIStandardURL);
+    uri.init(nsIStandardURL.URLTYPE_STANDARD, 80, spec, charset, baseURI);
+
+    return uri.QueryInterface(Components.interfaces.nsIURI);
+
+  },
+
+  newChannel: function(aURI)
+  {
+    /*The protocol has been called, therefore we want to enable tor, wait for it to activate return the new channel with the scheme of http.*/
+    var ios = Components.classes[kIOSERVICE_CONTRACTID].getService(nsIIOService);
+    var prompt = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
+                        .getService(Components.interfaces.nsIPromptService);
+    var prefs = Components.classes["@mozilla.org/preferences-service;1"]
+        .getService(Components.interfaces.nsIPrefBranch);
+    var tor_enabled = prefs.getBoolPref("extensions.torbutton.tor_enabled");
+    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
+                         .getService(Components.interfaces.nsIWindowMediator);
+    var chrome = wm.getMostRecentWindow("navigator:browser");
+    if (!ios.allowPort(aURI.port, aURI.scheme))
+      throw Components.results.NS_ERROR_FAILURE;
+    
+    if (!tor_enabled)
+    {
+      var result = prompt.confirm(null, "Allow Tor toggle?", "Do you want to enable Tor and navigate to " + aURI.spec + "?");   
+      if (!result)
+        throw Components.results.NS_ERROR_UNEXPECTED;        
+      chrome.torbutton_enable_tor(true);    
+    } 
+    
+    //if tor is turned on then, else we should throw exception of some sort.
+    tor_enabled = prefs.getBoolPref("extensions.torbutton.tor_enabled");
+    if (!tor_enabled)
+        throw Components.results.NS_ERROR_UNEXPECTED;
+    else
+    {
+        aURI.scheme = "http";    
+        return ios.newChannelFromURI(aURI);
+    }      
+  },
+}
+
+var ProtocolFactory = new Object();
+
+ProtocolFactory.createInstance = function (outer, iid)
+{
+  if (outer != null)
+    throw Components.results.NS_ERROR_NO_AGGREGATION;
+
+  if (!iid.equals(nsIProtocolHandler) &&
+      !iid.equals(nsISupports))
+    throw Components.results.NS_ERROR_NO_INTERFACE;
+
+  return new Protocol();
+}
+
+
+/**
+ * JS XPCOM component registration goop:
+ *
+ * We set ourselves up to observe the xpcom-startup category.  This provides
+ * us with a starting point.
+ */
+
+var TestModule = new Object();
+
+TestModule.registerSelf = function (compMgr, fileSpec, location, type)
+{
+  compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
+  compMgr.registerFactoryLocation(kPROTOCOL_CID,
+                                  kPROTOCOL_NAME,
+                                  kPROTOCOL_CONTRACTID,
+                                  fileSpec, 
+                                  location, 
+                                  type);
+}
+
+TestModule.getClassObject = function (compMgr, cid, iid)
+{
+  if (!cid.equals(kPROTOCOL_CID))
+    throw Components.results.NS_ERROR_NO_INTERFACE;
+
+  if (!iid.equals(Components.interfaces.nsIFactory))
+    throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
+    
+  return ProtocolFactory;
+}
+
+TestModule.canUnload = function (compMgr)
+{
+  return true;
+}
+
+function NSGetModule(compMgr, fileSpec)
+{
+  return TestModule;
+}
+
diff --git a/src/defaults/preferences/preferences.js b/src/defaults/preferences/preferences.js
index 1bd1911..75eda3e 100644
--- a/src/defaults/preferences/preferences.js
+++ b/src/defaults/preferences/preferences.js
@@ -57,6 +57,8 @@ pref("extensions.torbutton.saved.socks_port",0);
 pref("extensions.torbutton.saved.socks_remote_dns",false);
 pref("extensions.torbutton.saved.no_proxies_on","localhost, 127.0.0.1");
 
+pref("extensions.torbutton.saved.tor_protocol",false);
+
 pref("extensions.torbutton.saved.cookieLifetime",0);
 pref("extensions.torbutton.saved.full_page_plugins","");
 pref("extensions.torbutton.saved.disk_cache",true);
-- 
1.5.6.5




More information about the tor-commits mailing list