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