]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java
Release 2.8.1.
[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
43         // Set the proxy values
44         System.setProperty("http.proxyHost", proxyHost);
45         System.setProperty("http.proxyPort", proxyPort);
46         System.setProperty("https.proxyHost", proxyHost);
47         System.setProperty("https.proxyPort", proxyPort);
48
49         // Use reflection to apply the new proxy values.
50         try {
51             Class applicationClass = Class.forName("android.app.Application");
52             Field mLoadedApkField = applicationClass.getDeclaredField("mLoadedApk");
53             // `setAccessible(true)` allows us to change the value of `mLoadedApkField`.
54             mLoadedApkField.setAccessible(true);
55             Object mLoadedApkObject = mLoadedApkField.get(privacyBrowserContext);
56
57             @SuppressLint("PrivateApi") Class loadedApkClass = Class.forName("android.app.LoadedApk");
58             Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");
59             // `setAccessible(true)` allows us to change the value of `mReceiversField`.
60             mReceiversField.setAccessible(true);
61
62             ArrayMap receivers = (ArrayMap) mReceiversField.get(mLoadedApkObject);
63
64             for (Object receiverMap : receivers.values()) {
65                 for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
66                     // We have to use `Class<?>`, which is an `unbounded wildcard parameterized type`, instead of `Class`, which is a `raw type`.
67                     // Otherwise, `receiveClass.getDeclaredMethod` below will produce an error.
68                     Class<?> receiverClass = receiver.getClass();
69                     if (receiverClass.getName().contains("ProxyChangeListener")) {
70                         Method onReceiveMethod = receiverClass.getDeclaredMethod("onReceive", Context.class, Intent.class);
71                         Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
72                         onReceiveMethod.invoke(receiver, privacyBrowserContext, intent);
73                     }
74                 }
75             }
76         } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException exception) {
77             Log.d("enableProxyThroughOrbot", "Exception: " + exception);
78         }
79
80         if (proxyPort.equals("8118")) {  // Orbot proxy was turned on.
81             try {  // Check to see if Orbot is installed.
82                 PackageManager packageManager = privacyBrowserContext.getPackageManager();
83                 packageManager.getPackageInfo("org.torproject.android", PackageManager.GET_ACTIVITIES);
84
85                 // Ask Orbot to connect if its current status is not "ON".
86                 if (!MainWebViewActivity.orbotStatus.equals("ON")) {
87                     // Request Orbot to start.
88                     Intent orbotIntent = new Intent("org.torproject.android.intent.action.START");
89
90                     // Send the intent to the Orbot package.
91                     orbotIntent.setPackage("org.torproject.android");
92
93                     // Request a status response be sent back to this package.
94                     orbotIntent.putExtra("org.torproject.android.intent.extra.PACKAGE_NAME", privacyBrowserContext.getPackageName());
95
96                     // Make it so.
97                     privacyBrowserContext.sendBroadcast(orbotIntent);
98                 }
99             } catch (PackageManager.NameNotFoundException exception){  // If an exception is thrown, Orbot is not installed.
100                 // Use `AlertDialog.Builder` to create the `AlertDialog`.
101                 AlertDialog.Builder dialogBuilder;
102
103                 // Set the style according to the theme.
104                 if (MainWebViewActivity.darkTheme) {
105                     dialogBuilder = new AlertDialog.Builder(parentActivity, R.style.PrivacyBrowserAlertDialogDark);
106                 } else {
107                     dialogBuilder = new AlertDialog.Builder(parentActivity, R.style.PrivacyBrowserAlertDialogLight);
108                 }
109
110                 // Set the message.
111                 dialogBuilder.setMessage(R.string.orbot_proxy_not_installed);
112
113                 // Set the positive button.
114                 dialogBuilder.setPositiveButton(R.string.close, (DialogInterface dialog, int which) -> {
115                     // Do nothing.  The `AlertDialog` will close automatically.
116                 });
117
118                 // Convert `dialogBuilder` to `alertDialog`.
119                 AlertDialog alertDialog = dialogBuilder.create();
120
121                 // Make it so.
122                 alertDialog.show();
123             }
124         }
125     }
126 }