Commit 2241c5aa authored by Mygod's avatar Mygod

Prevent hanging connection for retrieving remote config

parent 90b05958
......@@ -67,7 +67,6 @@ dependencies {
api 'com.crashlytics.sdk.android:crashlytics:2.9.8'
api 'com.google.firebase:firebase-config:16.1.3'
api 'com.google.firebase:firebase-core:16.0.6'
api 'com.squareup.okhttp3:okhttp:3.12.1'
api "com.takisoft.preferencex:preferencex:$preferencexVersion"
api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0'
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycleVersion"
......
......@@ -36,11 +36,9 @@ import com.github.shadowsocks.utils.signaturesCompat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.File
import java.io.IOException
import java.net.HttpURLConnection
import java.net.InetAddress
import java.net.UnknownHostException
import java.security.MessageDigest
......@@ -56,19 +54,23 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro
suspend fun init() {
if (profile.host == "198.199.101.152") {
val client = OkHttpClient.Builder().build()
val mdg = MessageDigest.getInstance("SHA-1")
mdg.update(Core.packageInfo.signaturesCompat.first().toByteArray())
val requestBody = FormBody.Builder()
.add("sig", String(Base64.encode(mdg.digest(), 0)))
.build()
val request = Request.Builder()
.url(RemoteConfig.proxyUrl)
.post(requestBody)
.build()
val proxies = withTimeout(30_000) {
withContext(Dispatchers.IO) { client.newCall(request).execute().body()!!.string() }
val conn = RemoteConfig.proxyUrl.openConnection() as HttpURLConnection
conn.requestMethod = "POST"
conn.doOutput = true
val proxies = try {
withTimeout(30_000) {
withContext(Dispatchers.IO) {
conn.outputStream.bufferedWriter().use {
it.write("sig=" + String(Base64.encode(mdg.digest(), Base64.DEFAULT)))
}
conn.inputStream.bufferedReader().readText()
}
}
} finally {
conn.disconnect()
}.split('|').toMutableList()
proxies.shuffle()
val proxy = proxies.first().split(':')
......
......@@ -25,11 +25,12 @@ import androidx.core.os.bundleOf
import com.github.shadowsocks.Core
import com.github.shadowsocks.core.R
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import java.net.URL
object RemoteConfig {
private val config = FirebaseRemoteConfig.getInstance().apply { setDefaults(R.xml.default_configs) }
val proxyUrl get() = config.getString("proxy_url")
val proxyUrl get() = URL(config.getString("proxy_url"))
fun fetch() = config.fetch().addOnCompleteListener {
if (it.isSuccessful) config.activateFetched() else {
......
......@@ -22,5 +22,3 @@
#-renamesourcefileattribute SourceFile
-dontwarn com.google.android.gms.internal.**
-dontwarn okhttp3.**
-dontwarn okio.**
......@@ -22,5 +22,3 @@
#-renamesourcefileattribute SourceFile
-dontwarn com.google.android.gms.internal.**
-dontwarn okhttp3.**
-dontwarn okio.**
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment