From 0a482a749fd770827d3d6c71debb112a07e7fae3 Mon Sep 17 00:00:00 2001
From: sisbell <shane.isbell@gmail.com>
Date: Thu, 14 Nov 2019 13:43:09 -0800
Subject: [PATCH] Bug 30767: Custom obfs4 bridge does not work on Tor Browser
 for Android

TOPL fixes #134
---
 .../thali/toronionproxy/TorConfigBuilder.java | 63 ++++++++-----------
 1 file changed, 26 insertions(+), 37 deletions(-)

diff --git a/universal/src/main/java/com/msopentech/thali/toronionproxy/TorConfigBuilder.java b/universal/src/main/java/com/msopentech/thali/toronionproxy/TorConfigBuilder.java
index e324d9f..da9a6a2 100644
--- a/universal/src/main/java/com/msopentech/thali/toronionproxy/TorConfigBuilder.java
+++ b/universal/src/main/java/com/msopentech/thali/toronionproxy/TorConfigBuilder.java
@@ -99,25 +99,32 @@ public final class TorConfigBuilder {
         return this;
     }
 
+    @SettingsConfig
+    public TorConfigBuilder bridgesFromSettings() {
+        try {
+            addBridgesFromResources();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return this;
+    }
+
     public TorConfigBuilder configurePluggableTransportsFromSettings(File pluggableTransportClient) throws IOException {
-        List<String> supportedBridges = settings.getListOfSupportedBridges();
-        if (pluggableTransportClient == null || !settings.hasBridges() || supportedBridges.size() < 1) {
+        if (pluggableTransportClient == null) {
             return this;
         }
 
-        if (!pluggableTransportClient.exists() || !pluggableTransportClient.canExecute()) {
+        if (!pluggableTransportClient.exists()) {
             throw new IOException("Bridge binary does not exist: " + pluggableTransportClient
                     .getCanonicalPath());
         }
 
-        if (supportedBridges.contains("obfs3") || supportedBridges.contains("obfs4")) {
-            transportPluginObfs(pluggableTransportClient.getCanonicalPath());
-        }
-        if (supportedBridges.contains("meek")) {
-            transportPluginMeek(pluggableTransportClient.getCanonicalPath());
+        if (!pluggableTransportClient.canExecute()) {
+            throw new IOException("Bridge binary is not executable: " + pluggableTransportClient
+                    .getCanonicalPath());
         }
-        String type = supportedBridges.contains("meek") ? "meek_lite" : "obfs4";
-        addBridgesFromResources(type, 2);
+
+        transportPlugin(pluggableTransportClient.getCanonicalPath());
         return this;
     }
 
@@ -471,14 +478,8 @@ public final class TorConfigBuilder {
         return transPort(settings.transPort());
     }
 
-    public TorConfigBuilder transportPluginMeek(String clientPath) {
-        buffer.append("ClientTransportPlugin meek_lite exec ").append(clientPath).append('\n');
-        return this;
-    }
-
-    public TorConfigBuilder transportPluginObfs(String clientPath) {
-        buffer.append("ClientTransportPlugin obfs3 exec ").append(clientPath).append('\n');
-        buffer.append("ClientTransportPlugin obfs4 exec ").append(clientPath).append('\n');
+    public TorConfigBuilder transportPlugin(String clientPath) {
+        buffer.append("ClientTransportPlugin meek_lite,obfs3,obfs4 exec ").append(clientPath).append('\n');
         return this;
     }
 
@@ -489,7 +490,7 @@ public final class TorConfigBuilder {
 
     @SettingsConfig
     public TorConfigBuilder useBridgesFromSettings() {
-        return !settings.hasBridges() ? dontUseBridges() : this;
+        return settings.hasBridges() ? useBridges() : this;
     }
 
     public TorConfigBuilder virtualAddressNetwork(String address) {
@@ -518,12 +519,12 @@ public final class TorConfigBuilder {
      * </code>
      *
      */
-    TorConfigBuilder addBridgesFromResources(String type, int maxBridges) throws IOException {
+    TorConfigBuilder addBridgesFromResources() throws IOException {
         if(settings.hasBridges()) {
             InputStream bridgesStream = context.getInstaller().openBridgesStream();
             int formatType = bridgesStream.read();
-            if(formatType == 0) {
-                addBridges(bridgesStream, type, maxBridges);
+            if (formatType == 0) {
+                addBridges(bridgesStream);
             } else {
                 addCustomBridges(bridgesStream);
             }
@@ -534,23 +535,14 @@ public final class TorConfigBuilder {
     /**
      * Add bridges from bridges.txt file.
      */
-    private void addBridges(InputStream input, String bridgeType, int maxBridges) {
-        if (input == null || isNullOrEmpty(bridgeType) || maxBridges < 1) {
+    private void addBridges(InputStream input) {
+        if (input == null) {
             return;
         }
-        boolean hasAddedBridge = false;
         List<Bridge> bridges = readBridgesFromStream(input);
-        Collections.shuffle(bridges, new Random(System.nanoTime()));
-        int bridgeCount = 0;
         for (Bridge b : bridges) {
-            if (b.type.equals(bridgeType)) {
-                bridge(b.type, b.config);
-                hasAddedBridge = true;
-                if (++bridgeCount > maxBridges)
-                    break;
-            }
+            bridge(b.type, b.config);
         }
-        if(hasAddedBridge) useBridges();
     }
 
     /**
@@ -560,15 +552,12 @@ public final class TorConfigBuilder {
         if (input == null) {
             return;
         }
-        boolean hasAddedBridge = false;
         List<Bridge> bridges = readCustomBridgesFromStream(input);
         for (Bridge b : bridges) {
             if (b.type.equals("custom")) {
                 bridgeCustom(b.config);
-                hasAddedBridge = true;
             }
         }
-        if(hasAddedBridge) useBridges();
     }
 
     private static List<Bridge> readBridgesFromStream(InputStream input)  {
-- 
2.20.1

