@@ -73,7 +73,6 @@ enum class Http {;
7373 * can help make the app to work later when the current DNS system
7474 * isn't functional or available.
7575 *
76- *
7776 * Note: DNS Cache is stored in user data.
7877 */
7978 private class FallBackDNS (context : Context , parent : Dns , vararg fallbacks : String? ) : Dns {
@@ -429,8 +428,7 @@ enum class Http {;
429428 }
430429
431430 private fun followRedirects (
432- builder : OkHttpClient .Builder ,
433- followRedirects : Boolean
431+ builder : OkHttpClient .Builder , followRedirects : Boolean
434432 ): OkHttpClient .Builder {
435433 return builder.followRedirects(followRedirects).followSslRedirects(followRedirects)
436434 }
@@ -464,14 +462,11 @@ enum class Http {;
464462 needCaptchaAndroidacyHost = null
465463 }
466464
465+ @Suppress(" unused" )
467466 @JvmStatic
468467 @SuppressLint(" RestrictedApi" )
469468 @Throws(IOException ::class )
470469 fun doHttpGet (url : String , allowCache : Boolean ): ByteArray {
471- if (BuildConfig .DEBUG_HTTP ) {
472- // Log, but set all query parameters values to "****" while keeping the keys
473- Timber .d(" doHttpGet: %s" , url.replace(" =[^&]*" .toRegex(), " =****" ))
474- }
475470 var response: Response ?
476471 response = try {
477472 (if (allowCache) getHttpClientWithCache() else getHttpClient())!! .newCall(
@@ -480,7 +475,16 @@ enum class Http {;
480475 ).get().build()
481476 ).execute()
482477 } catch (e: IOException ) {
483- Timber .e(e, " Failed to fetch %s" , url.replace(" =[^&]*" .toRegex(), " =****" ))
478+ Timber .e(e, " Failed to post %s" , url)
479+ // detect ssl errors, i.e., cert authority invalid by looking at the message
480+ if (e.message != null && e.message!! .contains(" _CERT_" )) {
481+ MainActivity .getFoxActivity(MainApplication .INSTANCE !! ).runOnUiThread {
482+ // show toast
483+ Toast .makeText(
484+ MainApplication .INSTANCE , R .string.ssl_error, Toast .LENGTH_LONG
485+ ).show()
486+ }
487+ }
484488 throw HttpException (e.message, 0 )
485489 }
486490 if (BuildConfig .DEBUG_HTTP ) {
@@ -550,6 +554,7 @@ enum class Http {;
550554 return responseBody?.bytes() ? : ByteArray (0 )
551555 }
552556
557+ @Suppress(" unused" )
553558 @JvmStatic
554559 @Throws(IOException ::class )
555560 fun doHttpPost (url : String , data : String , allowCache : Boolean ): ByteArray {
@@ -560,11 +565,26 @@ enum class Http {;
560565 private fun doHttpPostRaw (url : String , data : String , allowCache : Boolean ): Any {
561566 Timber .d(" POST %s" , url)
562567 var response: Response ?
563- response = (if (allowCache) getHttpClientWithCache() else getHttpClient())!! .newCall(
564- Request .Builder ().url(url).post(
565- JsonRequestBody .from(data)
566- ).header(" Content-Type" , " application/json" ).build()
567- ).execute()
568+ try {
569+ response =
570+ (if (allowCache) getHttpClientWithCache() else getHttpClient())!! .newCall(
571+ Request .Builder ().url(url).post(
572+ JsonRequestBody .from(data)
573+ ).header(" Content-Type" , " application/json" ).build()
574+ ).execute()
575+ } catch (e: IOException ) {
576+ Timber .e(e, " Failed to post %s" , url)
577+ // detect ssl errors, i.e., cert authority invalid by looking at the message
578+ if (e.message != null && e.message!! .contains(" _CERT_" )) {
579+ MainActivity .getFoxActivity(MainApplication .INSTANCE !! ).runOnUiThread {
580+ // show toast
581+ Toast .makeText(
582+ MainApplication .INSTANCE , R .string.ssl_error, Toast .LENGTH_LONG
583+ ).show()
584+ }
585+ }
586+ throw HttpException (e.message, 0 )
587+ }
568588 if (response.isRedirect) {
569589 // follow redirect with same method
570590 Timber .d(" doHttpPostRaw: following redirect: %s" , response.header(" Location" ))
@@ -623,8 +643,23 @@ enum class Http {;
623643 @JvmStatic
624644 @Throws(IOException ::class )
625645 fun doHttpGet (url : String , progressListener : ProgressListener ): ByteArray {
626- val response =
627- getHttpClient()!! .newCall(Request .Builder ().url(url).get().build()).execute()
646+ val response: Response
647+ try {
648+ response =
649+ getHttpClient()!! .newCall(Request .Builder ().url(url).get().build()).execute()
650+ } catch (e: IOException ) {
651+ Timber .e(e, " Failed to post %s" , url)
652+ // detect ssl errors, i.e., cert authority invalid by looking at the message
653+ if (e.message != null && e.message!! .contains(" _CERT_" )) {
654+ MainActivity .getFoxActivity(MainApplication .INSTANCE !! ).runOnUiThread {
655+ // show toast
656+ Toast .makeText(
657+ MainApplication .INSTANCE , R .string.ssl_error, Toast .LENGTH_LONG
658+ ).show()
659+ }
660+ }
661+ throw HttpException (e.message, 0 )
662+ }
628663 if (response.code != 200 && response.code != 204 ) {
629664 Timber .e(" Failed to fetch " + url + " , code: " + response.code)
630665 checkNeedCaptchaAndroidacy(url, response.code)
@@ -733,7 +768,7 @@ enum class Http {;
733768 // are we connected to a network with internet capabilities?
734769 val networkCapabilities =
735770 connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
736- val systemSaysYes = networkCapabilities != null && networkCapabilities.hasCapability(
771+ val systemSaysYes = networkCapabilities != null && networkCapabilities.hasCapability(
737772 NetworkCapabilities .NET_CAPABILITY_INTERNET
738773 )
739774 Timber .d(" System says we have internet: $systemSaysYes " )
@@ -757,17 +792,20 @@ enum class Http {;
757792 if (! systemSaysYes) return false
758793 // check ourselves
759794 val hasInternet = try {
760- val resp = doHttpGet(" https://production-api.androidacy.com/ping " , false )
795+ val resp = doHttpGet(" https://production-api.androidacy.com/cdn-cgi/trace " , false )
761796 val respString = String (resp)
762797 Timber .d(" Ping response: $respString " )
763- true
798+ // resp should include that scheme is https and h is production-api.androidacy.com
799+ respString.contains(" scheme=https" ) && respString.contains(" h=production-api.androidacy.com" )
764800 } catch (e: HttpException ) {
765801 Timber .e(e, " Failed to check internet connection" )
766802 false
767803 }
768804 Timber .d(" We say we have internet: $hasInternet " )
769805 lastConnectivityCheck = System .currentTimeMillis()
770- lastConnectivityResult = systemSaysYes && hasInternet
806+ @Suppress(" KotlinConstantConditions" )
807+ lastConnectivityResult =
808+ systemSaysYes && hasInternet
771809 return lastConnectivityResult
772810 }
773811 }
0 commit comments