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.
Files changed (210) hide show
  1. package/README.md +8 -2
  2. package/assets/default-prefs.json +8 -8
  3. package/assets/icon.svg +89 -89
  4. package/assets/locale/notes/en.json +6 -6
  5. package/assets/locale/notes/zh-CN.json +6 -6
  6. package/assets/locale/reminder/en.json +6 -6
  7. package/assets/locale/reminder/zh-CN.json +6 -6
  8. package/assets/model/Cyrene.model3.json +441 -441
  9. package/assets/model/bongo-standard/Live2d_expression0.exp3.json +9 -9
  10. package/assets/model/bongo-standard/cat.cdi3.json +296 -296
  11. package/assets/model/bongo-standard/cat.model3.json +33 -33
  12. package/assets/model/bongo-standard/cat.physics3.json +232 -232
  13. package/assets/model/expressions/0.exp3.json +24 -24
  14. package/assets/model/expressions/1.exp3.json +11 -11
  15. package/assets/model/expressions/10.exp3.json +9 -9
  16. package/assets/model/expressions/11.exp3.json +9 -9
  17. package/assets/model/expressions/2.exp3.json +9 -9
  18. package/assets/model/expressions/3.exp3.json +9 -9
  19. package/assets/model/expressions/4.exp3.json +11 -11
  20. package/assets/model/expressions/5.exp3.json +11 -11
  21. package/assets/model/expressions/6.exp3.json +11 -11
  22. package/assets/model/expressions/7.exp3.json +11 -11
  23. package/assets/model/expressions/8.exp3.json +11 -11
  24. package/assets/model/expressions/9.exp3.json +9 -9
  25. package/assets/model/model.physics3.json +641 -641
  26. package/assets/model/motions/Tick3_3.motion3.json +1347 -1347
  27. package/assets/model/motions//345/212/250/344/275/2346_0.motion3.json +699 -699
  28. package/assets/model/motions//345/212/250/344/275/2346_1.motion3.json +1241 -1241
  29. package/assets/model/motions//345/212/250/344/275/2346_2.motion3.json +1117 -1117
  30. package/assets/model/motions//345/212/250/344/275/2346_3.motion3.json +1106 -1106
  31. package/assets/model/we-q/expressions/FileReferences_Expressions_0_File_0.exp3.json +19 -19
  32. package/assets/model/we-q/expressions/FileReferences_Expressions_1_File_0.exp3.json +41 -41
  33. package/assets/model/we-q/expressions/FileReferences_Expressions_2_File_0.exp3.json +104 -104
  34. package/assets/model/we-q/model0.model3.json +245 -245
  35. package/assets/model/we-q/motions/FileReferences_Motions_shake#1_0_File_0.motion3.json +520 -520
  36. package/assets/model/we-q/motions/FileReferences_Motions_talk#1_0_File_0.motion3.json +499 -499
  37. package/assets/model/we-q/motions/FileReferences_Motions_talk#1_1_File_0.motion3.json +453 -453
  38. package/assets/model/we-q/motions/FileReferences_Motions_wink#1_0_File_0.motion3.json +478 -478
  39. package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_0_File_0.motion3.json +450 -450
  40. package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_1_File_0.motion3.json +450 -450
  41. package/assets/model/we-q/motions/idle/FileReferences_Motions_Idleidle_0_File_0.motion3.json +422 -422
  42. package/assets/model/we-q/physics/FileReferences_Physics_0.physic3.json +236 -236
  43. package/assets/runtime/live2dcubismcore.min.js +8 -8
  44. package/assets/stickers.json +84 -84
  45. package/assets/vendor/cubism4.min.js +1 -1
  46. package/assets/vendor/pixi.min.js +9 -9
  47. package/assets/vendor/theme-cyrene-factory.js +4314 -4314
  48. package/assets/vendor/theme-cyrene.js +4317 -4317
  49. package/assets/voice/angry-00.opus +0 -0
  50. package/assets/voice/angry-02.opus +0 -0
  51. package/assets/voice/approval-00.opus +0 -0
  52. package/assets/voice/approval-01.opus +0 -0
  53. package/assets/voice/approval-02.opus +0 -0
  54. package/assets/voice/askAck-00.opus +0 -0
  55. package/assets/voice/away-01.opus +0 -0
  56. package/assets/voice/away-02.opus +0 -0
  57. package/assets/voice/away-03.opus +0 -0
  58. package/assets/voice/away-05.opus +0 -0
  59. package/assets/voice/back-00.opus +0 -0
  60. package/assets/voice/back-01.opus +0 -0
  61. package/assets/voice/backLong-05.opus +0 -0
  62. package/assets/voice/backShort-03.opus +0 -0
  63. package/assets/voice/backShort-04.opus +0 -0
  64. package/assets/voice/backShort-07.opus +0 -0
  65. package/assets/voice/backVeryLong-01.opus +0 -0
  66. package/assets/voice/backVeryLong-02.opus +0 -0
  67. package/assets/voice/backVeryLong-03.opus +0 -0
  68. package/assets/voice/backVeryLong-04.opus +0 -0
  69. package/assets/voice/backVeryLong-05.opus +0 -0
  70. package/assets/voice/backVeryLong-07.opus +0 -0
  71. package/assets/voice/backVeryLong-08.opus +0 -0
  72. package/assets/voice/balLow-00.opus +0 -0
  73. package/assets/voice/balLow-01.opus +0 -0
  74. package/assets/voice/birthday-02.opus +0 -0
  75. package/assets/voice/click-00.opus +0 -0
  76. package/assets/voice/click-01.opus +0 -0
  77. package/assets/voice/click-07.opus +0 -0
  78. package/assets/voice/click-10.opus +0 -0
  79. package/assets/voice/click-11.opus +0 -0
  80. package/assets/voice/click-12.opus +0 -0
  81. package/assets/voice/click-16.opus +0 -0
  82. package/assets/voice/click-17.opus +0 -0
  83. package/assets/voice/click-18.opus +0 -0
  84. package/assets/voice/comfort-03.opus +0 -0
  85. package/assets/voice/comfort-04.opus +0 -0
  86. package/assets/voice/cost10-00.opus +0 -0
  87. package/assets/voice/cost2-00.opus +0 -0
  88. package/assets/voice/cost5-00.opus +0 -0
  89. package/assets/voice/costTurn-01.opus +0 -0
  90. package/assets/voice/costTurn-02.opus +0 -0
  91. package/assets/voice/ctxFull-01.opus +0 -0
  92. package/assets/voice/ctxHigh-00.opus +0 -0
  93. package/assets/voice/dawn-01.opus +0 -0
  94. package/assets/voice/dawn-04.opus +0 -0
  95. package/assets/voice/dawn-06.opus +0 -0
  96. package/assets/voice/dawn-07.opus +0 -0
  97. package/assets/voice/deepWake-03.opus +0 -0
  98. package/assets/voice/deepWake-04.opus +0 -0
  99. package/assets/voice/deepWake-06.opus +0 -0
  100. package/assets/voice/deepWake-07.opus +0 -0
  101. package/assets/voice/done-05.opus +0 -0
  102. package/assets/voice/done-08.opus +0 -0
  103. package/assets/voice/done-09.opus +0 -0
  104. package/assets/voice/done-11.opus +0 -0
  105. package/assets/voice/empty-00.opus +0 -0
  106. package/assets/voice/encourage-02.opus +0 -0
  107. package/assets/voice/failed-01.opus +0 -0
  108. package/assets/voice/failed-05.opus +0 -0
  109. package/assets/voice/failkind-auth-01.opus +0 -0
  110. package/assets/voice/failkind-auth-02.opus +0 -0
  111. package/assets/voice/failkind-ctx-00.opus +0 -0
  112. package/assets/voice/failkind-missing-01.opus +0 -0
  113. package/assets/voice/focusEnd-02.opus +0 -0
  114. package/assets/voice/focusEnd-04.opus +0 -0
  115. package/assets/voice/forgive-00.opus +0 -0
  116. package/assets/voice/hello-00.opus +0 -0
  117. package/assets/voice/hello-03.opus +0 -0
  118. package/assets/voice/hold-00.opus +0 -0
  119. package/assets/voice/idleTap-00.opus +0 -0
  120. package/assets/voice/idleTapLong-02.opus +0 -0
  121. package/assets/voice/lateNight-00.opus +0 -0
  122. package/assets/voice/lateNight-02.opus +0 -0
  123. package/assets/voice/lateNight-03.opus +0 -0
  124. package/assets/voice/lateNight-06.opus +0 -0
  125. package/assets/voice/lateNight-07.opus +0 -0
  126. package/assets/voice/murmur-04.opus +0 -0
  127. package/assets/voice/night-01.opus +0 -0
  128. package/assets/voice/night-03.opus +0 -0
  129. package/assets/voice/night-05.opus +0 -0
  130. package/assets/voice/noon-04.opus +0 -0
  131. package/assets/voice/petArm-02.opus +0 -0
  132. package/assets/voice/petCheek-00.opus +0 -0
  133. package/assets/voice/petCheek-01.opus +0 -0
  134. package/assets/voice/petCheek-02.opus +0 -0
  135. package/assets/voice/petHead-01.opus +0 -0
  136. package/assets/voice/petHead-03.opus +0 -0
  137. package/assets/voice/petHead-04.opus +0 -0
  138. package/assets/voice/praised-01.opus +0 -0
  139. package/assets/voice/praised-03.opus +0 -0
  140. package/assets/voice/question-00.opus +0 -0
  141. package/assets/voice/question-03.opus +0 -0
  142. package/assets/voice/question-04.opus +0 -0
  143. package/assets/voice/question-06.opus +0 -0
  144. package/assets/voice/shy-00.opus +0 -0
  145. package/assets/voice/spot-0-0.opus +0 -0
  146. package/assets/voice/spot-0-1.opus +0 -0
  147. package/assets/voice/spot-0-2.opus +0 -0
  148. package/assets/voice/spot-3-0.opus +0 -0
  149. package/assets/voice/spot-4-0.opus +0 -0
  150. package/assets/voice/spot-4-2.opus +0 -0
  151. package/assets/voice/spot-5-2.opus +0 -0
  152. package/assets/voice/spot-6-0.opus +0 -0
  153. package/assets/voice/spot-6-1.opus +0 -0
  154. package/assets/voice/spot-7-0.opus +0 -0
  155. package/assets/voice/spot-7-2.opus +0 -0
  156. package/assets/voice/spot-8-1.opus +0 -0
  157. package/assets/voice/tagent-00.opus +0 -0
  158. package/assets/voice/thinkFast-01.opus +0 -0
  159. package/assets/voice/thinkFast-02.opus +0 -0
  160. package/assets/voice/thinkFast-03.opus +0 -0
  161. package/assets/voice/thinkSlow-01.opus +0 -0
  162. package/assets/voice/thinkSlow-03.opus +0 -0
  163. package/assets/voice/thinking-01.opus +0 -0
  164. package/assets/voice/thinking-02.opus +0 -0
  165. package/assets/voice/thinking-03.opus +0 -0
  166. package/assets/voice/thinking-09.opus +0 -0
  167. package/assets/voice/tread-00.opus +0 -0
  168. package/assets/voice/tread-01.opus +0 -0
  169. package/assets/voice/trun-02.opus +0 -0
  170. package/assets/voice/tsun-00.opus +0 -0
  171. package/assets/voice/tsun-01.opus +0 -0
  172. package/assets/voice/tsun-04.opus +0 -0
  173. package/assets/voice/twrite-00.opus +0 -0
  174. package/assets/voice/unknown-01.opus +0 -0
  175. package/assets/voice/waitApproval-00.opus +0 -0
  176. package/assets/voice/waitApproval-01.opus +0 -0
  177. package/assets/voice/waitApproval-02.opus +0 -0
  178. package/assets/voice/waitApproval-03.opus +0 -0
  179. package/assets/voice/waitApprovalLate-02.opus +0 -0
  180. package/assets/voice/waitApprovalLate-03.opus +0 -0
  181. package/assets/voice/waitAsk-00.opus +0 -0
  182. package/assets/voice/waitAsk-02.opus +0 -0
  183. package/assets/voice/waitAskLate-03.opus +0 -0
  184. package/assets/voice/wake-03.opus +0 -0
  185. package/assets/voice/wake-04.opus +0 -0
  186. package/assets/voice/wake-05.opus +0 -0
  187. package/assets/voice/wake-06.opus +0 -0
  188. package/lib/client.js +7 -7
  189. package/lib/features/notes.js +172 -172
  190. package/lib/features/reminder.js +216 -216
  191. package/lib/l2d-plan.js +295 -295
  192. package/lib/l2d-tools.js +293 -293
  193. package/lib/ledger.js +163 -163
  194. package/lib/mys/config.js +183 -183
  195. package/lib/mys/ds.js +209 -209
  196. package/lib/mys/http.js +85 -85
  197. package/lib/mys/mys.js +419 -419
  198. package/lib/mys/qr.js +434 -434
  199. package/lib/mys/scheduler.js +206 -206
  200. package/lib/mys/store.js +248 -248
  201. package/lib/mys/tz.js +174 -174
  202. package/lib/wavtrim.js +245 -245
  203. package/lib/worldbook/NOTICE.md +18 -18
  204. package/lib/worldbook/data/Cyrene.md +111 -111
  205. package/lib/worldbook/data/_glossary.md +181 -181
  206. package/lib/worldbook/data/characters.md +289 -289
  207. package/lib/worldbook/data/story.md +130 -130
  208. package/lib/worldbook/data/world.md +182 -182
  209. package/lib/worldbook.js +189 -189
  210. package/package.json +72 -69
