Commit 4674139d authored by Mygod's avatar Mygod

Only update RemoteConfig if it is used

parent 68490279
...@@ -31,7 +31,7 @@ buildscript { ...@@ -31,7 +31,7 @@ buildscript {
classpath 'com.github.ben-manes:gradle-versions-plugin:0.21.0' classpath 'com.github.ben-manes:gradle-versions-plugin:0.21.0'
classpath 'com.google.gms:google-services:4.2.0' classpath 'com.google.gms:google-services:4.2.0'
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.8.0' classpath 'com.vanniktech:gradle-maven-publish-plugin:0.8.0'
classpath 'io.fabric.tools:gradle:1.27.1' classpath 'io.fabric.tools:gradle:1.28.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
} }
} }
......
...@@ -54,8 +54,8 @@ dependencies { ...@@ -54,8 +54,8 @@ dependencies {
api 'androidx.preference:preference:1.0.0' api 'androidx.preference:preference:1.0.0'
api "androidx.room:room-runtime:$roomVersion" api "androidx.room:room-runtime:$roomVersion"
api 'com.crashlytics.sdk.android:crashlytics:2.9.9' api 'com.crashlytics.sdk.android:crashlytics:2.9.9'
api 'com.google.firebase:firebase-config:16.1.3' api 'com.google.firebase:firebase-config:16.4.1'
api 'com.google.firebase:firebase-core:16.0.7' api 'com.google.firebase:firebase-core:16.0.8'
api "com.takisoft.preferencex:preferencex:$preferencexVersion" api "com.takisoft.preferencex:preferencex:$preferencexVersion"
api 'dnsjava:dnsjava:2.1.8' api 'dnsjava:dnsjava:2.1.8'
api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1' api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
......
...@@ -90,10 +90,6 @@ object BaseService { ...@@ -90,10 +90,6 @@ object BaseService {
binder.stateChanged(s, msg) binder.stateChanged(s, msg)
state = s state = s
} }
init {
RemoteConfig.fetch()
}
} }
class Binder(private var data: Data? = null) : IShadowsocksService.Stub(), AutoCloseable { class Binder(private var data: Data? = null) : IShadowsocksService.Stub(), AutoCloseable {
...@@ -341,7 +337,6 @@ object BaseService { ...@@ -341,7 +337,6 @@ object BaseService {
proxy.scheduleUpdate() proxy.scheduleUpdate()
data.udpFallback?.scheduleUpdate() data.udpFallback?.scheduleUpdate()
RemoteConfig.fetch()
data.changeState(State.Connected) data.changeState(State.Connected)
} catch (_: CancellationException) { } catch (_: CancellationException) {
......
...@@ -40,6 +40,7 @@ import kotlinx.coroutines.* ...@@ -40,6 +40,7 @@ import kotlinx.coroutines.*
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL
import java.net.UnknownHostException import java.net.UnknownHostException
import java.security.MessageDigest import java.security.MessageDigest
...@@ -51,12 +52,16 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro ...@@ -51,12 +52,16 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro
var trafficMonitor: TrafficMonitor? = null var trafficMonitor: TrafficMonitor? = null
private val plugin = PluginConfiguration(profile.plugin ?: "").selectedOptions private val plugin = PluginConfiguration(profile.plugin ?: "").selectedOptions
val pluginPath by lazy { PluginManager.init(plugin) } val pluginPath by lazy { PluginManager.init(plugin) }
private var scheduleConfigUpdate = false
suspend fun init(service: BaseService.Interface) { suspend fun init(service: BaseService.Interface) {
if (profile.host == "198.199.101.152") { if (profile.host == "198.199.101.152") {
scheduleConfigUpdate = true
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 conn = service.openConnection(RemoteConfig.proxyUrl) as HttpURLConnection val (config, success) = RemoteConfig.fetch()
scheduleConfigUpdate = !success
val conn = service.openConnection(URL(config.getString("proxy_url"))) as HttpURLConnection
conn.requestMethod = "POST" conn.requestMethod = "POST"
conn.doOutput = true conn.doOutput = true
...@@ -134,6 +139,7 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro ...@@ -134,6 +139,7 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro
fun scheduleUpdate() { fun scheduleUpdate() {
if (route !in arrayOf(Acl.ALL, Acl.CUSTOM_RULES)) AclSyncer.schedule(route) if (route !in arrayOf(Acl.ALL, Acl.CUSTOM_RULES)) AclSyncer.schedule(route)
if (scheduleConfigUpdate) RemoteConfig.scheduleFetch()
} }
fun shutdown(scope: CoroutineScope) { fun shutdown(scope: CoroutineScope) {
......
...@@ -25,18 +25,30 @@ import androidx.core.os.bundleOf ...@@ -25,18 +25,30 @@ 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 import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
object RemoteConfig { object RemoteConfig {
private val config = FirebaseRemoteConfig.getInstance().apply { setDefaults(R.xml.default_configs) } private val config by lazy { FirebaseRemoteConfig.getInstance().apply { setDefaults(R.xml.default_configs) } }
val proxyUrl get() = URL(config.getString("proxy_url")) private fun Exception.log() {
Log.w("RemoteConfig", this)
Core.analytics.logEvent("femote_config_failure", bundleOf(Pair(javaClass.simpleName, message)))
}
fun scheduleFetch() = config.fetch().addOnCompleteListener {
if (it.isSuccessful) config.activateFetched() else it.exception?.log()
}
fun fetch() = config.fetch().addOnCompleteListener { suspend fun fetch() = suspendCancellableCoroutine<Pair<FirebaseRemoteConfig, Boolean>> { cont ->
if (it.isSuccessful) config.activateFetched() else { config.fetch().addOnCompleteListener {
val e = it.exception ?: return@addOnCompleteListener if (it.isSuccessful) {
Log.w("RemoteConfig", e) config.activateFetched()
Core.analytics.logEvent("femote_config_failure", bundleOf(Pair(e.javaClass.simpleName, e.message))) cont.resume(config to true)
} else {
it.exception?.log()
cont.resume(config to false)
}
} }
} }
} }
...@@ -76,7 +76,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd ...@@ -76,7 +76,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
private val job = SupervisorJob() private val job = SupervisorJob()
override val coroutineContext = job + CoroutineExceptionHandler { _, t -> printLog(t) } override val coroutineContext = job + CoroutineExceptionHandler { _, t -> printLog(t) }
suspend fun start(listen: SocketAddress) = DatagramChannel.open().apply { suspend fun start(listen: SocketAddress) = DatagramChannel.open().run {
configureBlocking(false) configureBlocking(false)
socket().bind(listen) socket().bind(listen)
monitor.register(this, SelectionKey.OP_READ) { handlePacket(this) } monitor.register(this, SelectionKey.OP_READ) { handlePacket(this) }
......
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