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