Commit 2241c5aa authored by Mygod's avatar Mygod

Prevent hanging connection for retrieving remote config

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