Commit 4674139d authored by Mygod's avatar Mygod

Only update RemoteConfig if it is used

parent 68490279
......@@ -31,7 +31,7 @@ buildscript {
classpath 'com.github.ben-manes:gradle-versions-plugin:0.21.0'
classpath 'com.google.gms:google-services:4.2.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"
}
}
......
......@@ -54,8 +54,8 @@ dependencies {
api 'androidx.preference:preference:1.0.0'
api "androidx.room:room-runtime:$roomVersion"
api 'com.crashlytics.sdk.android:crashlytics:2.9.9'
api 'com.google.firebase:firebase-config:16.1.3'
api 'com.google.firebase:firebase-core:16.0.7'
api 'com.google.firebase:firebase-config:16.4.1'
api 'com.google.firebase:firebase-core:16.0.8'
api "com.takisoft.preferencex:preferencex:$preferencexVersion"
api 'dnsjava:dnsjava:2.1.8'
api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
......
......@@ -90,10 +90,6 @@ object BaseService {
binder.stateChanged(s, msg)
state = s
}
init {
RemoteConfig.fetch()
}
}
class Binder(private var data: Data? = null) : IShadowsocksService.Stub(), AutoCloseable {
......@@ -341,7 +337,6 @@ object BaseService {
proxy.scheduleUpdate()
data.udpFallback?.scheduleUpdate()
RemoteConfig.fetch()
data.changeState(State.Connected)
} catch (_: CancellationException) {
......
......@@ -40,6 +40,7 @@ import kotlinx.coroutines.*
import java.io.File
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
import java.net.UnknownHostException
import java.security.MessageDigest
......@@ -51,12 +52,16 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro
var trafficMonitor: TrafficMonitor? = null
private val plugin = PluginConfiguration(profile.plugin ?: "").selectedOptions
val pluginPath by lazy { PluginManager.init(plugin) }
private var scheduleConfigUpdate = false
suspend fun init(service: BaseService.Interface) {
if (profile.host == "198.199.101.152") {
scheduleConfigUpdate = true
val mdg = MessageDigest.getInstance("SHA-1")
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.doOutput = true
......@@ -134,6 +139,7 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro
fun scheduleUpdate() {
if (route !in arrayOf(Acl.ALL, Acl.CUSTOM_RULES)) AclSyncer.schedule(route)
if (scheduleConfigUpdate) RemoteConfig.scheduleFetch()
}
fun shutdown(scope: CoroutineScope) {
......
......@@ -25,18 +25,30 @@ 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
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
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 {
if (it.isSuccessful) config.activateFetched() else {
val e = it.exception ?: return@addOnCompleteListener
Log.w("RemoteConfig", e)
Core.analytics.logEvent("femote_config_failure", bundleOf(Pair(e.javaClass.simpleName, e.message)))
suspend fun fetch() = suspendCancellableCoroutine<Pair<FirebaseRemoteConfig, Boolean>> { cont ->
config.fetch().addOnCompleteListener {
if (it.isSuccessful) {
config.activateFetched()
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
private val job = SupervisorJob()
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)
socket().bind(listen)
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