[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [torbutton/maint-1.2 15/37] Redirect Google queries to competitors' search engines.



Author: Mike Perry <mikeperry-git@xxxxxxxxxx>
Date: Tue, 30 Mar 2010 02:23:13 -0700
Subject: Redirect Google queries to competitors' search engines.
Commit: 9981b3502633399cb5918341329248342c690a9a

If google is giving captchas, ask the user if they want to be
redirected to another search engine. Default is IXquick.
---
 src/chrome/content/torbutton.js           |   76 ++++++++++++++++++++++++++++-
 src/chrome/locale/en/torbutton.dtd        |    4 ++
 src/chrome/locale/en/torbutton.properties |    5 ++
 src/defaults/preferences/preferences.js   |    8 +++
 4 files changed, 92 insertions(+), 1 deletions(-)

diff --git a/src/chrome/content/torbutton.js b/src/chrome/content/torbutton.js
index bc7ceee..c0d97bb 100644
--- a/src/chrome/content/torbutton.js
+++ b/src/chrome/content/torbutton.js
@@ -2705,6 +2705,71 @@ function torbutton_xfer_google_cookies(subject, topic, data) {
   }
 }
 
+/* Redirect the user to a different search engine if Google is blocking Tor */
+function torbutton_check_google_captcha(subject, topic, data) {
+  if (!m_tb_prefs.getBoolPref("extensions.torbutton.proxies_applied"))
+    return;
+
+  var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
+  var hostmatch = subject.URI.host.match(/^www\.google\.(co\.\S\S|com|\S\S|com\.\S\S)$/);
+
+  // check nsIURI
+  if (hostmatch && httpChannel.responseStatus == 302) {
+    // Now check for 302 to sorry.google.com
+    torbutton_log(3, "Got Google 302 response...");
+    var redir = httpChannel.getResponseHeader("Location");
+    var redirURI = Components.classes["@mozilla.org/network/standard-url;1"]
+                      .createInstance(Ci.nsIStandardURL);
+
+    redirURI.init(Ci.nsIStandardURL.URLTYPE_STANDARD, 80, redir,
+                  subject.URI.originCharset, null);
+    redirURI = redirURI.QueryInterface(Components.interfaces.nsIURI);
+    if (redirURI.host == "sorry.google.com") {
+      querymatch = subject.URI.path.match("[\?\&]q=([^&]+)[\&]");
+      if (!querymatch) {
+        torbutton_safelog(4, "No Google query found for captcha in: ",
+                subject.URI.spec);
+        return;
+      }
+      var newUrl = m_tb_prefs.getCharPref("extensions.torbutton.redir_url."+
+                    m_tb_prefs.getIntPref("extensions.torbutton.google_redir_url"));
+
+      if (!m_tb_prefs.getBoolPref("extensions.torbutton.asked_google_captcha")) {
+        var check = {value: false};
+        var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
+                                .getService(Components.interfaces.nsIPromptService);
+        var flags = prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_IS_STRING +
+                    prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_IS_STRING +
+                    prompts.BUTTON_POS_1_DEFAULT;
+
+        var bundle = torbutton_get_stringbundle();
+        var title = bundle.GetStringFromName("torbutton.popup.captcha.title");
+        var ask = bundle.GetStringFromName("torbutton.popup.captcha.ask");
+        var dontask = bundle.GetStringFromName("torbutton.popup.captcha.always");
+        var launch = bundle.GetStringFromName("torbutton.popup.redirect");
+        var cancel = bundle.GetStringFromName("torbutton.popup.no_redirect");
+
+        var result = prompts.confirmEx(window, title, ask, flags,
+                                       launch, cancel, "", dontask, check);
+
+        if (check.value) {
+          m_tb_prefs.setBoolPref("extensions.torbutton.asked_google_captcha",
+                                 true);
+          m_tb_prefs.setBoolPref("extensions.torbutton.dodge_google_captcha",
+                                 (result == 0));
+        }
+
+        if (result != 0) {
+          return;
+        }
+      }
+      // Split url into [?&]q=...[&$]
+      httpChannel.setResponseHeader("Location", newUrl+querymatch[1], false);
+      torbutton_log(4, "Got Google Captcha. Redirecting");
+    }
+  }
+}
+
 // Technique courtesy of:
 // http://xulsolutions.blogspot.com/2006/07/creating-uninstall-script-for.html
 // XXX: Exception here??
@@ -2836,10 +2901,19 @@ observe : function(subject, topic, data) {
 
   if (topic == "http-on-examine-response") {
       torbutton_eclog(3, 'Definitaly Examine response: '+subject.name);
+      if (m_tb_prefs.getBoolPref("extensions.torbutton.dodge_google_captcha")
+            && subject instanceof Ci.nsIHttpChannel) {
+        try {
+          torbutton_check_google_captcha(subject, topic, data);
+        } catch(e) {
+          torbutton_log(4, "Explosion on captcha redirect: "+e);
+        }
+      }
       torbutton_check_progress(null, subject, 0, false);
   } else if (topic == "http-on-modify-request") {
       torbutton_eclog(3, 'Modify request: '+subject.name);
-      if (m_tb_prefs.getBoolPref("extensions.torbutton.xfer_google_cookies")) {
+      if (m_tb_prefs.getBoolPref("extensions.torbutton.xfer_google_cookies")
+            && subject instanceof Ci.nsIHttpChannel) {
         try {
           torbutton_xfer_google_cookies(subject, topic, data);
         } catch(e) {
diff --git a/src/chrome/locale/en/torbutton.dtd b/src/chrome/locale/en/torbutton.dtd
index 04f211e..6f0caa9 100644
--- a/src/chrome/locale/en/torbutton.dtd
+++ b/src/chrome/locale/en/torbutton.dtd
@@ -98,3 +98,7 @@
 <!ENTITY torbutton.prefs.dtd_optional "(optional)">
 <!ENTITY torbutton.prefs.dtd_crucial "(crucial)">
 <!ENTITY torbutton.prefs.update_torbutton_via_tor "Redirect Torbutton updates through Tor">
+<!ENTITY torbutton.prefs.dodge_google_captcha "Automatically use an alternate search engine when presented with a Google Captcha">
+<!ENTITY torbutton.prefs.engine1 "ixquick.com">
+<!ENTITY torbutton.prefs.engine2 "Bing.com">
+<!ENTITY torbutton.prefs.engine3 "scroogle.org">
diff --git a/src/chrome/locale/en/torbutton.properties b/src/chrome/locale/en/torbutton.properties
index f904b25..c87c011 100644
--- a/src/chrome/locale/en/torbutton.properties
+++ b/src/chrome/locale/en/torbutton.properties
@@ -28,3 +28,8 @@ torbutton.popup.launch = Launch application
 torbutton.popup.cancel = Cancel
 torbutton.popup.dontask = Always launch applications from now on
 torbutton.popup.test.no_http_proxy = Tor proxy test: Local HTTP Proxy is unreachable. Is Polipo running properly?
+torbutton.popup.captcha.title = Avoid Google Captchas?
+torbutton.popup.captcha.ask = Torbutton detected a Google Captcha. Would you like to be redirected to another search engine for this query?
+torbutton.popup.captcha.always = Always perform this action from now on
+torbutton.popup.redirect = Redirect
+torbutton.popup.no_redirect = Don't Redirect
diff --git a/src/defaults/preferences/preferences.js b/src/defaults/preferences/preferences.js
index a355518..b39c4d2 100644
--- a/src/defaults/preferences/preferences.js
+++ b/src/defaults/preferences/preferences.js
@@ -171,6 +171,14 @@ pref("extensions.torbutton.regen_google_cookies", false);
 // Xfer google search cookies across all google domains
 pref("extensions.torbutton.xfer_google_cookies", true);
 
+// Google redirect prefs
+pref("extensions.torbutton.google_redir_url", 1);
+pref("extensions.torbutton.dodge_google_captcha", true);
+pref("extensions.torbutton.asked_google_captcha", false);
+pref("extensions.torbutton.redir_url.1", "https://www.ixquick.com/do/metasearch.pl?query=";);
+pref("extensions.torbutton.redir_url.2", "http://www.bing.com/search?q=";);
+pref("extensions.torbutton.redir_url.3", "https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=";);
+
 // User agent prefs:
 pref("extensions.torbutton.appname_override","Netscape");
 pref("extensions.torbutton.appversion_override","5.0 (Windows; LANG)");
-- 
1.6.5