Fix proxying through Orbot on API 19. https://redmine.stoutner.com/issues/474
[PrivacyBrowser.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.Build;
31 import android.os.Parcelable;
32 import android.preference.PreferenceManager;
33 import android.util.ArrayMap;
34 import android.util.Log;
35
36 import androidx.appcompat.app.AlertDialog;
37
38 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39 import com.stoutner.privacybrowser.R;
40
41 import java.lang.reflect.Field;
42 import java.lang.reflect.InvocationTargetException;
43 import java.lang.reflect.Method;
44
45 public class OrbotProxyHelper {
46     public static void setProxy(Context privacyBrowserContext, Activity parentActivity, String proxyHost, String proxyPort) {
47         if (proxyPort.equals("0")) {
48             // Clear the proxy values.
49             System.clearProperty("proxyHost");
50             System.clearProperty("proxyPort");
51         } else {
52             // Set the proxy values
53             System.setProperty("proxyHost", proxyHost);
54             System.setProperty("proxyPort", proxyPort);
55         }
56
57         // These entries shouldn't be needed if the above general settings are applied.  They are here for troubleshooting just in case.
58         // System.setProperty("http.proxyHost", proxyHost);
59         // System.setProperty("http.proxyPort", proxyPort);
60         // System.setProperty("https.proxyHost", proxyHost);
61         // System.setProperty("https.proxyPort", proxyPort);
62
63         // The SOCKS entries do not appear to do anything.  But maybe someday they will.
64         // System.setProperty("socksProxyHost", proxyHost);
65         // System.setProperty("socksProxyPort", "9050");
66         // System.setProperty("socks.ProxyHost", proxyHost);
67         // System.setProperty("socks.ProxyPort", "9050");
68
69         // Use reflection to apply the new proxy values.
70         try {
71             // Get the application and APK classes.  Suppress the lint warning that reflection may not always work in the future and on all devices.
72             Class applicationClass = Class.forName("android.app.Application");
73             @SuppressLint("PrivateApi") Class loadedApkClass = Class.forName("android.app.LoadedApk");
74
75             // Get the declared fields.  Suppress the lint warning that `mLoadedApk` cannot be resolved.
76             @SuppressWarnings("JavaReflectionMemberAccess") Field methodLoadedApkField = applicationClass.getDeclaredField("mLoadedApk");
77             Field methodReceiversField = loadedApkClass.getDeclaredField("mReceivers");
78
79             // Allow the values to be changed.
80             methodLoadedApkField.setAccessible(true);
81             methodReceiversField.setAccessible(true);
82
83             // Get the APK object.
84             Object methodLoadedApkObject = methodLoadedApkField.get(privacyBrowserContext);
85
86             // Get an array map of the receivers.
87             ArrayMap receivers = (ArrayMap) methodReceiversField.get(methodLoadedApkObject);
88
89             // Set the proxy.
90             for (Object receiverMap : receivers.values()) {
91                 for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
92                     // Get the receiver class.
93                     // `Class<?>`, which is an `unbounded wildcard parameterized type`, must be used instead of `Class`, which is a `raw type`.  Otherwise, `receiveClass.getDeclaredMethod()` is unhappy.
94                     Class<?> receiverClass = receiver.getClass();
95
96                     // Apply the new proxy settings to any classes whose names contain `ProxyChangeListener`.
97                     if (receiverClass.getName().contains("ProxyChangeListener")) {
98                         // Get the `onReceive` method from the class.
99                         Method onReceiveMethod = receiverClass.getDeclaredMethod("onReceive", Context.class, Intent.class);
100
101                         // Create a proxy change intent.
102                         Intent proxyChangeIntent = new Intent(Proxy.PROXY_CHANGE_ACTION);
103
104                         if (Build.VERSION.SDK_INT >= 21) {
105                             // Get a proxy info class.
106                             // `Class<?>`, which is an `unbounded wildcard parameterized type`, must be used instead of `Class`, which is a `raw type`.  Otherwise, `proxyInfoClass.getMethod()` is unhappy.
107                             Class<?> proxyInfoClass = Class.forName("android.net.ProxyInfo");
108
109                             // Get the build direct proxy method from the proxy info class.
110                             Method buildDirectProxyMethod = proxyInfoClass.getMethod("buildDirectProxy", String.class, Integer.TYPE);
111
112                             // Populate a proxy info object with the new proxy information.
113                             Object proxyInfoObject = buildDirectProxyMethod.invoke(proxyInfoClass, proxyHost, Integer.valueOf(proxyPort));
114
115                             // Add the proxy info object into the proxy change intent.
116                             proxyChangeIntent.putExtra("proxy", (Parcelable) proxyInfoObject);
117                         }
118
119                         // Pass the proxy change intent to the `onReceive` method of the receiver class.
120                         onReceiveMethod.invoke(receiver, privacyBrowserContext, proxyChangeIntent);
121                     }
122                 }
123             }
124         } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException exception) {
125             Log.d("enableProxyThroughOrbot", "Exception: " + exception);
126         }
127
128         if (proxyPort.equals("8118")) {  // Orbot proxy was turned on.
129             try {  // Check to see if Orbot is installed.
130                 PackageManager packageManager = privacyBrowserContext.getPackageManager();
131                 packageManager.getPackageInfo("org.torproject.android", 0);
132
133                 // Ask Orbot to connect if its current status is not "ON".
134                 if (!MainWebViewActivity.orbotStatus.equals("ON")) {
135                     // Request Orbot to start.
136                     Intent orbotIntent = new Intent("org.torproject.android.intent.action.START");
137
138                     // Send the intent to the Orbot package.
139                     orbotIntent.setPackage("org.torproject.android");
140
141                     // Request a status response be sent back to this package.
142                     orbotIntent.putExtra("org.torproject.android.intent.extra.PACKAGE_NAME", privacyBrowserContext.getPackageName());
143
144                     // Make it so.
145                     privacyBrowserContext.sendBroadcast(orbotIntent);
146                 }
147             } catch (PackageManager.NameNotFoundException exception){  // If an exception is thrown, Orbot is not installed.
148                 // Use `AlertDialog.Builder` to create the `AlertDialog`.
149                 AlertDialog.Builder dialogBuilder;
150
151                 // Get a handle for the shared preferences.
152                 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(privacyBrowserContext);
153
154                 // Get the theme preference.
155                 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
156
157                 // Set the style according to the theme.
158                 if (darkTheme) {
159                     dialogBuilder = new AlertDialog.Builder(parentActivity, R.style.PrivacyBrowserAlertDialogDark);
160                 } else {
161                     dialogBuilder = new AlertDialog.Builder(parentActivity, R.style.PrivacyBrowserAlertDialogLight);
162                 }
163
164                 // Set the message.
165                 dialogBuilder.setMessage(R.string.orbot_proxy_not_installed);
166
167                 // Set the positive button.
168                 dialogBuilder.setPositiveButton(R.string.close, (DialogInterface dialog, int which) -> {
169                     // Do nothing.  The `AlertDialog` will close automatically.
170                 });
171
172                 // Convert `dialogBuilder` to `alertDialog`.
173                 AlertDialog alertDialog = dialogBuilder.create();
174
175                 // Make it so.
176                 alertDialog.show();
177             }
178         }
179     }
180 }