Skip to content

Commit b82d128

Browse files
better handle http errors
Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent 4c34f3b commit b82d128

4 files changed

Lines changed: 65 additions & 24 deletions

File tree

app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ dependencies {
526526
implementation("org.jetbrains:annotations-java5:24.0.1")
527527

528528
// debugging
529-
debugImplementation("com.squareup.leakcanary:leakcanary-android:2.11")
529+
debugImplementation("com.squareup.leakcanary:leakcanary-android:2.12")
530530

531531
// desugaring
532532
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.3")

app/src/main/kotlin/com/fox2code/mmm/androidacy/AndroidacyActivity.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,18 @@ class AndroidacyActivity : FoxActivity() {
300300
}
301301

302302
override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean {
303-
if (BuildConfig.DEBUG_HTTP) {
303+
return if (BuildConfig.DEBUG) {
304304
when (consoleMessage.messageLevel()) {
305305
MessageLevel.TIP -> Timber.tag("JSLog").i(consoleMessage.message())
306306
MessageLevel.LOG -> Timber.tag("JSLog").d(consoleMessage.message())
307307
MessageLevel.WARNING -> Timber.tag("JSLog").w(consoleMessage.message())
308308
MessageLevel.ERROR -> Timber.tag("JSLog").e(consoleMessage.message())
309309
else -> Timber.tag("JSLog").v(consoleMessage.message())
310310
}
311+
true
312+
} else {
313+
false
311314
}
312-
return true
313315
}
314316

315317
override fun onProgressChanged(view: WebView, newProgress: Int) {
@@ -323,8 +325,8 @@ class AndroidacyActivity : FoxActivity() {
323325
Timber.i("Progress: %d, setting indeterminate to false", newProgress)
324326
prgInd.isIndeterminate = false
325327
}
326-
prgInd.setProgressCompat(newProgress, true)
327-
if (newProgress == 100 && prgInd.visibility != View.INVISIBLE) {
328+
prgInd.setProgress(newProgress, true)
329+
if (newProgress == 100 && prgInd.visibility != View.GONE) {
328330
Timber.i("Progress: %d, hiding progress bar", newProgress)
329331
prgInd.isIndeterminate = true
330332
prgInd.visibility = View.GONE

app/src/main/kotlin/com/fox2code/mmm/utils/io/net/Http.kt

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,5 @@
386386
<string name="logs_saved">Saved logs successfully</string>
387387
<string name="error_opening_notes">Error in opening module notes. Logs may have the reason.</string>
388388
<string name="submit_feedback">Submit feedback</string>
389+
<string name="ssl_error">Potential SSL interception detected.</string>
389390
</resources>

0 commit comments

Comments
 (0)