commit 25134efbece613bf35a6456f1f472bc33e082e60 Author: Amogh Pradeep amoghbl1@gmail.com Date: Fri Jun 12 02:07:45 2015 -0400
Orfox: Centralized proxy applied to AbstractCommunicator and BaseResources.
See Bug 1357997 for partial uplift.
Also: Bug 28051 - Use our Orbot for proxying our connections --- .../java/org/mozilla/gecko/util/ProxySelector.java | 25 +++++++++++++++++++++- .../org/mozilla/gecko/sync/net/BaseResource.java | 7 ++++++ .../service/utils/AbstractCommunicator.java | 11 +++++++--- 3 files changed, 39 insertions(+), 4 deletions(-)
diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/ProxySelector.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/ProxySelector.java index 3940d3c84249..9515975f680a 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/ProxySelector.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/ProxySelector.java @@ -29,6 +29,10 @@ import java.net.URLConnection; import java.util.List;
public class ProxySelector { + private static final String TOR_PROXY_ADDRESS = "127.0.0.1"; + private static final int TOR_SOCKS_PROXY_PORT = 9150; + private static final int TOR_HTTP_PROXY_PORT = 8218; + public static URLConnection openConnectionWithProxy(final URI uri) throws IOException { java.net.ProxySelector ps = java.net.ProxySelector.getDefault(); Proxy proxy = Proxy.NO_PROXY; @@ -39,7 +43,26 @@ public class ProxySelector { } }
- return uri.toURL().openConnection(proxy); + /* Ignore the proxy we found from the VM, only use Tor. We can probably + * safely use the logic in this class in the future. */ + return uri.toURL().openConnection(getProxy()); + } + + public static Proxy getProxy() { + // TODO make configurable + return new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(TOR_PROXY_ADDRESS, TOR_SOCKS_PROXY_PORT)); + } + + public static String getProxyHostAddress() { + return TOR_PROXY_ADDRESS; + } + + public static int getSocksProxyPort() { + return TOR_SOCKS_PROXY_PORT; + } + + public static int getHttpProxyPort() { + return TOR_HTTP_PROXY_PORT; }
public ProxySelector() { diff --git a/mobile/android/services/src/main/java/org/mozilla/gecko/sync/net/BaseResource.java b/mobile/android/services/src/main/java/org/mozilla/gecko/sync/net/BaseResource.java index e85ccb855bff..51ebd6ce990f 100644 --- a/mobile/android/services/src/main/java/org/mozilla/gecko/sync/net/BaseResource.java +++ b/mobile/android/services/src/main/java/org/mozilla/gecko/sync/net/BaseResource.java @@ -23,9 +23,11 @@ import org.json.simple.JSONObject; import org.mozilla.gecko.background.common.GlobalConstants; import org.mozilla.gecko.background.common.log.Logger; import org.mozilla.gecko.sync.ExtendedJSONObject; +import org.mozilla.gecko.util.ProxySelector;
import ch.boye.httpclientandroidlib.Header; import ch.boye.httpclientandroidlib.HttpEntity; +import ch.boye.httpclientandroidlib.HttpHost; import ch.boye.httpclientandroidlib.HttpResponse; import ch.boye.httpclientandroidlib.HttpVersion; import ch.boye.httpclientandroidlib.client.AuthCache; @@ -40,6 +42,7 @@ import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase; import ch.boye.httpclientandroidlib.client.methods.HttpUriRequest; import ch.boye.httpclientandroidlib.client.protocol.ClientContext; import ch.boye.httpclientandroidlib.conn.ClientConnectionManager; +import ch.boye.httpclientandroidlib.conn.params.ConnRoutePNames; import ch.boye.httpclientandroidlib.conn.scheme.PlainSocketFactory; import ch.boye.httpclientandroidlib.conn.scheme.Scheme; import ch.boye.httpclientandroidlib.conn.scheme.SchemeRegistry; @@ -215,6 +218,10 @@ public class BaseResource implements Resource { // We could reuse these client instances, except that we mess around // with their parameters… so we'd need a pool of some kind. client = new DefaultHttpClient(getConnectionManager()); + /* TBA: We need a HTTP Proxy here */ + HttpHost defaultProxy = new HttpHost(ProxySelector.getProxyHostAddress(), + ProxySelector.getHttpProxyPort()); + client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, defaultProxy);
// TODO: Eventually we should use Apache HttpAsyncClient. It's not out of alpha yet. // Until then, we synchronously make the request, then invoke our delegate's callback. diff --git a/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/utils/AbstractCommunicator.java b/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/utils/AbstractCommunicator.java index 4e30cb7deeb7..9b3ee98f89db 100644 --- a/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/utils/AbstractCommunicator.java +++ b/mobile/android/stumbler/java/org/mozilla/mozstumbler/service/utils/AbstractCommunicator.java @@ -7,6 +7,7 @@ package org.mozilla.mozstumbler.service.utils; import android.os.Build; import android.util.Log;
+import org.mozilla.gecko.util.ProxySelector; import org.mozilla.mozstumbler.service.AppGlobals; import org.mozilla.mozstumbler.service.Prefs;
@@ -16,7 +17,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; -import java.net.URL; +import java.net.URI;
public abstract class AbstractCommunicator {
@@ -72,8 +73,12 @@ public abstract class AbstractCommunicator { if (sMozApiKey == null || prefs != null) { sMozApiKey = prefs.getMozApiKey(); } - URL url = new URL(getUrlString() + "?key=" + sMozApiKey); - mHttpURLConnection = (HttpURLConnection) url.openConnection(); + + /* TBA: This was a URL, but the connection logic would simply + * convert the URL into a URI, and then later convert the URI back + * into a URL. Creating a URL at the beginning is only wasteful. */ + URI uri = new URI(getUrlString() + "?key=" + sMozApiKey); + mHttpURLConnection = (HttpURLConnection) ProxySelector.openConnectionWithProxy(uri); mHttpURLConnection.setRequestMethod("POST"); } catch (MalformedURLException e) { throw new IllegalArgumentException(e);
tbb-commits@lists.torproject.org