Skip to content

Commit 9ebdb0b

Browse files
committed
fix: route sign provider http through android native helper first
1 parent 2db5a75 commit 9ebdb0b

2 files changed

Lines changed: 77 additions & 12 deletions

File tree

acidify-core/src/commonMain/kotlin/org/ntqqrev/acidify/common/LagrangeUrlSignProvider.kt

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import kotlinx.serialization.SerialName
1212
import kotlinx.serialization.Serializable
1313
import kotlinx.serialization.json.Json
1414
import org.ntqqrev.acidify.exception.UrlSignException
15+
import org.ntqqrev.acidify.internal.util.platformCurlTextRequestOrNull
1516

1617
/**
1718
* 通过 HTTP 接口进行签名的 [SignProvider] 实现,用于对接 Lagrange V2 Sign API。
@@ -67,6 +68,47 @@ class LagrangeUrlSignProvider(
6768
): SignResult {
6869
val currentJwtToken = jwtToken
6970
val currentLauncherSignature = launcherSignature
71+
val requestBody = jsonModule.encodeToString(
72+
LagrangeUrlSignRequest(
73+
command = cmd,
74+
seq = seq,
75+
body = src.toHexString(),
76+
uin = uin,
77+
guid = guid,
78+
qua = qua,
79+
)
80+
)
81+
val requestHeaders = buildMap<String, String> {
82+
when {
83+
!currentJwtToken.isNullOrEmpty() -> put(HttpHeaders.Authorization, "Bearer $currentJwtToken")
84+
!currentLauncherSignature.isNullOrEmpty() -> put("X-Launcher-Signature", currentLauncherSignature)
85+
else -> put(HttpHeaders.Authorization, "Bearer $token")
86+
}
87+
}
88+
89+
platformCurlTextRequestOrNull(
90+
method = "POST",
91+
url = URLBuilder(signUrl).apply { appendPathSegments("api", "sign", "sec-sign") }.buildString(),
92+
headers = requestHeaders,
93+
body = requestBody,
94+
contentType = ContentType.Application.Json.toString(),
95+
proxy = httpProxy,
96+
)?.let { response ->
97+
response.header("x-set-token")
98+
?.takeUnless { it.isBlank() }
99+
?.let(::updateJwtToken)
100+
val respBody = jsonModule.decodeFromString<LagrangeUrlSignResponse>(response.body)
101+
if (respBody.code != 0 || respBody.value == null) {
102+
throw UrlSignException(respBody.message ?: "", respBody.code)
103+
}
104+
val value = respBody.value
105+
return SignResult(
106+
sign = value.sign.hexToByteArray(),
107+
token = value.token.hexToByteArray(),
108+
extra = value.extra.hexToByteArray(),
109+
)
110+
}
111+
70112
val resp = client.post {
71113
url {
72114
takeFrom(signUrl)
@@ -78,16 +120,7 @@ class LagrangeUrlSignProvider(
78120
else -> header(HttpHeaders.Authorization, "Bearer $token")
79121
}
80122
contentType(ContentType.Application.Json)
81-
setBody(
82-
LagrangeUrlSignRequest(
83-
command = cmd,
84-
seq = seq,
85-
body = src.toHexString(),
86-
uin = uin,
87-
guid = guid,
88-
qua = qua,
89-
)
90-
)
123+
setBody(requestBody)
91124
}
92125
resp.headers["X-SET-TOKEN"]
93126
?.takeUnless { it.isBlank() }
@@ -128,6 +161,18 @@ class LagrangeUrlSignProvider(
128161

129162
private suspend fun refreshToken() {
130163
val currentJwtToken = jwtToken ?: return
164+
platformCurlTextRequestOrNull(
165+
method = "POST",
166+
url = URLBuilder(signUrl).apply { appendPathSegments("token", "refresh") }.buildString(),
167+
headers = mapOf(HttpHeaders.Authorization to "Bearer $currentJwtToken"),
168+
proxy = httpProxy,
169+
)?.let { response ->
170+
response.header("x-set-token")
171+
?.takeUnless { it.isBlank() }
172+
?.let(::updateJwtToken)
173+
return
174+
}
175+
131176
val resp = client.post {
132177
url {
133178
takeFrom(signUrl)

acidify-core/src/commonMain/kotlin/org/ntqqrev/acidify/common/UrlSignProvider.kt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.ktor.serialization.kotlinx.json.json
1010
import kotlinx.serialization.Serializable
1111
import kotlinx.serialization.json.Json
1212
import org.ntqqrev.acidify.exception.UrlSignException
13+
import org.ntqqrev.acidify.internal.util.platformCurlTextRequestOrNull
1314

1415
/**
1516
* 通过 HTTP 接口进行签名的 [SignProvider] 实现
@@ -34,10 +35,29 @@ class UrlSignProvider(val url: String, val httpProxy: String? = null) : SignProv
3435
}
3536

3637
override suspend fun sign(cmd: String, seq: Int, src: ByteArray): SignResult {
38+
val requestBody = jsonModule.encodeToString(UrlSignRequest(cmd, seq, src.toHexString()))
39+
platformCurlTextRequestOrNull(
40+
method = "POST",
41+
url = signUrl.toString(),
42+
body = requestBody,
43+
contentType = ContentType.Application.Json.toString(),
44+
proxy = httpProxy,
45+
)?.let { response ->
46+
if (response.statusCode != HttpStatusCode.OK.value) {
47+
throw UrlSignException(HttpStatusCode.fromValue(response.statusCode).description, response.statusCode)
48+
}
49+
val value = jsonModule.decodeFromString<UrlSignResponse>(response.body).value
50+
return SignResult(
51+
sign = value.sign.hexToByteArray(),
52+
token = value.token.hexToByteArray(),
53+
extra = value.extra.hexToByteArray(),
54+
)
55+
}
56+
3757
val resp = client.post {
3858
url(signUrl)
3959
contentType(ContentType.Application.Json)
40-
setBody(UrlSignRequest(cmd, seq, src.toHexString()))
60+
setBody(requestBody)
4161
}
4262
if (resp.status != HttpStatusCode.OK) {
4363
throw UrlSignException(resp.status.description, resp.status.value)
@@ -87,4 +107,4 @@ private data class UrlSignValue(
87107
val sign: String,
88108
val token: String,
89109
val extra: String
90-
)
110+
)

0 commit comments

Comments
 (0)