From fa2aaeb4fd3c50056751150a58942b2341d0970c Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Mon, 6 Jun 2016 11:12:27 +0200 Subject: [PATCH] Reworked and simplified again. Now it seems to work. --- app/src/main/AndroidManifest.xml | 6 + .../activity/MainActivity.java | 29 ++-- .../diaspora_android/data/AppSettings.java | 2 +- .../ui/ContextMenuWebView.java | 16 ++ .../util/OrbotStatusReceiver.java | 111 ++++++++++++++ .../diaspora_android/util/ProxyHandler.java | 143 ------------------ 6 files changed, 148 insertions(+), 159 deletions(-) create mode 100644 app/src/main/java/com/github/dfa/diaspora_android/util/OrbotStatusReceiver.java delete mode 100644 app/src/main/java/com/github/dfa/diaspora_android/util/ProxyHandler.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d89beb83..53d03bd7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -70,6 +70,12 @@ android:exported="false" > + + + + + + diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index 708d5fd0..0b707629 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -82,7 +82,7 @@ import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener; import com.github.dfa.diaspora_android.ui.ContextMenuWebView; import com.github.dfa.diaspora_android.ui.CustomWebViewClient; import com.github.dfa.diaspora_android.util.Helpers; -import com.github.dfa.diaspora_android.util.ProxyHandler; +import com.github.dfa.diaspora_android.util.OrbotStatusReceiver; import org.json.JSONException; @@ -176,13 +176,12 @@ public class MainActivity extends AppCompatActivity appSettings = app.getSettings(); podUserProfile = new PodUserProfile(app, uiHandler, this); - ProxyHandler.getInstance(this, appSettings).registerOrbotReceiver(this); + //Orbot integration + OrbotHelper.requestStartTor(this.getApplicationContext()); if(appSettings.isProxyOrbot()) { if(!OrbotHelper.isOrbotInstalled(this)) { appSettings.setProxyOrbot(false); promptInstallOrbot(); - } else { - Toast.makeText(this, "Orbot Proxy: "+ProxyHandler.getInstance(null, null).getActiveProxy(), Toast.LENGTH_SHORT).show(); } } @@ -318,13 +317,11 @@ public class MainActivity extends AppCompatActivity } }); - if(!appSettings.isProxyOrbot()) { - ProxyHandler.getInstance(null, null).unregisterOrbotReceiver(this); - afterOnCreate(savedInstanceState); - } + afterOnCreate(savedInstanceState); } private void afterOnCreate(Bundle savedInstanceState) { + OrbotHelper.requestStartTor(this.getApplicationContext()); if (savedInstanceState == null) { if (Helpers.isOnline(MainActivity.this)) { webView.loadData("", "text/html", null); @@ -422,9 +419,6 @@ public class MainActivity extends AppCompatActivity @Override protected void onResume() { super.onResume(); - try { - ProxyHandler.getInstance(null, null).registerOrbotReceiver(this); - } catch (IllegalStateException e){} registerReceiver(brLoadUrl, new IntentFilter(URL_MESSAGE)); } @@ -462,7 +456,6 @@ public class MainActivity extends AppCompatActivity @Override protected void onPause() { unregisterReceiver(brLoadUrl); - ProxyHandler.getInstance(null, null).unregisterOrbotReceiver(this); super.onPause(); } @@ -983,12 +976,14 @@ public class MainActivity extends AppCompatActivity .show(); break; case 4: - if(appSettings.isProxyOrbot()) { - ProxyHandler.getInstance(null,null).setProxy(MainActivity.this, null, 0, ProxyHandler.NO_PROXY); + boolean before = appSettings.isProxyOrbot(); + appSettings.setProxyOrbot(!before); + if(before) { + OrbotStatusReceiver.resetProxy(MainActivity.this.getApplicationContext()); } else { OrbotHelper.requestStartTor(MainActivity.this); } - appSettings.setProxyOrbot(!appSettings.isProxyOrbot()); + } } }).show(); @@ -1125,4 +1120,8 @@ public class MainActivity extends AppCompatActivity startDialog.setNegativeButton(android.R.string.no, null); startDialog.show(); } + + public boolean useOrbot() { + return appSettings.isProxyOrbot(); + } } \ No newline at end of file diff --git a/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java b/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java index 5e668386..8c439e18 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java @@ -139,6 +139,6 @@ public class AppSettings { } public void setProxyOrbot(boolean active) { - setBool(prefApp, PREF.IS_PROXY_ORBOT, active); + prefApp.edit().putBoolean(PREF.IS_PROXY_ORBOT, active).commit(); } } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java b/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java index 6b42b313..e450e538 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java @@ -20,9 +20,12 @@ import android.widget.Toast; import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.activity.MainActivity; +import com.github.dfa.diaspora_android.util.OrbotStatusReceiver; import java.io.File; +import info.guardianproject.netcipher.proxy.OrbotHelper; + /** * Subclass of WebView which adds a context menu for long clicks on images or links to share, save * or open with another browser @@ -149,4 +152,17 @@ public class ContextMenuWebView extends NestedWebView { public void setParentActivity(Activity activity) { this.parentActivity = activity; } + + @Override + public void reload() { + OrbotHelper.requestStartTor(context.getApplicationContext()); + super.reload(); + } + + @Override + public void loadUrl(String url) { + if(!OrbotStatusReceiver.isProxySet()) + OrbotHelper.requestStartTor(context.getApplicationContext()); + super.loadUrl(url); + } } \ No newline at end of file diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/OrbotStatusReceiver.java b/app/src/main/java/com/github/dfa/diaspora_android/util/OrbotStatusReceiver.java new file mode 100644 index 00000000..b94e9fdd --- /dev/null +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/OrbotStatusReceiver.java @@ -0,0 +1,111 @@ +/* + This file is part of the Diaspora for Android. + + Diaspora for Android is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Diaspora for Android is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with the Diaspora for Android. + + If not, see . + */ + +package com.github.dfa.diaspora_android.util; + +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +import com.github.dfa.diaspora_android.App; +import com.github.dfa.diaspora_android.activity.MainActivity; +import com.github.dfa.diaspora_android.data.AppSettings; + +import info.guardianproject.netcipher.NetCipher; +import info.guardianproject.netcipher.proxy.OrbotHelper; +import info.guardianproject.netcipher.web.WebkitProxy; + +/** + * BroadcastReceiver that handles Orbot status intents and sets the proxy. + * Created by vanitas on 06.06.16. + */ +public class OrbotStatusReceiver extends BroadcastReceiver { + + private Intent lastStatus; + private Context lastContext; + private AppSettings appSettings; + private static boolean proxySet = false; + + public OrbotStatusReceiver() { + } + + @Override + public void onReceive(Context context, Intent intent) { + if(OrbotHelper.ACTION_STATUS.equals(intent.getAction())) { + lastStatus = intent; + lastContext = context; + if(appSettings == null) appSettings = new AppSettings(context.getApplicationContext()); + String orbotStatus = intent.getExtras().getString(OrbotHelper.EXTRA_STATUS); + if(appSettings.isProxyOrbot()) { + if (orbotStatus.equals(OrbotHelper.STATUS_ON) && !proxySet) { + setProxy(lastContext, lastStatus); + } else if(orbotStatus.equals(OrbotHelper.STATUS_OFF)) { + Log.d(App.TAG, "Warning: Orbot reports status off."); + OrbotHelper.requestStartTor(context.getApplicationContext()); + } else if(orbotStatus.equals(OrbotHelper.STATUS_STARTS_DISABLED)) { + Log.d(App.TAG, "Warning: Orbot has background starts disabled."); + } + } + } else { + Log.e(App.TAG, "Warning: Intents action "+intent.getAction()+ " does not equal "+OrbotHelper.ACTION_STATUS); + } + } + + public static void setProxy(Context context, Intent intent) { + if(intent != null) { + String status = intent.getStringExtra(OrbotHelper.EXTRA_STATUS); + if(status.equals(OrbotHelper.STATUS_ON)) { + try { + NetCipher.setProxy("127.0.0.1", 8118); + WebkitProxy.setProxy(MainActivity.class.getName(), context.getApplicationContext(), null, "127.0.0.1", 8118); + Log.d(App.TAG, "Proxy successfully set."); + proxySet = true; + } catch(Exception e) { + Log.e(App.TAG, "setProxy failed: "); + e.printStackTrace(); + } + } + } else { + Log.e(App.TAG, "OrbotStatusReceiver: lastStatus intent is null. Cannot set Proxy."); + } + } + + public static void resetProxy(Context context) { + try { + NetCipher.clearProxy(); + WebkitProxy.resetProxy(MainActivity.class.getName(), context.getApplicationContext()); + } catch (Exception e) { + //Fails in any case on android 6. Ignore it and restart application. + } + proxySet = false; + //Restart application + Intent restartActivity = new Intent(context, MainActivity.class); + PendingIntent pendingIntent = PendingIntent.getActivity(context, 12374, restartActivity, PendingIntent.FLAG_CANCEL_CURRENT); + AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent); + System.exit(0); + } + + public static boolean isProxySet() { + return proxySet; + } +} diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/ProxyHandler.java b/app/src/main/java/com/github/dfa/diaspora_android/util/ProxyHandler.java deleted file mode 100644 index 7cbb325d..00000000 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/ProxyHandler.java +++ /dev/null @@ -1,143 +0,0 @@ -package com.github.dfa.diaspora_android.util; - -import android.app.AlarmManager; -import android.app.PendingIntent; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.util.Log; - -import com.github.dfa.diaspora_android.App; -import com.github.dfa.diaspora_android.activity.MainActivity; -import com.github.dfa.diaspora_android.data.AppSettings; - -import info.guardianproject.netcipher.NetCipher; -import info.guardianproject.netcipher.proxy.OrbotHelper; -import info.guardianproject.netcipher.web.WebkitProxy; - -/** - * Handle proxy configurations - * In this particular case integration of Orbot as a proxy for the tor network has been done, - * but other proxies can easily and similarly be added as well. - * Created by vanitas on 05.06.16. - */ -public class ProxyHandler { - //TODO: Remove when NetCipher > 1.2.1 releases - public final static String EXTRA_PROXY_PORT_HTTP = "org.torproject.android.intent.extra.HTTP_PROXY_PORT"; - //Proxy types - public static final int NO_PROXY = 0, ORBOT_PROXY = 1; - private int activeProxy = NO_PROXY; - - private static ProxyHandler instance; - private MainActivity mainActivity; - private AppSettings appSettings; - private OrbotReceiver orbotReceiver; - - - private ProxyHandler(MainActivity main, AppSettings settings) { - orbotReceiver = new OrbotReceiver(); - mainActivity = main; - appSettings = settings; - } - - public static ProxyHandler getInstance(MainActivity main, AppSettings settings) { - if(instance == null) instance = new ProxyHandler(main, settings); - return instance; - } - - public void registerOrbotReceiver(Context context) { - if(!orbotReceiver.isRegistered()) { - context.registerReceiver(orbotReceiver, new IntentFilter(OrbotHelper.ACTION_STATUS)); - orbotReceiver.setRegistered(true); - } - else throw new IllegalStateException("OrbotReceiver is already registered."); - } - - public void unregisterOrbotReceiver(Context context) { - if(orbotReceiver.isRegistered()) { - context.unregisterReceiver(orbotReceiver); - orbotReceiver.setRegistered(false); - } - else throw new IllegalStateException("OrbotReceiver was not registered and can therefore not be unregistered."); - } - - public void setProxy(Context context, String host, int port, int proxyType) { - if(proxyType == NO_PROXY) { - try { - NetCipher.clearProxy(); - WebkitProxy.resetProxy(MainActivity.class.getName(), context); - activeProxy = proxyType; - } - catch (Exception e) { - Log.e(App.TAG, "ProxyHandler caught exception "+e.getClass().getName()+" while resetting proxy."); - } - restartApplication(context); - return; - } - - if(proxyType == ORBOT_PROXY) { - try { - NetCipher.setProxy(host, port); - WebkitProxy.setProxy(MainActivity.class.getName(), context.getApplicationContext(), null, host, port); - activeProxy = proxyType; - } catch (Exception e) { - Log.d(App.TAG, "ProxyHandler caught exception " + e.getClass().getName() + " while setting proxy."); - e.printStackTrace(); - } - } - - //Add further proxies here - } - - private static void restartApplication(Context context) { - Intent mStartActivity = new Intent(context, MainActivity.class); - PendingIntent mPendingIntent = PendingIntent.getActivity(context, 12374, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT); - AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); - mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); - System.exit(0); - } - - private static class OrbotReceiver extends BroadcastReceiver { - private boolean registered; - - private boolean orbotRunning = false; - private int proxyPort = -1; - private String proxyHost = "127.0.0.1"; - - - @Override - public void onReceive(Context context, Intent intent) { - if(OrbotHelper.ACTION_STATUS.equals(intent.getAction())) { - - if(OrbotHelper.STATUS_ON.equals(intent.getStringExtra(OrbotHelper.EXTRA_STATUS))) { - proxyPort = intent.getIntExtra(EXTRA_PROXY_PORT_HTTP, -1); - if(instance.appSettings.isProxyOrbot()) { - instance.setProxy(context, proxyHost, proxyPort, ORBOT_PROXY); - } - orbotRunning = true; - } - - if(OrbotHelper.STATUS_OFF.equals(intent.getStringExtra(OrbotHelper.EXTRA_STATUS))) { - instance.mainActivity.requestOrbotStart(false); - } - - if(OrbotHelper.STATUS_STARTS_DISABLED.equals(intent.getStringExtra(OrbotHelper.EXTRA_STATUS))) { - instance.mainActivity.requestOrbotStart(true); - } - } - } - - public boolean isRegistered() { - return registered; - } - - public void setRegistered(boolean r) { - registered = r; - } - } - - public int getActiveProxy() { - return activeProxy; - } -}