package/lib/mys/ds.js CHANGED
@@ -1,209 +1,209 @@
1
- /**
2
- * Request signing and the header set every miHoYo endpoint expects.
3
- *
4
- * Two signatures exist and the endpoint decides which one applies:
5
- * - DS1 (`x-rpc-client_type: 5`) guards the luna check-in endpoints and the
6
- * binding query: `md5("salt=<salt>&t=<t>&r=<6 random chars>")`.
7
- * - DS2 (`x-rpc-client_type: 2`) guards the BBS app endpoints and folds the
8
- * raw body and the sorted query string into the hash.
9
- *
10
- * The salt is tied to the advertised app version; both travel in the config so
11
- * a future client release can be adopted without touching this file.
12
- */
13
- import { createHash, randomInt } from 'node:crypto'
14
-
15
- const ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
16
-
17
- /** Six-character random token used by DS1. */
18
- export function randomToken(random = Math.random) {
19
- let out = ''
20
- for (let index = 0; index < 6; index += 1) {
21
- out += ALPHABET[Math.floor(random() * ALPHABET.length) % ALPHABET.length]
22
- }
23
- return out
24
- }
25
-
26
- function md5(text) {
27
- return createHash('md5').update(text, 'utf8').digest('hex')
28
- }
29
-
30
- /** Build one DS1 header value: `<t>,<r>,<md5>`. */
31
- export function signDs1({ salt, now = Date.now(), random = Math.random, token }) {
32
- const t = Math.floor(now / 1000)
33
- const r = token ?? randomToken(random)
34
- const hash = md5(`salt=${salt}&t=${t}&r=${r}`)
35
- return `${t},${r},${hash}`
36
- }
37
-
38
- /** Build one DS2 header value: `<t>,<r>,<md5>` over body and sorted query. */
39
- export function signDs2({ salt, body = '', query = '', now = Date.now(), random = Math.random }) {
40
- const t = Math.floor(now / 1000)
41
- const r = randomInt(100001, 200000)
42
- const sortedQuery = query instanceof URLSearchParams ? [...query.entries()].sort(([a], [b]) => a.localeCompare(b)) : query
43
- const queryText = Array.isArray(sortedQuery)
44
- ? sortedQuery.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join('&')
45
- : String(sortedQuery)
46
- const hash = md5(`salt=${salt}&t=${t}&r=${r}&b=${body}&q=${queryText}`)
47
- return `${t},${r},${hash}`
48
- }
49
-
50
- /** The mobile-app user agent matching the advertised client version. */
51
- export function appUserAgent(appVersion) {
52
- return `Mozilla/5.0 (Linux; Android 12; Unspecified Device) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/103.0.5060.129 Mobile Safari/537.36 miHoYoBBS/${appVersion}`
53
- }
54
-
55
- /**
56
- * Headers for a takumi/act API call.
57
- *
58
- * `signgame` is only sent for the games that require it; the others reject an
59
- * unknown value far more often than a missing one.
60
- */
61
- export function apiHeaders({ config, deviceId, signature, cookie, signgame, deviceFp }) {
62
- const { appVersion } = config.client
63
- const { name, model, sysVersion, channel } = config.device
64
- const headers = {
65
- 'User-Agent': appUserAgent(appVersion),
66
- Accept: 'application/json, text/plain, */*',
67
- 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
68
- DS: signature,
69
- 'x-rpc-app_version': appVersion,
70
- 'x-rpc-client_type': '5',
71
- 'x-rpc-device_id': deviceId,
72
- 'x-rpc-device_name': name,
73
- 'x-rpc-device_model': model,
74
- 'x-rpc-sys_version': sysVersion,
75
- 'x-rpc-channel': channel,
76
- 'x-rpc-platform': 'android',
77
- Origin: 'https://act.mihoyo.com',
78
- Referer: 'https://act.mihoyo.com/',
79
- 'X-Requested-With': 'com.mihoyo.hyperion',
80
- }
81
- if (typeof signgame === 'string' && signgame !== '') headers['x-rpc-signgame'] = signgame
82
- if (typeof deviceFp === 'string' && deviceFp !== '') headers['x-rpc-device_fp'] = deviceFp
83
- if (typeof cookie === 'string' && cookie !== '') headers.Cookie = cookie
84
- return headers
85
- }
86
-
87
- /** Headers for the passport web QR-login flow. */
88
- export function passportHeaders({ appVersion, deviceId, cookie }) {
89
- const headers = {
90
- 'User-Agent': appUserAgent(appVersion),
91
- Accept: 'application/json, text/plain, */*',
92
- 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
93
- 'Content-Type': 'application/json',
94
- 'x-rpc-app_id': 'bll8iq97cem8',
95
- 'x-rpc-client_type': '4',
96
- 'x-rpc-device_id': deviceId,
97
- 'x-rpc-app_version': appVersion,
98
- 'x-rpc-channel': 'miyousheluodi',
99
- 'x-rpc-sys_version': '12',
100
- }
101
- if (typeof cookie === 'string' && cookie !== '') headers.Cookie = cookie
102
- return headers
103
- }
104
-
105
- /** Serialize a cookie map into a `Cookie` header value. */
106
- export function cookieHeader(cookies) {
107
- if (cookies === null || typeof cookies !== 'object') return ''
108
- return Object.entries(cookies)
109
- .filter(([, value]) => typeof value === 'string' && value !== '')
110
- .map(([name, value]) => `${name}=${value}`)
111
- .join('; ')
112
- }
113
-
114
- /**
115
- * Parse `Set-Cookie` lines into a name/value map.
116
- *
117
- * The attribute tail is dropped; values may legally contain `=`, so only the
118
- * first separator splits each line.
119
- */
120
- export function parseSetCookie(lines) {
121
- const cookies = {}
122
- for (const line of lines ?? []) {
123
- if (typeof line !== 'string') continue
124
- const head = line.split(';', 1)[0]
125
- const index = head.indexOf('=')
126
- if (index <= 0) continue
127
- const name = head.slice(0, index).trim()
128
- const value = head.slice(index + 1).trim()
129
- if (name === '' || value === '') continue
130
- cookies[name] = value
131
- }
132
- return cookies
133
- }
134
-
135
- /** The subset of cookie names that carry session credentials. */
136
- export const CREDENTIAL_COOKIES = [
137
- 'ltoken_v2',
138
- 'ltoken',
139
- 'ltuid_v2',
140
- 'ltuid',
141
- 'ltmid_v2',
142
- 'ltmid',
143
- 'stoken_v2',
144
- 'stoken',
145
- 'stuid',
146
- 'mid',
147
- 'cookie_token_v2',
148
- 'cookie_token',
149
- 'account_id_v2',
150
- 'account_mid_v2',
151
- 'account_id',
152
- 'login_ticket',
153
- 'login_uid',
154
- ]
155
-
156
- /** Keep only credential cookies, so unrelated tracking cookies stay out of the file. */
157
- export function credentialCookies(cookies) {
158
- const kept = {}
159
- for (const name of CREDENTIAL_COOKIES) {
160
- const value = cookies?.[name]
161
- if (typeof value === 'string' && value !== '') kept[name] = value
162
- }
163
- return kept
164
- }
165
-
166
- /** `true` when the cookie map still carries a usable session token. */
167
- export function hasSession(cookies) {
168
- if (cookies === null || typeof cookies !== 'object') return false
169
- const ltoken = cookies.ltoken_v2 ?? cookies.ltoken
170
- return typeof ltoken === 'string' && ltoken !== ''
171
- }
172
-
173
- /**
174
- * The long-lived stoken carried by the cookie map, or `null` when absent.
175
- *
176
- * A blank value counts as absent: an empty string would otherwise make the UI
177
- * claim the session can be renewed while every refresh call fails with -100.
178
- */
179
- export function sessionStoken(cookies) {
180
- if (cookies === null || typeof cookies !== 'object') return null
181
- const stoken = cookies.stoken_v2 ?? cookies.stoken
182
- return typeof stoken === 'string' && stoken.trim() !== '' ? stoken : null
183
- }
184
-
185
- /**
186
- * Keep every ID cookie the token families can pair with.
187
- *
188
- * V1 tokens (`ltoken`, `cookie_token`) authenticate with `ltuid` / `account_id`;
189
- * V2 tokens pair with `ltmid_v2` / `account_mid_v2`. The passport returns only
190
- * part of that set (the App QR flow returns the V2 ids plus a V1 ltoken derived
191
- * from the stoken), which makes luna endpoints answer -100. Mirroring the known
192
- * account id into the missing aliases keeps both families usable.
193
- */
194
- export function ensureIdentityCookies(cookies, { aid, mid } = {}) {
195
- if (cookies === null || typeof cookies !== 'object') return cookies
196
- const uid = typeof aid === 'string' && aid !== '' ? aid : null
197
- const accountMid = typeof mid === 'string' && mid !== '' ? mid : null
198
- if (uid !== null) {
199
- for (const name of ['ltuid', 'ltuid_v2', 'account_id', 'account_id_v2', 'stuid']) {
200
- if (cookies[name] === undefined) cookies[name] = uid
201
- }
202
- }
203
- if (accountMid !== null) {
204
- for (const name of ['mid', 'ltmid_v2', 'account_mid_v2']) {
205
- if (cookies[name] === undefined) cookies[name] = accountMid
206
- }
207
- }
208
- return cookies
209
- }
1
+ /**
2
+ * Request signing and the header set every miHoYo endpoint expects.
3
+ *
4
+ * Two signatures exist and the endpoint decides which one applies:
5
+ * - DS1 (`x-rpc-client_type: 5`) guards the luna check-in endpoints and the
6
+ * binding query: `md5("salt=<salt>&t=<t>&r=<6 random chars>")`.
7
+ * - DS2 (`x-rpc-client_type: 2`) guards the BBS app endpoints and folds the
8
+ * raw body and the sorted query string into the hash.
9
+ *
10
+ * The salt is tied to the advertised app version; both travel in the config so
11
+ * a future client release can be adopted without touching this file.
12
+ */
13
+ import { createHash, randomInt } from 'node:crypto'
14
+
15
+ const ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
16
+
17
+ /** Six-character random token used by DS1. */
18
+ export function randomToken(random = Math.random) {
19
+ let out = ''
20
+ for (let index = 0; index < 6; index += 1) {
21
+ out += ALPHABET[Math.floor(random() * ALPHABET.length) % ALPHABET.length]
22
+ }
23
+ return out
24
+ }
25
+
26
+ function md5(text) {
27
+ return createHash('md5').update(text, 'utf8').digest('hex')
28
+ }
29
+
30
+ /** Build one DS1 header value: `<t>,<r>,<md5>`. */
31
+ export function signDs1({ salt, now = Date.now(), random = Math.random, token }) {
32
+ const t = Math.floor(now / 1000)
33
+ const r = token ?? randomToken(random)
34
+ const hash = md5(`salt=${salt}&t=${t}&r=${r}`)
35
+ return `${t},${r},${hash}`
36
+ }
37
+
38
+ /** Build one DS2 header value: `<t>,<r>,<md5>` over body and sorted query. */
39
+ export function signDs2({ salt, body = '', query = '', now = Date.now(), random = Math.random }) {
40
+ const t = Math.floor(now / 1000)
41
+ const r = randomInt(100001, 200000)
42
+ const sortedQuery = query instanceof URLSearchParams ? [...query.entries()].sort(([a], [b]) => a.localeCompare(b)) : query
43
+ const queryText = Array.isArray(sortedQuery)
44
+ ? sortedQuery.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join('&')
45
+ : String(sortedQuery)
46
+ const hash = md5(`salt=${salt}&t=${t}&r=${r}&b=${body}&q=${queryText}`)
47
+ return `${t},${r},${hash}`
48
+ }
49
+
50
+ /** The mobile-app user agent matching the advertised client version. */
51
+ export function appUserAgent(appVersion) {
52
+ return `Mozilla/5.0 (Linux; Android 12; Unspecified Device) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/103.0.5060.129 Mobile Safari/537.36 miHoYoBBS/${appVersion}`
53
+ }
54
+
55
+ /**
56
+ * Headers for a takumi/act API call.
57
+ *
58
+ * `signgame` is only sent for the games that require it; the others reject an
59
+ * unknown value far more often than a missing one.
60
+ */
61
+ export function apiHeaders({ config, deviceId, signature, cookie, signgame, deviceFp }) {
62
+ const { appVersion } = config.client
63
+ const { name, model, sysVersion, channel } = config.device
64
+ const headers = {
65
+ 'User-Agent': appUserAgent(appVersion),
66
+ Accept: 'application/json, text/plain, */*',
67
+ 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
68
+ DS: signature,
69
+ 'x-rpc-app_version': appVersion,
70
+ 'x-rpc-client_type': '5',
71
+ 'x-rpc-device_id': deviceId,
72
+ 'x-rpc-device_name': name,
73
+ 'x-rpc-device_model': model,
74
+ 'x-rpc-sys_version': sysVersion,
75
+ 'x-rpc-channel': channel,
76
+ 'x-rpc-platform': 'android',
77
+ Origin: 'https://act.mihoyo.com',
78
+ Referer: 'https://act.mihoyo.com/',
79
+ 'X-Requested-With': 'com.mihoyo.hyperion',
80
+ }
81
+ if (typeof signgame === 'string' && signgame !== '') headers['x-rpc-signgame'] = signgame
82
+ if (typeof deviceFp === 'string' && deviceFp !== '') headers['x-rpc-device_fp'] = deviceFp
83
+ if (typeof cookie === 'string' && cookie !== '') headers.Cookie = cookie
84
+ return headers
85
+ }
86
+
87
+ /** Headers for the passport web QR-login flow. */
88
+ export function passportHeaders({ appVersion, deviceId, cookie }) {
89
+ const headers = {
90
+ 'User-Agent': appUserAgent(appVersion),
91
+ Accept: 'application/json, text/plain, */*',
92
+ 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
93
+ 'Content-Type': 'application/json',
94
+ 'x-rpc-app_id': 'bll8iq97cem8',
95
+ 'x-rpc-client_type': '4',
96
+ 'x-rpc-device_id': deviceId,
97
+ 'x-rpc-app_version': appVersion,
98
+ 'x-rpc-channel': 'miyousheluodi',
99
+ 'x-rpc-sys_version': '12',
100
+ }
101
+ if (typeof cookie === 'string' && cookie !== '') headers.Cookie = cookie
102
+ return headers
103
+ }
104
+
105
+ /** Serialize a cookie map into a `Cookie` header value. */
106
+ export function cookieHeader(cookies) {
107
+ if (cookies === null || typeof cookies !== 'object') return ''
108
+ return Object.entries(cookies)
109
+ .filter(([, value]) => typeof value === 'string' && value !== '')
110
+ .map(([name, value]) => `${name}=${value}`)
111
+ .join('; ')
112
+ }
113
+
114
+ /**
115
+ * Parse `Set-Cookie` lines into a name/value map.
116
+ *
117
+ * The attribute tail is dropped; values may legally contain `=`, so only the
118
+ * first separator splits each line.
119
+ */
120
+ export function parseSetCookie(lines) {
121
+ const cookies = {}
122
+ for (const line of lines ?? []) {
123
+ if (typeof line !== 'string') continue
124
+ const head = line.split(';', 1)[0]
125
+ const index = head.indexOf('=')
126
+ if (index <= 0) continue
127
+ const name = head.slice(0, index).trim()
128
+ const value = head.slice(index + 1).trim()
129
+ if (name === '' || value === '') continue
130
+ cookies[name] = value
131
+ }
132
+ return cookies
133
+ }
134
+
135
+ /** The subset of cookie names that carry session credentials. */
136
+ export const CREDENTIAL_COOKIES = [
137
+ 'ltoken_v2',
138
+ 'ltoken',
139
+ 'ltuid_v2',
140
+ 'ltuid',
141
+ 'ltmid_v2',
142
+ 'ltmid',
143
+ 'stoken_v2',
144
+ 'stoken',
145
+ 'stuid',
146
+ 'mid',
147
+ 'cookie_token_v2',
148
+ 'cookie_token',
149
+ 'account_id_v2',
150
+ 'account_mid_v2',
151
+ 'account_id',
152
+ 'login_ticket',
153
+ 'login_uid',
154
+ ]
155
+
156
+ /** Keep only credential cookies, so unrelated tracking cookies stay out of the file. */
157
+ export function credentialCookies(cookies) {
158
+ const kept = {}
159
+ for (const name of CREDENTIAL_COOKIES) {
160
+ const value = cookies?.[name]
161
+ if (typeof value === 'string' && value !== '') kept[name] = value
162
+ }
163
+ return kept
164
+ }
165
+
166
+ /** `true` when the cookie map still carries a usable session token. */
167
+ export function hasSession(cookies) {
168
+ if (cookies === null || typeof cookies !== 'object') return false
169
+ const ltoken = cookies.ltoken_v2 ?? cookies.ltoken
170
+ return typeof ltoken === 'string' && ltoken !== ''
171
+ }
172
+
173
+ /**
174
+ * The long-lived stoken carried by the cookie map, or `null` when absent.
175
+ *
176
+ * A blank value counts as absent: an empty string would otherwise make the UI
177
+ * claim the session can be renewed while every refresh call fails with -100.
178
+ */
179
+ export function sessionStoken(cookies) {
180
+ if (cookies === null || typeof cookies !== 'object') return null
181
+ const stoken = cookies.stoken_v2 ?? cookies.stoken
182
+ return typeof stoken === 'string' && stoken.trim() !== '' ? stoken : null
183
+ }
184
+
185
+ /**
186
+ * Keep every ID cookie the token families can pair with.
187
+ *
188
+ * V1 tokens (`ltoken`, `cookie_token`) authenticate with `ltuid` / `account_id`;
189
+ * V2 tokens pair with `ltmid_v2` / `account_mid_v2`. The passport returns only
190
+ * part of that set (the App QR flow returns the V2 ids plus a V1 ltoken derived
191
+ * from the stoken), which makes luna endpoints answer -100. Mirroring the known
192
+ * account id into the missing aliases keeps both families usable.
193
+ */
194
+ export function ensureIdentityCookies(cookies, { aid, mid } = {}) {
195
+ if (cookies === null || typeof cookies !== 'object') return cookies
196
+ const uid = typeof aid === 'string' && aid !== '' ? aid : null
197
+ const accountMid = typeof mid === 'string' && mid !== '' ? mid : null
198
+ if (uid !== null) {
199
+ for (const name of ['ltuid', 'ltuid_v2', 'account_id', 'account_id_v2', 'stuid']) {
200
+ if (cookies[name] === undefined) cookies[name] = uid
201
+ }
202
+ }
203
+ if (accountMid !== null) {
204
+ for (const name of ['mid', 'ltmid_v2', 'account_mid_v2']) {
205
+ if (cookies[name] === undefined) cookies[name] = accountMid
206
+ }
207
+ }
208
+ return cookies
209
+ }
package/lib/mys/http.js CHANGED
@@ -1,85 +1,85 @@
1
- /**
2
- * Minimal JSON-over-HTTP client.
3
- *
4
- * Node's global `fetch` already handles TLS, proxies from the environment, and
5
- * decompression; this wrapper adds the three things every miHoYo call needs:
6
- * a hard timeout, capture of `Set-Cookie`, and one jittered retry for transport
7
- * failures (never for an HTTP error status — a refusal must not be retried).
8
- */
9
-
10
- export class HttpError extends Error {
11
- constructor(message, { url, status, body, cause } = {}) {
12
- super(message, cause === undefined ? undefined : { cause })
13
- this.name = 'HttpError'
14
- this.url = url
15
- this.status = status
16
- this.body = body
17
- }
18
- }
19
-
20
- function sleep(ms) {
21
- return new Promise((resolve) => {
22
- const timer = setTimeout(resolve, ms)
23
- if (typeof timer.unref === 'function') timer.unref()
24
- })
25
- }
26
-
27
- /**
28
- * Perform one request and decode the JSON body when there is one.
29
- *
30
- * @param {string} url
31
- * @param {object} [options]
32
- * @param {string} [options.method] - HTTP verb, default GET.
33
- * @param {Record<string, string>} [options.headers]
34
- * @param {string} [options.body] - already-serialized request body.
35
- * @param {number} [options.timeoutMs] - per-attempt timeout.
36
- * @param {number} [options.retries] - transport-failure retries.
37
- * @param {() => number} [options.random] - jitter source.
38
- * @returns {Promise<{status: number, ok: boolean, headers: Headers, setCookies: string[], text: string, json: any}>}
39
- */
40
- export async function request(url, options = {}) {
41
- const {
42
- method = 'GET',
43
- headers = {},
44
- body,
45
- timeoutMs = 20000,
46
- retries = 1,
47
- random = Math.random,
48
- } = options
49
-
50
- let attempt = 0
51
- let lastError
52
- while (attempt <= retries) {
53
- attempt += 1
54
- try {
55
- const response = await fetch(url, {
56
- method,
57
- headers,
58
- body,
59
- redirect: 'manual',
60
- signal: AbortSignal.timeout(timeoutMs),
61
- })
62
- const text = await response.text()
63
- let json
64
- try {
65
- json = text === '' ? undefined : JSON.parse(text)
66
- } catch {
67
- json = undefined
68
- }
69
- const setCookies = typeof response.headers.getSetCookie === 'function'
70
- ? response.headers.getSetCookie()
71
- : []
72
- return { status: response.status, ok: response.ok, headers: response.headers, setCookies, text, json }
73
- } catch (error) {
74
- lastError = error
75
- if (attempt > retries) break
76
- // Transport jitter keeps repeated attempts from arriving in lockstep.
77
- await sleep(600 + Math.floor(random() * 900))
78
- }
79
- }
80
- const reason = lastError?.name === 'TimeoutError' ? '请求超时' : String(lastError?.message ?? lastError)
81
- throw new HttpError(`${reason}(${method} ${url})`, { url, cause: lastError })
82
- }
83
-
84
- /** Sleep helper shared by the pacing code. */
85
- export { sleep }
1
+ /**
2
+ * Minimal JSON-over-HTTP client.
3
+ *
4
+ * Node's global `fetch` already handles TLS, proxies from the environment, and
5
+ * decompression; this wrapper adds the three things every miHoYo call needs:
6
+ * a hard timeout, capture of `Set-Cookie`, and one jittered retry for transport
7
+ * failures (never for an HTTP error status — a refusal must not be retried).
8
+ */
9
+
10
+ export class HttpError extends Error {
11
+ constructor(message, { url, status, body, cause } = {}) {
12
+ super(message, cause === undefined ? undefined : { cause })
13
+ this.name = 'HttpError'
14
+ this.url = url
15
+ this.status = status
16
+ this.body = body
17
+ }
18
+ }
19
+
20
+ function sleep(ms) {
21
+ return new Promise((resolve) => {
22
+ const timer = setTimeout(resolve, ms)
23
+ if (typeof timer.unref === 'function') timer.unref()
24
+ })
25
+ }
26
+
27
+ /**
28
+ * Perform one request and decode the JSON body when there is one.
29
+ *
30
+ * @param {string} url
31
+ * @param {object} [options]
32
+ * @param {string} [options.method] - HTTP verb, default GET.
33
+ * @param {Record<string, string>} [options.headers]
34
+ * @param {string} [options.body] - already-serialized request body.
35
+ * @param {number} [options.timeoutMs] - per-attempt timeout.
36
+ * @param {number} [options.retries] - transport-failure retries.
37
+ * @param {() => number} [options.random] - jitter source.
38
+ * @returns {Promise<{status: number, ok: boolean, headers: Headers, setCookies: string[], text: string, json: any}>}
39
+ */
40
+ export async function request(url, options = {}) {
41
+ const {
42
+ method = 'GET',
43
+ headers = {},
44
+ body,
45
+ timeoutMs = 20000,
46
+ retries = 1,
47
+ random = Math.random,
48
+ } = options
49
+
50
+ let attempt = 0
51
+ let lastError
52
+ while (attempt <= retries) {
53
+ attempt += 1
54
+ try {
55
+ const response = await fetch(url, {
56
+ method,
57
+ headers,
58
+ body,
59
+ redirect: 'manual',
60
+ signal: AbortSignal.timeout(timeoutMs),
61
+ })
62
+ const text = await response.text()
63
+ let json
64
+ try {
65
+ json = text === '' ? undefined : JSON.parse(text)
66
+ } catch {
67
+ json = undefined
68
+ }
69
+ const setCookies = typeof response.headers.getSetCookie === 'function'
70
+ ? response.headers.getSetCookie()
71
+ : []
72
+ return { status: response.status, ok: response.ok, headers: response.headers, setCookies, text, json }
73
+ } catch (error) {
74
+ lastError = error
75
+ if (attempt > retries) break
76
+ // Transport jitter keeps repeated attempts from arriving in lockstep.
77
+ await sleep(600 + Math.floor(random() * 900))
78
+ }
79
+ }
80
+ const reason = lastError?.name === 'TimeoutError' ? '请求超时' : String(lastError?.message ?? lastError)
81
+ throw new HttpError(`${reason}(${method} ${url})`, { url, cause: lastError })
82
+ }
83
+
84
+ /** Sleep helper shared by the pacing code. */
85
+ export { sleep }