48
48
import java .security .KeyManagementException ;
49
49
import java .security .NoSuchAlgorithmException ;
50
50
import java .security .SecureRandom ;
51
+ import java .util .Collections ;
51
52
import java .util .LinkedList ;
52
53
import java .util .List ;
53
54
import java .util .Set ;
57
58
import javax .net .ssl .TrustManager ;
58
59
import javax .net .ssl .X509TrustManager ;
59
60
61
+ import okhttp3 .ConnectionSpec ;
60
62
import okhttp3 .Interceptor ;
61
63
import okhttp3 .MediaType ;
62
64
import okhttp3 .OkHttpClient ;
65
+ import okhttp3 .Protocol ;
63
66
import okhttp3 .Request ;
64
67
import okhttp3 .RequestBody ;
65
68
import okhttp3 .Response ;
@@ -571,9 +574,8 @@ private Response getConnection(String urlFragment, String method, String body)
571
574
SSLContext context = SSLContext .getInstance ("TLS" );
572
575
context .init (null , trustManagers , null );
573
576
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 ]);
577
579
578
580
Request .Builder request = new Request .Builder ();
579
581
request .url (String .format ("%s%s" , url , urlFragment ));
@@ -592,11 +594,18 @@ private Response getConnection(String urlFragment, String method, String body)
592
594
request .addHeader ("X-Signal-Agent" , userAgent );
593
595
}
594
596
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
+
595
603
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 ());
597
606
}
598
607
599
- return okHttpClient .newCall (request .build ()).execute ();
608
+ return okHttpClientBuilder . build () .newCall (request .build ()).execute ();
600
609
} catch (IOException e ) {
601
610
throw new PushNetworkException (e );
602
611
} catch (NoSuchAlgorithmException | KeyManagementException e ) {
@@ -650,31 +659,18 @@ public String getLocation() {
650
659
}
651
660
}
652
661
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
-
668
662
private static class SignalConnectionInformation {
669
663
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 ;
673
668
674
669
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 ());
678
674
}
679
675
680
676
String getUrl () {
@@ -688,5 +684,9 @@ Optional<String> getHostHeader() {
688
684
TrustManager [] getTrustManagers () {
689
685
return trustManagers ;
690
686
}
687
+
688
+ Optional <ConnectionSpec > getConnectionSpec () {
689
+ return connectionSpec ;
690
+ }
691
691
}
692
692
}
0 commit comments