1111import java .io .IOException ;
1212import java .io .InputStream ;
1313import java .net .InetAddress ;
14+ import java .net .Proxy ;
1415import java .net .UnknownHostException ;
16+ import java .util .Arrays ;
1517import java .util .Collections ;
1618import java .util .HashMap ;
1719import java .util .Iterator ;
2224import okhttp3 .Cache ;
2325import okhttp3 .Cookie ;
2426import okhttp3 .CookieJar ;
27+ import okhttp3 .Dns ;
2528import okhttp3 .HttpUrl ;
2629import okhttp3 .OkHttpClient ;
2730import okhttp3 .Request ;
3134
3235public class Http {
3336 private static final OkHttpClient httpClient ;
34- private static final OkHttpClient httpClientCachable ;
37+ private static final OkHttpClient httpClientWithCache ;
3538
3639 static {
3740 OkHttpClient .Builder httpclientBuilder = new OkHttpClient .Builder ();
38- httpclientBuilder .connectTimeout (11 , TimeUnit .SECONDS );
41+ // Default is 10, extend it a bit for slow mobile connections.
42+ httpclientBuilder .connectTimeout (15 , TimeUnit .SECONDS );
43+ httpclientBuilder .writeTimeout (15 , TimeUnit .SECONDS );
44+ httpclientBuilder .readTimeout (15 , TimeUnit .SECONDS );
45+ httpclientBuilder .proxy (Proxy .NO_PROXY ); // Do not use system proxy
46+ Dns dns = Dns .SYSTEM ;
3947 try {
48+ InetAddress [] cloudflareBootstrap = new InetAddress [] {
49+ InetAddress .getByName ("162.159.36.1" ),
50+ InetAddress .getByName ("162.159.46.1" ),
51+ InetAddress .getByName ("1.1.1.1" ),
52+ InetAddress .getByName ("1.0.0.1" ),
53+ InetAddress .getByName ("162.159.132.53" ),
54+ InetAddress .getByName ("2606:4700:4700::1111" ),
55+ InetAddress .getByName ("2606:4700:4700::1001" ),
56+ InetAddress .getByName ("2606:4700:4700::0064" ),
57+ InetAddress .getByName ("2606:4700:4700::6400" )
58+ };
59+ dns = s -> {
60+ if ("cloudflare-dns.com" .equals (s )) {
61+ return Arrays .asList (cloudflareBootstrap );
62+ }
63+ return Dns .SYSTEM .lookup (s );
64+ };
65+ httpclientBuilder .dns (dns );
4066 httpclientBuilder .cookieJar (new CDNCookieJar ());
41- httpclientBuilder . dns ( new DnsOverHttps .Builder ().client (httpclientBuilder .build ()).url (
67+ dns = new DnsOverHttps .Builder ().client (httpclientBuilder .build ()).url (
4268 Objects .requireNonNull (HttpUrl .parse ("https://cloudflare-dns.com/dns-query" )))
43- .bootstrapDnsHosts (
44- InetAddress .getByName ("162.159.36.1" ),
45- InetAddress .getByName ("162.159.46.1" ),
46- InetAddress .getByName ("1.1.1.1" ),
47- InetAddress .getByName ("1.0.0.1" ),
48- InetAddress .getByName ("162.159.132.53" ),
49- InetAddress .getByName ("2606:4700:4700::1111" ),
50- InetAddress .getByName ("2606:4700:4700::1001" ),
51- InetAddress .getByName ("2606:4700:4700::0064" ),
52- InetAddress .getByName ("2606:4700:4700::6400" )
53- ).resolvePrivateAddresses (true ).build ());
69+ .bootstrapDnsHosts (cloudflareBootstrap ).resolvePrivateAddresses (true ).build ();
5470 } catch (UnknownHostException |RuntimeException e ) {
5571 Log .e ("Http" , "Failed to init DoH" , e );
5672 }
5773 httpclientBuilder .cookieJar (CookieJar .NO_COOKIES );
74+ httpclientBuilder .dns (dns );
5875 httpClient = httpclientBuilder .build ();
5976 MainApplication mainApplication = MainApplication .getINSTANCE ();
6077 if (mainApplication != null ) {
6178 httpclientBuilder .cache (new Cache (
6279 new File (mainApplication .getCacheDir (), "http_cache" ),
6380 2L * 1024L * 1024L )); // 2Mib of cache
6481 httpclientBuilder .cookieJar (new CDNCookieJar ());
65- httpClientCachable = httpclientBuilder .build ();
82+ httpClientWithCache = httpclientBuilder .build ();
6683 } else {
67- httpClientCachable = httpClient ;
84+ httpClientWithCache = httpClient ;
6885 }
6986 }
7087
@@ -73,7 +90,7 @@ public static OkHttpClient getHttpclientNoCache() {
7390 }
7491
7592 public static OkHttpClient getHttpclientWithCache () {
76- return httpClientCachable ;
93+ return httpClientWithCache ;
7794 }
7895
7996 private static Request .Builder makeRequestBuilder () {
@@ -82,7 +99,7 @@ private static Request.Builder makeRequestBuilder() {
8299 }
83100
84101 public static byte [] doHttpGet (String url ,boolean allowCache ) throws IOException {
85- Response response = (allowCache ? httpClientCachable : httpClient ).newCall (
102+ Response response = (allowCache ? httpClientWithCache : httpClient ).newCall (
86103 makeRequestBuilder ().url (url ).get ().build ()
87104 ).execute ();
88105 // 200 == success, 304 == cache valid
0 commit comments