From 6d34e5c2cf935a6d889744e2e352557a1b281554 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Sun, 5 Jun 2016 19:30:31 +0200 Subject: [PATCH 1/9] Reworked basic orbot integration --- app/build.gradle | 1 + .../activity/MainActivity.java | 80 +++++++++- .../diaspora_android/data/AppSettings.java | 9 ++ .../diaspora_android/util/ProxyHandler.java | 143 ++++++++++++++++++ app/src/main/res/values/strings.xml | 11 ++ 5 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/github/dfa/diaspora_android/util/ProxyHandler.java diff --git a/app/build.gradle b/app/build.gradle index 3072760b..a7f02870 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,6 +37,7 @@ dependencies { // More libraries compile 'com.getbase:floatingactionbutton:1.9.1' compile 'com.jakewharton:butterknife:8.0.1' + compile 'info.guardianproject.netcipher:netcipher:1.2.1' apt 'com.jakewharton:butterknife-compiler:8.0.1' } 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 4183f68e..708d5fd0 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,6 +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 org.json.JSONException; @@ -97,6 +98,7 @@ import java.util.Locale; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; +import info.guardianproject.netcipher.proxy.OrbotHelper; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, WebUserProfileChangedListener { @@ -174,6 +176,16 @@ public class MainActivity extends AppCompatActivity appSettings = app.getSettings(); podUserProfile = new PodUserProfile(app, uiHandler, this); + ProxyHandler.getInstance(this, appSettings).registerOrbotReceiver(this); + 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(); + } + } + this.registerForContextMenu(webView); webView.setParentActivity(this); webView.setOverScrollMode(WebView.OVER_SCROLL_ALWAYS); @@ -306,7 +318,13 @@ public class MainActivity extends AppCompatActivity } }); + if(!appSettings.isProxyOrbot()) { + ProxyHandler.getInstance(null, null).unregisterOrbotReceiver(this); + afterOnCreate(savedInstanceState); + } + } + private void afterOnCreate(Bundle savedInstanceState) { if (savedInstanceState == null) { if (Helpers.isOnline(MainActivity.this)) { webView.loadData("", "text/html", null); @@ -404,6 +422,9 @@ 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)); } @@ -441,6 +462,7 @@ public class MainActivity extends AppCompatActivity @Override protected void onPause() { unregisterReceiver(brLoadUrl); + ProxyHandler.getInstance(null, null).unregisterOrbotReceiver(this); super.onPause(); } @@ -926,7 +948,8 @@ public class MainActivity extends AppCompatActivity case R.id.nav_settings_app: { final CharSequence[] options = {getString(R.string.settings_font), getString(R.string.settings_view), appSettings.isLoadImages() ? - getString(R.string.settings_images_switch_off) : getString(R.string.settings_images_switch_on), getString(R.string.jb_pod)}; + getString(R.string.settings_images_switch_off) : getString(R.string.settings_images_switch_on), getString(R.string.jb_pod), + appSettings.isProxyOrbot() ? getString(R.string.orbot_proxy_enabled) : getString(R.string.orbot_proxy_disabled)}; if (Helpers.isOnline(MainActivity.this)) { new AlertDialog.Builder(MainActivity.this) @@ -959,6 +982,13 @@ public class MainActivity extends AppCompatActivity }) .show(); break; + case 4: + if(appSettings.isProxyOrbot()) { + ProxyHandler.getInstance(null,null).setProxy(MainActivity.this, null, 0, ProxyHandler.NO_PROXY); + } else { + OrbotHelper.requestStartTor(MainActivity.this); + } + appSettings.setProxyOrbot(!appSettings.isProxyOrbot()); } } }).show(); @@ -1029,7 +1059,7 @@ public class MainActivity extends AppCompatActivity } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main__layout); - drawer.closeDrawer(GravityCompat.START); + if(drawer != null) drawer.closeDrawer(GravityCompat.START); return true; } @@ -1049,4 +1079,50 @@ public class MainActivity extends AppCompatActivity grantResults); } } + + /** + * Ask the user whether to install Orbot or not. Check if installing from + * F-Droid or Google Play, otherwise take the user to the Orbot download + * page on f-droid.org. + */ + void promptInstallOrbot() { + String message = this.getString(R.string.you_must_have_orbot) + " "; + + final Intent intent = OrbotHelper.getOrbotInstallIntent(MainActivity.this); + if (intent.getPackage() == null) { + message += MainActivity.this.getString(R.string.download_orbot_from_fdroid); + } else { + message += MainActivity.this.getString(R.string.get_orbot_from_fdroid); + } + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.install_orbot_); + builder.setMessage(message); + builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + MainActivity.this.startActivity(intent); + } + }); + builder.setNegativeButton(android.R.string.no, null); + builder.show(); + } + + public void requestOrbotStart(boolean backgroundStartsDisabled) { + AlertDialog.Builder startDialog = new AlertDialog.Builder(this); + startDialog.setTitle(R.string.start_orbot_); + if(backgroundStartsDisabled) + startDialog.setMessage(R.string.orbot_starts_disabled_message); + else + startDialog.setMessage(R.string.orbot_doesn_t_appear_to_be_running_would_you_like_to_start_it_up_and_connect_to_tor_); + + startDialog.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + startActivityForResult(OrbotHelper.getShowOrbotStartIntent(), 1); + } + }); + startDialog.setNegativeButton(android.R.string.no, null); + startDialog.show(); + } } \ 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 12b0dd57..5e668386 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 @@ -65,6 +65,7 @@ public class AppSettings { private static final String PODUSERPROFILE_NAME = "podUserProfile_name"; private static final String PODUSERPROFILE_ID = "podUserProfile_guid"; private static final String PODDOMAIN = "podDomain"; + private static final String IS_PROXY_ORBOT = "proxyViaOrbot"; } @@ -132,4 +133,12 @@ public class AppSettings { public void setPreviousPodlist(String[] pods){ setStringArray(prefApp, PREF.PREVIOUS_PODLIST, pods); } + + public boolean isProxyOrbot() { + return prefApp.getBoolean(PREF.IS_PROXY_ORBOT, false); + } + + public void setProxyOrbot(boolean active) { + setBool(prefApp, PREF.IS_PROXY_ORBOT, active); + } } 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 new file mode 100644 index 00000000..7cbb325d --- /dev/null +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/ProxyHandler.java @@ -0,0 +1,143 @@ +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; + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ac6920ca..2fca7364 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -211,4 +211,15 @@ https:// Share… #DiasporaForAndroid + + Install Orbot? + You must have Orbot installed and activated to proxy traffic through it. + Would you like to install it from F-Droid? + Would you like to download it from f-droid.org? + Start Orbot? + Orbot doesn\'t appear to be running. Would you like to start it up and connect to Tor? + Orbot has background starts disabled. Would you like to open Orbot? + Orbot Proxy: Enabled + Orbot Proxy: Disabled + From fa2aaeb4fd3c50056751150a58942b2341d0970c Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Mon, 6 Jun 2016 11:12:27 +0200 Subject: [PATCH 2/9] 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; - } -} From 8ebb8b18fb2d3754d34e43dd2ebbac1713759674 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Mon, 6 Jun 2016 11:32:23 +0200 Subject: [PATCH 3/9] Added german strings and reload webview after setting proxy --- .../dfa/diaspora_android/activity/MainActivity.java | 1 + .../dfa/diaspora_android/ui/ContextMenuWebView.java | 1 + app/src/main/res/values-de/strings.xml | 9 +++++++++ 3 files changed, 11 insertions(+) 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 0b707629..350e1b46 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 @@ -982,6 +982,7 @@ public class MainActivity extends AppCompatActivity OrbotStatusReceiver.resetProxy(MainActivity.this.getApplicationContext()); } else { OrbotHelper.requestStartTor(MainActivity.this); + webView.reload(); } } 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 e450e538..b7841ab7 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 @@ -24,6 +24,7 @@ import com.github.dfa.diaspora_android.util.OrbotStatusReceiver; import java.io.File; +import info.guardianproject.netcipher.NetCipher; import info.guardianproject.netcipher.proxy.OrbotHelper; /** diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 977b8857..fc3c09c6 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -166,5 +166,14 @@ along with this program. If not, see http://www.gnu.org/licenses.<br> <br Speichere Bild als Linkadresse kopiert … Teilen… + Möchten Sie Orbot nun von f-droid.org herunterladen? + Möchten Sie Orbot nun über F-Droid installieren? + Orbot installieren? + Orbot scheint nicht aktiv zu sein. Möchten Sie es nun starten um sich mit dem Tor-Netzwerk zu verbinden? + Orbot-Proxy: Deaktiviert + Orbot-Proxy: Aktiviert + Orbot scheint Hintergrund-Starts deaktiviert zu haben. Möchten Sie Orbot jetzt öffnen? + Orbot starten? + Um das Tor-Netzwerk als Proxy nutzen zu können, müssen Sie Orbot installieren. From 68d7bdaa407097fdd88e2d2b1babb52e5eb4652f Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Mon, 6 Jun 2016 11:50:04 +0200 Subject: [PATCH 4/9] Precautionary set Orbot proxy, if configured to do so. Handle errors like Orbot not installed/running etc. afterwards. --- .../activity/MainActivity.java | 4 +++ .../util/OrbotStatusReceiver.java | 30 +++++++++++++------ 2 files changed, 25 insertions(+), 9 deletions(-) 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 d8efacbd..3fbb29cc 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 @@ -179,11 +179,15 @@ public class MainActivity extends AppCompatActivity podUserProfile.setListener(this); //Orbot integration + OrbotStatusReceiver.setMainActivity(this); OrbotHelper.requestStartTor(this.getApplicationContext()); if(appSettings.isProxyOrbot()) { if(!OrbotHelper.isOrbotInstalled(this)) { appSettings.setProxyOrbot(false); promptInstallOrbot(); + } else { + //precautionary set Proxy + OrbotStatusReceiver.setProxy(this.getApplicationContext(), OrbotStatusReceiver.defaultHost, OrbotStatusReceiver.defaultPort); } } 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 index b94e9fdd..7a561f40 100644 --- 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 @@ -40,8 +40,11 @@ import info.guardianproject.netcipher.web.WebkitProxy; */ public class OrbotStatusReceiver extends BroadcastReceiver { + public static final String defaultHost = "127.0.0.1"; + public static final int defaultPort = 8118; private Intent lastStatus; private Context lastContext; + private static MainActivity mainActivity; private AppSettings appSettings; private static boolean proxySet = false; @@ -63,6 +66,7 @@ public class OrbotStatusReceiver extends BroadcastReceiver { OrbotHelper.requestStartTor(context.getApplicationContext()); } else if(orbotStatus.equals(OrbotHelper.STATUS_STARTS_DISABLED)) { Log.d(App.TAG, "Warning: Orbot has background starts disabled."); + if(mainActivity != null) mainActivity.requestOrbotStart(true); } } } else { @@ -74,21 +78,25 @@ public class OrbotStatusReceiver extends BroadcastReceiver { 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(); - } + setProxy(context, defaultHost, defaultPort); } } else { Log.e(App.TAG, "OrbotStatusReceiver: lastStatus intent is null. Cannot set Proxy."); } } + public static void setProxy(Context context, String host, int port) { + try { + NetCipher.setProxy(host, port); + WebkitProxy.setProxy(MainActivity.class.getName(), context.getApplicationContext(), null, host, port); + Log.d(App.TAG, "Proxy successfully set."); + proxySet = true; + } catch(Exception e) { + Log.e(App.TAG, "setProxy failed: "); + e.printStackTrace(); + } + } + public static void resetProxy(Context context) { try { NetCipher.clearProxy(); @@ -108,4 +116,8 @@ public class OrbotStatusReceiver extends BroadcastReceiver { public static boolean isProxySet() { return proxySet; } + + public static void setMainActivity(MainActivity main) { + mainActivity = main; + } } From 53e65eb75665123829b53214b48acb63447acc2d Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Mon, 6 Jun 2016 12:00:19 +0200 Subject: [PATCH 5/9] Use NetCipher.getHttpsUrlConnection in GetPodsService --- .../dfa/diaspora_android/task/GetPodsService.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java b/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java index 8791ac00..c3aac33c 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java @@ -44,6 +44,10 @@ import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; +import javax.net.ssl.HttpsURLConnection; + +import info.guardianproject.netcipher.NetCipher; + public class GetPodsService extends Service { public static final String MESSAGE_PODS_RECEIVED = "com.github.dfa.diaspora.podsreceived"; private static final String TAG = App.TAG; @@ -73,16 +77,13 @@ public class GetPodsService extends Service { // TODO: Update deprecated code StringBuilder builder = new StringBuilder(); - HttpClient client = new DefaultHttpClient(); + //HttpClient client = new DefaultHttpClient(); List list = null; try { - HttpGet httpGet = new HttpGet("http://podupti.me/api.php?key=4r45tg&format=json"); - HttpResponse response = client.execute(httpGet); - StatusLine statusLine = response.getStatusLine(); - int statusCode = statusLine.getStatusCode(); + HttpsURLConnection connection = NetCipher.getHttpsURLConnection("http://podupti.me/api.php?key=4r45tg&format=json"); + int statusCode = connection.getResponseCode(); if (statusCode == 200) { - HttpEntity entity = response.getEntity(); - InputStream content = entity.getContent(); + InputStream content = connection.getInputStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(content)); String line; From 595a0d779404d82bacd2a29a4927272186a88bbe Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Mon, 6 Jun 2016 12:09:24 +0200 Subject: [PATCH 6/9] Let ImageDownloadTask and ProfileFetchTask use NetCipher and remove unused imports --- .../github/dfa/diaspora_android/task/GetPodsService.java | 9 +-------- .../dfa/diaspora_android/task/ImageDownloadTask.java | 5 ++++- .../dfa/diaspora_android/task/ProfileFetchTask.java | 7 +++++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java b/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java index c3aac33c..9d39bfa1 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java @@ -28,12 +28,6 @@ import android.util.Log; import com.github.dfa.diaspora_android.App; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONObject; @@ -80,7 +74,7 @@ public class GetPodsService extends Service { //HttpClient client = new DefaultHttpClient(); List list = null; try { - HttpsURLConnection connection = NetCipher.getHttpsURLConnection("http://podupti.me/api.php?key=4r45tg&format=json"); + HttpsURLConnection connection = NetCipher.getHttpsURLConnection("https://podupti.me/api.php?key=4r45tg&format=json"); int statusCode = connection.getResponseCode(); if (statusCode == 200) { InputStream content = connection.getInputStream(); @@ -91,7 +85,6 @@ public class GetPodsService extends Service { builder.append(line); } } else { - //TODO Notify User about failure Log.e(TAG, "Failed to download list of pods"); } } catch (IOException e) { diff --git a/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java b/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java index 2b5a3d23..9d4f6aac 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java @@ -13,6 +13,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import info.guardianproject.netcipher.NetCipher; + /** * Created by Gregor Santner (gsantner) on 24.03.16. */ @@ -36,7 +38,8 @@ public class ImageDownloadTask extends AsyncTask { Bitmap bitmap = null; FileOutputStream out = null; try { - InputStream in = new java.net.URL(url).openStream(); + //InputStream in = new java.net.URL(url).openStream(); + InputStream in = NetCipher.getHttpsURLConnection(url).getInputStream(); bitmap = BitmapFactory.decodeStream(in); // Save to file if not null diff --git a/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java b/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java index f54774f0..ef66e4ba 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java @@ -11,9 +11,12 @@ import com.github.dfa.diaspora_android.data.PodUserProfile; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.net.HttpURLConnection; import java.net.URL; +import javax.net.ssl.HttpsURLConnection; + +import info.guardianproject.netcipher.NetCipher; + /** * Created by Gregor Santner (gsantner) on 30.03.16. */ @@ -39,7 +42,7 @@ public class ProfileFetchTask extends AsyncTask { try { URL url = new URL("https://" + app.getSettings().getPodDomain() + "/stream"); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + HttpsURLConnection conn = NetCipher.getHttpsURLConnection(url); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.setRequestMethod("GET"); From d336152238f3898818505bc6b69256c63ebbc8b9 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Mon, 6 Jun 2016 12:36:05 +0200 Subject: [PATCH 7/9] Respect custom orbot proxy port/host configuration --- .../util/OrbotStatusReceiver.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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 index 7a561f40..71012d64 100644 --- 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 @@ -40,8 +40,13 @@ import info.guardianproject.netcipher.web.WebkitProxy; */ public class OrbotStatusReceiver extends BroadcastReceiver { + public static final String EXTRA_HTTP_HOST = "org.torproject.android.intent.extra.HTTP_PROXY_HOST"; + public static final String EXTRA_HTTP_PORT = "org.torproject.android.intent.extra.HTTP_PROXY_PORT"; public static final String defaultHost = "127.0.0.1"; public static final int defaultPort = 8118; + + private static String host = ""; + private static int port = 0; private Intent lastStatus; private Context lastContext; private static MainActivity mainActivity; @@ -59,7 +64,7 @@ public class OrbotStatusReceiver extends BroadcastReceiver { 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) { + if (orbotStatus.equals(OrbotHelper.STATUS_ON)) { setProxy(lastContext, lastStatus); } else if(orbotStatus.equals(OrbotHelper.STATUS_OFF)) { Log.d(App.TAG, "Warning: Orbot reports status off."); @@ -78,7 +83,16 @@ public class OrbotStatusReceiver extends BroadcastReceiver { if(intent != null) { String status = intent.getStringExtra(OrbotHelper.EXTRA_STATUS); if(status.equals(OrbotHelper.STATUS_ON)) { - setProxy(context, defaultHost, defaultPort); + String nHost = intent.getExtras().getString(EXTRA_HTTP_HOST, null); + int nPort = intent.getIntExtra(EXTRA_HTTP_PORT, -1); + //Got no values from intent + if((nHost == null || nPort == -1)) { + if(host.equals("") || port == 0) { + setProxy(context, defaultHost, defaultPort); + } + } else { + setProxy(context, nHost, nPort); + } } } else { Log.e(App.TAG, "OrbotStatusReceiver: lastStatus intent is null. Cannot set Proxy."); @@ -87,6 +101,8 @@ public class OrbotStatusReceiver extends BroadcastReceiver { public static void setProxy(Context context, String host, int port) { try { + OrbotStatusReceiver.host = host; + OrbotStatusReceiver.port = port; NetCipher.setProxy(host, port); WebkitProxy.setProxy(MainActivity.class.getName(), context.getApplicationContext(), null, host, port); Log.d(App.TAG, "Proxy successfully set."); From b75e8e41742fb5406cf3958c3502fdb368b8087a Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Mon, 6 Jun 2016 12:37:53 +0200 Subject: [PATCH 8/9] Reset stored values on resetProxy --- .../github/dfa/diaspora_android/util/OrbotStatusReceiver.java | 2 ++ 1 file changed, 2 insertions(+) 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 index 71012d64..38fa8984 100644 --- 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 @@ -115,6 +115,8 @@ public class OrbotStatusReceiver extends BroadcastReceiver { public static void resetProxy(Context context) { try { + OrbotStatusReceiver.host = ""; + OrbotStatusReceiver.port = 0; NetCipher.clearProxy(); WebkitProxy.resetProxy(MainActivity.class.getName(), context.getApplicationContext()); } catch (Exception e) { From 8bbd7b996e3287ba30849f4afb4cc61f68f59f36 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Mon, 6 Jun 2016 14:35:07 +0200 Subject: [PATCH 9/9] Fixed most points mentioned in comments --- .../activity/MainActivity.java | 57 ++++++------------- .../diaspora_android/task/GetPodsService.java | 14 ++++- .../task/ImageDownloadTask.java | 16 +++++- .../task/ProfileFetchTask.java | 26 ++++++--- .../util/OrbotStatusReceiver.java | 6 +- 5 files changed, 63 insertions(+), 56 deletions(-) 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 3fbb29cc..10ccd55e 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 @@ -180,14 +180,14 @@ public class MainActivity extends AppCompatActivity //Orbot integration OrbotStatusReceiver.setMainActivity(this); - OrbotHelper.requestStartTor(this.getApplicationContext()); + OrbotHelper.requestStartTor(getApplicationContext()); if(appSettings.isProxyOrbot()) { - if(!OrbotHelper.isOrbotInstalled(this)) { + if(!OrbotHelper.isOrbotInstalled(getApplicationContext())) { appSettings.setProxyOrbot(false); promptInstallOrbot(); } else { //precautionary set Proxy - OrbotStatusReceiver.setProxy(this.getApplicationContext(), OrbotStatusReceiver.defaultHost, OrbotStatusReceiver.defaultPort); + OrbotStatusReceiver.setProxy(getApplicationContext(), OrbotStatusReceiver.DEFAULT_HOST, OrbotStatusReceiver.DEFAULT_PORT); } } @@ -323,13 +323,9 @@ public class MainActivity extends AppCompatActivity } }); - afterOnCreate(savedInstanceState); - } - - private void afterOnCreate(Bundle savedInstanceState) { - OrbotHelper.requestStartTor(this.getApplicationContext()); + OrbotHelper.requestStartTor(getApplicationContext()); if (savedInstanceState == null) { - if (Helpers.isOnline(MainActivity.this)) { + if (Helpers.isOnline(this)) { webView.loadData("", "text/html", null); webView.loadUrl("https://" + podDomain); } else { @@ -986,11 +982,12 @@ public class MainActivity extends AppCompatActivity boolean before = appSettings.isProxyOrbot(); appSettings.setProxyOrbot(!before); if(before) { - OrbotStatusReceiver.resetProxy(MainActivity.this.getApplicationContext()); + OrbotStatusReceiver.resetProxy(getApplicationContext()); } else { - OrbotHelper.requestStartTor(MainActivity.this); + OrbotHelper.requestStartTor(getApplicationContext()); webView.reload(); } + break; } } @@ -1089,47 +1086,27 @@ public class MainActivity extends AppCompatActivity * page on f-droid.org. */ void promptInstallOrbot() { - String message = this.getString(R.string.you_must_have_orbot) + " "; - final Intent intent = OrbotHelper.getOrbotInstallIntent(MainActivity.this); - if (intent.getPackage() == null) { - message += MainActivity.this.getString(R.string.download_orbot_from_fdroid); - } else { - message += MainActivity.this.getString(R.string.get_orbot_from_fdroid); - } - - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.install_orbot_); - builder.setMessage(message); - builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { + String message = this.getString(R.string.you_must_have_orbot) + " " + + (intent.getPackage() == null ? getString(R.string.download_orbot_from_fdroid) + : getString(R.string.get_orbot_from_fdroid)); + new AlertDialog.Builder(this).setTitle(R.string.install_orbot_).setMessage(message).setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { MainActivity.this.startActivity(intent); } - }); - builder.setNegativeButton(android.R.string.no, null); - builder.show(); + }).setNegativeButton(android.R.string.no, null).show(); } public void requestOrbotStart(boolean backgroundStartsDisabled) { - AlertDialog.Builder startDialog = new AlertDialog.Builder(this); - startDialog.setTitle(R.string.start_orbot_); - if(backgroundStartsDisabled) - startDialog.setMessage(R.string.orbot_starts_disabled_message); - else - startDialog.setMessage(R.string.orbot_doesn_t_appear_to_be_running_would_you_like_to_start_it_up_and_connect_to_tor_); - + AlertDialog.Builder startDialog = new AlertDialog.Builder(this).setTitle(R.string.start_orbot_) + .setMessage((backgroundStartsDisabled ? R.string.orbot_starts_disabled_message + : R.string.orbot_doesn_t_appear_to_be_running_would_you_like_to_start_it_up_and_connect_to_tor_)); startDialog.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { startActivityForResult(OrbotHelper.getShowOrbotStartIntent(), 1); } - }); - startDialog.setNegativeButton(android.R.string.no, null); - startDialog.show(); - } - - public boolean useOrbot() { - return appSettings.isProxyOrbot(); + }).setNegativeButton(android.R.string.no, null).show(); } } \ No newline at end of file diff --git a/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java b/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java index 9d39bfa1..2f643a83 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java @@ -73,17 +73,25 @@ public class GetPodsService extends Service { StringBuilder builder = new StringBuilder(); //HttpClient client = new DefaultHttpClient(); List list = null; + HttpsURLConnection connection; + InputStream inStream; try { - HttpsURLConnection connection = NetCipher.getHttpsURLConnection("https://podupti.me/api.php?key=4r45tg&format=json"); + connection = NetCipher.getHttpsURLConnection("https://podupti.me/api.php?key=4r45tg&format=json"); int statusCode = connection.getResponseCode(); if (statusCode == 200) { - InputStream content = connection.getInputStream(); + inStream = connection.getInputStream(); BufferedReader reader = new BufferedReader( - new InputStreamReader(content)); + new InputStreamReader(inStream)); String line; while ((line = reader.readLine()) != null) { builder.append(line); } + + try { + inStream.close(); + } catch (IOException e) {} + + connection.disconnect(); } else { Log.e(TAG, "Failed to download list of pods"); } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java b/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java index 9d4f6aac..31cec3c8 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/task/ImageDownloadTask.java @@ -13,6 +13,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import javax.net.ssl.HttpsURLConnection; + import info.guardianproject.netcipher.NetCipher; /** @@ -37,10 +39,12 @@ public class ImageDownloadTask extends AsyncTask { String url = urls[0]; Bitmap bitmap = null; FileOutputStream out = null; + InputStream inStream; + HttpsURLConnection connection; try { - //InputStream in = new java.net.URL(url).openStream(); - InputStream in = NetCipher.getHttpsURLConnection(url).getInputStream(); - bitmap = BitmapFactory.decodeStream(in); + connection = NetCipher.getHttpsURLConnection(url); + inStream = connection.getInputStream(); + bitmap = BitmapFactory.decodeStream(inStream); // Save to file if not null if (savePath != null) { @@ -48,6 +52,12 @@ public class ImageDownloadTask extends AsyncTask { bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); } + try { + inStream.close(); + } catch (IOException e) {} + + connection.disconnect(); + } catch (Exception e) { Log.e(App.TAG, e.getMessage()); } finally { diff --git a/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java b/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java index ef66e4ba..beab5eff 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/task/ProfileFetchTask.java @@ -10,6 +10,7 @@ import com.github.dfa.diaspora_android.data.PodUserProfile; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; @@ -40,18 +41,21 @@ public class ProfileFetchTask extends AsyncTask { String cookies = cookieManager.getCookie("https://" + app.getSettings().getPodDomain()); Log.d(App.TAG, cookies); + HttpsURLConnection connection; + InputStream inStream; try { URL url = new URL("https://" + app.getSettings().getPodDomain() + "/stream"); - HttpsURLConnection conn = NetCipher.getHttpsURLConnection(url); - conn.setReadTimeout(10000); - conn.setConnectTimeout(15000); - conn.setRequestMethod("GET"); + connection = NetCipher.getHttpsURLConnection(url); + connection.setReadTimeout(10000); + connection.setConnectTimeout(15000); + connection.setRequestMethod("GET"); if (cookies != null) { - conn.setRequestProperty("Cookie", cookies); + connection.setRequestProperty("Cookie", cookies); } - conn.connect(); + connection.connect(); - BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); + inStream = connection.getInputStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(inStream)); String line; final String TARGET_TAG = "window.gon={};gon.user="; while ((line = br.readLine()) != null && !line.startsWith(" { break; } } + + try{ + br.close(); + inStream.close(); + } catch (IOException e){} + + connection.disconnect(); + } catch (IOException e) { e.printStackTrace(); } 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 index 38fa8984..d7073f58 100644 --- 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 @@ -42,8 +42,8 @@ public class OrbotStatusReceiver extends BroadcastReceiver { public static final String EXTRA_HTTP_HOST = "org.torproject.android.intent.extra.HTTP_PROXY_HOST"; public static final String EXTRA_HTTP_PORT = "org.torproject.android.intent.extra.HTTP_PROXY_PORT"; - public static final String defaultHost = "127.0.0.1"; - public static final int defaultPort = 8118; + public static final String DEFAULT_HOST = "127.0.0.1"; + public static final int DEFAULT_PORT = 8118; private static String host = ""; private static int port = 0; @@ -88,7 +88,7 @@ public class OrbotStatusReceiver extends BroadcastReceiver { //Got no values from intent if((nHost == null || nPort == -1)) { if(host.equals("") || port == 0) { - setProxy(context, defaultHost, defaultPort); + setProxy(context, DEFAULT_HOST, DEFAULT_PORT); } } else { setProxy(context, nHost, nPort);