77import android .system .ErrnoException ;
88import android .system .Os ;
99import android .util .Log ;
10+ import android .webkit .CookieManager ;
1011
1112import androidx .annotation .NonNull ;
1213
2829import java .util .HashSet ;
2930import java .util .Iterator ;
3031import java .util .List ;
31- import java .util .Locale ;
32+ import java .util .Map ;
3233import java .util .Objects ;
3334import java .util .concurrent .TimeUnit ;
3435
3536import okhttp3 .Cache ;
36- import okhttp3 .ConnectionSpec ;
3737import okhttp3 .Cookie ;
3838import okhttp3 .CookieJar ;
3939import okhttp3 .Dns ;
@@ -50,6 +50,7 @@ public class Http {
5050 private static final OkHttpClient httpClient ;
5151 private static final OkHttpClient httpClientWithCache ;
5252 private static final FallBackDNS fallbackDNS ;
53+ private static final String androidacyUA ;
5354
5455 static {
5556 MainApplication mainApplication = MainApplication .getINSTANCE ();
@@ -66,6 +67,7 @@ public class Http {
6667 }
6768 throw error ;
6869 }
70+ CookieManager .getInstance ().setAcceptCookie (true );
6971 OkHttpClient .Builder httpclientBuilder = new OkHttpClient .Builder ();
7072 // Default is 10, extend it a bit for slow mobile connections.
7173 httpclientBuilder .connectTimeout (15 , TimeUnit .SECONDS );
@@ -93,15 +95,15 @@ public class Http {
9395 return Dns .SYSTEM .lookup (s );
9496 };
9597 httpclientBuilder .dns (dns );
96- httpclientBuilder .cookieJar (new CDNCookieJar ());
98+ httpclientBuilder .cookieJar (new CDNCookieJar (false ));
9799 dns = new DnsOverHttps .Builder ().client (httpclientBuilder .build ()).url (
98100 Objects .requireNonNull (HttpUrl .parse ("https://cloudflare-dns.com/dns-query" )))
99101 .bootstrapDnsHosts (cloudflareBootstrap ).resolvePrivateAddresses (true ).build ();
100102 } catch (UnknownHostException |RuntimeException e ) {
101103 Log .e (TAG , "Failed to init DoH" , e );
102104 }
103105 httpclientBuilder .cookieJar (CookieJar .NO_COOKIES );
104- final String androidacyUA = // User-Agent format was agreed on telegram
106+ androidacyUA = // User-Agent format was agreed on telegram
105107 "Mozilla/5.0 (Linux; Android " + Build .VERSION .RELEASE + "; " + Build .DEVICE +")" +
106108 " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Mobile Safari/537.36"
107109 + " FoxMmm/" + BuildConfig .VERSION_CODE ;
@@ -125,11 +127,11 @@ public class Http {
125127 "camo.githubusercontent.com" , "user-images.githubusercontent.com" ,
126128 "cdn.jsdelivr.net" , "img.shields.io" , "magisk-modules-repo.github.io" ,
127129 "www.androidacy.com" , "api.androidacy.com" ));
130+ httpclientBuilder .cookieJar (new CDNCookieJar (true ));
128131 httpClient = httpclientBuilder .build ();
129132 httpclientBuilder .cache (new Cache (
130133 new File (mainApplication .getCacheDir (), "http_cache" ),
131134 16L * 1024L * 1024L )); // 16Mib of cache
132- httpclientBuilder .cookieJar (new CDNCookieJar ());
133135 httpClientWithCache = httpclientBuilder .build ();
134136 Log .i (TAG , "Initialized Http successfully!" );
135137 }
@@ -208,6 +210,10 @@ public static void cleanDnsCache() {
208210 }
209211 }
210212
213+ public static String getAndroidacyUA () {
214+ return androidacyUA ;
215+ }
216+
211217 /**
212218 * Cookie jar that allow CDN cookies, reset on app relaunch
213219 * Note: An argument can be made that it allow tracking but
@@ -219,11 +225,21 @@ public static void cleanDnsCache() {
219225 * */
220226 private static class CDNCookieJar implements CookieJar {
221227 private final HashMap <String , Cookie > cookieMap = new HashMap <>();
228+ private final boolean androidacySupport ;
229+
230+ private CDNCookieJar (boolean androidacySupport ) {
231+ this .androidacySupport = androidacySupport ;
232+ }
222233
223234 @ NonNull
224235 @ Override
225236 public List <Cookie > loadForRequest (@ NonNull HttpUrl httpUrl ) {
226237 if (!httpUrl .isHttps ()) return Collections .emptyList ();
238+ if (this .androidacySupport && httpUrl .host ().endsWith (".androidacy.com" )) {
239+ String cookies = CookieManager .getInstance ().getCookie (httpUrl .uri ().toString ());
240+ if (cookies == null || cookies .isEmpty ()) return Collections .emptyList ();
241+
242+ }
227243 Cookie cookies = cookieMap .get (httpUrl .url ().getHost ());
228244 return cookies == null || cookies .expiresAt () < System .currentTimeMillis () ?
229245 Collections .emptyList () : Collections .singletonList (cookies );
0 commit comments