mirror of
https://github.com/moparisthebest/open-keychain
synced 2025-01-07 03:28:09 -05:00
fixed Proxy generation, proxy working for for cloud import
This commit is contained in:
parent
590748a42b
commit
aebafe4f78
@ -190,21 +190,6 @@ public class HkpKeyserver extends Keyserver {
|
||||
return mSecure ? "https://" : "http://";
|
||||
}
|
||||
|
||||
private HttpURLConnection openConnectioan(URL url) throws IOException {
|
||||
HttpURLConnection conn = null;
|
||||
try {
|
||||
conn = (HttpURLConnection) TlsHelper.opeanConnection(url);
|
||||
} catch (TlsHelper.TlsHelperException e) {
|
||||
Log.w(Constants.TAG, e);
|
||||
}
|
||||
if (conn == null) {
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
}
|
||||
conn.setConnectTimeout(5000);
|
||||
conn.setReadTimeout(25000);
|
||||
return conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a client with pinned certificate if necessary
|
||||
*
|
||||
@ -223,8 +208,8 @@ public class HkpKeyserver extends Keyserver {
|
||||
|
||||
client.setProxy(proxy);
|
||||
// TODO: PHILIP if proxy !=null increase timeout?
|
||||
client.setConnectTimeout(5000, TimeUnit.MILLISECONDS);
|
||||
client.setReadTimeout(25000, TimeUnit.MILLISECONDS);
|
||||
client.setConnectTimeout(proxy != null ? 30000 : 5000, TimeUnit.MILLISECONDS);
|
||||
client.setReadTimeout(45000, TimeUnit.MILLISECONDS);
|
||||
|
||||
return client;
|
||||
}
|
||||
@ -233,9 +218,12 @@ public class HkpKeyserver extends Keyserver {
|
||||
try {
|
||||
URL url = new URL(getUrlPrefix() + mHost + ":" + mPort + request);
|
||||
Log.d(Constants.TAG, "hkp keyserver query: " + url);
|
||||
Log.d("PHILIP", "hkpKeyserver query(): " + proxy);
|
||||
OkHttpClient client = getClient(url, proxy);
|
||||
Response response = client.newCall(new Request.Builder().url(url).build()).execute();
|
||||
|
||||
tempIpTest(proxy);
|
||||
|
||||
String responseBody = response.body().string();// contains body both in case of success or failure
|
||||
|
||||
if (response.isSuccessful()) {
|
||||
@ -249,6 +237,12 @@ public class HkpKeyserver extends Keyserver {
|
||||
}
|
||||
}
|
||||
|
||||
private void tempIpTest(Proxy proxy) throws IOException {
|
||||
URL url = new URL("https://wtfismyip.com/text");
|
||||
Response response = getClient(url, proxy).newCall(new Request.Builder().url(url).build()).execute();
|
||||
Log.e("PHILIP", "proxy Test: " + response.body().string());
|
||||
}
|
||||
|
||||
/**
|
||||
* Results are sorted by creation date of key!
|
||||
*
|
||||
|
@ -44,9 +44,11 @@ import org.sufficientlysecure.keychain.ui.util.Notify;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
import org.sufficientlysecure.keychain.util.ParcelableFileCache;
|
||||
import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize;
|
||||
import org.sufficientlysecure.keychain.util.ParcelableProxy;
|
||||
import org.sufficientlysecure.keychain.util.Preferences;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Proxy;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ImportKeysActivity extends BaseNfcActivity {
|
||||
@ -362,14 +364,14 @@ public class ImportKeysActivity extends BaseNfcActivity {
|
||||
}
|
||||
}
|
||||
|
||||
mListFragment.loadNew(loaderState, mProxyPrefs.proxy);
|
||||
mListFragment.loadNew(loaderState, mProxyPrefs.parcelableProxy);
|
||||
}
|
||||
|
||||
/**
|
||||
* disables use of Tor as proxy for this session
|
||||
*/
|
||||
private void disableTorForSession() {
|
||||
mProxyPrefs = new Preferences.ProxyPrefs(false, false, null);
|
||||
mProxyPrefs = new Preferences.ProxyPrefs(false, false, null, -1, null);
|
||||
}
|
||||
|
||||
private void handleMessage(Message message) {
|
||||
@ -476,6 +478,8 @@ public class ImportKeysActivity extends BaseNfcActivity {
|
||||
|
||||
data.putString(KeychainService.IMPORT_KEY_SERVER, sls.mCloudPrefs.keyserver);
|
||||
|
||||
data.putParcelable(KeychainService.EXTRA_PARCELABLE_PROXY, mProxyPrefs.parcelableProxy);
|
||||
|
||||
// get selected key entries
|
||||
ArrayList<ParcelableKeyRing> keys = new ArrayList<>();
|
||||
{
|
||||
|
@ -41,6 +41,7 @@ import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListLoader;
|
||||
import org.sufficientlysecure.keychain.util.InputData;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize;
|
||||
import org.sufficientlysecure.keychain.util.ParcelableProxy;
|
||||
import org.sufficientlysecure.keychain.util.Preferences;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@ -65,7 +66,7 @@ public class ImportKeysListFragment extends ListFragment implements
|
||||
private ImportKeysAdapter mAdapter;
|
||||
|
||||
private LoaderState mLoaderState;
|
||||
private Proxy mProxy;
|
||||
private ParcelableProxy mProxy;
|
||||
|
||||
private static final int LOADER_ID_BYTES = 0;
|
||||
private static final int LOADER_ID_CLOUD = 1;
|
||||
@ -262,7 +263,7 @@ public class ImportKeysListFragment extends ListFragment implements
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void loadNew(LoaderState loaderState, Proxy proxy) {
|
||||
public void loadNew(LoaderState loaderState, ParcelableProxy proxy) {
|
||||
mProxy = proxy;
|
||||
|
||||
mLoaderState = loaderState;
|
||||
|
@ -22,8 +22,15 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.preference.*;
|
||||
import android.preference.CheckBoxPreference;
|
||||
import android.preference.EditTextPreference;
|
||||
import android.preference.ListPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.preference.PreferenceFragment;
|
||||
import android.preference.PreferenceScreen;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.LinearLayout;
|
||||
@ -349,7 +356,7 @@ public class SettingsActivity extends PreferenceActivity {
|
||||
mProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
if (newValue.equals("")) {
|
||||
if (TextUtils.isEmpty((String) newValue)) {
|
||||
Notify.create(
|
||||
ProxyPrefsFragment.this.getActivity(),
|
||||
R.string.pref_proxy_host_err_invalid,
|
||||
|
@ -27,6 +27,7 @@ import org.sufficientlysecure.keychain.keyimport.Keyserver;
|
||||
import org.sufficientlysecure.keychain.operations.results.GetKeyResult;
|
||||
import org.sufficientlysecure.keychain.operations.results.OperationResult;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
import org.sufficientlysecure.keychain.util.ParcelableProxy;
|
||||
import org.sufficientlysecure.keychain.util.Preferences;
|
||||
|
||||
import java.net.Proxy;
|
||||
@ -39,18 +40,18 @@ public class ImportKeysListCloudLoader
|
||||
|
||||
Preferences.CloudSearchPrefs mCloudPrefs;
|
||||
String mServerQuery;
|
||||
private Proxy mProxy;
|
||||
private ParcelableProxy mParcelableProxy;
|
||||
|
||||
private ArrayList<ImportKeysListEntry> mEntryList = new ArrayList<>();
|
||||
private AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> mEntryListWrapper;
|
||||
|
||||
public ImportKeysListCloudLoader(Context context, String serverQuery, Preferences.CloudSearchPrefs cloudPrefs,
|
||||
Proxy proxy) {
|
||||
ParcelableProxy proxy) {
|
||||
super(context);
|
||||
mContext = context;
|
||||
mServerQuery = serverQuery;
|
||||
mCloudPrefs = cloudPrefs;
|
||||
mProxy = proxy;
|
||||
mParcelableProxy = proxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -100,7 +101,11 @@ public class ImportKeysListCloudLoader
|
||||
*/
|
||||
private void queryServer(boolean enforceFingerprint) {
|
||||
try {
|
||||
ArrayList<ImportKeysListEntry> searchResult = CloudSearch.search(mServerQuery, mCloudPrefs, mProxy);
|
||||
ArrayList<ImportKeysListEntry> searchResult = CloudSearch.search(
|
||||
mServerQuery,
|
||||
mCloudPrefs,
|
||||
mParcelableProxy.getProxy()
|
||||
);
|
||||
|
||||
mEntryList.clear();
|
||||
// add result to data
|
||||
|
@ -34,13 +34,11 @@ public class ParcelableProxy implements Parcelable {
|
||||
private final int TYPE_HTTP = 1;
|
||||
private final int TYPE_SOCKS = 2;
|
||||
|
||||
public ParcelableProxy(Proxy proxy) {
|
||||
InetSocketAddress address = (InetSocketAddress) proxy.address();
|
||||
public ParcelableProxy(String hostName, int port, Proxy.Type type) {
|
||||
mProxyHost = hostName;
|
||||
mProxyPort = port;
|
||||
|
||||
mProxyHost = address.getHostName();
|
||||
mProxyPort = address.getPort();
|
||||
|
||||
switch (proxy.type()) {
|
||||
switch (type) {
|
||||
case HTTP: {
|
||||
mProxyType = TYPE_HTTP;
|
||||
break;
|
||||
|
@ -306,23 +306,22 @@ public class Preferences {
|
||||
}
|
||||
|
||||
public ProxyPrefs getProxyPrefs() {
|
||||
Proxy proxy = null;
|
||||
boolean useTor = getUseTorProxy();
|
||||
boolean useNormalProxy = getUseNormalProxy();
|
||||
|
||||
if (useTor) {
|
||||
proxy = new Proxy(Constants.Orbot.PROXY_TYPE,
|
||||
new InetSocketAddress(Constants.Orbot.PROXY_HOST, Constants.Orbot.PROXY_PORT));
|
||||
return new ProxyPrefs(true, false, Constants.Orbot.PROXY_HOST, Constants.Orbot.PROXY_PORT,
|
||||
Constants.Orbot.PROXY_TYPE);
|
||||
}
|
||||
else if (useNormalProxy) {
|
||||
proxy = new Proxy(getProxyType(), new InetSocketAddress(getProxyHost(), getProxyPort()));
|
||||
return new ProxyPrefs(useTor, useNormalProxy, getProxyHost(), getProxyPort(), getProxyType());
|
||||
} else {
|
||||
return new ProxyPrefs(false, false, null, -1, null);
|
||||
}
|
||||
|
||||
return new ProxyPrefs(getUseTorProxy(), getUseNormalProxy(), proxy);
|
||||
}
|
||||
|
||||
public static class ProxyPrefs {
|
||||
public final Proxy proxy;
|
||||
public final ParcelableProxy parcelableProxy;
|
||||
public final boolean torEnabled;
|
||||
public final boolean normalPorxyEnabled;
|
||||
|
||||
@ -331,12 +330,12 @@ public class Preferences {
|
||||
*
|
||||
* @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) {
|
||||
public ProxyPrefs(boolean torEnabled, boolean normalPorxyEnabled, String hostName, int port, Proxy.Type type) {
|
||||
this.torEnabled = torEnabled;
|
||||
this.normalPorxyEnabled = normalPorxyEnabled;
|
||||
this.proxy = proxy;
|
||||
if(!torEnabled && !normalPorxyEnabled) this.parcelableProxy = null;
|
||||
else this.parcelableProxy = new ParcelableProxy(hostName, port, type);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user