Backport Updater Tor support patch to 14.1

This commit is contained in:
Tad 2018-09-24 06:46:54 -04:00
parent 08c65c8334
commit 776be6f992
6 changed files with 356 additions and 5 deletions

View File

@ -6,4 +6,4 @@ pkgdesc='Metapackage to pull all dependencies required to build Android'
arch=('x86_64') arch=('x86_64')
url='https://wiki.archlinux.org/index.php/Android#Building_Android' url='https://wiki.archlinux.org/index.php/Android#Building_Android'
license=('None') license=('None')
depends=('gcc-multilib' 'git' 'gnupg' 'flex' 'bison' 'gperf' 'sdl' 'wxgtk' 'squashfs-tools' 'curl' 'ncurses' 'zlib' 'schedtool' 'perl-switch' 'zip' 'unzip' 'libxslt' 'python2-virtualenv' 'bc' 'rsync' 'lib32-zlib' 'lib32-ncurses' 'lib32-readline' 'xml2' 'lzop' 'pngcrush' 'imagemagick' 'repo' 'ccache' 'maven' 'gradle' 'ninja' 'lib32-ncurses5-compat-libs' 'ncurses5-compat-libs' 'firejail') depends=('gcc' 'git' 'gnupg' 'flex' 'bison' 'gperf' 'sdl' 'wxgtk' 'squashfs-tools' 'curl' 'ncurses' 'zlib' 'schedtool' 'perl-switch' 'zip' 'unzip' 'libxslt' 'python2-virtualenv' 'bc' 'rsync' 'lib32-zlib' 'lib32-ncurses' 'lib32-readline' 'xml2' 'lzop' 'pngcrush' 'imagemagick' 'repo' 'ccache' 'maven' 'gradle' 'ninja' 'lib32-ncurses5-compat-libs' 'ncurses5-compat-libs' 'firejail')

View File

