From ca6dc33cddfdaca6e11d291e61f531a28b8525f6 Mon Sep 17 00:00:00 2001
From: Torsten Grote <t@grobox.de>
Date: Mon, 8 Oct 2018 16:47:11 -0300
Subject: [PATCH] Add obfs4 proxy and switch to obfs4 bridges

---
 bramble-android/build.gradle                  |  1 +
 .../bramble/plugin/tor/TorPlugin.java         | 21 ++++++++++++++++++-
 bramble-core/src/main/resources/bridges       | 12 ++++-------
 bramble-java/build.gradle                     |  1 +
 4 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/bramble-android/build.gradle b/bramble-android/build.gradle
index 0def628f35..9ae889fdd5 100644
--- a/bramble-android/build.gradle
+++ b/bramble-android/build.gradle
@@ -29,6 +29,7 @@ configurations {
 dependencies {
 	implementation project(path: ':bramble-core', configuration: 'default')
 	tor 'org.briarproject:tor-android:0.3.4.8@zip'
+	tor 'org.briarproject:obfs4proxy-android:0.0.7@zip'
 
 	annotationProcessor 'com.google.dagger:dagger-compiler:2.0.2'
 
diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tor/TorPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tor/TorPlugin.java
index bd97673a8d..374238d0d6 100644
--- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tor/TorPlugin.java
+++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tor/TorPlugin.java
@@ -102,7 +102,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
 	private final CircumventionProvider circumventionProvider;
 	private final ResourceProvider resourceProvider;
 	private final int maxLatency, maxIdleTime, socketTimeout;
-	private final File torDirectory, torFile, geoIpFile, configFile;
+	private final File torDirectory, torFile, geoIpFile, obfs4File, configFile;
 	private final File doneFile, cookieFile;
 	private final ConnectionStatus connectionStatus;
 	private final AtomicBoolean used = new AtomicBoolean(false);
@@ -142,6 +142,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
 		this.torDirectory = torDirectory;
 		torFile = new File(torDirectory, "tor");
 		geoIpFile = new File(torDirectory, "geoip");
+		obfs4File = new File(torDirectory, "obfs4proxy");
 		configFile = new File(torDirectory, "torrc");
 		doneFile = new File(torDirectory, "done");
 		cookieFile = new File(torDirectory, ".tor/control_auth_cookie");
@@ -284,6 +285,12 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
 			in = getGeoIpInputStream();
 			out = new FileOutputStream(geoIpFile);
 			IoUtils.copyAndClose(in, out);
+			// Unzip the Obfs4 proxy to the filesystem
+			in = getObfs4InputStream();
+			out = new FileOutputStream(obfs4File);
+			IoUtils.copyAndClose(in, out);
+			// Make the Obfs4 proxy executable
+			if (!obfs4File.setExecutable(true, true)) throw new IOException();
 			// Copy the config file to the filesystem
 			in = getConfigInputStream();
 			out = new FileOutputStream(configFile);
@@ -314,6 +321,16 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
 		return zin;
 	}
 
+	private InputStream getObfs4InputStream() throws IOException {
+		if (LOG.isLoggable(INFO))
+			LOG.info("Installing obfs4proxy binary for " + architecture);
+		InputStream in = resourceProvider
+				.getResourceInputStream("obfs4proxy_" + architecture, ".zip");
+		ZipInputStream zin = new ZipInputStream(in);
+		if (zin.getNextEntry() == null) throw new IOException();
+		return zin;
+	}
+
 	private InputStream getConfigInputStream() {
 		return getClass().getClassLoader().getResourceAsStream("torrc");
 	}
@@ -472,6 +489,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
 		if (enable) {
 			Collection<String> conf = new ArrayList<>();
 			conf.add("UseBridges 1");
+			conf.add("ClientTransportPlugin obfs4 exec " +
+					obfs4File.getAbsolutePath());
 			conf.addAll(circumventionProvider.getBridges());
 			controlConnection.setConf(conf);
 		} else {
diff --git a/bramble-core/src/main/resources/bridges b/bramble-core/src/main/resources/bridges
index 628a7b33e2..e7a30ebdc9 100644
--- a/bramble-core/src/main/resources/bridges
+++ b/bramble-core/src/main/resources/bridges
@@ -1,8 +1,4 @@
-Bridge 131.252.210.150:8081 0E858AC201BF0F3FA3C462F64844CBFFC7297A42
-Bridge 67.205.189.122:8443 12D64D5D44E20169585E7378580C0D33A872AD98
-Bridge 45.32.148.146:8443 0CE016FB2462D8BF179AE71F7D702D09DEAC3F1D
-Bridge 148.251.90.59:7510 019F727CA6DCA6CA5C90B55E477B7D87981E75BC
-Bridge 45.55.1.74:8443 6F18FEFBB0CAECD5ABA755312FCCB34FC11A7AB8
-Bridge 85.229.131.78:444 50E433CCC5FEC11CC34CB4D92033561E065EA106
-Bridge 178.62.62.193:8443 391B1F9B6A28A1C5FAE1872283985F975E5DB029
-Bridge 45.76.29.92:8443 ECF1DD51A46FDEF2C50CED992EEEAE8DED18DA0C
\ No newline at end of file
+Bridge obfs4 204.13.164.110:33742 B31A7DAD9AACEDDB9915A16617BB8F06BA429D6B cert=d8iZDGoQ6OwLZRWbuyb+3lK0ESwCl5u8mV7rAbMte6pDqEeR5DoRRa5yKiPIiianX9HdSQ iat-mode=0
+Bridge obfs4 78.46.188.239:37356 5A2D2F4158D0453E00C7C176978D3F41D69C45DB cert=3c0SwxpOisbohNxEc4tb875RVW8eOu1opRTVXJhafaKA/PNNtI7ElQIVOVZg1AdL5bxGCw iat-mode=0
+Bridge obfs4 52.15.78.72:9443 02069A3C5362476936B62BA6F5ACC41ABD573A9B cert=ijYG/OKc7kqu2YzKNFfeXN7/BG2BOgfEP2KyYEiGDQthnHbsOiTWHeIG0WJVW+BckzDgKw iat-mode=0
+Bridge obfs4 13.58.29.242:9443 0C58939A77DA6B6B29D4B5236A75865659607AE0 cert=OylWIEHb/ezpq1zWxW0sgKRn+9ARH2eOcQOZ8/Gew+4l+oKOhQ2jUX/Y+FSl61JorXZUWA iat-mode=0
\ No newline at end of file
diff --git a/bramble-java/build.gradle b/bramble-java/build.gradle
index 41e81ea24b..1b11148624 100644
--- a/bramble-java/build.gradle
+++ b/bramble-java/build.gradle
@@ -17,6 +17,7 @@ dependencies {
 	implementation 'net.java.dev.jna:jna:4.5.2'
 	implementation 'net.java.dev.jna:jna-platform:4.5.2'
 	tor 'org.briarproject:tor:0.3.4.8@zip'
+	tor 'org.briarproject:obfs4proxy:0.0.7@zip'
 
 	apt 'com.google.dagger:dagger-compiler:2.0.2'
 
-- 
GitLab