Commit c71a79a6 authored by sisbell's avatar sisbell Committed by Matthew Finkel
Browse files

Bug 30767: Custom obfs4 bridge does not work on Tor Browser for Android

TOPL fixes #134
parent a720e25b
Loading
Loading
Loading
Loading
+24 −23
Original line number Diff line number Diff line
@@ -99,25 +99,38 @@ 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 || supportedBridges == null || supportedBridges.isEmpty()) {
            return this;
        }

        if (!pluggableTransportClient.exists() || !pluggableTransportClient.canExecute()) {
        if (!pluggableTransportClient.exists()) {
            throw new IOException("Bridge binary does not exist: " + pluggableTransportClient
                    .getCanonicalPath());
        }

        if (!pluggableTransportClient.canExecute()) {
            throw new IOException("Bridge binary is not executable: " + pluggableTransportClient
                    .getCanonicalPath());
        }

        if (supportedBridges.contains("obfs3") || supportedBridges.contains("obfs4")) {
            transportPluginObfs(pluggableTransportClient.getCanonicalPath());
        }
        if (supportedBridges.contains("meek")) {
            transportPluginMeek(pluggableTransportClient.getCanonicalPath());
        }
        String type = supportedBridges.contains("meek") ? "meek_lite" : "obfs4";
        addBridgesFromResources(type, 2);
        return this;
    }

@@ -489,7 +502,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 +531,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);
                addBridges(bridgesStream);
            } else {
                addCustomBridges(bridgesStream);
            }
@@ -534,23 +547,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;
            }
        }
        if(hasAddedBridge) useBridges();
    }

    /**
@@ -560,15 +564,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)  {