]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java
02f76466a7aeb592e84e3a73c8eeba4323457a18
[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.preference.PreferenceManager;
31 import android.util.ArrayMap;
32 import android.util.Log;
33
34 import androidx.appcompat.app.AlertDialog;
35
36 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
37 import com.stoutner.privacybrowser.R;
38
39 import java.lang.reflect.Field;
40 import java.lang.reflect.InvocationTargetException;
41 import java.lang.reflect.Method;
42
43 public class OrbotProxyHelper {
44     public static void setProxy(Context privacyBrowserContext, Activity parentActivity, String proxyHost, String proxyPort) {
45         if (proxyPort.equals("0")) {
46             // Clear the proxy values.
47             System.clearProperty("proxyHost");
48             System.clearProperty("proxyPort");
49         } else {
50             // Set the proxy values
51             System.setProperty("proxyHost", proxyHost);
52             System.setProperty("proxyPort", proxyPort);
53         }
54
55         // These entries shouldn't be needed if the above general settings are applied.  They are here for troubleshooting just in case.
56         // System.setProperty("http.proxyHost", proxyHost);
57         // System.setProperty("http.proxyPort", proxyPort);
58         // System.setProperty("https.proxyHost", proxyHost);
59         // System.setProperty("https.proxyPort", proxyPort);
60
61         // Use reflection to apply the new proxy values.
62         try {
63             // Get the application and APK classes.  Suppress the lint warning that reflection may not always work in the future and on all devices.
64             Class applicationClass = Class.forName("android.app.Application");
65             @SuppressLint("PrivateApi") Class loadedApkClass = Class.forName("android.app.LoadedApk");
66
67             // Get the declared fields.  Suppress the lint warning that `mLoadedApk` cannot be resolved.
68             @SuppressWarnings("JavaReflectionMemberAccess") Field mLoadedApkField = applicationClass.getDeclaredField("mLoadedApk");
69             Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");
70
71             // Allow the values to be changed.
72             mLoadedApkField.setAccessible(true);
73             mReceiversField.setAccessible(true);
74
75             // Get the APK object.
76             Object mLoadedApkObject = mLoadedApkField.get(privacyBrowserContext);
77
78             // Get an array map of the receivers.
79             ArrayMap receivers = (ArrayMap) mReceiversField.get(mLoadedApkObject);
80
81             // Set the proxy.
82             for (Object receiverMap : receivers.values()) {
83                 for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
84                     // `Class<?>`, which is an `unbounded wildcard parameterized type`, must be used instead of `Class`, which is a `raw type`.  Otherwise, `receiveClass.getDeclaredMethod` is unhappy.
85                     Class<?> receiverClass = receiver.getClass();
86
87                     // Get the declared fields.
88                     final Field[] declaredFieldArray = receiverClass.getDeclaredFields();
89
90                     // Set the proxy for each field that is a `ProxyChangeListener`.
91                     for (Field field : declaredFieldArray) {
92                         if (field.getType().getName().contains("ProxyChangeListener")) {
93                             Method onReceiveMethod = receiverClass.getDeclaredMethod("onReceive", Context.class, Intent.class);
94                             Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
95                             onReceiveMethod.invoke(receiver, privacyBrowserContext, intent);
96                         }
97                     }
98                 }
99             }
100         } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException exception) {
101             Log.d("enableProxyThroughOrbot", "Exception: " + exception);
102         }
103
104         if (proxyPort.equals("8118")) {  // Orbot proxy was turned on.
105             try {  // Check to see if Orbot is installed.
106                 PackageManager packageManager = privacyBrowserContext.getPackageManager();
107                 packageManager.getPackageInfo("org.torproject.android", 0);
108
109                 // Ask Orbot to connect if its current status is not "ON".
110                 if (!MainWebViewActivity.orbotStatus.equals("ON")) {
111                     // Request Orbot to start.
112                     Intent orbotIntent = new Intent("org.torproject.android.intent.action.START");
113
114                     // Send the intent to the Orbot package.
115                     orbotIntent.setPackage("org.torproject.android");
116
117                     // Request a status response be sent back to this package.
118                     orbotIntent.putExtra("org.torproject.android.intent.extra.PACKAGE_NAME", privacyBrowserContext.getPackageName());
119
120                     // Make it so.
121                     privacyBrowserContext.sendBroadcast(orbotIntent);
122                 }
123             } catch (PackageManager.NameNotFoundException exception){  // If an exception is thrown, Orbot is not installed.
124                 // Use `AlertDialog.Builder` to create the `AlertDialog`.
125                 AlertDialog.Builder dialogBuilder;
126
127                 // Get a handle for the shared preferences.
128                 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(privacyBrowserContext);
129
130                 // Get the theme preference.
131                 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
132
133                 // Set the style according to the theme.
134                 if (darkTheme) {
135                     dialogBuilder = new AlertDialog.Builder(parentActivity, R.style.PrivacyBrowserAlertDialogDark);
136                 } else {
137                     dialogBuilder = new AlertDialog.Builder(parentActivity, R.style.PrivacyBrowserAlertDialogLight);
138                 }
139
140                 // Set the message.
141                 dialogBuilder.setMessage(R.string.orbot_proxy_not_installed);
142
143                 // Set the positive button.
144                 dialogBuilder.setPositiveButton(R.string.close, (DialogInterface dialog, int which) -> {
145                     // Do nothing.  The `AlertDialog` will close automatically.
146                 });
147
148                 // Convert `dialogBuilder` to `alertDialog`.
149                 AlertDialog alertDialog = dialogBuilder.create();
150
151                 // Make it so.
152                 alertDialog.show();
153             }
154         }
155     }
156 }