Commit b161ab81 authored by chenhuaqing's avatar chenhuaqing

fixed bug

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