2 * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.helpers;
22 import android.annotation.SuppressLint;
23 import android.app.Activity;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.content.Intent;
27 import android.content.SharedPreferences;
28 import android.content.pm.PackageManager;
29 import android.net.Proxy;
30 import android.os.Parcelable;
31 import android.preference.PreferenceManager;
32 import android.util.ArrayMap;
33 import android.util.Log;
35 import androidx.appcompat.app.AlertDialog;
37 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
38 import com.stoutner.privacybrowser.R;
40 import java.lang.reflect.Field;
41 import java.lang.reflect.InvocationTargetException;
42 import java.lang.reflect.Method;
44 public class OrbotProxyHelper {
45 public static void setProxy(Context privacyBrowserContext, Activity parentActivity, String proxyHost, String proxyPort) {
46 if (proxyPort.equals("0")) {
47 // Clear the proxy values.
48 System.clearProperty("proxyHost");
49 System.clearProperty("proxyPort");
51 // Set the proxy values
52 System.setProperty("proxyHost", proxyHost);
53 System.setProperty("proxyPort", proxyPort);
56 // These entries shouldn't be needed if the above general settings are applied. They are here for troubleshooting just in case.
57 // System.setProperty("http.proxyHost", proxyHost);
58 // System.setProperty("http.proxyPort", proxyPort);
59 // System.setProperty("https.proxyHost", proxyHost);
60 // System.setProperty("https.proxyPort", proxyPort);
62 // The SOCKS entries do not appear to do anything. But maybe someday they will.
63 // System.setProperty("socksProxyHost", proxyHost);
64 // System.setProperty("socksProxyPort", "9050");
65 // System.setProperty("socks.ProxyHost", proxyHost);
66 // System.setProperty("socks.ProxyPort", "9050");
68 // Use reflection to apply the new proxy values.
70 // Get the application and APK classes. Suppress the lint warning that reflection may not always work in the future and on all devices.
71 Class applicationClass = Class.forName("android.app.Application");
72 @SuppressLint("PrivateApi") Class loadedApkClass = Class.forName("android.app.LoadedApk");
74 // Get the declared fields. Suppress the lint warning that `mLoadedApk` cannot be resolved.
75 @SuppressWarnings("JavaReflectionMemberAccess") Field mLoadedApkField = applicationClass.getDeclaredField("mLoadedApk");
76 Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");
78 // Allow the values to be changed.
79 mLoadedApkField.setAccessible(true);
80 mReceiversField.setAccessible(true);
82 // Get the APK object.
83 Object mLoadedApkObject = mLoadedApkField.get(privacyBrowserContext);
85 // Get an array map of the receivers.
86 ArrayMap receivers = (ArrayMap) mReceiversField.get(mLoadedApkObject);
89 for (Object receiverMap : receivers.values()) {
90 for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
91 // Get the receiver class.
92 // `Class<?>`, which is an `unbounded wildcard parameterized type`, must be used instead of `Class`, which is a `raw type`. Otherwise, `receiveClass.getDeclaredMethod()` is unhappy.
93 Class<?> receiverClass = receiver.getClass();
95 // Apply the new proxy settings to any classes whose names contain `ProxyChangeListener`.
96 if (receiverClass.getName().contains("ProxyChangeListener")) {
97 // Get the `onReceive` method from the class.
98 Method onReceiveMethod = receiverClass.getDeclaredMethod("onReceive", Context.class, Intent.class);
100 // Create a proxy change intent.
101 Intent proxyChangeIntent = new Intent(Proxy.PROXY_CHANGE_ACTION);
103 // Get a proxy info class.
104 // `Class<?>`, which is an `unbounded wildcard parameterized type`, must be used instead of `Class`, which is a `raw type`. Otherwise, `proxyInfoClass.getMethod()` is unhappy.
105 Class<?> proxyInfoClass = Class.forName("android.net.ProxyInfo");
107 // Get the build direct proxy method from the proxy info class.
108 Method buildDirectProxyMethod = proxyInfoClass.getMethod("buildDirectProxy", String.class, Integer.TYPE);
110 // Populate a proxy info object with the new proxy information.
111 Object proxyInfoObject = buildDirectProxyMethod.invoke(proxyInfoClass, proxyHost, Integer.valueOf(proxyPort));
113 // Add the proxy info object into the proxy change intent.
114 proxyChangeIntent.putExtra("proxy", (Parcelable) proxyInfoObject);
116 // Pass the proxy change intent to the `onReceive` method of the receiver class.
117 onReceiveMethod.invoke(receiver, privacyBrowserContext, proxyChangeIntent);
121 } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException exception) {
122 Log.d("enableProxyThroughOrbot", "Exception: " + exception);
125 if (proxyPort.equals("8118")) { // Orbot proxy was turned on.
126 try { // Check to see if Orbot is installed.
127 PackageManager packageManager = privacyBrowserContext.getPackageManager();
128 packageManager.getPackageInfo("org.torproject.android", 0);
130 // Ask Orbot to connect if its current status is not "ON".
131 if (!MainWebViewActivity.orbotStatus.equals("ON")) {
132 // Request Orbot to start.
133 Intent orbotIntent = new Intent("org.torproject.android.intent.action.START");
135 // Send the intent to the Orbot package.
136 orbotIntent.setPackage("org.torproject.android");
138 // Request a status response be sent back to this package.
139 orbotIntent.putExtra("org.torproject.android.intent.extra.PACKAGE_NAME", privacyBrowserContext.getPackageName());
142 privacyBrowserContext.sendBroadcast(orbotIntent);
144 } catch (PackageManager.NameNotFoundException exception){ // If an exception is thrown, Orbot is not installed.
145 // Use `AlertDialog.Builder` to create the `AlertDialog`.
146 AlertDialog.Builder dialogBuilder;
148 // Get a handle for the shared preferences.
149 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(privacyBrowserContext);
151 // Get the theme preference.
152 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
154 // Set the style according to the theme.
156 dialogBuilder = new AlertDialog.Builder(parentActivity, R.style.PrivacyBrowserAlertDialogDark);
158 dialogBuilder = new AlertDialog.Builder(parentActivity, R.style.PrivacyBrowserAlertDialogLight);
162 dialogBuilder.setMessage(R.string.orbot_proxy_not_installed);
164 // Set the positive button.
165 dialogBuilder.setPositiveButton(R.string.close, (DialogInterface dialog, int which) -> {
166 // Do nothing. The `AlertDialog` will close automatically.
169 // Convert `dialogBuilder` to `alertDialog`.
170 AlertDialog alertDialog = dialogBuilder.create();