[or-cvs] [torbutton/master] Fixed a few silly bugs, added tors-protocol.js for tors:// protocol handling.

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


Author: Kory Kork <kory at korykirk.com>
Date: Sun, 21 Jun 2009 18:28:51 +0000
Subject: Fixed a few silly bugs, added tors-protocol.js for tors:// protocol handling.
Commit: 8cd758bcdff20aebfaa414fdf857d3e4ec08afc5

svn:r19769
---
 src/chrome/content/preferences.js       |    9 ++-
 src/chrome/content/preferences.xul      |   12 ++-
 src/chrome/locale/en/torbutton.dtd      |    6 ++
 src/components/tor-protocol.js          |    2 +-
 src/components/torRefSpoofer.js         |   84 ++++++++++++++++---
 src/components/tors-protocol.js         |  139 +++++++++++++++++++++++++++++++
 src/defaults/preferences/preferences.js |    2 +
 7 files changed, 234 insertions(+), 20 deletions(-)
 create mode 100644 src/components/tors-protocol.js

diff --git a/src/chrome/content/preferences.js b/src/chrome/content/preferences.js
index 57d8c39..5c6d7b9 100644
--- a/src/chrome/content/preferences.js
+++ b/src/chrome/content/preferences.js
@@ -243,7 +243,9 @@ function torbutton_prefs_init(doc) {
                 doc.getElementById('torbutton_startPrevious');
             break;
     }
-
+    
+    doc.getElementById('torbutton_refererSpoofGroup').selectedIndex = o_torprefs.getIntPref('refererspoof');
+    
     doc.getElementById('torbutton_torSessionStore').checked = !o_torprefs.getBoolPref('notor_sessionstore');
     doc.getElementById('torbutton_nonTorSessionStore').checked = !o_torprefs.getBoolPref('nonontor_sessionstore');
 
