commit de62a5af6026a9caa6448eaac4e0bc9709a20817 Author: sisbell shane.isbell@gmail.com Date: Sat Apr 20 19:46:21 2019 -0700
Bug 30166: If custom bridges are specified, only use those bridges for connecting --- .../android/service/CustomTorInstaller.java | 20 +++++++- .../org/torproject/android/service/TorService.java | 60 ++++++++++++---------- .../org/torproject/android/service/util/Prefs.java | 7 ++- 3 files changed, 55 insertions(+), 32 deletions(-)
diff --git a/service/src/main/java/org/torproject/android/service/CustomTorInstaller.java b/service/src/main/java/org/torproject/android/service/CustomTorInstaller.java index 6b5a1b8..e962137 100644 --- a/service/src/main/java/org/torproject/android/service/CustomTorInstaller.java +++ b/service/src/main/java/org/torproject/android/service/CustomTorInstaller.java @@ -32,10 +32,26 @@ public class CustomTorInstaller extends TorInstaller { updateTorConfigCustom(torrcFile, content); }
+ /** + * Opens bridges list as <code>InputStream</code>. First checks for user defined bridges from the user pref file. + * If it finds user defined bridges, then the stream will contain only these bridges. Otherwise, it returns + * a set of predefined bridges. + */ @Override public InputStream openBridgesStream() throws IOException { - ByteArrayInputStream userDefinedBridges = new ByteArrayInputStream((Prefs.getBridgesList() + "\r\n").getBytes()); - return new SequenceInputStream(userDefinedBridges, context.getResources().getAssets().open("common/bridges.txt")); + /* + BridgesList is an overloaded field, which can cause some confusion. The list can be: + 1) a filter like obfs4 or meek OR 2) it can be a custom bridge + For (1), we just pass back all bridges, the filter will occur elsewhere in the library. + For (2) we return the bridge list as a raw stream + If length is greater than 5, then we know this is a custom bridge + */ + String userDefinedBridgeList = Prefs.getBridgesList(); + byte bridgeType = (byte) (userDefinedBridgeList.length() > 5 ? 1 : 0); + ByteArrayInputStream bridgeTypeStream = new ByteArrayInputStream(new byte[]{bridgeType}); + InputStream bridgeStream = (bridgeType == 1) ? new ByteArrayInputStream((userDefinedBridgeList + "\r\n").getBytes()) + : context.getResources().getAssets().open("common/bridges.txt"); + return new SequenceInputStream(bridgeTypeStream, bridgeStream); }
private static void copy(InputStream is, File target) throws IOException { diff --git a/service/src/main/java/org/torproject/android/service/TorService.java b/service/src/main/java/org/torproject/android/service/TorService.java index a748241..dea4bd2 100644 --- a/service/src/main/java/org/torproject/android/service/TorService.java +++ b/service/src/main/java/org/torproject/android/service/TorService.java @@ -290,37 +290,12 @@ public final class TorService extends Service implements TorServiceConstants, Or private boolean setupTor() { try { onionProxyManager.setup(); + return true; } catch (Exception e) { Log.e(OrbotConstants.TAG, "Error installing Tor binaries", e); mEventBroadcaster.broadcastNotice("There was an error installing Tor binaries"); return false; } - - try { - mEventBroadcaster.broadcastNotice(getString(R.string - .updating_settings_in_tor_service)); - TorConfigBuilder builder = onionProxyManager.getContext() - .newConfigBuilder().updateTorConfig(); - - File nativeDir = new File(getApplicationInfo().nativeLibraryDir); - File pluggableTransport = new File(nativeDir, "libObfs4proxy.so"); - if(!pluggableTransport.canExecute()) pluggableTransport.setExecutable(true); - - builder.configurePluggableTransportsFromSettings(pluggableTransport); - mDataService.updateConfigBuilder(builder); - onionProxyManager.getTorInstaller().updateTorConfigCustom - (builder.asString()); - mEventBroadcaster.broadcastNotice("updating torrc custom configuration..."); - mEventBroadcaster.broadcastDebug("torrc.custom=" + builder.asString()); - mEventBroadcaster.broadcastNotice("success."); - } catch (Exception e) { - e.printStackTrace(); - Log.e(OrbotConstants.TAG, e.getMessage()); - mEventBroadcaster.broadcastNotice("Error configuring tor: " + e.toString()); - return false; - } - - return true; }
private synchronized void startTor() { @@ -331,6 +306,7 @@ public final class TorService extends Service implements TorServiceConstants, Or setTorNetworkEnabledAsync(true); } else { try { + updateTorrcConfig(); mEventBroadcaster.broadcastNotice("checking binary version: " + TOR_VERSION); mEventBroadcaster.getStatus().starting(); notify(getString(R.string.status_starting_up), NOTIFY_ID, @@ -380,6 +356,38 @@ public final class TorService extends Service implements TorServiceConstants, Or }).start(); }
+ /** + * Updates the torrc file based on the current user preferences + */ + private boolean updateTorrcConfig() { + try { + mEventBroadcaster.broadcastNotice(getString(R.string + .updating_settings_in_tor_service)); + TorConfigBuilder builder = onionProxyManager.getContext() + .newConfigBuilder().updateTorConfig(); + + //Check bridges to see if we need this + File nativeDir = new File(getApplicationInfo().nativeLibraryDir); + File pluggableTransport = new File(nativeDir, "libObfs4proxy.so"); + if(!pluggableTransport.canExecute()) pluggableTransport.setExecutable(true); + + builder.configurePluggableTransportsFromSettings(pluggableTransport); + mDataService.updateConfigBuilder(builder); + onionProxyManager.getTorInstaller().updateTorConfigCustom + (builder.asString()); + mEventBroadcaster.broadcastNotice("updating torrc custom configuration..."); + mEventBroadcaster.broadcastDebug("torrc.custom=" + builder.asString()); + mEventBroadcaster.broadcastNotice("success."); + } catch (Exception e) { + e.printStackTrace(); + Log.e(OrbotConstants.TAG, e.getMessage()); + mEventBroadcaster.broadcastNotice("Error configuring tor: " + e.toString()); + return false; + } + + return true; + } + private class ActionBroadcastReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if (intent != null || CMD_NEWNYM.equals(intent.getAction())) { diff --git a/service/src/main/java/org/torproject/android/service/util/Prefs.java b/service/src/main/java/org/torproject/android/service/util/Prefs.java index 0a2bf45..6274088 100644 --- a/service/src/main/java/org/torproject/android/service/util/Prefs.java +++ b/service/src/main/java/org/torproject/android/service/util/Prefs.java @@ -52,10 +52,9 @@ public class Prefs { }
public static String getBridgesList() { - String defaultBridgeType = "obfs4"; - if (Locale.getDefault().getLanguage().equals("fa")) - defaultBridgeType = "meek"; //if Farsi, use meek as the default bridge type - return prefs.getString(PREF_BRIDGES_LIST, defaultBridgeType); + String defaultBridgeType = (Locale.getDefault().getLanguage().equals("fa")) ? "meek": "obfs4"; + String list = prefs.getString(PREF_BRIDGES_LIST, defaultBridgeType); + return (list.trim().isEmpty()) ? defaultBridgeType : list; }
public static void setBridgesList(String value) {