Commit b161ab81 authored by chenhuaqing's avatar chenhuaqing

fixed bug

parent 5b98f8d8
......@@ -2,19 +2,16 @@ package com.github.shadowsocks.bg
import android.os.Build
import com.github.shadowsocks.Core
import com.github.shadowsocks.http.HttpConfig
import com.github.shadowsocks.acl.Acl
import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager
import com.github.shadowsocks.http.FreshToken
import com.github.shadowsocks.http.HttpConfig
import com.github.shadowsocks.http.LineConfig
import com.google.gson.Gson
import com.google.gson.JsonStreamParser
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
......@@ -80,18 +77,16 @@ object AuthManager {
MMKV.defaultMMKV().putLong("ACC_TIME", acc_time)
}
suspend fun initAuthProfile() {
suspend fun initAuthProfile(profile: Profile) {
withContext(Dispatchers.IO) {
getLineConfig()?.let {
if (it.code != 200) {
return@withContext
}
val profileId = 1L
val profile = ProfileManager.getProfile(profileId) ?: Profile()
profile.host = it.data.host
profile.token = it.data.token
profile.remotePort = it.data.port
Timber.d("start vpn with games: ${profile.individual}")
Timber.d("start vpn with games: ${profile.individual}, profileId: ${profile.id}, route: ${profile.route}")
ProfileManager.updateProfile(profile)
}
}
......@@ -101,7 +96,7 @@ object AuthManager {
if (data.state != BaseService.State.Connected) {
return
}
scheduleAtFixedRate?.cancel(false)
scheduleAtFixedRate = executor.scheduleAtFixedRate({
val context =
if (Build.VERSION.SDK_INT < 24 || Core.user.isUserUnlocked) Core.app else Core.deviceStorage
......@@ -110,13 +105,15 @@ object AuthManager {
.single().asJsonObject
if (data.state == BaseService.State.Connected) {
getToken()?.let {
if (it.code == 200) {
configJson.addProperty("token", it.data.token)
configFile.writeText(configJson.toString())
} else {
Core.stopService()
data.proxy?.let { proxyInstance ->
getToken(proxyInstance.profile.token ?: "")?.let {
if (it.code == 200) {
proxyInstance.profile.token = it.data.token
configJson.addProperty("token", it.data.token)
configFile.writeText(configJson.toString())
} else {
Core.stopService()
}
}
}
}
......@@ -124,19 +121,15 @@ object AuthManager {
}, 1, 1, TimeUnit.MINUTES)
}
fun getToken(): FreshToken? {
ProfileManager.getProfile(1L)?.let {
val map = mutableMapOf<String, String>()
it.token?.let { it1 -> map.put("token", it1) }
return postSync<FreshToken>("/api/acc/line/refresh", map)
}
return null
fun getToken(token: String): FreshToken? {
val map = mapOf(Pair("token", token))
return postSync<FreshToken>("/api/acc/line/refresh", map)
}
val jsonType get() = "application/json; charset=utf-8".toMediaTypeOrNull()
fun getLineConfig(
private fun getLineConfig(
): LineConfig? {
val map = mutableMapOf<String, String>()
area?.let { map.put("area", it) }
......
......@@ -348,7 +348,7 @@ object BaseService {
try {
Executable.killAll() // clean up old processes
preInit()
AuthManager.initAuthProfile()
AuthManager.initAuthProfile(profile)
if (profile.route == Acl.CUSTOM_RULES) try {
withContext(Dispatchers.IO) {
Acl.customRules.flatten(10, this@Interface::openConnection).also {
......
......@@ -129,7 +129,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 (route !in arrayOf(Acl.ALL, Acl.CUSTOM_RULES, Acl.BYPASS_LAN)) AclSyncer.schedule(route)
}
fun shutdown(scope: CoroutineScope) {
......
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