You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
6.3 KiB
171 lines
6.3 KiB
6 years ago
|
package org.purplei2p.lightning.utils;
|
||
9 years ago
|
|
||
|
import android.app.Activity;
|
||
8 years ago
|
import android.app.Dialog;
|
||
9 years ago
|
import android.content.DialogInterface;
|
||
8 years ago
|
import android.support.annotation.NonNull;
|
||
9 years ago
|
import android.support.v7.app.AlertDialog;
|
||
9 years ago
|
import android.util.Log;
|
||
|
|
||
8 years ago
|
import javax.inject.Inject;
|
||
|
import javax.inject.Singleton;
|
||
|
|
||
6 years ago
|
import org.purplei2p.lightning.R;
|
||
|
import org.purplei2p.lightning.BrowserApp;
|
||
|
import org.purplei2p.lightning.constant.Constants;
|
||
|
import org.purplei2p.lightning.dialog.BrowserDialog;
|
||
|
import org.purplei2p.lightning.preference.PreferenceManager;
|
||
9 years ago
|
import info.guardianproject.netcipher.proxy.OrbotHelper;
|
||
7 years ago
|
import info.guardianproject.netcipher.webkit.WebkitProxy;
|
||
9 years ago
|
|
||
8 years ago
|
@Singleton
|
||
9 years ago
|
public class ProxyUtils {
|
||
7 years ago
|
|
||
|
private static final String TAG = "ProxyUtils";
|
||
|
|
||
8 years ago
|
@Inject PreferenceManager mPreferences;
|
||
9 years ago
|
|
||
8 years ago
|
@Inject
|
||
|
public ProxyUtils() {
|
||
|
BrowserApp.getAppComponent().inject(this);
|
||
9 years ago
|
}
|
||
|
|
||
|
/*
|
||
|
* If Orbot/Tor or I2P is installed, prompt the user if they want to enable
|
||
9 years ago
|
* proxying for this session
|
||
|
*/
|
||
8 years ago
|
public void checkForProxy(@NonNull final Activity activity) {
|
||
9 years ago
|
boolean useProxy = mPreferences.getUseProxy();
|
||
|
|
||
9 years ago
|
final boolean orbotInstalled = OrbotHelper.isOrbotInstalled(activity);
|
||
9 years ago
|
boolean orbotChecked = mPreferences.getCheckedForTor();
|
||
|
boolean orbot = orbotInstalled && !orbotChecked;
|
||
|
|
||
6 years ago
|
boolean i2pInstalled = true;
|
||
9 years ago
|
boolean i2pChecked = mPreferences.getCheckedForI2P();
|
||
|
boolean i2p = i2pInstalled && !i2pChecked;
|
||
|
|
||
|
// TODO Is the idea to show this per-session, or only once?
|
||
|
if (!useProxy && (orbot || i2p)) {
|
||
|
if (orbot) mPreferences.setCheckedForTor(true);
|
||
|
if (i2p) mPreferences.setCheckedForI2P(true);
|
||
|
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||
|
|
||
|
if (orbotInstalled && i2pInstalled) {
|
||
|
String[] proxyChoices = activity.getResources().getStringArray(R.array.proxy_choices_array);
|
||
|
builder.setTitle(activity.getResources().getString(R.string.http_proxy))
|
||
8 years ago
|
.setSingleChoiceItems(proxyChoices, mPreferences.getProxyChoice(),
|
||
|
new DialogInterface.OnClickListener() {
|
||
|
@Override
|
||
|
public void onClick(DialogInterface dialog, int which) {
|
||
|
mPreferences.setProxyChoice(which);
|
||
|
}
|
||
|
})
|
||
|
.setPositiveButton(activity.getResources().getString(R.string.action_ok),
|
||
|
new DialogInterface.OnClickListener() {
|
||
|
@Override
|
||
|
public void onClick(DialogInterface dialog, int which) {
|
||
|
if (mPreferences.getUseProxy())
|
||
|
initializeProxy(activity);
|
||
|
}
|
||
|
});
|
||
9 years ago
|
} else {
|
||
|
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
|
||
|
@Override
|
||
|
public void onClick(DialogInterface dialog, int which) {
|
||
|
switch (which) {
|
||
|
case DialogInterface.BUTTON_POSITIVE:
|
||
|
mPreferences.setProxyChoice(orbotInstalled ?
|
||
8 years ago
|
Constants.PROXY_ORBOT : Constants.PROXY_I2P);
|
||
9 years ago
|
initializeProxy(activity);
|
||
|
break;
|
||
|
case DialogInterface.BUTTON_NEGATIVE:
|
||
|
mPreferences.setProxyChoice(Constants.NO_PROXY);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
builder.setMessage(orbotInstalled ? R.string.use_tor_prompt : R.string.use_i2p_prompt)
|
||
8 years ago
|
.setPositiveButton(R.string.yes, dialogClickListener)
|
||
|
.setNegativeButton(R.string.no, dialogClickListener);
|
||
9 years ago
|
}
|
||
8 years ago
|
Dialog dialog = builder.show();
|
||
|
BrowserDialog.setDialogSize(activity, dialog);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Initialize WebKit Proxying
|
||
9 years ago
|
*/
|
||
8 years ago
|
private void initializeProxy(@NonNull Activity activity) {
|
||
9 years ago
|
String host;
|
||
|
int port;
|
||
|
|
||
|
switch (mPreferences.getProxyChoice()) {
|
||
|
case Constants.NO_PROXY:
|
||
|
// We shouldn't be here
|
||
|
return;
|
||
|
case Constants.PROXY_ORBOT:
|
||
7 years ago
|
if (!OrbotHelper.isOrbotRunning(activity)) {
|
||
9 years ago
|
OrbotHelper.requestStartTor(activity);
|
||
7 years ago
|
}
|
||
9 years ago
|
host = "localhost";
|
||
|
port = 8118;
|
||
|
break;
|
||
|
case Constants.PROXY_I2P:
|
||
|
host = "localhost";
|
||
|
port = 4444;
|
||
|
break;
|
||
|
default:
|
||
|
host = mPreferences.getProxyHost();
|
||
|
port = mPreferences.getProxyPort();
|
||
8 years ago
|
break;
|
||
|
case Constants.PROXY_MANUAL:
|
||
|
host = mPreferences.getProxyHost();
|
||
|
port = mPreferences.getProxyPort();
|
||
|
break;
|
||
9 years ago
|
}
|
||
|
|
||
|
try {
|
||
9 years ago
|
WebkitProxy.setProxy(BrowserApp.class.getName(), activity.getApplicationContext(), null, host, port);
|
||
9 years ago
|
} catch (Exception e) {
|
||
7 years ago
|
Log.d(TAG, "error enabling web proxying", e);
|
||
9 years ago
|
}
|
||
|
|
||
|
}
|
||
|
|
||
8 years ago
|
public boolean isProxyReady(@NonNull Activity activity) {
|
||
9 years ago
|
return true;
|
||
|
}
|
||
|
|
||
8 years ago
|
public void updateProxySettings(@NonNull Activity activity) {
|
||
9 years ago
|
if (mPreferences.getUseProxy()) {
|
||
|
initializeProxy(activity);
|
||
|
} else {
|
||
9 years ago
|
try {
|
||
|
WebkitProxy.resetProxy(BrowserApp.class.getName(), activity.getApplicationContext());
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
@Constants.Proxy
|
||
8 years ago
|
public static int setProxyChoice(int choice, @NonNull Activity activity) {
|
||
9 years ago
|
switch (choice) {
|
||
|
case Constants.PROXY_ORBOT:
|
||
9 years ago
|
if (!OrbotHelper.isOrbotInstalled(activity)) {
|
||
9 years ago
|
choice = Constants.NO_PROXY;
|
||
9 years ago
|
Utils.showSnackbar(activity, R.string.install_orbot);
|
||
9 years ago
|
}
|
||
|
break;
|
||
|
case Constants.PROXY_I2P:
|
||
|
break;
|
||
|
case Constants.PROXY_MANUAL:
|
||
|
break;
|
||
|
}
|
||
|
return choice;
|
||
|
}
|
||
|
}
|