Commit d62e4e00 authored by liusheng's avatar liusheng

合并

parents 2ac9369d 5a00e8be
...@@ -5,9 +5,7 @@ import android.util.Log ...@@ -5,9 +5,7 @@ import android.util.Log
import com.github.shadowsocks.Core import com.github.shadowsocks.Core
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.*
import com.github.shadowsocks.http.HttpConfig
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
...@@ -19,7 +17,6 @@ import okhttp3.OkHttpClient ...@@ -19,7 +17,6 @@ import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.RequestBody import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import timber.log.Timber import timber.log.Timber
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
...@@ -79,7 +76,7 @@ object AuthManager { ...@@ -79,7 +76,7 @@ object AuthManager {
MMKV.defaultMMKV().putLong("ACC_TIME", acc_time) MMKV.defaultMMKV().putLong("ACC_TIME", acc_time)
} }
var Last_ACC_TIme:Long var Last_ACC_TIme: Long
get() = MMKV.defaultMMKV().decodeLong("LAST_ACC_TIME", 0L) get() = MMKV.defaultMMKV().decodeLong("LAST_ACC_TIME", 0L)
set(last_acc_time) { set(last_acc_time) {
MMKV.defaultMMKV().putLong("LAST_ACC_TIME", last_acc_time) MMKV.defaultMMKV().putLong("LAST_ACC_TIME", last_acc_time)
...@@ -89,19 +86,19 @@ object AuthManager { ...@@ -89,19 +86,19 @@ object AuthManager {
suspend fun initAuthProfile(profile: Profile) { suspend fun initAuthProfile(profile: Profile) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
getLineConfig()?.let { val gameId: Int = gameId?.toInt() ?: throw AuthException("游戏不存在")
if(it.code==1001){//vip过期 val lineConfigResult = postSync<LineConfig>("/api/acc/line", LineConfigReq(gameId, area))
throw AuthException(it.error) if (lineConfigResult.isSuccessful()) {
}else if (it.code != 200) { val lineConfig = lineConfigResult.unwrapData()
throw AuthException(it.error) profile.host = lineConfig.host
} profile.token = lineConfig.token
profile.host = it.data.host profile.remotePort = lineConfig.port
profile.token = it.data.token
profile.remotePort = it.data.port
Timber.d("start vpn with games: ${profile.individual}, profileId: ${profile.id}, route: ${profile.route}") Timber.d("start vpn with games: ${profile.individual}, profileId: ${profile.id}, route: ${profile.route}")
ProfileManager.updateProfile(profile) ProfileManager.updateProfile(profile)
}?: let { } else if (lineConfigResult.code == 1001) {
throw AuthException("未知错误") throw AuthException(lineConfigResult.error!!)
} else {
throw AuthException(lineConfigResult.error!!)
} }
} }
} }
...@@ -120,12 +117,19 @@ object AuthManager { ...@@ -120,12 +117,19 @@ object AuthManager {
if (data.state == BaseService.State.Connected) { if (data.state == BaseService.State.Connected) {
data.proxy?.let { proxyInstance -> data.proxy?.let { proxyInstance ->
getToken(proxyInstance.profile.token ?: "")?.let { val refreshTokenResult = postSync<RefreshTokenRes>(
if (it.code == 200) { "/api/acc/line/refresh",
proxyInstance.profile.token = it.data.token RefreshTokenReq(proxyInstance.profile.token ?: "")
configJson.addProperty("token", it.data.token) )
Timber.d("refreshTokenResult: $refreshTokenResult")
if (refreshTokenResult.isSuccessful()) {
proxyInstance.profile.token = refreshTokenResult.unwrapData().token
configJson.addProperty("token", proxyInstance.profile.token)
configFile.writeText(configJson.toString()) configFile.writeText(configJson.toString())
} } else if (refreshTokenResult.code == 1001) {
throw AuthException(refreshTokenResult.error!!)
} else {
// TODO: error handling
} }
} }
} }
...@@ -133,53 +137,31 @@ object AuthManager { ...@@ -133,53 +137,31 @@ object AuthManager {
}, 1, 1, TimeUnit.MINUTES) }, 1, 1, TimeUnit.MINUTES)
} }
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() val jsonType get() = "application/json; charset=utf-8".toMediaTypeOrNull()
private fun getLineConfig(
): LineConfig? {
val map = mutableMapOf<String, String>()
area?.let { map.put("area", it) }
gameId?.let {
map.put("gameId", it)
}
return postSync<LineConfig>("/api/acc/line", map)
}
inline fun <reified T> postSync( inline fun <reified T> postSync(
url: String, url: String,
map: Map<String, String> params: Any
): T? { ): ApiResult<T> {
try { val body: RequestBody = gson.toJson(params).toString().toRequestBody(jsonType)
val body: RequestBody = gson.toJson(map).toString().toRequestBody(jsonType)
val request = Request.Builder() val request = Request.Builder()
.url(HttpConfig.baseUrl + url) .url(HttpConfig.baseUrl + url)
.post(body) .post(body)
authToken?.let { authToken?.let {
request.addHeader("Authorization", it) request.addHeader("Authorization", it)
} }
val call = okHttpClient.newCall(request.build()) val call = okHttpClient.newCall(request.build())
val response: Response = call.execute() val resultType = object : TypeToken<ApiResult<T>>() {}.type
if (response.isSuccessful) { try {
call.execute().use { response ->
val string = response.body?.string() val string = response.body?.string()
Log.e("postSync","=====url:"+HttpConfig.baseUrl + url)
Log.e("postSync","=====result:"+string)
println(string) println(string)
val type = object : TypeToken<T>() {}.type return gson.fromJson(string, resultType)
return gson.fromJson(string, type)
} }
return null } catch (e: IOException) {
} catch (e: java.lang.Exception) { Timber.e(e, "AuthManager 调用接口错误")
println(e) return ApiResult(ApiError.NetworkError)
} }
return null
} }
private fun initSSLSocketFactory(): SSLSocketFactory { private fun initSSLSocketFactory(): SSLSocketFactory {
......
package com.github.shadowsocks.http package com.github.shadowsocks.http
data class FreshToken( data class RefreshTokenReq(
val code: Int, val token: String
val error:String?,
val `data`: Token
) )
data class Token( data class RefreshTokenRes(
val token: String? val token: String
) )
package com.github.shadowsocks.http package com.github.shadowsocks.http
data class LineConfig( //data class LineConfig(
val code: Int, // val code: Int,
val `data`: Data, // val `data`: Data
val error:String //)
data class LineConfigReq(
val gameId: Int,
val area: String?,
) )
data class Data( data class LineConfig(
val host: String, val host: String,
val method: String, val method: String,
val port: Int, val port: Int,
......
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