mirror of
https://github.com/gsantner/dandelion
synced 2024-11-24 21:32:07 +01:00
Reworked and simplified again. Now it seems to work.
This commit is contained in:
parent
6d34e5c2cf
commit
fa2aaeb4fd
6 changed files with 148 additions and 159 deletions
|
@ -70,6 +70,12 @@
|
||||||
android:exported="false" >
|
android:exported="false" >
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
<receiver android:name=".util.OrbotStatusReceiver">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="org.torproject.android.intent.action.STATUS"/>
|
||||||
|
</intent-filter>
|
||||||
|
</receiver>
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|
||||||
|
|
|
@ -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.ContextMenuWebView;
|
||||||
import com.github.dfa.diaspora_android.ui.CustomWebViewClient;
|
import com.github.dfa.diaspora_android.ui.CustomWebViewClient;
|
||||||
import com.github.dfa.diaspora_android.util.Helpers;
|
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;
|
import org.json.JSONException;
|
||||||
|
|
||||||
|
@ -176,13 +176,12 @@ public class MainActivity extends AppCompatActivity
|
||||||
appSettings = app.getSettings();
|
appSettings = app.getSettings();
|
||||||
podUserProfile = new PodUserProfile(app, uiHandler, this);
|
podUserProfile = new PodUserProfile(app, uiHandler, this);
|
||||||
|
|
||||||
ProxyHandler.getInstance(this, appSettings).registerOrbotReceiver(this);
|
//Orbot integration
|
||||||
|
OrbotHelper.requestStartTor(this.getApplicationContext());
|
||||||
if(appSettings.isProxyOrbot()) {
|
if(appSettings.isProxyOrbot()) {
|
||||||
if(!OrbotHelper.isOrbotInstalled(this)) {
|
if(!OrbotHelper.isOrbotInstalled(this)) {
|
||||||
appSettings.setProxyOrbot(false);
|
appSettings.setProxyOrbot(false);
|
||||||
promptInstallOrbot();
|
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()) {
|
afterOnCreate(savedInstanceState);
|
||||||
ProxyHandler.getInstance(null, null).unregisterOrbotReceiver(this);
|
|
||||||
afterOnCreate(savedInstanceState);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void afterOnCreate(Bundle savedInstanceState) {
|
private void afterOnCreate(Bundle savedInstanceState) {
|
||||||
|
OrbotHelper.requestStartTor(this.getApplicationContext());
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
if (Helpers.isOnline(MainActivity.this)) {
|
if (Helpers.isOnline(MainActivity.this)) {
|
||||||
webView.loadData("", "text/html", null);
|
webView.loadData("", "text/html", null);
|
||||||
|
@ -422,9 +419,6 @@ public class MainActivity extends AppCompatActivity
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
try {
|
|
||||||
ProxyHandler.getInstance(null, null).registerOrbotReceiver(this);
|
|
||||||
} catch (IllegalStateException e){}
|
|
||||||
registerReceiver(brLoadUrl, new IntentFilter(URL_MESSAGE));
|
registerReceiver(brLoadUrl, new IntentFilter(URL_MESSAGE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -462,7 +456,6 @@ public class MainActivity extends AppCompatActivity
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
unregisterReceiver(brLoadUrl);
|
unregisterReceiver(brLoadUrl);
|
||||||
ProxyHandler.getInstance(null, null).unregisterOrbotReceiver(this);
|
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -983,12 +976,14 @@ public class MainActivity extends AppCompatActivity
|
||||||
.show();
|
.show();
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
if(appSettings.isProxyOrbot()) {
|
boolean before = appSettings.isProxyOrbot();
|
||||||
ProxyHandler.getInstance(null,null).setProxy(MainActivity.this, null, 0, ProxyHandler.NO_PROXY);
|
appSettings.setProxyOrbot(!before);
|
||||||
|
if(before) {
|
||||||
|
OrbotStatusReceiver.resetProxy(MainActivity.this.getApplicationContext());
|
||||||
} else {
|
} else {
|
||||||
OrbotHelper.requestStartTor(MainActivity.this);
|
OrbotHelper.requestStartTor(MainActivity.this);
|
||||||
}
|
}
|
||||||
appSettings.setProxyOrbot(!appSettings.isProxyOrbot());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).show();
|
}).show();
|
||||||
|
@ -1125,4 +1120,8 @@ public class MainActivity extends AppCompatActivity
|
||||||
startDialog.setNegativeButton(android.R.string.no, null);
|
startDialog.setNegativeButton(android.R.string.no, null);
|
||||||
startDialog.show();
|
startDialog.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean useOrbot() {
|
||||||
|
return appSettings.isProxyOrbot();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -139,6 +139,6 @@ public class AppSettings {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProxyOrbot(boolean active) {
|
public void setProxyOrbot(boolean active) {
|
||||||
setBool(prefApp, PREF.IS_PROXY_ORBOT, active);
|
prefApp.edit().putBoolean(PREF.IS_PROXY_ORBOT, active).commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,12 @@ import android.widget.Toast;
|
||||||
|
|
||||||
import com.github.dfa.diaspora_android.R;
|
import com.github.dfa.diaspora_android.R;
|
||||||
import com.github.dfa.diaspora_android.activity.MainActivity;
|
import com.github.dfa.diaspora_android.activity.MainActivity;
|
||||||
|
import com.github.dfa.diaspora_android.util.OrbotStatusReceiver;
|
||||||
|
|
||||||
import java.io.File;
|
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
|
* Subclass of WebView which adds a context menu for long clicks on images or links to share, save
|
||||||
* or open with another browser
|
* or open with another browser
|
||||||
|
@ -149,4 +152,17 @@ public class ContextMenuWebView extends NestedWebView {
|
||||||
public void setParentActivity(Activity activity) {
|
public void setParentActivity(Activity activity) {
|
||||||
this.parentActivity = 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);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue