179 lines
7.4 KiB
TypeScript
179 lines
7.4 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
import test from 'node:test'
|
|
|
|
import { buildHuyaLoginContext, ensureHuyaWebCookieIdentity, x64hash128 } from './cookie.js'
|
|
import { HuyaNativeConnector } from './huya-native-connector.js'
|
|
import { parseBindStruct } from './huya-rpc.js'
|
|
|
|
const BIND_RESP_HEX =
|
|
'0a0100c816002a0001100120013a060016002602717136004612e6b3aae6958fe6849fe' +
|
|
'79a84e59b9ee5bf86560066000b4a06001612e6b3aae99ba8e585a8e698afe8b08ee8a8' +
|
|
'802604e6898b51360be88bb9e69e9c28694f53294600560066000b526a2595806c0b36' +
|
|
'0ce5928ce5b9b3e7b2bee88bb1460050070b'
|
|
|
|
test('QIMEI x64hash128 与 SDK 向量一致', () => {
|
|
assert.equal(x64hash128('', 31), '24700f9f1986800ab4fcc880530dd0ed')
|
|
assert.equal(x64hash128('a', 31), 'f8b09930c1df727ef75677a1d6ae5bd6')
|
|
assert.equal(x64hash128('abc', 31), '31844fd43df93041e52bcddd13312671')
|
|
})
|
|
|
|
test('登录 context 使用网页设备 Cookie 的 guiddata、yamid 和 guid', () => {
|
|
const identity = ensureHuyaWebCookieIdentity({
|
|
cookie:
|
|
'udb_guiddata=838ed427bbdb491d8e6f6782addb0364; ' +
|
|
'__yamid_new=CBE2807148F00001332411CB1E296A00; ' +
|
|
'guid=0a7dee10017ef16aad0229302a3029e3',
|
|
guid: 'ignored-because-existing',
|
|
})
|
|
assert.equal(
|
|
identity.context,
|
|
'WB-838ed427bbdb491d8e6f6782addb0364-CBE2807148F00001332411CB1E296A00-0a7dee10017ef16aad0229302a3029e3',
|
|
)
|
|
assert.equal(buildHuyaLoginContext(identity.cookie), identity.context)
|
|
assert.match(identity.cookie, /(?:^|; )udb_appid=5002(?:;|$)/)
|
|
})
|
|
|
|
test('真实虎牙绑定响应尾部新增 ZERO 字段仍可解析', () => {
|
|
const response = parseBindStruct(Buffer.from(BIND_RESP_HEX, 'hex'))
|
|
assert.equal(response.status, 200)
|
|
assert.equal(response.gameName, '和平精英')
|
|
assert.equal(response.accountData.gameAccount.nick, '泪敏感的回忆')
|
|
assert.equal(response.accountData.gameRole.roleName, '泪雨全是谎言')
|
|
assert.equal(response.accountData.gameRole.areaName, '手Q')
|
|
assert.equal(response.accountData.gameRole.platName, '苹果(iOS)')
|
|
})
|
|
|
|
test('内置 Connector 完成扫码、CK 兑换和一次性票据且不泄露 CK', async () => {
|
|
const requests: Array<{ url: string; body: string; headers: Record<string, string> }> = []
|
|
let stage = 0
|
|
let now = Date.now()
|
|
const fetchImpl = (async (urlInput: string | URL | Request, init?: RequestInit) => {
|
|
const url = String(urlInput)
|
|
const body = typeof init?.body === 'string' ? init.body : ''
|
|
const headers = Object.fromEntries(new Headers(init?.headers).entries())
|
|
requests.push({ url, body, headers })
|
|
if (url.includes('/web/middle/')) {
|
|
return new Response('<html></html>', {
|
|
status: 200,
|
|
headers: { 'content-type': 'text/html;charset=UTF-8' },
|
|
})
|
|
}
|
|
if (url.endsWith('/qrLgn/getQrId')) {
|
|
return new Response(JSON.stringify({ returnCode: 0, data: { qrId: 'qr-test-1' } }), {
|
|
status: 200,
|
|
headers: { 'content-type': 'application/json' },
|
|
})
|
|
}
|
|
if (url.includes('/qrLgn/getQrImg')) {
|
|
return new Response(Buffer.from('fake-png'), {
|
|
status: 200,
|
|
headers: { 'content-type': 'image/png' },
|
|
})
|
|
}
|
|
if (url.endsWith('/qrLgn/tryQrLogin')) {
|
|
const headers = new Headers({ 'content-type': 'application/json' })
|
|
if (stage >= 2) {
|
|
headers.append('set-cookie', 'udb_cred=server-issued-cred; Path=/; Domain=huya.com')
|
|
headers.append('set-cookie', 'udb_uid=123456; Path=/; Domain=huya.com')
|
|
headers.append('set-cookie', 'yyuid=123456; Path=/; Domain=huya.com')
|
|
headers.append('set-cookie', 'udb_passport=hy_123456; Path=/; Domain=huya.com')
|
|
headers.append('set-cookie', 'username=hy_123456; Path=/; Domain=huya.com')
|
|
}
|
|
return new Response(
|
|
JSON.stringify({
|
|
returnCode: 0,
|
|
data:
|
|
stage >= 2
|
|
? {
|
|
stage: 2,
|
|
uid: 123456,
|
|
passport: 'hy_123456',
|
|
nickname: '测试用户',
|
|
biztoken: 'secret-biztoken',
|
|
}
|
|
: { stage },
|
|
}),
|
|
{ status: 200, headers },
|
|
)
|
|
}
|
|
if (url.endsWith('/web/cookie/verify')) {
|
|
return new Response(JSON.stringify({ returnCode: 0, data: null }), {
|
|
status: 200,
|
|
headers: { 'content-type': 'application/json' },
|
|
})
|
|
}
|
|
throw new Error(`unexpected request: ${url}`)
|
|
}) as typeof fetch
|
|
|
|
const connector = new HuyaNativeConnector({
|
|
fetchImpl,
|
|
getFingerprint: async () => ({
|
|
sdid: '0UnHUgv0_qmfD4KAKlwzhqTZaMGpYcbggchzyjwi8f6G4SHn50pqpnCn8hbYX2xJ2ahI0jmPutYTHv17Q0HdNPxBBhH9f3lfLj955gZ0iFULWVkn9LtfFJw_Qo4kgKr8OZHDqNnuwg612sGyflFn1dotW4-PHrIqa6GBd1hvMDgYhO03aCkuTa7UwhiUlpluU',
|
|
hdid: '28744de42736586c8a20e59b5ce9c5f5146fb5e8',
|
|
cookies: {
|
|
udb_appid: '5008',
|
|
game_did: '4p5Gmgx92o-TZA5lK3bgimTfc3p9Wbw6Vl2',
|
|
_qimei_uuid42: '1ab09100209100ee4d631bc545468c6addc489a95a',
|
|
__yamid_new: 'CBE2807148F00001332411CB1E296A00',
|
|
udb_guiddata: '838ed427bbdb491d8e6f6782addb0364',
|
|
guid: '0a7dee10017ef16aad0229302a3029e3',
|
|
},
|
|
source: 'fingerprint',
|
|
}),
|
|
now: () => now,
|
|
stateRoot: path.join(os.tmpdir(), 'huya-native-test'),
|
|
})
|
|
|
|
const created = await connector.createLoginSession({ taskId: 1, orderNo: 'ORDER-1' })
|
|
assert.equal(created.status, 'waiting_scan')
|
|
assert.equal(created.hasCredential, false)
|
|
assert.equal(
|
|
created.qrImage,
|
|
`data:image/png;base64,${Buffer.from('fake-png').toString('base64')}`,
|
|
)
|
|
const getQrId = requests.find((item) => item.url.endsWith('/qrLgn/getQrId'))
|
|
assert.ok(getQrId)
|
|
const envelope = JSON.parse(getQrId.body) as Record<string, unknown>
|
|
assert.match(String(envelope.sdid), /^csid_[0-9a-f]{32}$/)
|
|
assert.equal(
|
|
envelope.context,
|
|
'WB-838ed427bbdb491d8e6f6782addb0364-CBE2807148F00001332411CB1E296A00-0a7dee10017ef16aad0229302a3029e3',
|
|
)
|
|
assert.equal(getQrId.headers.reqid, envelope.requestId)
|
|
|
|
stage = 0
|
|
assert.equal((await connector.getLoginSession(created.sessionId)).status, 'waiting_scan')
|
|
stage = 1
|
|
assert.equal((await connector.getLoginSession(created.sessionId)).status, 'scanned')
|
|
stage = 2
|
|
const authenticated = await connector.getLoginSession(created.sessionId)
|
|
assert.equal(authenticated.status, 'authenticated')
|
|
assert.equal(authenticated.huyaUid, '123456')
|
|
assert.equal(authenticated.nickname, '测试用户')
|
|
assert.equal(JSON.stringify(authenticated).includes('secret-biztoken'), false)
|
|
assert.equal(JSON.stringify(authenticated).includes('server-issued-cred'), false)
|
|
|
|
const ticket = await connector.createBrowserTicket({
|
|
sessionId: created.sessionId,
|
|
targetUrl: 'https://www.huya.com/',
|
|
})
|
|
assert.match(ticket.ticket, /^native-ticket-/)
|
|
assert.equal(ticket.ticket.includes('secret-biztoken'), false)
|
|
const payload = await connector.consumeBrowserTicket(ticket.ticket)
|
|
assert.equal(payload.platform, 'huya')
|
|
assert.equal(
|
|
payload.cookies.find((item) => item.name === 'udb_cred')?.value,
|
|
'server-issued-cred',
|
|
)
|
|
await assert.rejects(() => connector.consumeBrowserTicket(ticket.ticket), /不存在或已过期/)
|
|
|
|
const expiring = await connector.createBrowserTicket({ sessionId: created.sessionId })
|
|
now += 2 * 60 * 1000 + 1
|
|
await assert.rejects(() => connector.consumeBrowserTicket(expiring.ticket), /不存在或已过期/)
|
|
|
|
await connector.revokeSession(created.sessionId)
|
|
await assert.rejects(() => connector.getLoginSession(created.sessionId), /不存在或已过期/)
|
|
})
|