Skip to content

Commit

Permalink
Prevent hanging connection for retrieving remote config
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed Jan 24, 2019
1 parent 90b0595 commit 2241c5a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
1 change: 0 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 16 additions & 14 deletions core/src/main/java/com/github/shadowsocks/bg/ProxyInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(':')
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/com/github/shadowsocks/bg/RemoteConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions mobile/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@
#-renamesourcefileattribute SourceFile

-dontwarn com.google.android.gms.internal.**
-dontwarn okhttp3.**
-dontwarn okio.**
2 changes: 0 additions & 2 deletions tv/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@
#-renamesourcefileattribute SourceFile

-dontwarn com.google.android.gms.internal.**
-dontwarn okhttp3.**
-dontwarn okio.**

0 comments on commit 2241c5a

Please sign in to comment.