Skip to content

Commit 502ad35

Browse files
committed
Support for specifying domain fronting ConnectionSpecs
// FREEBIE
1 parent b5ab94e commit 502ad35

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

java/src/main/java/org/whispersystems/signalservice/internal/push/PushServiceSocket.java

+26-26
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.security.KeyManagementException;
4949
import java.security.NoSuchAlgorithmException;
5050
import java.security.SecureRandom;
51+
import java.util.Collections;
5152
import java.util.LinkedList;
5253
import java.util.List;
5354
import java.util.Set;
@@ -57,9 +58,11 @@
5758
import javax.net.ssl.TrustManager;
5859
import javax.net.ssl.X509TrustManager;
5960

61+
import okhttp3.ConnectionSpec;
6062
import okhttp3.Interceptor;
6163
import okhttp3.MediaType;
6264
import okhttp3.OkHttpClient;
65+
import okhttp3.Protocol;
6366
import okhttp3.Request;
6467
import okhttp3.RequestBody;
6568
import okhttp3.Response;
@@ -571,9 +574,8 @@ private Response getConnection(String urlFragment, String method, String body)
571574
SSLContext context = SSLContext.getInstance("TLS");
572575
context.init(null, trustManagers, null);
573576

574-
OkHttpClient okHttpClient = new OkHttpClient.Builder()
575-
.sslSocketFactory(context.getSocketFactory(), (X509TrustManager)trustManagers[0])
576-
.build();
577+
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
578+
.sslSocketFactory(context.getSocketFactory(), (X509TrustManager)trustManagers[0]);
577579

578580
Request.Builder request = new Request.Builder();
579581
request.url(String.format("%s%s", url, urlFragment));
@@ -592,11 +594,18 @@ private Response getConnection(String urlFragment, String method, String body)
592594
request.addHeader("X-Signal-Agent", userAgent);
593595
}
594596

597+
if (connectionInformation.getConnectionSpec().isPresent()) {
598+
okHttpClientBuilder.connectionSpecs(Collections.singletonList(connectionInformation.getConnectionSpec().get()));
599+
} else {
600+
okHttpClientBuilder.connectionSpecs(Util.immutableList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS));
601+
}
602+
595603
if (hostHeader.isPresent()) {
596-
okHttpClient.networkInterceptors().add(new HostInterceptor(hostHeader.get()));
604+
okHttpClientBuilder.protocols(Collections.singletonList(Protocol.HTTP_1_1));
605+
request.addHeader("Host", hostHeader.get());
597606
}
598607

599-
return okHttpClient.newCall(request.build()).execute();
608+
return okHttpClientBuilder.build().newCall(request.build()).execute();
600609
} catch (IOException e) {
601610
throw new PushNetworkException(e);
602611
} catch (NoSuchAlgorithmException | KeyManagementException e) {
@@ -650,31 +659,18 @@ public String getLocation() {
650659
}
651660
}
652661

653-
private static class HostInterceptor implements Interceptor {
654-
655-
private final String host;
656-
657-
HostInterceptor(String host) {
658-
this.host = host;
659-
}
660-
661-
@Override
662-
public Response intercept(Chain chain) throws IOException {
663-
Request request = chain.request();
664-
return chain.proceed(request.newBuilder().header("Host", host).build());
665-
}
666-
}
667-
668662
private static class SignalConnectionInformation {
669663

670-
private final String url;
671-
private final Optional<String> hostHeader;
672-
private final TrustManager[] trustManagers;
664+
private final String url;
665+
private final Optional<String> hostHeader;
666+
private final Optional<ConnectionSpec> connectionSpec;
667+
private final TrustManager[] trustManagers;
673668

674669
private SignalConnectionInformation(SignalServiceUrl signalServiceUrl) {
675-
this.url = signalServiceUrl.getUrl();
676-
this.hostHeader = signalServiceUrl.getHostHeader();
677-
this.trustManagers = BlacklistingTrustManager.createFor(signalServiceUrl.getTrustStore());
670+
this.url = signalServiceUrl.getUrl();
671+
this.hostHeader = signalServiceUrl.getHostHeader();
672+
this.connectionSpec = signalServiceUrl.getConnectionSpec();
673+
this.trustManagers = BlacklistingTrustManager.createFor(signalServiceUrl.getTrustStore());
678674
}
679675

680676
String getUrl() {
@@ -688,5 +684,9 @@ Optional<String> getHostHeader() {
688684
TrustManager[] getTrustManagers() {
689685
return trustManagers;
690686
}
687+
688+
Optional<ConnectionSpec> getConnectionSpec() {
689+
return connectionSpec;
690+
}
691691
}
692692
}

java/src/main/java/org/whispersystems/signalservice/internal/push/SignalServiceUrl.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@
44
import org.whispersystems.libsignal.util.guava.Optional;
55
import org.whispersystems.signalservice.api.push.TrustStore;
66

7+
import okhttp3.ConnectionSpec;
8+
79
public class SignalServiceUrl {
810

9-
private final String url;
10-
private final Optional<String> hostHeader;
11-
private TrustStore trustStore;
11+
private final String url;
12+
private final Optional<String> hostHeader;
13+
private final Optional<ConnectionSpec> connectionSpec;
14+
private TrustStore trustStore;
1215

1316
public SignalServiceUrl(String url, TrustStore trustStore) {
14-
this(url, null, trustStore);
17+
this(url, null, trustStore, null);
1518
}
1619

17-
public SignalServiceUrl(String url, String hostHeader, TrustStore trustStore) {
18-
this.url = url;
19-
this.hostHeader = Optional.fromNullable(hostHeader);
20-
this.trustStore = trustStore;
20+
public SignalServiceUrl(String url, String hostHeader,
21+
TrustStore trustStore,
22+
ConnectionSpec connectionSpec)
23+
{
24+
this.url = url;
25+
this.hostHeader = Optional.fromNullable(hostHeader);
26+
this.trustStore = trustStore;
27+
this.connectionSpec = Optional.fromNullable(connectionSpec);
2128
}
2229

2330

@@ -32,4 +39,8 @@ public String getUrl() {
3239
public TrustStore getTrustStore() {
3340
return trustStore;
3441
}
42+
43+
public Optional<ConnectionSpec> getConnectionSpec() {
44+
return connectionSpec;
45+
}
3546
}

java/src/main/java/org/whispersystems/signalservice/internal/util/Util.java

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import java.io.OutputStream;
1313
import java.security.NoSuchAlgorithmException;
1414
import java.security.SecureRandom;
15+
import java.util.Arrays;
16+
import java.util.Collections;
17+
import java.util.List;
1518

1619
public class Util {
1720

@@ -121,4 +124,8 @@ public static int toIntExact(long value) {
121124
return (int)value;
122125
}
123126

127+
public static <T> List<T> immutableList(T... elements) {
128+
return Collections.unmodifiableList(Arrays.asList(elements.clone()));
129+
}
130+
124131
}

0 commit comments

Comments
 (0)