dsh-plugin-cyrene-live2d 0.2.9 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/assets/default-prefs.json +8 -8
- package/assets/icon.svg +89 -89
- package/assets/locale/notes/en.json +6 -6
- package/assets/locale/notes/zh-CN.json +6 -6
- package/assets/locale/reminder/en.json +6 -6
- package/assets/locale/reminder/zh-CN.json +6 -6
- package/assets/model/Cyrene.model3.json +441 -441
- package/assets/model/bongo-standard/Live2d_expression0.exp3.json +9 -9
- package/assets/model/bongo-standard/cat.cdi3.json +296 -296
- package/assets/model/bongo-standard/cat.model3.json +33 -33
- package/assets/model/bongo-standard/cat.physics3.json +232 -232
- package/assets/model/expressions/0.exp3.json +24 -24
- package/assets/model/expressions/1.exp3.json +11 -11
- package/assets/model/expressions/10.exp3.json +9 -9
- package/assets/model/expressions/11.exp3.json +9 -9
- package/assets/model/expressions/2.exp3.json +9 -9
- package/assets/model/expressions/3.exp3.json +9 -9
- package/assets/model/expressions/4.exp3.json +11 -11
- package/assets/model/expressions/5.exp3.json +11 -11
- package/assets/model/expressions/6.exp3.json +11 -11
- package/assets/model/expressions/7.exp3.json +11 -11
- package/assets/model/expressions/8.exp3.json +11 -11
- package/assets/model/expressions/9.exp3.json +9 -9
- package/assets/model/model.physics3.json +641 -641
- package/assets/model/motions/Tick3_3.motion3.json +1347 -1347
- package/assets/model/motions//345/212/250/344/275/2346_0.motion3.json +699 -699
- package/assets/model/motions//345/212/250/344/275/2346_1.motion3.json +1241 -1241
- package/assets/model/motions//345/212/250/344/275/2346_2.motion3.json +1117 -1117
- package/assets/model/motions//345/212/250/344/275/2346_3.motion3.json +1106 -1106
- package/assets/model/we-q/expressions/FileReferences_Expressions_0_File_0.exp3.json +19 -19
- package/assets/model/we-q/expressions/FileReferences_Expressions_1_File_0.exp3.json +41 -41
- package/assets/model/we-q/expressions/FileReferences_Expressions_2_File_0.exp3.json +104 -104
- package/assets/model/we-q/model0.model3.json +245 -245
- package/assets/model/we-q/motions/FileReferences_Motions_shake#1_0_File_0.motion3.json +520 -520
- package/assets/model/we-q/motions/FileReferences_Motions_talk#1_0_File_0.motion3.json +499 -499
- package/assets/model/we-q/motions/FileReferences_Motions_talk#1_1_File_0.motion3.json +453 -453
- package/assets/model/we-q/motions/FileReferences_Motions_wink#1_0_File_0.motion3.json +478 -478
- package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_0_File_0.motion3.json +450 -450
- package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_1_File_0.motion3.json +450 -450
- package/assets/model/we-q/motions/idle/FileReferences_Motions_Idleidle_0_File_0.motion3.json +422 -422
- package/assets/model/we-q/physics/FileReferences_Physics_0.physic3.json +236 -236
- package/assets/runtime/live2dcubismcore.min.js +8 -8
- package/assets/stickers.json +84 -84
- package/assets/vendor/cubism4.min.js +1 -1
- package/assets/vendor/pixi.min.js +9 -9
- package/assets/vendor/theme-cyrene-factory.js +4314 -4314
- package/assets/vendor/theme-cyrene.js +4317 -4317
- package/assets/voice/angry-00.opus +0 -0
- package/assets/voice/angry-02.opus +0 -0
- package/assets/voice/approval-00.opus +0 -0
- package/assets/voice/approval-01.opus +0 -0
- package/assets/voice/approval-02.opus +0 -0
- package/assets/voice/askAck-00.opus +0 -0
- package/assets/voice/away-01.opus +0 -0
- package/assets/voice/away-02.opus +0 -0
- package/assets/voice/away-03.opus +0 -0
- package/assets/voice/away-05.opus +0 -0
- package/assets/voice/back-00.opus +0 -0
- package/assets/voice/back-01.opus +0 -0
- package/assets/voice/backLong-05.opus +0 -0
- package/assets/voice/backShort-03.opus +0 -0
- package/assets/voice/backShort-04.opus +0 -0
- package/assets/voice/backShort-07.opus +0 -0
- package/assets/voice/backVeryLong-01.opus +0 -0
- package/assets/voice/backVeryLong-02.opus +0 -0
- package/assets/voice/backVeryLong-03.opus +0 -0
- package/assets/voice/backVeryLong-04.opus +0 -0
- package/assets/voice/backVeryLong-05.opus +0 -0
- package/assets/voice/backVeryLong-07.opus +0 -0
- package/assets/voice/backVeryLong-08.opus +0 -0
- package/assets/voice/balLow-00.opus +0 -0
- package/assets/voice/balLow-01.opus +0 -0
- package/assets/voice/birthday-02.opus +0 -0
- package/assets/voice/click-00.opus +0 -0
- package/assets/voice/click-01.opus +0 -0
- package/assets/voice/click-07.opus +0 -0
- package/assets/voice/click-10.opus +0 -0
- package/assets/voice/click-11.opus +0 -0
- package/assets/voice/click-12.opus +0 -0
- package/assets/voice/click-16.opus +0 -0
- package/assets/voice/click-17.opus +0 -0
- package/assets/voice/click-18.opus +0 -0
- package/assets/voice/comfort-03.opus +0 -0
- package/assets/voice/comfort-04.opus +0 -0
- package/assets/voice/cost10-00.opus +0 -0
- package/assets/voice/cost2-00.opus +0 -0
- package/assets/voice/cost5-00.opus +0 -0
- package/assets/voice/costTurn-01.opus +0 -0
- package/assets/voice/costTurn-02.opus +0 -0
- package/assets/voice/ctxFull-01.opus +0 -0
- package/assets/voice/ctxHigh-00.opus +0 -0
- package/assets/voice/dawn-01.opus +0 -0
- package/assets/voice/dawn-04.opus +0 -0
- package/assets/voice/dawn-06.opus +0 -0
- package/assets/voice/dawn-07.opus +0 -0
- package/assets/voice/deepWake-03.opus +0 -0
- package/assets/voice/deepWake-04.opus +0 -0
- package/assets/voice/deepWake-06.opus +0 -0
- package/assets/voice/deepWake-07.opus +0 -0
- package/assets/voice/done-05.opus +0 -0
- package/assets/voice/done-08.opus +0 -0
- package/assets/voice/done-09.opus +0 -0
- package/assets/voice/done-11.opus +0 -0
- package/assets/voice/empty-00.opus +0 -0
- package/assets/voice/encourage-02.opus +0 -0
- package/assets/voice/failed-01.opus +0 -0
- package/assets/voice/failed-05.opus +0 -0
- package/assets/voice/failkind-auth-01.opus +0 -0
- package/assets/voice/failkind-auth-02.opus +0 -0
- package/assets/voice/failkind-ctx-00.opus +0 -0
- package/assets/voice/failkind-missing-01.opus +0 -0
- package/assets/voice/focusEnd-02.opus +0 -0
- package/assets/voice/focusEnd-04.opus +0 -0
- package/assets/voice/forgive-00.opus +0 -0
- package/assets/voice/hello-00.opus +0 -0
- package/assets/voice/hello-03.opus +0 -0
- package/assets/voice/hold-00.opus +0 -0
- package/assets/voice/idleTap-00.opus +0 -0
- package/assets/voice/idleTapLong-02.opus +0 -0
- package/assets/voice/lateNight-00.opus +0 -0
- package/assets/voice/lateNight-02.opus +0 -0
- package/assets/voice/lateNight-03.opus +0 -0
- package/assets/voice/lateNight-06.opus +0 -0
- package/assets/voice/lateNight-07.opus +0 -0
- package/assets/voice/murmur-04.opus +0 -0
- package/assets/voice/night-01.opus +0 -0
- package/assets/voice/night-03.opus +0 -0
- package/assets/voice/night-05.opus +0 -0
- package/assets/voice/noon-04.opus +0 -0
- package/assets/voice/petArm-02.opus +0 -0
- package/assets/voice/petCheek-00.opus +0 -0
- package/assets/voice/petCheek-01.opus +0 -0
- package/assets/voice/petCheek-02.opus +0 -0
- package/assets/voice/petHead-01.opus +0 -0
- package/assets/voice/petHead-03.opus +0 -0
- package/assets/voice/petHead-04.opus +0 -0
- package/assets/voice/praised-01.opus +0 -0
- package/assets/voice/praised-03.opus +0 -0
- package/assets/voice/question-00.opus +0 -0
- package/assets/voice/question-03.opus +0 -0
- package/assets/voice/question-04.opus +0 -0
- package/assets/voice/question-06.opus +0 -0
- package/assets/voice/shy-00.opus +0 -0
- package/assets/voice/spot-0-0.opus +0 -0
- package/assets/voice/spot-0-1.opus +0 -0
- package/assets/voice/spot-0-2.opus +0 -0
- package/assets/voice/spot-3-0.opus +0 -0
- package/assets/voice/spot-4-0.opus +0 -0
- package/assets/voice/spot-4-2.opus +0 -0
- package/assets/voice/spot-5-2.opus +0 -0
- package/assets/voice/spot-6-0.opus +0 -0
- package/assets/voice/spot-6-1.opus +0 -0
- package/assets/voice/spot-7-0.opus +0 -0
- package/assets/voice/spot-7-2.opus +0 -0
- package/assets/voice/spot-8-1.opus +0 -0
- package/assets/voice/tagent-00.opus +0 -0
- package/assets/voice/thinkFast-01.opus +0 -0
- package/assets/voice/thinkFast-02.opus +0 -0
- package/assets/voice/thinkFast-03.opus +0 -0
- package/assets/voice/thinkSlow-01.opus +0 -0
- package/assets/voice/thinkSlow-03.opus +0 -0
- package/assets/voice/thinking-01.opus +0 -0
- package/assets/voice/thinking-02.opus +0 -0
- package/assets/voice/thinking-03.opus +0 -0
- package/assets/voice/thinking-09.opus +0 -0
- package/assets/voice/tread-00.opus +0 -0
- package/assets/voice/tread-01.opus +0 -0
- package/assets/voice/trun-02.opus +0 -0
- package/assets/voice/tsun-00.opus +0 -0
- package/assets/voice/tsun-01.opus +0 -0
- package/assets/voice/tsun-04.opus +0 -0
- package/assets/voice/twrite-00.opus +0 -0
- package/assets/voice/unknown-01.opus +0 -0
- package/assets/voice/waitApproval-00.opus +0 -0
- package/assets/voice/waitApproval-01.opus +0 -0
- package/assets/voice/waitApproval-02.opus +0 -0
- package/assets/voice/waitApproval-03.opus +0 -0
- package/assets/voice/waitApprovalLate-02.opus +0 -0
- package/assets/voice/waitApprovalLate-03.opus +0 -0
- package/assets/voice/waitAsk-00.opus +0 -0
- package/assets/voice/waitAsk-02.opus +0 -0
- package/assets/voice/waitAskLate-03.opus +0 -0
- package/assets/voice/wake-03.opus +0 -0
- package/assets/voice/wake-04.opus +0 -0
- package/assets/voice/wake-05.opus +0 -0
- package/assets/voice/wake-06.opus +0 -0
- package/lib/client.js +7 -7
- package/lib/features/notes.js +172 -172
- package/lib/features/reminder.js +216 -216
- package/lib/l2d-plan.js +295 -295
- package/lib/l2d-tools.js +293 -293
- package/lib/ledger.js +163 -163
- package/lib/mys/config.js +183 -183
- package/lib/mys/ds.js +209 -209
- package/lib/mys/http.js +85 -85
- package/lib/mys/mys.js +419 -419
- package/lib/mys/qr.js +434 -434
- package/lib/mys/scheduler.js +206 -206
- package/lib/mys/store.js +248 -248
- package/lib/mys/tz.js +174 -174
- package/lib/wavtrim.js +245 -245
- package/lib/worldbook/NOTICE.md +18 -18
- package/lib/worldbook/data/Cyrene.md +111 -111
- package/lib/worldbook/data/_glossary.md +181 -181
- package/lib/worldbook/data/characters.md +289 -289
- package/lib/worldbook/data/story.md +130 -130
- package/lib/worldbook/data/world.md +182 -182
- package/lib/worldbook.js +189 -189
- package/package.json +72 -69
package/lib/mys/mys.js
CHANGED
|
@@ -1,419 +1,419 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The miHoYo BBS protocol, as far as daily game check-in needs it.
|
|
3
|
-
*
|
|
4
|
-
* Every function takes an explicit `config`/`deviceId` so the same code can be
|
|
5
|
-
* unit-tested against a fake transport, and returns the raw `retcode` alongside
|
|
6
|
-
* a normalized outcome instead of throwing for business failures.
|
|
7
|
-
*/
|
|
8
|
-
import { request } from './http.js'
|
|
9
|
-
import {
|
|
10
|
-
apiHeaders,
|
|
11
|
-
cookieHeader,
|
|
12
|
-
credentialCookies,
|
|
13
|
-
ensureIdentityCookies,
|
|
14
|
-
passportHeaders,
|
|
15
|
-
parseSetCookie,
|
|
16
|
-
sessionStoken,
|
|
17
|
-
signDs1,
|
|
18
|
-
signDs2,
|
|
19
|
-
} from './ds.js'
|
|
20
|
-
|
|
21
|
-
const PASSPORT_WEB = 'https://passport-api.miyoushe.com'
|
|
22
|
-
const PASSPORT_CN = 'https://passport-api.mihoyo.com'
|
|
23
|
-
const API_TAKUMI = 'https://api-takumi.mihoyo.com'
|
|
24
|
-
|
|
25
|
-
function passportBase(flow) {
|
|
26
|
-
return flow === 'app' ? PASSPORT_CN : PASSPORT_WEB
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** Return codes that mean the session is gone and only a re-scan can fix it. */
|
|
30
|
-
export const LOGIN_EXPIRED = new Set([-100, -111, 10001, 10010])
|
|
31
|
-
/** Return codes that mean risk control wants a human, so the run must stop. */
|
|
32
|
-
export const CAPTCHA_REQUIRED = new Set([1034, -999])
|
|
33
|
-
/** Return codes that mean the account has no role in that game. */
|
|
34
|
-
export const NOT_BOUND = new Set([-1002, 1016])
|
|
35
|
-
|
|
36
|
-
function asRetcode(json) {
|
|
37
|
-
const value = json?.retcode
|
|
38
|
-
return typeof value === 'number' ? value : -1
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function asMessage(json) {
|
|
42
|
-
const value = json?.message
|
|
43
|
-
return typeof value === 'string' && value !== '' ? value : `未知返回(retcode=${String(json?.retcode)})`
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** `true` when this retcode means "log in again". */
|
|
47
|
-
export function isLoginExpired(retcode) {
|
|
48
|
-
return LOGIN_EXPIRED.has(retcode)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** `true` when this retcode means "today is already signed". */
|
|
52
|
-
export function isAlreadySigned(retcode) {
|
|
53
|
-
return retcode === -5003
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** `true` when risk control interrupted the request. */
|
|
57
|
-
export function isCaptcha(retcode) {
|
|
58
|
-
return CAPTCHA_REQUIRED.has(retcode)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Create one QR login ticket.
|
|
63
|
-
*
|
|
64
|
-
* The web flow is the documented one; the app flow is a fallback for the case
|
|
65
|
-
* where the web endpoint answers with a protocol error.
|
|
66
|
-
*/
|
|
67
|
-
export async function createQrLogin({ config, deviceId, timeoutMs = 20000, flow = 'web' }) {
|
|
68
|
-
const url = flow === 'app'
|
|
69
|
-
? `${passportBase(flow)}/account/ma-cn-passport/app/createQRLogin`
|
|
70
|
-
: `${passportBase(flow)}/account/ma-cn-passport/web/createQRLogin`
|
|
71
|
-
const headers = flow === 'app'
|
|
72
|
-
? {
|
|
73
|
-
...passportHeaders({ appVersion: config.client.appVersion, deviceId }),
|
|
74
|
-
'x-rpc-app_id': 'ddxf5dufpuyo',
|
|
75
|
-
'x-rpc-client_type': '3',
|
|
76
|
-
'User-Agent': 'HYPContainer/1.3.3.182',
|
|
77
|
-
}
|
|
78
|
-
: passportHeaders({ appVersion: config.client.appVersion, deviceId })
|
|
79
|
-
|
|
80
|
-
const response = await request(url, { method: 'POST', headers, body: '{}', timeoutMs })
|
|
81
|
-
const json = response.json
|
|
82
|
-
if (asRetcode(json) !== 0) {
|
|
83
|
-
return { ok: false, flow, retcode: asRetcode(json), message: asMessage(json) }
|
|
84
|
-
}
|
|
85
|
-
const data = json?.data
|
|
86
|
-
const qrUrl = typeof data?.url === 'string' ? data.url : null
|
|
87
|
-
const ticket = typeof data?.ticket === 'string' ? data.ticket : null
|
|
88
|
-
if (qrUrl === null || ticket === null) {
|
|
89
|
-
return { ok: false, flow, retcode: -1, message: '二维码接口未返回 url/ticket' }
|
|
90
|
-
}
|
|
91
|
-
return { ok: true, flow, url: qrUrl, ticket }
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Store one inline token under the cookie name its value format belongs to.
|
|
96
|
-
*
|
|
97
|
-
* The passport marks the kind with `token_type` (1 stoken, 2 ltoken,
|
|
98
|
-
* 4 cookie_token); the `v2_` prefix then decides whether the V2 cookie name
|
|
99
|
-
* (paired with `mid`) or the legacy V1 name (paired with `stuid`) applies.
|
|
100
|
-
*/
|
|
101
|
-
function storeToken(cookies, token) {
|
|
102
|
-
if (typeof token?.token !== 'string' || token.token === '') return
|
|
103
|
-
const byName = { stoken: 1, ltoken: 2, cookie_token: 4 }
|
|
104
|
-
const type = typeof token.token_type === 'number' ? token.token_type : byName[token.name]
|
|
105
|
-
if (type !== 1 && type !== 2 && type !== 4) return
|
|
106
|
-
const v2 = token.token.startsWith('v2_')
|
|
107
|
-
const name = type === 1 ? 'stoken' : type === 2 ? 'ltoken' : 'cookie_token'
|
|
108
|
-
cookies[v2 ? `${name}_v2` : name] = token.token
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Poll one QR login ticket.
|
|
113
|
-
*
|
|
114
|
-
* `status` is normalized to `pending | scanned | confirmed | expired`.
|
|
115
|
-
* Confirmed responses carry the credential cookies in `Set-Cookie`. The App
|
|
116
|
-
* channel additionally answers with an inline `tokens` array — that is the
|
|
117
|
-
* only channel that requests a stoken (`token_types=1` in the QR URL).
|
|
118
|
-
*/
|
|
119
|
-
export async function queryQrLogin({ config, deviceId, ticket, flow = 'web', timeoutMs = 20000 }) {
|
|
120
|
-
const path = flow === 'app' ? '/account/ma-cn-passport/app/queryQRLoginStatus' : '/account/ma-cn-passport/web/queryQRLoginStatus'
|
|
121
|
-
const query = `ticket=${encodeURIComponent(ticket)}`
|
|
122
|
-
const headers = flow === 'app'
|
|
123
|
-
? {
|
|
124
|
-
...passportHeaders({ appVersion: config.client.appVersion, deviceId }),
|
|
125
|
-
'x-rpc-app_id': 'ddxf5dufpuyo',
|
|
126
|
-
'x-rpc-client_type': '3',
|
|
127
|
-
'User-Agent': 'HYPContainer/1.3.3.182',
|
|
128
|
-
}
|
|
129
|
-
: passportHeaders({ appVersion: config.client.appVersion, deviceId })
|
|
130
|
-
|
|
131
|
-
const response = await request(`${passportBase(flow)}${path}?${query}`, {
|
|
132
|
-
method: 'POST',
|
|
133
|
-
headers,
|
|
134
|
-
body: JSON.stringify({ ticket }),
|
|
135
|
-
timeoutMs,
|
|
136
|
-
})
|
|
137
|
-
const json = response.json
|
|
138
|
-
const retcode = asRetcode(json)
|
|
139
|
-
const rawStatus = typeof json?.data?.status === 'string' ? json.data.status : ''
|
|
140
|
-
const normalized = rawStatus.toLowerCase()
|
|
141
|
-
|
|
142
|
-
let status = 'pending'
|
|
143
|
-
if (retcode === -3501 || normalized === 'expired') status = 'expired'
|
|
144
|
-
else if (normalized === 'scanned' || normalized === 'scannedwaitingconfirmation') status = 'scanned'
|
|
145
|
-
else if (normalized === 'confirmed') status = 'confirmed'
|
|
146
|
-
else if (retcode === -3505) status = 'expired'
|
|
147
|
-
else if (retcode !== 0) status = 'error'
|
|
148
|
-
|
|
149
|
-
const userInfo = json?.data?.user_info ?? null
|
|
150
|
-
|
|
151
|
-
// Confirmed tokens arrive as cookies; the App channel also inlines them.
|
|
152
|
-
const cookies = credentialCookies(parseSetCookie(response.setCookies))
|
|
153
|
-
const tokens = json?.data?.tokens
|
|
154
|
-
if (Array.isArray(tokens)) {
|
|
155
|
-
for (const token of tokens) storeToken(cookies, token)
|
|
156
|
-
}
|
|
157
|
-
// V1 ltoken/cookie_token need ltuid/account_id, V2 tokens need ltmid_v2;
|
|
158
|
-
// the passport may return only one family, so fill the aliases it omitted.
|
|
159
|
-
ensureIdentityCookies(cookies, { aid: userInfo?.aid, mid: userInfo?.mid })
|
|
160
|
-
|
|
161
|
-
return {
|
|
162
|
-
retcode,
|
|
163
|
-
message: asMessage(json),
|
|
164
|
-
status,
|
|
165
|
-
userInfo,
|
|
166
|
-
cookies,
|
|
167
|
-
hasCookies: Object.keys(cookies).length > 0,
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* List the account's roles for one game.
|
|
173
|
-
*
|
|
174
|
-
* An empty list is a normal answer meaning "this account has no character in
|
|
175
|
-
* that game", not an error.
|
|
176
|
-
*/
|
|
177
|
-
export async function getRoles({ config, deviceId, cookies, biz, timeoutMs = 20000 }) {
|
|
178
|
-
const url = `${API_TAKUMI}/binding/api/getUserGameRolesByCookie?game_biz=${encodeURIComponent(biz)}`
|
|
179
|
-
const headers = apiHeaders({
|
|
180
|
-
config,
|
|
181
|
-
deviceId,
|
|
182
|
-
signature: signDs1({ salt: config.client.salt }),
|
|
183
|
-
cookie: cookieHeader(cookies),
|
|
184
|
-
})
|
|
185
|
-
const response = await request(url, { headers, timeoutMs })
|
|
186
|
-
const json = response.json
|
|
187
|
-
const retcode = asRetcode(json)
|
|
188
|
-
if (retcode !== 0) {
|
|
189
|
-
return { ok: false, retcode, message: asMessage(json), roles: [] }
|
|
190
|
-
}
|
|
191
|
-
const list = Array.isArray(json?.data?.list) ? json.data.list : []
|
|
192
|
-
const roles = list.map((item) => ({
|
|
193
|
-
uid: String(item?.game_uid ?? ''),
|
|
194
|
-
region: String(item?.region ?? ''),
|
|
195
|
-
regionName: typeof item?.region_name === 'string' ? item.region_name : '',
|
|
196
|
-
nickname: typeof item?.nickname === 'string' ? item.nickname : '',
|
|
197
|
-
level: typeof item?.level === 'number' ? item.level : null,
|
|
198
|
-
biz: typeof item?.game_biz === 'string' ? item.game_biz : biz,
|
|
199
|
-
}))
|
|
200
|
-
return { ok: true, retcode, message: 'ok', roles }
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/** DS2 salt used by the BBS app endpoints; stable across client releases. */
|
|
204
|
-
const BBS_SALT = 't0qEgfub6cvueAPgR5m9aQWWVciEer7v'
|
|
205
|
-
|
|
206
|
-
function bbsHeaders({ config, deviceId, cookie, signature }) {
|
|
207
|
-
return {
|
|
208
|
-
'User-Agent': 'okhttp/4.9.3',
|
|
209
|
-
Accept: 'application/json',
|
|
210
|
-
DS: signature,
|
|
211
|
-
'x-rpc-app_version': config.client.appVersion,
|
|
212
|
-
'x-rpc-client_type': '2',
|
|
213
|
-
'x-rpc-device_id': deviceId,
|
|
214
|
-
'x-rpc-device_name': config.device.name,
|
|
215
|
-
'x-rpc-device_model': config.device.model,
|
|
216
|
-
'x-rpc-sys_version': config.device.sysVersion,
|
|
217
|
-
'x-rpc-channel': config.device.channel,
|
|
218
|
-
'x-rpc-verify_key': 'bll8iq97cem8',
|
|
219
|
-
Referer: 'https://app.mihoyo.com',
|
|
220
|
-
...(cookie === '' ? {} : { Cookie: cookie }),
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Best-effort community profile lookup.
|
|
226
|
-
*
|
|
227
|
-
* The QR confirmation yields the numeric UID but no nickname, so this reads the
|
|
228
|
-
* community profile once after login. Failure is harmless: the account card
|
|
229
|
-
* falls back to the in-game role nickname.
|
|
230
|
-
*/
|
|
231
|
-
export async function getAccountProfile({ config, deviceId, cookies, uid, timeoutMs = 10000 }) {
|
|
232
|
-
if (typeof uid !== 'string' || uid === '') return { ok: false, nickname: null, avatar: null }
|
|
233
|
-
const query = new URLSearchParams({ uid })
|
|
234
|
-
const headers = bbsHeaders({
|
|
235
|
-
config,
|
|
236
|
-
deviceId,
|
|
237
|
-
cookie: cookieHeader(cookies),
|
|
238
|
-
signature: signDs2({ salt: BBS_SALT, query }),
|
|
239
|
-
})
|
|
240
|
-
const response = await request(`https://bbs-api.miyoushe.com/user/api/getUserFullInfo?${query}`, {
|
|
241
|
-
headers,
|
|
242
|
-
timeoutMs,
|
|
243
|
-
})
|
|
244
|
-
const json = response.json
|
|
245
|
-
if (asRetcode(json) !== 0) return { ok: false, retcode: asRetcode(json), message: asMessage(json), nickname: null, avatar: null }
|
|
246
|
-
const info = json?.data?.user?.user_info ?? json?.data?.user_info ?? null
|
|
247
|
-
const nickname = typeof info?.nickname === 'string' && info.nickname.trim() !== '' ? info.nickname.trim() : null
|
|
248
|
-
const avatar = typeof info?.avatar_url === 'string' && info.avatar_url !== '' ? info.avatar_url : null
|
|
249
|
-
return { ok: nickname !== null, nickname, avatar }
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* Read today's check-in state before signing.
|
|
254
|
-
*
|
|
255
|
-
* One cheap call per game provides the cumulative sign-in day shown in the
|
|
256
|
-
* result cards, and avoids re-signing when the account is already done.
|
|
257
|
-
*/
|
|
258
|
-
export async function getSignInfo({ config, deviceId, cookies, game, role, timeoutMs = 15000 }) {
|
|
259
|
-
const query = new URLSearchParams({
|
|
260
|
-
act_id: game.actId,
|
|
261
|
-
region: role.region,
|
|
262
|
-
uid: role.uid,
|
|
263
|
-
lang: 'zh-cn',
|
|
264
|
-
})
|
|
265
|
-
const headers = apiHeaders({
|
|
266
|
-
config,
|
|
267
|
-
deviceId,
|
|
268
|
-
signature: signDs1({ salt: config.client.salt }),
|
|
269
|
-
cookie: cookieHeader(cookies),
|
|
270
|
-
signgame: game.sendSigngame === true ? game.signgame : undefined,
|
|
271
|
-
})
|
|
272
|
-
const response = await request(`${game.host}/event/luna/${game.signgame}/info?${query}`, { headers, timeoutMs })
|
|
273
|
-
const json = response.json
|
|
274
|
-
const retcode = asRetcode(json)
|
|
275
|
-
if (retcode !== 0) return { ok: false, retcode, message: asMessage(json) }
|
|
276
|
-
const data = json?.data ?? {}
|
|
277
|
-
return {
|
|
278
|
-
ok: true,
|
|
279
|
-
isSign: data.is_sign === true,
|
|
280
|
-
firstBind: data.first_bind === true,
|
|
281
|
-
totalDays: typeof data.total_sign_day === 'number' ? data.total_sign_day : null,
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Sign one game for one role.
|
|
287
|
-
*
|
|
288
|
-
* `outcome` classifies the business result so the caller never has to know the
|
|
289
|
-
* retcode table: `signed | already | captcha | expired | not-bound | error`.
|
|
290
|
-
*/
|
|
291
|
-
export async function signGame({ config, deviceId, cookies, game, role, timeoutMs = 25000 }) {
|
|
292
|
-
const path = `/event/luna/${game.signgame}/sign`
|
|
293
|
-
const body = JSON.stringify({
|
|
294
|
-
act_id: game.actId,
|
|
295
|
-
region: role.region,
|
|
296
|
-
uid: role.uid,
|
|
297
|
-
lang: 'zh-cn',
|
|
298
|
-
})
|
|
299
|
-
const headers = {
|
|
300
|
-
...apiHeaders({
|
|
301
|
-
config,
|
|
302
|
-
deviceId,
|
|
303
|
-
signature: signDs1({ salt: config.client.salt }),
|
|
304
|
-
cookie: cookieHeader(cookies),
|
|
305
|
-
signgame: game.sendSigngame === true ? game.signgame : undefined,
|
|
306
|
-
}),
|
|
307
|
-
'Content-Type': 'application/json',
|
|
308
|
-
}
|
|
309
|
-
const response = await request(`${game.host}${path}`, { method: 'POST', headers, body, timeoutMs })
|
|
310
|
-
const json = response.json
|
|
311
|
-
const retcode = asRetcode(json)
|
|
312
|
-
const message = asMessage(json)
|
|
313
|
-
|
|
314
|
-
if (retcode === 0) {
|
|
315
|
-
const success = json?.data?.success
|
|
316
|
-
if (success === 1) {
|
|
317
|
-
return {
|
|
318
|
-
outcome: 'captcha',
|
|
319
|
-
retcode,
|
|
320
|
-
message: '触发验证码,需在米游社手动完成',
|
|
321
|
-
captcha: { gt: json?.data?.gt, challenge: json?.data?.challenge },
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
return { outcome: 'signed', retcode, message, awards: Array.isArray(json?.data?.awards) ? json.data.awards : [] }
|
|
325
|
-
}
|
|
326
|
-
if (isAlreadySigned(retcode)) return { outcome: 'already', retcode, message }
|
|
327
|
-
if (isLoginExpired(retcode)) return { outcome: 'expired', retcode, message }
|
|
328
|
-
if (isCaptcha(retcode)) return { outcome: 'captcha', retcode, message }
|
|
329
|
-
if (NOT_BOUND.has(retcode)) return { outcome: 'not-bound', retcode, message }
|
|
330
|
-
return { outcome: 'error', retcode, message }
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Exchange the long-lived stoken for a fresh cookie_token (and ltoken).
|
|
335
|
-
*
|
|
336
|
-
* Success updates only the fields that were returned; a failing exchange means
|
|
337
|
-
* the stoken itself is dead and only a new scan can restore the session.
|
|
338
|
-
*/
|
|
339
|
-
export async function refreshCredentials({ config, deviceId, cookies, timeoutMs = 20000 }) {
|
|
340
|
-
const stoken = sessionStoken(cookies)
|
|
341
|
-
if (stoken === null) {
|
|
342
|
-
return { ok: false, reason: 'no-stoken', cookies, message: '没有可续期的 stoken' }
|
|
343
|
-
}
|
|
344
|
-
const stuid = cookies.stuid ?? cookies.ltuid_v2 ?? cookies.ltuid ?? ''
|
|
345
|
-
const mid = cookies.mid ?? cookies.ltmid_v2 ?? cookies.ltmid ?? ''
|
|
346
|
-
// V2 tokens pair with `mid`, V1 tokens with `stuid`; send whichever exists.
|
|
347
|
-
const parts = []
|
|
348
|
-
if (stuid !== '') parts.push(`stuid=${stuid}`)
|
|
349
|
-
if (mid !== '') parts.push(`mid=${mid}`)
|
|
350
|
-
parts.push(`stoken=${stoken}`)
|
|
351
|
-
const headers = {
|
|
352
|
-
...passportHeaders({ appVersion: config.client.appVersion, deviceId }),
|
|
353
|
-
Cookie: parts.join('; '),
|
|
354
|
-
}
|
|
355
|
-
// The documented call also takes `stoken` and `uid` as query parameters.
|
|
356
|
-
const query = stuid === '' ? '' : `?stoken=${encodeURIComponent(stoken)}&uid=${encodeURIComponent(stuid)}`
|
|
357
|
-
|
|
358
|
-
const next = { ...cookies }
|
|
359
|
-
let changed = false
|
|
360
|
-
let reached = false
|
|
361
|
-
|
|
362
|
-
try {
|
|
363
|
-
const cookieResponse = await request(
|
|
364
|
-
`${PASSPORT_CN}/account/auth/api/getCookieAccountInfoBySToken${query}`,
|
|
365
|
-
{ headers, timeoutMs },
|
|
366
|
-
)
|
|
367
|
-
reached = true
|
|
368
|
-
if (asRetcode(cookieResponse.json) === 0) {
|
|
369
|
-
const token = cookieResponse.json?.data?.cookie_token
|
|
370
|
-
if (typeof token === 'string' && token !== '') {
|
|
371
|
-
if (token.startsWith('v2_')) next.cookie_token_v2 = token
|
|
372
|
-
else next.cookie_token = token
|
|
373
|
-
changed = true
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
} catch {
|
|
377
|
-
/* a transport failure must not invalidate stored credentials */
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
try {
|
|
381
|
-
const ltokenResponse = await request(
|
|
382
|
-
`${PASSPORT_CN}/account/auth/api/getLTokenBySToken`,
|
|
383
|
-
{ headers, timeoutMs },
|
|
384
|
-
)
|
|
385
|
-
reached = true
|
|
386
|
-
if (asRetcode(ltokenResponse.json) === 0) {
|
|
387
|
-
const token = ltokenResponse.json?.data?.ltoken
|
|
388
|
-
if (typeof token === 'string' && token !== '') {
|
|
389
|
-
if (token.startsWith('v2_')) next.ltoken_v2 = token
|
|
390
|
-
else next.ltoken = token
|
|
391
|
-
changed = true
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
} catch {
|
|
395
|
-
/* same */
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
if (!changed) {
|
|
399
|
-
if (reached === false) {
|
|
400
|
-
return { ok: false, reason: 'network', cookies, message: '续期请求失败(网络原因)' }
|
|
401
|
-
}
|
|
402
|
-
return { ok: false, reason: 'stoken-invalid', cookies, message: 'stoken 续期失败,登录状态已失效' }
|
|
403
|
-
}
|
|
404
|
-
// getLTokenBySToken returns a V1 ltoken; pair it with the V1 id cookies too,
|
|
405
|
-
// otherwise luna endpoints answer -100 even though binding queries work.
|
|
406
|
-
ensureIdentityCookies(next, {
|
|
407
|
-
aid: stuid !== '' ? stuid : next.ltuid ?? next.ltuid_v2,
|
|
408
|
-
mid: mid !== '' ? mid : next.ltmid_v2 ?? next.mid,
|
|
409
|
-
})
|
|
410
|
-
return { ok: true, cookies: next, message: '凭据已续期' }
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
/** Verify a session with the cheapest authenticated call. */
|
|
414
|
-
export async function verifySession({ config, deviceId, cookies, timeoutMs = 20000 }) {
|
|
415
|
-
const result = await getRoles({ config, deviceId, cookies, biz: 'hk4e_cn', timeoutMs })
|
|
416
|
-
if (result.ok) return { ok: true }
|
|
417
|
-
if (isLoginExpired(result.retcode)) return { ok: false, reason: 'expired', ...result }
|
|
418
|
-
return { ok: false, reason: 'error', ...result }
|
|
419
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The miHoYo BBS protocol, as far as daily game check-in needs it.
|
|
3
|
+
*
|
|
4
|
+
* Every function takes an explicit `config`/`deviceId` so the same code can be
|
|
5
|
+
* unit-tested against a fake transport, and returns the raw `retcode` alongside
|
|
6
|
+
* a normalized outcome instead of throwing for business failures.
|
|
7
|
+
*/
|
|
8
|
+
import { request } from './http.js'
|
|
9
|
+
import {
|
|
10
|
+
apiHeaders,
|
|
11
|
+
cookieHeader,
|
|
12
|
+
credentialCookies,
|
|
13
|
+
ensureIdentityCookies,
|
|
14
|
+
passportHeaders,
|
|
15
|
+
parseSetCookie,
|
|
16
|
+
sessionStoken,
|
|
17
|
+
signDs1,
|
|
18
|
+
signDs2,
|
|
19
|
+
} from './ds.js'
|
|
20
|
+
|
|
21
|
+
const PASSPORT_WEB = 'https://passport-api.miyoushe.com'
|
|
22
|
+
const PASSPORT_CN = 'https://passport-api.mihoyo.com'
|
|
23
|
+
const API_TAKUMI = 'https://api-takumi.mihoyo.com'
|
|
24
|
+
|
|
25
|
+
function passportBase(flow) {
|
|
26
|
+
return flow === 'app' ? PASSPORT_CN : PASSPORT_WEB
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Return codes that mean the session is gone and only a re-scan can fix it. */
|
|
30
|
+
export const LOGIN_EXPIRED = new Set([-100, -111, 10001, 10010])
|
|
31
|
+
/** Return codes that mean risk control wants a human, so the run must stop. */
|
|
32
|
+
export const CAPTCHA_REQUIRED = new Set([1034, -999])
|
|
33
|
+
/** Return codes that mean the account has no role in that game. */
|
|
34
|
+
export const NOT_BOUND = new Set([-1002, 1016])
|
|
35
|
+
|
|
36
|
+
function asRetcode(json) {
|
|
37
|
+
const value = json?.retcode
|
|
38
|
+
return typeof value === 'number' ? value : -1
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function asMessage(json) {
|
|
42
|
+
const value = json?.message
|
|
43
|
+
return typeof value === 'string' && value !== '' ? value : `未知返回(retcode=${String(json?.retcode)})`
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** `true` when this retcode means "log in again". */
|
|
47
|
+
export function isLoginExpired(retcode) {
|
|
48
|
+
return LOGIN_EXPIRED.has(retcode)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** `true` when this retcode means "today is already signed". */
|
|
52
|
+
export function isAlreadySigned(retcode) {
|
|
53
|
+
return retcode === -5003
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** `true` when risk control interrupted the request. */
|
|
57
|
+
export function isCaptcha(retcode) {
|
|
58
|
+
return CAPTCHA_REQUIRED.has(retcode)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Create one QR login ticket.
|
|
63
|
+
*
|
|
64
|
+
* The web flow is the documented one; the app flow is a fallback for the case
|
|
65
|
+
* where the web endpoint answers with a protocol error.
|
|
66
|
+
*/
|
|
67
|
+
export async function createQrLogin({ config, deviceId, timeoutMs = 20000, flow = 'web' }) {
|
|
68
|
+
const url = flow === 'app'
|
|
69
|
+
? `${passportBase(flow)}/account/ma-cn-passport/app/createQRLogin`
|
|
70
|
+
: `${passportBase(flow)}/account/ma-cn-passport/web/createQRLogin`
|
|
71
|
+
const headers = flow === 'app'
|
|
72
|
+
? {
|
|
73
|
+
...passportHeaders({ appVersion: config.client.appVersion, deviceId }),
|
|
74
|
+
'x-rpc-app_id': 'ddxf5dufpuyo',
|
|
75
|
+
'x-rpc-client_type': '3',
|
|
76
|
+
'User-Agent': 'HYPContainer/1.3.3.182',
|
|
77
|
+
}
|
|
78
|
+
: passportHeaders({ appVersion: config.client.appVersion, deviceId })
|
|
79
|
+
|
|
80
|
+
const response = await request(url, { method: 'POST', headers, body: '{}', timeoutMs })
|
|
81
|
+
const json = response.json
|
|
82
|
+
if (asRetcode(json) !== 0) {
|
|
83
|
+
return { ok: false, flow, retcode: asRetcode(json), message: asMessage(json) }
|
|
84
|
+
}
|
|
85
|
+
const data = json?.data
|
|
86
|
+
const qrUrl = typeof data?.url === 'string' ? data.url : null
|
|
87
|
+
const ticket = typeof data?.ticket === 'string' ? data.ticket : null
|
|
88
|
+
if (qrUrl === null || ticket === null) {
|
|
89
|
+
return { ok: false, flow, retcode: -1, message: '二维码接口未返回 url/ticket' }
|
|
90
|
+
}
|
|
91
|
+
return { ok: true, flow, url: qrUrl, ticket }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Store one inline token under the cookie name its value format belongs to.
|
|
96
|
+
*
|
|
97
|
+
* The passport marks the kind with `token_type` (1 stoken, 2 ltoken,
|
|
98
|
+
* 4 cookie_token); the `v2_` prefix then decides whether the V2 cookie name
|
|
99
|
+
* (paired with `mid`) or the legacy V1 name (paired with `stuid`) applies.
|
|
100
|
+
*/
|
|
101
|
+
function storeToken(cookies, token) {
|
|
102
|
+
if (typeof token?.token !== 'string' || token.token === '') return
|
|
103
|
+
const byName = { stoken: 1, ltoken: 2, cookie_token: 4 }
|
|
104
|
+
const type = typeof token.token_type === 'number' ? token.token_type : byName[token.name]
|
|
105
|
+
if (type !== 1 && type !== 2 && type !== 4) return
|
|
106
|
+
const v2 = token.token.startsWith('v2_')
|
|
107
|
+
const name = type === 1 ? 'stoken' : type === 2 ? 'ltoken' : 'cookie_token'
|
|
108
|
+
cookies[v2 ? `${name}_v2` : name] = token.token
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Poll one QR login ticket.
|
|
113
|
+
*
|
|
114
|
+
* `status` is normalized to `pending | scanned | confirmed | expired`.
|
|
115
|
+
* Confirmed responses carry the credential cookies in `Set-Cookie`. The App
|
|
116
|
+
* channel additionally answers with an inline `tokens` array — that is the
|
|
117
|
+
* only channel that requests a stoken (`token_types=1` in the QR URL).
|
|
118
|
+
*/
|
|
119
|
+
export async function queryQrLogin({ config, deviceId, ticket, flow = 'web', timeoutMs = 20000 }) {
|
|
120
|
+
const path = flow === 'app' ? '/account/ma-cn-passport/app/queryQRLoginStatus' : '/account/ma-cn-passport/web/queryQRLoginStatus'
|
|
121
|
+
const query = `ticket=${encodeURIComponent(ticket)}`
|
|
122
|
+
const headers = flow === 'app'
|
|
123
|
+
? {
|
|
124
|
+
...passportHeaders({ appVersion: config.client.appVersion, deviceId }),
|
|
125
|
+
'x-rpc-app_id': 'ddxf5dufpuyo',
|
|
126
|
+
'x-rpc-client_type': '3',
|
|
127
|
+
'User-Agent': 'HYPContainer/1.3.3.182',
|
|
128
|
+
}
|
|
129
|
+
: passportHeaders({ appVersion: config.client.appVersion, deviceId })
|
|
130
|
+
|
|
131
|
+
const response = await request(`${passportBase(flow)}${path}?${query}`, {
|
|
132
|
+
method: 'POST',
|
|
133
|
+
headers,
|
|
134
|
+
body: JSON.stringify({ ticket }),
|
|
135
|
+
timeoutMs,
|
|
136
|
+
})
|
|
137
|
+
const json = response.json
|
|
138
|
+
const retcode = asRetcode(json)
|
|
139
|
+
const rawStatus = typeof json?.data?.status === 'string' ? json.data.status : ''
|
|
140
|
+
const normalized = rawStatus.toLowerCase()
|
|
141
|
+
|
|
142
|
+
let status = 'pending'
|
|
143
|
+
if (retcode === -3501 || normalized === 'expired') status = 'expired'
|
|
144
|
+
else if (normalized === 'scanned' || normalized === 'scannedwaitingconfirmation') status = 'scanned'
|
|
145
|
+
else if (normalized === 'confirmed') status = 'confirmed'
|
|
146
|
+
else if (retcode === -3505) status = 'expired'
|
|
147
|
+
else if (retcode !== 0) status = 'error'
|
|
148
|
+
|
|
149
|
+
const userInfo = json?.data?.user_info ?? null
|
|
150
|
+
|
|
151
|
+
// Confirmed tokens arrive as cookies; the App channel also inlines them.
|
|
152
|
+
const cookies = credentialCookies(parseSetCookie(response.setCookies))
|
|
153
|
+
const tokens = json?.data?.tokens
|
|
154
|
+
if (Array.isArray(tokens)) {
|
|
155
|
+
for (const token of tokens) storeToken(cookies, token)
|
|
156
|
+
}
|
|
157
|
+
// V1 ltoken/cookie_token need ltuid/account_id, V2 tokens need ltmid_v2;
|
|
158
|
+
// the passport may return only one family, so fill the aliases it omitted.
|
|
159
|
+
ensureIdentityCookies(cookies, { aid: userInfo?.aid, mid: userInfo?.mid })
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
retcode,
|
|
163
|
+
message: asMessage(json),
|
|
164
|
+
status,
|
|
165
|
+
userInfo,
|
|
166
|
+
cookies,
|
|
167
|
+
hasCookies: Object.keys(cookies).length > 0,
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* List the account's roles for one game.
|
|
173
|
+
*
|
|
174
|
+
* An empty list is a normal answer meaning "this account has no character in
|
|
175
|
+
* that game", not an error.
|
|
176
|
+
*/
|
|
177
|
+
export async function getRoles({ config, deviceId, cookies, biz, timeoutMs = 20000 }) {
|
|
178
|
+
const url = `${API_TAKUMI}/binding/api/getUserGameRolesByCookie?game_biz=${encodeURIComponent(biz)}`
|
|
179
|
+
const headers = apiHeaders({
|
|
180
|
+
config,
|
|
181
|
+
deviceId,
|
|
182
|
+
signature: signDs1({ salt: config.client.salt }),
|
|
183
|
+
cookie: cookieHeader(cookies),
|
|
184
|
+
})
|
|
185
|
+
const response = await request(url, { headers, timeoutMs })
|
|
186
|
+
const json = response.json
|
|
187
|
+
const retcode = asRetcode(json)
|
|
188
|
+
if (retcode !== 0) {
|
|
189
|
+
return { ok: false, retcode, message: asMessage(json), roles: [] }
|
|
190
|
+
}
|
|
191
|
+
const list = Array.isArray(json?.data?.list) ? json.data.list : []
|
|
192
|
+
const roles = list.map((item) => ({
|
|
193
|
+
uid: String(item?.game_uid ?? ''),
|
|
194
|
+
region: String(item?.region ?? ''),
|
|
195
|
+
regionName: typeof item?.region_name === 'string' ? item.region_name : '',
|
|
196
|
+
nickname: typeof item?.nickname === 'string' ? item.nickname : '',
|
|
197
|
+
level: typeof item?.level === 'number' ? item.level : null,
|
|
198
|
+
biz: typeof item?.game_biz === 'string' ? item.game_biz : biz,
|
|
199
|
+
}))
|
|
200
|
+
return { ok: true, retcode, message: 'ok', roles }
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** DS2 salt used by the BBS app endpoints; stable across client releases. */
|
|
204
|
+
const BBS_SALT = 't0qEgfub6cvueAPgR5m9aQWWVciEer7v'
|
|
205
|
+
|
|
206
|
+
function bbsHeaders({ config, deviceId, cookie, signature }) {
|
|
207
|
+
return {
|
|
208
|
+
'User-Agent': 'okhttp/4.9.3',
|
|
209
|
+
Accept: 'application/json',
|
|
210
|
+
DS: signature,
|
|
211
|
+
'x-rpc-app_version': config.client.appVersion,
|
|
212
|
+
'x-rpc-client_type': '2',
|
|
213
|
+
'x-rpc-device_id': deviceId,
|
|
214
|
+
'x-rpc-device_name': config.device.name,
|
|
215
|
+
'x-rpc-device_model': config.device.model,
|
|
216
|
+
'x-rpc-sys_version': config.device.sysVersion,
|
|
217
|
+
'x-rpc-channel': config.device.channel,
|
|
218
|
+
'x-rpc-verify_key': 'bll8iq97cem8',
|
|
219
|
+
Referer: 'https://app.mihoyo.com',
|
|
220
|
+
...(cookie === '' ? {} : { Cookie: cookie }),
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Best-effort community profile lookup.
|
|
226
|
+
*
|
|
227
|
+
* The QR confirmation yields the numeric UID but no nickname, so this reads the
|
|
228
|
+
* community profile once after login. Failure is harmless: the account card
|
|
229
|
+
* falls back to the in-game role nickname.
|
|
230
|
+
*/
|
|
231
|
+
export async function getAccountProfile({ config, deviceId, cookies, uid, timeoutMs = 10000 }) {
|
|
232
|
+
if (typeof uid !== 'string' || uid === '') return { ok: false, nickname: null, avatar: null }
|
|
233
|
+
const query = new URLSearchParams({ uid })
|
|
234
|
+
const headers = bbsHeaders({
|
|
235
|
+
config,
|
|
236
|
+
deviceId,
|
|
237
|
+
cookie: cookieHeader(cookies),
|
|
238
|
+
signature: signDs2({ salt: BBS_SALT, query }),
|
|
239
|
+
})
|
|
240
|
+
const response = await request(`https://bbs-api.miyoushe.com/user/api/getUserFullInfo?${query}`, {
|
|
241
|
+
headers,
|
|
242
|
+
timeoutMs,
|
|
243
|
+
})
|
|
244
|
+
const json = response.json
|
|
245
|
+
if (asRetcode(json) !== 0) return { ok: false, retcode: asRetcode(json), message: asMessage(json), nickname: null, avatar: null }
|
|
246
|
+
const info = json?.data?.user?.user_info ?? json?.data?.user_info ?? null
|
|
247
|
+
const nickname = typeof info?.nickname === 'string' && info.nickname.trim() !== '' ? info.nickname.trim() : null
|
|
248
|
+
const avatar = typeof info?.avatar_url === 'string' && info.avatar_url !== '' ? info.avatar_url : null
|
|
249
|
+
return { ok: nickname !== null, nickname, avatar }
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Read today's check-in state before signing.
|
|
254
|
+
*
|
|
255
|
+
* One cheap call per game provides the cumulative sign-in day shown in the
|
|
256
|
+
* result cards, and avoids re-signing when the account is already done.
|
|
257
|
+
*/
|
|
258
|
+
export async function getSignInfo({ config, deviceId, cookies, game, role, timeoutMs = 15000 }) {
|
|
259
|
+
const query = new URLSearchParams({
|
|
260
|
+
act_id: game.actId,
|
|
261
|
+
region: role.region,
|
|
262
|
+
uid: role.uid,
|
|
263
|
+
lang: 'zh-cn',
|
|
264
|
+
})
|
|
265
|
+
const headers = apiHeaders({
|
|
266
|
+
config,
|
|
267
|
+
deviceId,
|
|
268
|
+
signature: signDs1({ salt: config.client.salt }),
|
|
269
|
+
cookie: cookieHeader(cookies),
|
|
270
|
+
signgame: game.sendSigngame === true ? game.signgame : undefined,
|
|
271
|
+
})
|
|
272
|
+
const response = await request(`${game.host}/event/luna/${game.signgame}/info?${query}`, { headers, timeoutMs })
|
|
273
|
+
const json = response.json
|
|
274
|
+
const retcode = asRetcode(json)
|
|
275
|
+
if (retcode !== 0) return { ok: false, retcode, message: asMessage(json) }
|
|
276
|
+
const data = json?.data ?? {}
|
|
277
|
+
return {
|
|
278
|
+
ok: true,
|
|
279
|
+
isSign: data.is_sign === true,
|
|
280
|
+
firstBind: data.first_bind === true,
|
|
281
|
+
totalDays: typeof data.total_sign_day === 'number' ? data.total_sign_day : null,
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Sign one game for one role.
|
|
287
|
+
*
|
|
288
|
+
* `outcome` classifies the business result so the caller never has to know the
|
|
289
|
+
* retcode table: `signed | already | captcha | expired | not-bound | error`.
|
|
290
|
+
*/
|
|
291
|
+
export async function signGame({ config, deviceId, cookies, game, role, timeoutMs = 25000 }) {
|
|
292
|
+
const path = `/event/luna/${game.signgame}/sign`
|
|
293
|
+
const body = JSON.stringify({
|
|
294
|
+
act_id: game.actId,
|
|
295
|
+
region: role.region,
|
|
296
|
+
uid: role.uid,
|
|
297
|
+
lang: 'zh-cn',
|
|
298
|
+
})
|
|
299
|
+
const headers = {
|
|
300
|
+
...apiHeaders({
|
|
301
|
+
config,
|
|
302
|
+
deviceId,
|
|
303
|
+
signature: signDs1({ salt: config.client.salt }),
|
|
304
|
+
cookie: cookieHeader(cookies),
|
|
305
|
+
signgame: game.sendSigngame === true ? game.signgame : undefined,
|
|
306
|
+
}),
|
|
307
|
+
'Content-Type': 'application/json',
|
|
308
|
+
}
|
|
309
|
+
const response = await request(`${game.host}${path}`, { method: 'POST', headers, body, timeoutMs })
|
|
310
|
+
const json = response.json
|
|
311
|
+
const retcode = asRetcode(json)
|
|
312
|
+
const message = asMessage(json)
|
|
313
|
+
|
|
314
|
+
if (retcode === 0) {
|
|
315
|
+
const success = json?.data?.success
|
|
316
|
+
if (success === 1) {
|
|
317
|
+
return {
|
|
318
|
+
outcome: 'captcha',
|
|
319
|
+
retcode,
|
|
320
|
+
message: '触发验证码,需在米游社手动完成',
|
|
321
|
+
captcha: { gt: json?.data?.gt, challenge: json?.data?.challenge },
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return { outcome: 'signed', retcode, message, awards: Array.isArray(json?.data?.awards) ? json.data.awards : [] }
|
|
325
|
+
}
|
|
326
|
+
if (isAlreadySigned(retcode)) return { outcome: 'already', retcode, message }
|
|
327
|
+
if (isLoginExpired(retcode)) return { outcome: 'expired', retcode, message }
|
|
328
|
+
if (isCaptcha(retcode)) return { outcome: 'captcha', retcode, message }
|
|
329
|
+
if (NOT_BOUND.has(retcode)) return { outcome: 'not-bound', retcode, message }
|
|
330
|
+
return { outcome: 'error', retcode, message }
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Exchange the long-lived stoken for a fresh cookie_token (and ltoken).
|
|
335
|
+
*
|
|
336
|
+
* Success updates only the fields that were returned; a failing exchange means
|
|
337
|
+
* the stoken itself is dead and only a new scan can restore the session.
|
|
338
|
+
*/
|
|
339
|
+
export async function refreshCredentials({ config, deviceId, cookies, timeoutMs = 20000 }) {
|
|
340
|
+
const stoken = sessionStoken(cookies)
|
|
341
|
+
if (stoken === null) {
|
|
342
|
+
return { ok: false, reason: 'no-stoken', cookies, message: '没有可续期的 stoken' }
|
|
343
|
+
}
|
|
344
|
+
const stuid = cookies.stuid ?? cookies.ltuid_v2 ?? cookies.ltuid ?? ''
|
|
345
|
+
const mid = cookies.mid ?? cookies.ltmid_v2 ?? cookies.ltmid ?? ''
|
|
346
|
+
// V2 tokens pair with `mid`, V1 tokens with `stuid`; send whichever exists.
|
|
347
|
+
const parts = []
|
|
348
|
+
if (stuid !== '') parts.push(`stuid=${stuid}`)
|
|
349
|
+
if (mid !== '') parts.push(`mid=${mid}`)
|
|
350
|
+
parts.push(`stoken=${stoken}`)
|
|
351
|
+
const headers = {
|
|
352
|
+
...passportHeaders({ appVersion: config.client.appVersion, deviceId }),
|
|
353
|
+
Cookie: parts.join('; '),
|
|
354
|
+
}
|
|
355
|
+
// The documented call also takes `stoken` and `uid` as query parameters.
|
|
356
|
+
const query = stuid === '' ? '' : `?stoken=${encodeURIComponent(stoken)}&uid=${encodeURIComponent(stuid)}`
|
|
357
|
+
|
|
358
|
+
const next = { ...cookies }
|
|
359
|
+
let changed = false
|
|
360
|
+
let reached = false
|
|
361
|
+
|
|
362
|
+
try {
|
|
363
|
+
const cookieResponse = await request(
|
|
364
|
+
`${PASSPORT_CN}/account/auth/api/getCookieAccountInfoBySToken${query}`,
|
|
365
|
+
{ headers, timeoutMs },
|
|
366
|
+
)
|
|
367
|
+
reached = true
|
|
368
|
+
if (asRetcode(cookieResponse.json) === 0) {
|
|
369
|
+
const token = cookieResponse.json?.data?.cookie_token
|
|
370
|
+
if (typeof token === 'string' && token !== '') {
|
|
371
|
+
if (token.startsWith('v2_')) next.cookie_token_v2 = token
|
|
372
|
+
else next.cookie_token = token
|
|
373
|
+
changed = true
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
} catch {
|
|
377
|
+
/* a transport failure must not invalidate stored credentials */
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
try {
|
|
381
|
+
const ltokenResponse = await request(
|
|
382
|
+
`${PASSPORT_CN}/account/auth/api/getLTokenBySToken`,
|
|
383
|
+
{ headers, timeoutMs },
|
|
384
|
+
)
|
|
385
|
+
reached = true
|
|
386
|
+
if (asRetcode(ltokenResponse.json) === 0) {
|
|
387
|
+
const token = ltokenResponse.json?.data?.ltoken
|
|
388
|
+
if (typeof token === 'string' && token !== '') {
|
|
389
|
+
if (token.startsWith('v2_')) next.ltoken_v2 = token
|
|
390
|
+
else next.ltoken = token
|
|
391
|
+
changed = true
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
} catch {
|
|
395
|
+
/* same */
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (!changed) {
|
|
399
|
+
if (reached === false) {
|
|
400
|
+
return { ok: false, reason: 'network', cookies, message: '续期请求失败(网络原因)' }
|
|
401
|
+
}
|
|
402
|
+
return { ok: false, reason: 'stoken-invalid', cookies, message: 'stoken 续期失败,登录状态已失效' }
|
|
403
|
+
}
|
|
404
|
+
// getLTokenBySToken returns a V1 ltoken; pair it with the V1 id cookies too,
|
|
405
|
+
// otherwise luna endpoints answer -100 even though binding queries work.
|
|
406
|
+
ensureIdentityCookies(next, {
|
|
407
|
+
aid: stuid !== '' ? stuid : next.ltuid ?? next.ltuid_v2,
|
|
408
|
+
mid: mid !== '' ? mid : next.ltmid_v2 ?? next.mid,
|
|
409
|
+
})
|
|
410
|
+
return { ok: true, cookies: next, message: '凭据已续期' }
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** Verify a session with the cheapest authenticated call. */
|
|
414
|
+
export async function verifySession({ config, deviceId, cookies, timeoutMs = 20000 }) {
|
|
415
|
+
const result = await getRoles({ config, deviceId, cookies, biz: 'hk4e_cn', timeoutMs })
|
|
416
|
+
if (result.ok) return { ok: true }
|
|
417
|
+
if (isLoginExpired(result.retcode)) return { ok: false, reason: 'expired', ...result }
|
|
418
|
+
return { ok: false, reason: 'error', ...result }
|
|
419
|
+
}
|