added proxy support to OperationHelper

This commit is contained in:
Adithya Abraham Philip
2015-07-03 07:03:16 +05:30
parent 1856ca385d
commit 4d81a83baa
28 changed files with 388 additions and 161 deletions

View File

@@ -27,6 +27,7 @@ import org.sufficientlysecure.keychain.operations.results.ImportKeyResult;
import org.sufficientlysecure.keychain.service.ImportKeyringParcel;
import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@@ -34,6 +35,7 @@ import java.util.Locale;
import java.util.Set;
public class EmailKeyHelper {
// TODO: Make this not require a proxy in it's constructor, redesign when it is to be used
// to import keys, simply use CryptoOperationHelper with this callback
public abstract class ImportContactKeysCallback
implements CryptoOperationHelper.Callback<ImportKeyringParcel, ImportKeyResult> {
@@ -41,14 +43,15 @@ public class EmailKeyHelper {
private ArrayList<ParcelableKeyRing> mKeyList;
private String mKeyserver;
public ImportContactKeysCallback(Context context, String keyserver) {
this(context, ContactHelper.getContactMails(context), keyserver);
public ImportContactKeysCallback(Context context, String keyserver, Proxy proxy) {
this(context, ContactHelper.getContactMails(context), keyserver, proxy);
}
public ImportContactKeysCallback(Context context, List<String> mails, String keyserver) {
public ImportContactKeysCallback(Context context, List<String> mails, String keyserver,
Proxy proxy) {
Set<ImportKeysListEntry> entries = new HashSet<>();
for (String mail : mails) {
entries.addAll(getEmailKeys(context, mail));
entries.addAll(getEmailKeys(context, mail, proxy));
}
// Put them in a list and import
@@ -65,7 +68,7 @@ public class EmailKeyHelper {
}
}
public static Set<ImportKeysListEntry> getEmailKeys(Context context, String mail) {
public static Set<ImportKeysListEntry> getEmailKeys(Context context, String mail, Proxy proxy) {
Set<ImportKeysListEntry> keys = new HashSet<>();
// Try _hkp._tcp SRV record first
@@ -73,7 +76,7 @@ public class EmailKeyHelper {
if (mailparts.length == 2) {
HkpKeyserver hkp = HkpKeyserver.resolve(mailparts[1]);
if (hkp != null) {
keys.addAll(getEmailKeys(mail, hkp));
keys.addAll(getEmailKeys(mail, hkp, proxy));
}
}
@@ -82,16 +85,17 @@ public class EmailKeyHelper {
String server = Preferences.getPreferences(context).getPreferredKeyserver();
if (server != null) {
HkpKeyserver hkp = new HkpKeyserver(server);
keys.addAll(getEmailKeys(mail, hkp));
keys.addAll(getEmailKeys(mail, hkp, proxy));
}
}
return keys;
}
public static List<ImportKeysListEntry> getEmailKeys(String mail, Keyserver keyServer) {
public static List<ImportKeysListEntry> getEmailKeys(String mail, Keyserver keyServer,
Proxy proxy) {
Set<ImportKeysListEntry> keys = new HashSet<>();
try {
for (ImportKeysListEntry key : keyServer.search(mail)) {
for (ImportKeysListEntry key : keyServer.search(mail, proxy)) {
if (key.isRevoked() || key.isExpired()) continue;
for (String userId : key.getUserIds()) {
if (userId.toLowerCase().contains(mail.toLowerCase(Locale.ENGLISH))) {

View File

@@ -29,10 +29,7 @@ import java.net.Proxy;
public class ParcelableProxy implements Parcelable {
private String mProxyHost;
private int mProxyPort;
private int mProxyType;
private final int TYPE_HTTP = 1;
private final int TYPE_SOCKS = 2;
private Proxy.Type mProxyType;
public ParcelableProxy(String hostName, int port, Proxy.Type type) {
mProxyHost = hostName;
@@ -41,37 +38,23 @@ public class ParcelableProxy implements Parcelable {
mProxyPort = port;
switch (type) {
case HTTP: {
mProxyType = TYPE_HTTP;
break;
}
case SOCKS: {
mProxyType = TYPE_SOCKS;
break;
}
}
mProxyType = type;
}
public static ParcelableProxy getForNoProxy() {
return new ParcelableProxy(null, -1, null);
}
public Proxy getProxy() {
if (mProxyHost == null) return null;
Proxy.Type type = null;
switch (mProxyType) {
case TYPE_HTTP:
type = Proxy.Type.HTTP;
break;
case TYPE_SOCKS:
type = Proxy.Type.SOCKS;
break;
}
return new Proxy(type, new InetSocketAddress(mProxyHost, mProxyPort));
return new Proxy(mProxyType, new InetSocketAddress(mProxyHost, mProxyPort));
}
protected ParcelableProxy(Parcel in) {
mProxyHost = in.readString();
mProxyPort = in.readInt();
mProxyType = in.readInt();
mProxyType = (Proxy.Type) in.readSerializable();
}
@Override
@@ -83,7 +66,7 @@ public class ParcelableProxy implements Parcelable {
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mProxyHost);
dest.writeInt(mProxyPort);
dest.writeInt(mProxyType);
dest.writeSerializable(mProxyType);
}
@SuppressWarnings("unused")

View File

@@ -59,6 +59,7 @@ import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.service.ConsolidateInputParcel;
import org.sufficientlysecure.keychain.ui.dialog.SupportInstallDialogFragment;
import org.sufficientlysecure.keychain.ui.dialog.OrbotStartDialogFragment;
import org.sufficientlysecure.keychain.ui.dialog.PreferenceInstallDialogFragment;
@@ -130,6 +131,16 @@ public class OrbotHelper {
return intent;
}
public static boolean isOrbotInRequiredState(Context context) {
Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(context).getProxyPrefs();
if (!proxyPrefs.torEnabled) {
return true;
} else if (!OrbotHelper.isOrbotInstalled(context) || !OrbotHelper.isOrbotRunning()) {
return false;
}
return true;
}
/**
* checks if Tor is enabled and if it is, that Orbot is installed and runnign. Generates appropriate dialogs.
*
@@ -139,8 +150,9 @@ public class OrbotHelper {
* @param fragmentActivity
* @return true if Tor is not enabled or Tor is enabled and Orbot is installed and running, else false
*/
public static boolean isOrbotInRequiredState(int middleButton, final Runnable middleButtonRunnable,
Preferences.ProxyPrefs proxyPrefs, FragmentActivity fragmentActivity) {
public static boolean putOrbotInRequiredState(int middleButton, final Runnable middleButtonRunnable,
Preferences.ProxyPrefs proxyPrefs,
FragmentActivity fragmentActivity) {
Handler ignoreTorHandler = new Handler() {
@Override
public void handleMessage(Message msg) {