@@ -261,7 +263,7 @@ function torbutton_prefs_init(doc) {
     doc.getElementById('torbutton_closeNonTor').checked = o_torprefs.getBoolPref('close_nontor');
     doc.getElementById('torbutton_noUpdates').checked = o_torprefs.getBoolPref('no_updates');
     doc.getElementById('torbutton_setUagent').checked = o_torprefs.getBoolPref('set_uagent');
-    doc.getElementById('torbutton_noReferer').checked = o_torprefs.getBoolPref('disable_referer');
+    doc.getElementById('torbutton_spoofRefresh').checked = o_torprefs.getBoolPref('fakerefresh');
     doc.getElementById('torbutton_spoofEnglish').checked = o_torprefs.getBoolPref('spoof_english');
     doc.getElementById('torbutton_clearHttpAuth').checked = o_torprefs.getBoolPref('clear_http_auth');
     doc.getElementById('torbutton_blockJSHistory').checked = o_torprefs.getBoolPref('block_js_history');
@@ -429,10 +431,11 @@ function torbutton_prefs_save(doc) {
     o_torprefs.setBoolPref('no_updates', doc.getElementById('torbutton_noUpdates').checked);
     
     o_torprefs.setBoolPref('set_uagent', doc.getElementById('torbutton_setUagent').checked);
-    o_torprefs.setBoolPref('disable_referer', doc.getElementById('torbutton_noReferer').checked);
+    o_torprefs.setBoolPref('fakerefresh', doc.getElementById('torbutton_spoofRefresh').checked);
     o_torprefs.setBoolPref('spoof_english', doc.getElementById('torbutton_spoofEnglish').checked);
     
     o_torprefs.setBoolPref('locked_mode', doc.getElementById('torbutton_lockedMode').checked);
+    o_torprefs.setIntPref('refererspoof',doc.getElementById('torbutton_refererSpoofGroup').selectedIndex);
     /*
     o_torprefs.setBoolPref('jar_certs', doc.getElementById('torbutton_jarCerts').checked);
     o_torprefs.setBoolPref('jar_ca_certs',
diff --git a/src/chrome/content/preferences.xul b/src/chrome/content/preferences.xul
index cdba07a..e8f4012 100644
--- a/src/chrome/content/preferences.xul
+++ b/src/chrome/content/preferences.xul
@@ -329,9 +329,17 @@
                    oncommand="torbutton_prefs_set_field_attributes(document)"/>
           <checkbox id="torbutton_jarCACerts" label="&torbutton.prefs.jar_ca_certs;" 
                    oncommand="torbutton_prefs_set_field_attributes(document)"/>
--->
-          <checkbox id="torbutton_noReferer" label="&torbutton.prefs.disable_referer;" 
+
+          <checkbox id="torbutton_no  er" label="&torbutton.prefs.disable_referer;" 
                    oncommand="torbutton_prefs_set_field_attributes(document)"/>
+-->
+            <checkbox id="torbutton_spoofRefresh" label="&torbutton.prefs.spoofreresh;" oncommand="torbutton_prefs_set_field_attributes(document)" />
+            <radiogroup id="torbutton_refererSpoofGroup" label="&torbutton.prefs.refereroptions;">
+              <radio id="torbutton_noRefSpoof" label="&torbutton.prefs.nospoof;" oncommand="torbutton_prefs_set_field_attributes(document)" />
+              <radio id="torbutton_SpoofRoot" label="&torbutton.prefs.spoofroot;" selected="true" oncommand="torbutton_prefs_set_field_attributes(document)" />
+              <radio id="torbutton_SpoofDomain" label="&torbutton.prefs.spoofdomain;" oncommand="torbutton_prefs_set_field_attributes(document)" />
+              <radio id="torbutton_BlankReferer" label="&torbutton.prefs.spoofblank;" oncommand="torbutton_prefs_set_field_attributes(document)" />
+            </radiogroup>
            </vbox>
           </tabpanel>
          </tabpanels>
diff --git a/src/chrome/locale/en/torbutton.dtd b/src/chrome/locale/en/torbutton.dtd
index cd4b386..782fc26 100644
--- a/src/chrome/locale/en/torbutton.dtd
+++ b/src/chrome/locale/en/torbutton.dtd
@@ -92,3 +92,9 @@
 <!ENTITY torbutton.prefs.socks_vfive "SOCKS v5">
 <!ENTITY torbutton.prefs.no_proxies_on "No Proxies for: ">
 <!ENTITY torbutton.prefs.no_proxy_warning "Warning: Avoid using any hostnames above">
+<!ENTITY torbutton.prefs.spoofreresh "Spoof Refresh">
+<!ENTITY torbutton.prefs.refereroptions "Referer Spoofing Options">
+<!ENTITY torbutton.prefs.nospoof "Do not spoof referer">
+<!ENTITY torbutton.prefs.spoofroot "Spoof the root of the site.">
+<!ENTITY torbutton.prefs.spoofdomain "Spoof the domain as referer.">
+<!ENTITY torbutton.prefs.spoofblank "Make referer blank.">
diff --git a/src/components/tor-protocol.js b/src/components/tor-protocol.js
index 1fdb354..6b9d66a 100644
--- a/src/components/tor-protocol.js
+++ b/src/components/tor-protocol.js
@@ -1,4 +1,4 @@
-g// Test protocol related
+// Test protocol related
 const kSCHEME = "tor";
 const kPROTOCOL_NAME = "tor";
 const kPROTOCOL_CONTRACTID = "@mozilla.org/network/protocol;1?name=" + kSCHEME;
diff --git a/src/components/torRefSpoofer.js b/src/components/torRefSpoofer.js
index 7273208..1e8abdb 100644
--- a/src/components/torRefSpoofer.js
+++ b/src/components/torRefSpoofer.js
@@ -1,8 +1,12 @@
 
 function LOG(text)
 {
-  var logger = Components.classes["@torproject.org/torbutton-logger;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
-  logger.log(text);
+ //var logger = Components.classes["@torproject.org/torbutton-logger;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
+ //logger.log("RefSpoof " + text);
+  var prompt = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
+                        .getService(Components.interfaces.nsIPromptService);
+  prompt.alert(null, "debug", text);
+ 
 }
 
 
@@ -11,19 +15,71 @@ var refObserver = {
   observe: function(subject, topic, data)
   {
     if (topic == "http-on-modify-request") {
-      LOG("----------------------------> (" + subject + ") mod request");
-      var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
-      httpChannel.setRequestHeader("referer", "http://foo.com", false);
+      //LOG("----------------------------> (" + subject + ") mod request");
+      subject.QueryInterface(Components.interfaces.nsIHttpChannel);
+      this.onModifyRequest(subject);
       return;
-      }
+    }
     if (topic == "app-startup") {
-      LOG("----------------------------> app-startup");
+      //LOG("----------------------------> app-startup");
       var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
       os.addObserver(this, "http-on-modify-request", false);
       return;
     }
   },
- 
+  onModifyRequest: function(oHttpChannel)
+	{
+	  var prefs = Components.classes["@mozilla.org/preferences-service;1"]
+    .getService(Components.interfaces.nsIPrefBranch);
+    
+    var spoofmode = prefs.getIntPref("extensions.torbutton.refererspoof");
+    try {
+    oHttpChannel.QueryInterface(Components.interfaces.nsIChannel);
+    var requestURI = oHttpChannel.URI;
+    
+		
+    	switch(spoofmode)
+			{
+			   //no spoof, should give the regular referer (not recommended)  	    
+        case 0:
+          return;        
+        //spoof document root  
+        case 1:          
+          this.adjustRef(oHttpChannel, requestURI.host + requestURI.path);        
+        break;
+        //spoof domain
+        case 2:
+          this.adjustRef(oHttpChannel, requestURI.host);
+        break;
+        //spoof no referer
+        case 3:
+          this.adjustRef(oHttpChannel, "");
+        break;      
+      }
+
+			// handle wildcarding
+			// try matching "www.foo.example.com", "foo.example.com", "example.com", ...
+			
+			// didn't find any matches, fall back on configured default action
+		} catch (ex) {
+			LOG("onModifyRequest: " + ex);
+		}
+	},
+	adjustRef: function(oChannel, sRef)
+	{
+		try {
+			if (oChannel.referrer)
+			{
+				oChannel.referrer.spec = sRef;
+        oChannel.setRequestHeader("Referer", sRef, false);
+      }
+			return true;
+		} 
+    catch (ex) {
+			LOG("adjustRef: " + ex);
+		}
+		return false;
+	},
   QueryInterface: function(iid)
 	{
 		if (!iid.equals(Components.interfaces.nsISupports) &&
@@ -36,19 +92,19 @@ var refObserver = {
 
 var myModule = {
     
-  myCID: Components.ID("{65be2be0-ceb4-44c2-91a5-9c75c53430bf}"),
+  myCID: Components.ID("65be2be0-ceb4-44c2-91a5-9c75c53430bf"),
   myProgID: "@torproject.org/torRefSpoofer;1",
-  myName:   "Ref Spoofer Component",
+  myName:   "RefSpoofComp",
   registerSelf: function (compMgr, fileSpec, location, type) {
-    var compMgr = compMgr.QueryInterface(Components.interfacesnsIComponentRegistrar);
+    var compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
     compMgr.registerFactoryLocation(this.myCID,this.myName,this.myProgID,fileSpec,location,type);
-    LOG("----------------------------> registerSelf");
+    //LOG("----------------------------> registerSelf");
     var catMgr = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
     catMgr.addCategoryEntry("app-startup", this.myName, this.myProgID, true, true);
   },
   
   getClassObject: function (compMgr, cid, iid) {
-    LOG("----------------------------> getClassObject");
+    //LOG("----------------------------> getClassObject");
     return this.myFactory;
   },
 
@@ -78,7 +134,7 @@ var myModule = {
 		{
 			if (outer != null)
 				throw Components.results.NS_ERROR_NO_AGGREGATION;			
-			return myObserver.QueryInterface(iid);
+			return refObserver.QueryInterface(iid);
     }
   }    
 };
diff --git a/src/components/tors-protocol.js b/src/components/tors-protocol.js
new file mode 100644
index 0000000..bfced4c
--- /dev/null
+++ b/src/components/tors-protocol.js
@@ -0,0 +1,139 @@
+// Test protocol related
+const kSCHEME = "tors";
+const kPROTOCOL_NAME = "tors";
+const kPROTOCOL_CONTRACTID = "@mozilla.org/network/protocol;1?name=" + kSCHEME;
+const kPROTOCOL_CID = Components.ID("a5a4bc50-5e8d-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, 433, 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 https.*/
+    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 = "https";    
+        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 75eda3e..b7207cf 100644
--- a/src/defaults/preferences/preferences.js
+++ b/src/defaults/preferences/preferences.js
@@ -139,6 +139,8 @@ pref("extensions.torbutton.tor_memory_jar",false);
 pref("extensions.torbutton.nontor_memory_jar",false);
 pref("extensions.torbutton.tz_string","");
 pref("extensions.torbutton.launch_warning",true);
+pref("extensions.torbutton.refererspoof", 0); //0=no spoof, 1=root spoof, 2=domain spoof, 3=blank spoof
+pref("extensions.torbutton.fakerefresh", false);
 
 // User agent prefs:
 pref("extensions.torbutton.appname_override","Netscape");
-- 
1.5.6.5




More information about the tor-commits mailing list