@ -0,0 +1,351 @@
From 8953ad8f1c496ea7d483193550ed50bfcfd45009 Mon Sep 17 00:00:00 2001
From: Tad <tad@spotco.us>
Date: Thu, 20 Sep 2018 21:44:53 -0400
Subject: [PATCH] Add support for routing over Tor
Change-Id: Ibfe080c3d801af34fb64fda1b6b8f4f39a2b1ccf
---
res/layout/preferences_dialog.xml | 8 +++
res/values/strings.xml | 2 +
.../lineageos/updater/UpdatesActivity.java | 12 ++++
.../updater/UpdatesCheckReceiver.java | 4 ++
.../updater/controller/UpdaterController.java | 4 ++
.../updater/download/DownloadClient.java | 8 ++-
.../download/HttpURLConnectionClient.java | 20 +++++-
src/org/lineageos/updater/misc/Constants.java | 1 +
src/org/lineageos/updater/misc/Utils.java | 65 +++++++++++++++++++
9 files changed, 120 insertions(+), 4 deletions(-)
diff --git a/res/layout/preferences_dialog.xml b/res/layout/preferences_dialog.xml
index 999d67f..58e8b6e 100644
--- a/res/layout/preferences_dialog.xml
+++ b/res/layout/preferences_dialog.xml
@@ -15,6 +15,14 @@
android:text="@string/menu_auto_updates_check"
android:textSize="16sp" />
+ <Switch
+ android:id="@+id/preferences_onion_routing"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_marginBottom="16dp"
+ android:text="@string/menu_onion_routing"
+ android:textSize="16sp" />
+
<Switch
android:id="@+id/preferences_auto_delete_updates"
android:layout_width="match_parent"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 511bc37..bf0f9c6 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -64,6 +64,7 @@
<string name="menu_refresh">Refresh</string>
<string name="menu_preferences">Preferences</string>
<string name="menu_auto_updates_check">Auto updates check</string>
+ <string name="menu_onion_routing">Perform requests over Tor</string>
<string name="menu_auto_delete_updates">Delete updates when installed</string>
<string name="menu_delete_update">Delete</string>
<string name="menu_copy_url">Copy URL</string>
@@ -78,6 +79,7 @@
<string name="snack_download_failed">The download failed. Please check your internet connection and try again later.</string>
<string name="snack_download_verification_failed">The update verification failed.</string>
<string name="snack_download_verified">Download completed.</string>
+ <string name="snack_orbot_not_available">Orbot is not installed, disabling Tor routing!</string>
<string name="snack_update_not_installable">This update can\'t be installed on top of the current build.</string>
diff --git a/src/org/lineageos/updater/UpdatesActivity.java b/src/org/lineageos/updater/UpdatesActivity.java
index 4d3c104..02eb4db 100644
--- a/src/org/lineageos/updater/UpdatesActivity.java
+++ b/src/org/lineageos/updater/UpdatesActivity.java
@@ -340,10 +340,14 @@ public class UpdatesActivity extends UpdatesListActivity {
final DownloadClient downloadClient;
try {
+ if(Utils.isOnionRoutingEnabled(getApplicationContext())) {
+ Utils.requestStartOrbot(getApplicationContext());
+ }
downloadClient = new DownloadClient.Builder()
.setUrl(url)
.setDestination(jsonFileTmp)
.setDownloadCallback(callback)
+ .setUseOnionRouting(Utils.isOnionRoutingEnabled(getApplicationContext()))
.build();
} catch (IOException exception) {
Log.e(TAG, "Could not build download client");
@@ -407,11 +411,13 @@ public class UpdatesActivity extends UpdatesListActivity {
private void showPreferencesDialog() {
View view = LayoutInflater.from(this).inflate(R.layout.preferences_dialog, null);
Switch autoCheck = (Switch) view.findViewById(R.id.preferences_auto_updates_check);
+ Switch onionRouting = (Switch) view.findViewById(R.id.preferences_onion_routing);
Switch autoDelete = (Switch) view.findViewById(R.id.preferences_auto_delete_updates);
Switch dataWarning = (Switch) view.findViewById(R.id.preferences_mobile_data_warning);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
autoCheck.setChecked(prefs.getBoolean(Constants.PREF_AUTO_UPDATES_CHECK, true));
+ onionRouting.setChecked(prefs.getBoolean(Constants.PREF_ONION_ROUTING, false));
autoDelete.setChecked(prefs.getBoolean(Constants.PREF_AUTO_DELETE_UPDATES, false));
dataWarning.setChecked(prefs.getBoolean(Constants.PREF_MOBILE_DATA_WARNING, true));
@@ -422,6 +428,8 @@ public class UpdatesActivity extends UpdatesListActivity {
prefs.edit()
.putBoolean(Constants.PREF_AUTO_UPDATES_CHECK,
autoCheck.isChecked())
+ .putBoolean(Constants.PREF_ONION_ROUTING,
+ onionRouting.isChecked() && Utils.isOrbotInstalled(getApplicationContext()))
.putBoolean(Constants.PREF_AUTO_DELETE_UPDATES,
autoDelete.isChecked())
.putBoolean(Constants.PREF_MOBILE_DATA_WARNING,
@@ -434,6 +442,10 @@ public class UpdatesActivity extends UpdatesListActivity {
UpdatesCheckReceiver.cancelRepeatingUpdatesCheck(this);
UpdatesCheckReceiver.cancelUpdatesCheck(this);
}
+
+ if(onionRouting.isChecked() && !Utils.isOrbotInstalled(getApplicationContext())) {
+ showSnackbar(R.string.snack_orbot_not_available, Snackbar.LENGTH_LONG);
+ }
})
.show();
}
diff --git a/src/org/lineageos/updater/UpdatesCheckReceiver.java b/src/org/lineageos/updater/UpdatesCheckReceiver.java
index 83348b9..84a6287 100644
--- a/src/org/lineageos/updater/UpdatesCheckReceiver.java
+++ b/src/org/lineageos/updater/UpdatesCheckReceiver.java
@@ -107,10 +107,14 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
};
try {
+ if(Utils.isOnionRoutingEnabled(context)) {
+ Utils.requestStartOrbot(context);
+ }
DownloadClient downloadClient = new DownloadClient.Builder()
.setUrl(url)
.setDestination(jsonNew)
.setDownloadCallback(callback)
+ .setUseOnionRouting(Utils.isOnionRoutingEnabled(context))
.build();
downloadClient.start();
} catch (IOException e) {
diff --git a/src/org/lineageos/updater/controller/UpdaterController.java b/src/org/lineageos/updater/controller/UpdaterController.java
index 12b02dd..1515e45 100644
--- a/src/org/lineageos/updater/controller/UpdaterController.java
+++ b/src/org/lineageos/updater/controller/UpdaterController.java
@@ -357,12 +357,16 @@ public class UpdaterController {
update.setFile(destination);
DownloadClient downloadClient;
try {
+ if(Utils.isOnionRoutingEnabled(mContext)) {
+ Utils.requestStartOrbot(mContext);
+ }
downloadClient = new DownloadClient.Builder()
.setUrl(update.getDownloadUrl())
.setDestination(update.getFile())
.setDownloadCallback(getDownloadCallback(downloadId))
.setProgressListener(getProgressListener(downloadId))
.setUseDuplicateLinks(true)
+ .setUseOnionRouting(Utils.isOnionRoutingEnabled(mContext))
.build();
} catch (IOException exception) {
Log.e(TAG, "Could not build download client");
diff --git a/src/org/lineageos/updater/download/DownloadClient.java b/src/org/lineageos/updater/download/DownloadClient.java
index 6a2a490..374e017 100644
--- a/src/org/lineageos/updater/download/DownloadClient.java
+++ b/src/org/lineageos/updater/download/DownloadClient.java
@@ -64,6 +64,7 @@ public interface DownloadClient {
private DownloadClient.DownloadCallback mCallback;
private DownloadClient.ProgressListener mProgressListener;
private boolean mUseDuplicateLinks;
+ private boolean mOnionRouting;
public DownloadClient build() throws IOException {
if (mUrl == null) {
@@ -74,7 +75,7 @@ public interface DownloadClient {
throw new IllegalStateException("No download callback defined");
}
return new HttpURLConnectionClient(mUrl, mDestination, mProgressListener, mCallback,
- mUseDuplicateLinks);
+ mUseDuplicateLinks, mOnionRouting);
}
public Builder setUrl(String url) {
@@ -101,5 +102,10 @@ public interface DownloadClient {
mUseDuplicateLinks = useDuplicateLinks;
return this;
}
+
+ public Builder setUseOnionRouting(boolean onionRouting) {
+ mOnionRouting = onionRouting;
+ return this;
+ }
}
}
diff --git a/src/org/lineageos/updater/download/HttpURLConnectionClient.java b/src/org/lineageos/updater/download/HttpURLConnectionClient.java
index 2b7c80e..c76d747 100644
--- a/src/org/lineageos/updater/download/HttpURLConnectionClient.java
+++ b/src/org/lineageos/updater/download/HttpURLConnectionClient.java
@@ -18,12 +18,16 @@ package org.lineageos.updater.download;
import android.os.SystemClock;
import android.util.Log;
+import org.lineageos.updater.misc.Utils;
+
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
+import java.net.InetSocketAddress;
+import java.net.Proxy;
import java.net.URL;
import java.util.Comparator;
import java.util.List;
@@ -42,6 +46,7 @@ public class HttpURLConnectionClient implements DownloadClient {
private final DownloadClient.ProgressListener mProgressListener;
private final DownloadClient.DownloadCallback mCallback;
private final boolean mUseDuplicateLinks;
+ private final boolean mUseOnionRouting;
private DownloadThread mDownloadThread;
@@ -60,8 +65,14 @@ public class HttpURLConnectionClient implements DownloadClient {
HttpURLConnectionClient(String url, File destination,
DownloadClient.ProgressListener progressListener,
DownloadClient.DownloadCallback callback,
- boolean useDuplicateLinks) throws IOException {
- mClient = (HttpURLConnection) new URL(url).openConnection();
+ boolean useDuplicateLinks, boolean useOnionRouting) throws IOException {
+ mUseOnionRouting = useOnionRouting;
+ if(useOnionRouting) {
+ Proxy orbot = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 9050));
+ mClient = (HttpURLConnection) new URL(url).openConnection(orbot);
+ } else {
+ mClient = (HttpURLConnection) new URL(url).openConnection();
+ }
mDestination = destination;
mProgressListener = progressListener;
mCallback = callback;
@@ -224,7 +235,7 @@ public class HttpURLConnectionClient implements DownloadClient {
}
Log.d(TAG, "Downloading from " + newUrl);
changeClientUrl(url);
- mClient.setConnectTimeout(5000);
+ mClient.setConnectTimeout(mUseOnionRouting ? 45000 : 5000);
mClient.connect();
if (!isSuccessCode(mClient.getResponseCode())) {
throw new IOException("Server replied with " + mClient.getResponseCode());
@@ -246,6 +257,9 @@ public class HttpURLConnectionClient implements DownloadClient {
@Override
public void run() {
try {
+ if(mUseOnionRouting) {
+ Utils.waitUntilOrbotIsAvailable();
+ }
mClient.setInstanceFollowRedirects(!mUseDuplicateLinks);
mClient.connect();
int responseCode = mClient.getResponseCode();
diff --git a/src/org/lineageos/updater/misc/Constants.java b/src/org/lineageos/updater/misc/Constants.java
index 80fb19e..3cb64dd 100644
--- a/src/org/lineageos/updater/misc/Constants.java
+++ b/src/org/lineageos/updater/misc/Constants.java
@@ -25,6 +25,7 @@ public final class Constants {
public static final String PREF_LAST_UPDATE_CHECK = "last_update_check";
public static final String PREF_AUTO_UPDATES_CHECK = "auto_updates_check";
+ public static final String PREF_ONION_ROUTING = "onion_routing";
public static final String PREF_AUTO_DELETE_UPDATES = "auto_delete_updates";
public static final String PREF_MOBILE_DATA_WARNING = "pref_mobile_data_warning";
public static final String PREF_NEEDS_REBOOT_ID = "needs_reboot_id";
diff --git a/src/org/lineageos/updater/misc/Utils.java b/src/org/lineageos/updater/misc/Utils.java
index a31492e..bea09ca 100644
--- a/src/org/lineageos/updater/misc/Utils.java
+++ b/src/org/lineageos/updater/misc/Utils.java
@@ -44,6 +44,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
+import java.net.Socket;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
@@ -145,12 +146,76 @@ public class Utils {
return updates;
}
+ //Credit: https://stackoverflow.com/a/6758962
+ public static boolean isPackageInstalled(Context context, String packageID) {
+ PackageManager pm = context.getPackageManager();
+ try {
+ pm.getPackageInfo(packageID, PackageManager.GET_META_DATA);
+ } catch(PackageManager.NameNotFoundException e) {
+ return false;
+ }
+ return true;
+ }
+
+ public static boolean isOrbotInstalled(Context context) {
+ return isPackageInstalled(context, "org.torproject.android");
+ }
+
+ public static boolean isOnionRoutingEnabled(Context context) {
+ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
+ return preferences.getBoolean(Constants.PREF_ONION_ROUTING, false);
+ }
+
+ //Credit: OrbotHelper/NetCipher
+ public static void requestStartOrbot(Context context) {
+ Intent intent = new Intent("org.torproject.android.intent.action.START");
+ intent.setPackage("org.torproject.android");
+ intent.putExtra("org.torproject.android.intent.extra.PACKAGE_NAME", context.getPackageName());
+ context.sendBroadcast(intent);
+ }
+
+ //Credit: https://www.geekality.net/2013/04/30/java-simple-check-to-see-if-a-server-is-listening-on-a-port/
+ public static boolean isPortListening(String host, int port) {
+ Socket s = null;
+ try {
+ s = new Socket(host, port);
+ return true;
+ } catch(Exception e) {
+ return false;
+ } finally {
+ if (s != null) {
+ try {
+ s.close();
+ } catch(Exception e1) {
+ }
+ }
+ }
+ }
+
+ public static boolean waitUntilOrbotIsAvailable() {
+ int tries = 0;
+ boolean listening;
+ while(!(listening = isPortListening("127.0.0.1", 9050)) && tries <= 60) {
+ tries++;
+ try {
+ Thread.sleep(1000);
+ } catch(Exception e) {
+
+ }
+ }
+ return listening;
+ }
+
public static String getServerURL(Context context) {
String incrementalVersion = SystemProperties.get(Constants.PROP_BUILD_VERSION_INCREMENTAL);
String device = SystemProperties.get(Constants.PROP_NEXT_DEVICE,
SystemProperties.get(Constants.PROP_DEVICE));
String server = "0OTA_SERVER_CLEARNET0";
+ String serverOnion = "0OTA_SERVER_ONION0";
+ if(serverOnion.toLowerCase().startsWith("http") && isOnionRoutingEnabled(context)) {
+ server = serverOnion;
+ }
return server + "?base=LineageOS&device=" + device + "&inc=" + incrementalVersion;
}
--
2.19.0

View File

@ -96,8 +96,8 @@ export -f buildAll;
patchWorkspace() { patchWorkspace() {
if [ "$DOS_MALWARE_SCAN_ENABLED" = true ]; then scanForMalware false "$DOS_PREBUILT_APPS $DOS_BUILD_BASE/build $DOS_BUILD_BASE/device $DOS_BUILD_BASE/vendor/cm"; fi; if [ "$DOS_MALWARE_SCAN_ENABLED" = true ]; then scanForMalware false "$DOS_PREBUILT_APPS $DOS_BUILD_BASE/build $DOS_BUILD_BASE/device $DOS_BUILD_BASE/vendor/cm"; fi;
#source build/envsetup.sh; source build/envsetup.sh;
#repopick -t n_asb_09-2018; repopick -t n_asb_09-2018-qcom;
source "$DOS_SCRIPTS/Patch.sh"; source "$DOS_SCRIPTS/Patch.sh";
source "$DOS_SCRIPTS/Defaults.sh"; source "$DOS_SCRIPTS/Defaults.sh";

View File

@ -134,7 +134,7 @@ patch -p1 < "$DOS_PATCHES/android_packages_apps_SetupWizard/0001-Remove_Analytic
enterAndClear "packages/apps/Updater"; enterAndClear "packages/apps/Updater";
patch -p1 < "$DOS_PATCHES_COMMON/android_packages_apps_Updater/0001-Server.patch"; #Switch to our server patch -p1 < "$DOS_PATCHES_COMMON/android_packages_apps_Updater/0001-Server.patch"; #Switch to our server
patch -p1 < "$DOS_PATCHES_COMMON/android_packages_apps_Updater/0002-Tor_Support.patch"; #Add Tor support patch -p1 < "$DOS_PATCHES/android_packages_apps_Updater/0002-Tor_Support.patch"; #Add Tor support
#TODO: Remove changelog #TODO: Remove changelog
enterAndClear "packages/apps/WallpaperPicker"; enterAndClear "packages/apps/WallpaperPicker";

View File

@ -138,7 +138,7 @@ patch -p1 < "$DOS_PATCHES/android_packages_apps_SetupWizard/0001-Remove_Analytic
enterAndClear "packages/apps/Updater"; enterAndClear "packages/apps/Updater";
patch -p1 < "$DOS_PATCHES_COMMON/android_packages_apps_Updater/0001-Server.patch"; #Switch to our server patch -p1 < "$DOS_PATCHES_COMMON/android_packages_apps_Updater/0001-Server.patch"; #Switch to our server
patch -p1 < "$DOS_PATCHES_COMMON/android_packages_apps_Updater/0002-Tor_Support.patch"; #Add Tor support patch -p1 < "$DOS_PATCHES/android_packages_apps_Updater/0002-Tor_Support.patch"; #Add Tor support
#TODO: Remove changelog #TODO: Remove changelog
enterAndClear "packages/apps/WallpaperPicker"; enterAndClear "packages/apps/WallpaperPicker";