]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java
Fix proxying through Orbot. https://redmine.stoutner.com/issues/473
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / OrbotProxyHelper.java
1 /*
2  * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.helpers;
21
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;
34
35 import androidx.appcompat.app.AlertDialog;
36
37 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
38 import com.stoutner.privacybrowser.R;
39
40 import java.lang.reflect.Field;
41 import java.lang.reflect.InvocationTargetException;
42 import java.lang.reflect.Method;
43
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");
50         } else {
51             // Set the proxy values
52             System.setProperty("proxyHost", proxyHost);
53             System.setProperty("proxyPort", proxyPort);
54         }
55
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);
61
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");
67
68         // Use reflection to apply the new proxy values.
69         try {
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");
73
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");
77
78             // Allow the values to be changed.
79             mLoadedApkField.setAccessible(true);
80             mReceiversField.setAccessible(true);
81
82             // Get the APK object.
83             Object mLoadedApkObject = mLoadedApkField.get(privacyBrowserContext);
84
85             // Get an array map of the receivers.
86             ArrayMap receivers = (ArrayMap) mReceiversField.get(mLoadedApkObject);
87
88             // Set the proxy.
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();
94
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);
99
100                         // Create a proxy change intent.
101                         Intent proxyChangeIntent = new Intent(Proxy.PROXY_CHANGE_ACTION);
102
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");
106
107                         // Get the build direct proxy method from the proxy info class.
108                         Method buildDirectProxyMethod = proxyInfoClass.getMethod("buildDirectProxy", String.class, Integer.TYPE);
109
110                         // Populate a proxy info object with the new proxy information.
111                         Object proxyInfoObject = buildDirectProxyMethod.invoke(proxyInfoClass, proxyHost, Integer.valueOf(proxyPort));
112
113                         // Add the proxy info object into the proxy change intent.
114                         proxyChangeIntent.putExtra("proxy", (Parcelable) proxyInfoObject);
115
116                         // Pass the proxy change intent to the `onReceive` method of the receiver class.
117                         onReceiveMethod.invoke(receiver, privacyBrowserContext, proxyChangeIntent);
118                     }
119                 }
120             }
121         } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException exception) {
122             Log.d("enableProxyThroughOrbot", "Exception: " + exception);
123         }
124
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);
129
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");
134
135                     // Send the intent to the Orbot package.
136                     orbotIntent.setPackage("org.torproject.android");
137
138                     // Request a status response be sent back to this package.
139                     orbotIntent.putExtra("org.torproject.android.intent.extra.PACKAGE_NAME", privacyBrowserContext.getPackageName());
140
141                     // Make it so.
142                     privacyBrowserContext.sendBroadcast(orbotIntent);
143                 }
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;
147
148                 // Get a handle for the shared preferences.
149                 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(privacyBrowserContext);
150
151                 // Get the theme preference.
152                 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
153
154                 // Set the style according to the theme.
155                 if (darkTheme) {
156                     dialogBuilder = new AlertDialog.Builder(parentActivity, R.style.PrivacyBrowserAlertDialogDark);
157                 } else {
158                     dialogBuilder = new AlertDialog.Builder(parentActivity, R.style.PrivacyBrowserAlertDialogLight);
159                 }
160
161                 // Set the message.
162                 dialogBuilder.setMessage(R.string.orbot_proxy_not_installed);
163
164                 // Set the positive button.
165                 dialogBuilder.setPositiveButton(R.string.close, (DialogInterface dialog, int which) -> {
166                     // Do nothing.  The `AlertDialog` will close automatically.
167                 });
168
169                 // Convert `dialogBuilder` to `alertDialog`.
170                 AlertDialog alertDialog = dialogBuilder.create();
171
172                 // Make it so.
173                 alertDialog.show();
174             }
175         }
176     }
177 }