From 3e6b991c9ddd24177d7633a974671879ae5fa66a Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Fri, 8 Mar 2024 14:13:05 +0000
Subject: [PATCH] Don't use vanilla bridges when non-default obfs4 are
 recommended.

---
 .../onionwrapper/CircumventionProvider.java   | 15 ++++++----
 .../CircumventionProviderImpl.java            | 29 ++++++++-----------
 .../CircumventionProviderImplTest.java        | 19 +++++++-----
 .../briarproject/onionwrapper/BridgeTest.java | 19 +++++++-----
 4 files changed, 44 insertions(+), 38 deletions(-)

diff --git a/onionwrapper-core/src/main/java/org/briarproject/onionwrapper/CircumventionProvider.java b/onionwrapper-core/src/main/java/org/briarproject/onionwrapper/CircumventionProvider.java
index f95668c..c437a42 100644
--- a/onionwrapper-core/src/main/java/org/briarproject/onionwrapper/CircumventionProvider.java
+++ b/onionwrapper-core/src/main/java/org/briarproject/onionwrapper/CircumventionProvider.java
@@ -28,22 +28,27 @@ public interface CircumventionProvider {
 	/**
 	 * Countries where default obfs4 bridges should be used.
 	 */
-	String[] COUNTRIES_DEFAULT_BRIDGES = {"BY"};
+	String[] COUNTRIES_DEFAULT_OBFS4 = {"BY"};
 
 	/**
-	 * Countries where non-default obfs4 and vanilla bridges should be used.
+	 * Countries where non-default obfs4 bridges should be used.
 	 */
-	String[] COUNTRIES_NON_DEFAULT_BRIDGES = {"BY", "CN", "EG", "HK", "IR", "RU"};
+	String[] COUNTRIES_NON_DEFAULT_OBFS4 = {"BY", "CN", "EG", "HK", "IR", "RU"};
+
+	/**
+	 * Countries where vanilla bridges should be used.
+	 */
+	String[] COUNTRIES_VANILLA = {"BY"};
 
 	/**
 	 * Countries where meek bridges should be used.
 	 */
-	String[] COUNTRIES_MEEK_BRIDGES = {"TM"};
+	String[] COUNTRIES_MEEK = {"TM"};
 
 	/**
 	 * Countries where snowflake bridges should be used.
 	 */
-	String[] COUNTRIES_SNOWFLAKE_BRIDGES = {"BY", "CN", "EG", "HK", "IR", "RU", "TM"};
+	String[] COUNTRIES_SNOWFLAKE = {"BY", "CN", "EG", "HK", "IR", "RU", "TM"};
 
 	/**
 	 * Returns the types of bridge connection that are suitable for the given country, or
diff --git a/onionwrapper-core/src/main/java/org/briarproject/onionwrapper/CircumventionProviderImpl.java b/onionwrapper-core/src/main/java/org/briarproject/onionwrapper/CircumventionProviderImpl.java
index eef54fc..4b53217 100644
--- a/onionwrapper-core/src/main/java/org/briarproject/onionwrapper/CircumventionProviderImpl.java
+++ b/onionwrapper-core/src/main/java/org/briarproject/onionwrapper/CircumventionProviderImpl.java
@@ -27,14 +27,13 @@ class CircumventionProviderImpl implements CircumventionProvider {
 
 	private static final String DEFAULT_COUNTRY_CODE = "ZZ";
 
-	private static final Set<String> DEFAULT_BRIDGES =
-			new HashSet<>(asList(COUNTRIES_DEFAULT_BRIDGES));
-	private static final Set<String> NON_DEFAULT_BRIDGES =
-			new HashSet<>(asList(COUNTRIES_NON_DEFAULT_BRIDGES));
-	private static final Set<String> MEEK_BRIDGES =
-			new HashSet<>(asList(COUNTRIES_MEEK_BRIDGES));
-	private static final Set<String> SNOWFLAKE_BRIDGES =
-			new HashSet<>(asList(COUNTRIES_SNOWFLAKE_BRIDGES));
+	private static final Set<String> USE_DEFAULT_OBFS4 =
+			new HashSet<>(asList(COUNTRIES_DEFAULT_OBFS4));
+	private static final Set<String> USE_NON_DEFAULT_OBFS4 =
+			new HashSet<>(asList(COUNTRIES_NON_DEFAULT_OBFS4));
+	private static final Set<String> USE_VANILLA = new HashSet<>(asList(COUNTRIES_VANILLA));
+	private static final Set<String> USE_MEEK = new HashSet<>(asList(COUNTRIES_MEEK));
+	private static final Set<String> USE_SNOWFLAKE = new HashSet<>(asList(COUNTRIES_SNOWFLAKE));
 
 	@Inject
 	CircumventionProviderImpl() {
@@ -43,15 +42,11 @@ class CircumventionProviderImpl implements CircumventionProvider {
 	@Override
 	public List<BridgeType> getSuitableBridgeTypes(String countryCode) {
 		List<BridgeType> types = new ArrayList<>();
-		if (DEFAULT_BRIDGES.contains(countryCode)) {
-			types.add(DEFAULT_OBFS4);
-		}
-		if (NON_DEFAULT_BRIDGES.contains(countryCode)) {
-			types.add(NON_DEFAULT_OBFS4);
-			types.add(VANILLA);
-		}
-		if (MEEK_BRIDGES.contains(countryCode)) types.add(MEEK);
-		if (SNOWFLAKE_BRIDGES.contains(countryCode)) types.add(SNOWFLAKE);
+		if (USE_DEFAULT_OBFS4.contains(countryCode)) types.add(DEFAULT_OBFS4);
+		if (USE_NON_DEFAULT_OBFS4.contains(countryCode)) types.add(NON_DEFAULT_OBFS4);
+		if (USE_VANILLA.contains(countryCode)) types.add(VANILLA);
+		if (USE_MEEK.contains(countryCode)) types.add(MEEK);
+		if (USE_SNOWFLAKE.contains(countryCode)) types.add(SNOWFLAKE);
 		// If we don't have any recommendations for this country then use the defaults
 		if (types.isEmpty()) {
 			types.add(DEFAULT_OBFS4);
diff --git a/onionwrapper-core/src/test/java/org/briarproject/onionwrapper/CircumventionProviderImplTest.java b/onionwrapper-core/src/test/java/org/briarproject/onionwrapper/CircumventionProviderImplTest.java
index 3dd57ac..83b8991 100644
--- a/onionwrapper-core/src/test/java/org/briarproject/onionwrapper/CircumventionProviderImplTest.java
+++ b/onionwrapper-core/src/test/java/org/briarproject/onionwrapper/CircumventionProviderImplTest.java
@@ -8,10 +8,11 @@ import static org.briarproject.onionwrapper.CircumventionProvider.BridgeType.MEE
 import static org.briarproject.onionwrapper.CircumventionProvider.BridgeType.NON_DEFAULT_OBFS4;
 import static org.briarproject.onionwrapper.CircumventionProvider.BridgeType.SNOWFLAKE;
 import static org.briarproject.onionwrapper.CircumventionProvider.BridgeType.VANILLA;
-import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_DEFAULT_BRIDGES;
-import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_MEEK_BRIDGES;
-import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_NON_DEFAULT_BRIDGES;
-import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_SNOWFLAKE_BRIDGES;
+import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_DEFAULT_OBFS4;
+import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_MEEK;
+import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_NON_DEFAULT_OBFS4;
+import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_SNOWFLAKE;
+import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_VANILLA;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
@@ -22,17 +23,19 @@ public class CircumventionProviderImplTest extends BaseTest {
 
 	@Test
 	public void testGetSuitableBridgeTypes() {
-		for (String countryCode : COUNTRIES_DEFAULT_BRIDGES) {
+		for (String countryCode : COUNTRIES_DEFAULT_OBFS4) {
 			testBridgesAreSuitableAndExist(DEFAULT_OBFS4, countryCode);
 		}
-		for (String countryCode : COUNTRIES_NON_DEFAULT_BRIDGES) {
+		for (String countryCode : COUNTRIES_NON_DEFAULT_OBFS4) {
 			testBridgesAreSuitableAndExist(NON_DEFAULT_OBFS4, countryCode);
+		}
+		for (String countryCode : COUNTRIES_VANILLA) {
 			testBridgesAreSuitableAndExist(VANILLA, countryCode);
 		}
-		for (String countryCode : COUNTRIES_MEEK_BRIDGES) {
+		for (String countryCode : COUNTRIES_MEEK) {
 			testBridgesAreSuitableAndExist(MEEK, countryCode);
 		}
-		for (String countryCode : COUNTRIES_SNOWFLAKE_BRIDGES) {
+		for (String countryCode : COUNTRIES_SNOWFLAKE) {
 			testBridgesAreSuitableAndExist(SNOWFLAKE, countryCode);
 		}
 		// If bridges are enabled manually in a country with no specific bridge recommendations,
diff --git a/onionwrapper-java/src/test/java/org/briarproject/onionwrapper/BridgeTest.java b/onionwrapper-java/src/test/java/org/briarproject/onionwrapper/BridgeTest.java
index e6a166f..1a4b54b 100644
--- a/onionwrapper-java/src/test/java/org/briarproject/onionwrapper/BridgeTest.java
+++ b/onionwrapper-java/src/test/java/org/briarproject/onionwrapper/BridgeTest.java
@@ -30,10 +30,11 @@ import static org.briarproject.onionwrapper.CircumventionProvider.BridgeType.MEE
 import static org.briarproject.onionwrapper.CircumventionProvider.BridgeType.NON_DEFAULT_OBFS4;
 import static org.briarproject.onionwrapper.CircumventionProvider.BridgeType.SNOWFLAKE;
 import static org.briarproject.onionwrapper.CircumventionProvider.BridgeType.VANILLA;
-import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_DEFAULT_BRIDGES;
-import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_MEEK_BRIDGES;
-import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_NON_DEFAULT_BRIDGES;
-import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_SNOWFLAKE_BRIDGES;
+import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_DEFAULT_OBFS4;
+import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_MEEK;
+import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_NON_DEFAULT_OBFS4;
+import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_SNOWFLAKE;
+import static org.briarproject.onionwrapper.CircumventionProvider.COUNTRIES_VANILLA;
 import static org.briarproject.onionwrapper.TestUtils.deleteTestDirectory;
 import static org.briarproject.onionwrapper.TestUtils.getArchitectureForTorBinary;
 import static org.briarproject.onionwrapper.TestUtils.getTestDirectory;
@@ -71,27 +72,29 @@ public class BridgeTest extends BaseTest {
 					if (bridges.add(bridge)) states.add(new Params(bridge, type, stats));
 				}
 			}
-			for (String countryCode : COUNTRIES_DEFAULT_BRIDGES) {
+			for (String countryCode : COUNTRIES_DEFAULT_OBFS4) {
 				for (String bridge : provider.getBridges(DEFAULT_OBFS4, countryCode)) {
 					if (bridges.add(bridge)) states.add(new Params(bridge, DEFAULT_OBFS4, stats));
 				}
 			}
-			for (String countryCode : COUNTRIES_NON_DEFAULT_BRIDGES) {
+			for (String countryCode : COUNTRIES_NON_DEFAULT_OBFS4) {
 				for (String bridge : provider.getBridges(NON_DEFAULT_OBFS4, countryCode)) {
 					if (bridges.add(bridge)) {
 						states.add(new Params(bridge, NON_DEFAULT_OBFS4, stats));
 					}
 				}
+			}
+			for (String countryCode : COUNTRIES_VANILLA) {
 				for (String bridge : provider.getBridges(VANILLA, countryCode)) {
 					if (bridges.add(bridge)) states.add(new Params(bridge, VANILLA, stats));
 				}
 			}
-			for (String countryCode : COUNTRIES_MEEK_BRIDGES) {
+			for (String countryCode : COUNTRIES_MEEK) {
 				for (String bridge : provider.getBridges(MEEK, countryCode)) {
 					if (bridges.add(bridge)) states.add(new Params(bridge, MEEK, stats));
 				}
 			}
-			for (String countryCode : COUNTRIES_SNOWFLAKE_BRIDGES) {
+			for (String countryCode : COUNTRIES_SNOWFLAKE) {
 				for (String bridge : provider.getBridges(SNOWFLAKE, countryCode)) {
 					if (bridges.add(bridge)) states.add(new Params(bridge, SNOWFLAKE, stats));
 				}
-- 
GitLab