From 3b6cd5759993e8046f3ee487f14689d0611ae149 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sat, 6 Jun 2015 19:56:07 +0530 Subject: [PATCH] added ProxyPrefs --- .../keychain/Constants.java | 12 ++++++ .../keychain/util/Preferences.java | 41 +++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java index 75b3ccb44..07602e618 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java @@ -24,6 +24,8 @@ import org.spongycastle.bcpg.SymmetricKeyAlgorithmTags; import org.spongycastle.jce.provider.BouncyCastleProvider; import java.io.File; +import java.net.InetSocketAddress; +import java.net.Proxy; public final class Constants { @@ -99,6 +101,16 @@ public final class Constants { public static final String PROXY_TYPE = "proxyType"; } + /** + * information to connect to Orbot's localhost HTTP proxy + */ + public static final class Orbot { + public static final String PROXY_HOST = "127.0.0.1"; + public static final int PROXY_PORT = 8118; + public static final Proxy.Type PROXY_TYPE = Proxy.Type.HTTP; + public static final Proxy PROXY = new Proxy(PROXY_TYPE, new InetSocketAddress(PROXY_HOST, PROXY_PORT)); + } + public static final class Defaults { public static final String KEY_SERVERS = "hkps://hkps.pool.sks-keyservers.net, hkps://pgp.mit.edu"; public static final int PREF_VERSION = 4; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java index 8bc5b777f..4cd361bcb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Preferences.java @@ -22,10 +22,12 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.res.Resources; +import info.guardianproject.onionkit.ui.OrbotHelper; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants.Pref; import org.sufficientlysecure.keychain.R; +import java.net.InetSocketAddress; import java.net.Proxy; import java.util.ArrayList; import java.util.Arrays; @@ -208,7 +210,6 @@ public class Preferences { } - public void setUseArmor(boolean useArmor) { SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putBoolean(Pref.USE_ARMOR, useArmor); @@ -287,14 +288,48 @@ public class Preferences { String type = mSharedPreferences.getString(Pref.PROXY_TYPE, typeHttp); - if(type.equals(typeHttp)) return Proxy.Type.HTTP; - else if(type.equals(typeSocks)) return Proxy.Type.SOCKS; + if (type.equals(typeHttp)) return Proxy.Type.HTTP; + else if (type.equals(typeSocks)) return Proxy.Type.SOCKS; else { // shouldn't happen Log.e(Constants.TAG, "Invalid Proxy Type in preferences"); return null; } } + public ProxyPrefs getProxyPrefs() { + Proxy proxy = null; + boolean useTor = getUseTorProxy(); + boolean useNormalProxy = getUseNormalProxy(); + + if (useTor) { + proxy = Constants.Orbot.PROXY; + } + else if (useNormalProxy) { + proxy = new Proxy(getProxyType(), new InetSocketAddress(getProxyHost(), getProxyPort())); + } + + return new ProxyPrefs(getUseTorProxy(), getUseNormalProxy(), proxy); + } + + public static class ProxyPrefs { + public final Proxy proxy; + public final boolean torEnabled; + public final boolean normalPorxyEnabled; + + /** + * torEnabled and normalProxyEnabled are not expected to both be true + * + * @param torEnabled if Tor is to be used + * @param normalPorxyEnabled if user-specified proxy is to be used + * @param proxy proxy to use, leave null if none + */ + public ProxyPrefs(boolean torEnabled, boolean normalPorxyEnabled, Proxy proxy) { + this.torEnabled = torEnabled; + this.normalPorxyEnabled = normalPorxyEnabled; + this.proxy = proxy; + } + } + // proxy preference functions ends here public CloudSearchPrefs getCloudSearchPrefs() {