完善半自动真实履约链路及虎牙账号资料保存
This commit is contained in:
@@ -157,6 +157,10 @@ function extractInt(value: unknown): number {
|
||||
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0
|
||||
}
|
||||
|
||||
function errorMessage(error: unknown): string {
|
||||
return error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
|
||||
function readString(value: unknown): string {
|
||||
return String(value ?? '').trim()
|
||||
}
|
||||
@@ -680,13 +684,13 @@ export class HuyaNativeConnector {
|
||||
errorCode: 'huya_livelink_param_failed',
|
||||
})
|
||||
}
|
||||
const profileResponse = await rpc.getUserProfileBatch({
|
||||
const profile = await rpc.getUserProfile({
|
||||
uid: session.uid,
|
||||
cookie: session.cookie,
|
||||
targetUids: [session.uid],
|
||||
targetUid: session.uid,
|
||||
})
|
||||
const profile = profileResponse?.profiles[0]
|
||||
const nickname = profile?.nick || profile?.passport || session.nickname
|
||||
session.nickname = nickname
|
||||
const avatar = profile?.avatar || ''
|
||||
const redirectUrl =
|
||||
'https://zt.huya.com/b02faae1/pc/index.html' +
|
||||
@@ -972,7 +976,7 @@ export class HuyaNativeConnector {
|
||||
data.username ??
|
||||
cookieValue(responseCookie, 'udb_passport'),
|
||||
) || ''
|
||||
const nickname = readString(data.nickname ?? data.nickName ?? data.nick ?? username) || username
|
||||
let nickname = readString(data.nickname ?? data.nickName ?? data.nick ?? username) || username
|
||||
session.deviceInfo.sdid = session.deviceInfo.sdid || session.sdid
|
||||
session.deviceInfo.hdid = session.deviceInfo.hdid || session.hdid
|
||||
try {
|
||||
@@ -1005,6 +1009,23 @@ export class HuyaNativeConnector {
|
||||
qimeiFeature: qimeiFeatureFromDeviceProfile(session.deviceInfo),
|
||||
})
|
||||
session.uid = uid
|
||||
const profile = await new HuyaRpcClient({
|
||||
fetchImpl: this.fetchImpl,
|
||||
timeoutMs: this.rpcTimeoutMs,
|
||||
})
|
||||
.getUserProfile({
|
||||
uid,
|
||||
cookie: session.cookie,
|
||||
targetUid: uid,
|
||||
})
|
||||
.catch((error) => {
|
||||
logWarn('huya/profile', '登录后查询虎牙用户资料失败,回退登录返回昵称', {
|
||||
uid,
|
||||
error: errorMessage(error),
|
||||
})
|
||||
return null
|
||||
})
|
||||
nickname = readString(profile?.nick || profile?.passport || nickname) || nickname
|
||||
session.nickname = nickname
|
||||
session.status = 'authenticated'
|
||||
session.smsSessionData = ''
|
||||
|
||||
@@ -8,6 +8,7 @@ import { buildHuyaLoginContext, ensureHuyaWebCookieIdentity, x64hash128 } from '
|
||||
import { HuyaNativeConnector } from './huya-native-connector.js'
|
||||
import { detectHuyaSliderDistance } from './huya-sms-role.js'
|
||||
import { hasExistingHuyaBinding, parseBindStruct } from './huya-rpc.js'
|
||||
import { TAF_TYPE, TafWriter } from './taf.js'
|
||||
|
||||
const BIND_RESP_HEX =
|
||||
'0a0100c816002a0001100120013a060016002602717136004612e6b3aae6958fe6849fe' +
|
||||
@@ -15,6 +16,40 @@ const BIND_RESP_HEX =
|
||||
'802604e6898b51360be88bb9e69e9c28694f53294600560066000b526a2595806c0b36' +
|
||||
'0ce5928ce5b9b3e7b2bee88bb1460050070b'
|
||||
|
||||
function buildHuyaProfileResponse(input: {
|
||||
uid: number
|
||||
nickname: string
|
||||
avatar?: string
|
||||
passport?: string
|
||||
}): Buffer {
|
||||
const profileWriter = new TafWriter()
|
||||
profileWriter.writeStruct(0, (writer) => {
|
||||
writer.writeInt64(0, input.uid)
|
||||
writer.writeString(1, input.nickname)
|
||||
writer.writeString(2, input.avatar || '')
|
||||
writer.writeString(17, input.passport || String(input.uid))
|
||||
})
|
||||
|
||||
const dataWriter = new TafWriter()
|
||||
dataWriter.writeHead(0, TAF_TYPE.MAP)
|
||||
dataWriter.writeInt32(0, 1)
|
||||
dataWriter.writeString(0, 'tRsp')
|
||||
dataWriter.writeBytes(1, profileWriter.toBuffer())
|
||||
|
||||
const responseWriter = new TafWriter()
|
||||
responseWriter.writeInt16(1, 3)
|
||||
responseWriter.writeInt8(2, 0)
|
||||
responseWriter.writeInt32(3, 0)
|
||||
responseWriter.writeInt32(4, 1)
|
||||
responseWriter.writeString(5, 'huyauserui')
|
||||
responseWriter.writeString(6, 'getUserProfile')
|
||||
responseWriter.writeBytes(7, dataWriter.toBuffer())
|
||||
responseWriter.writeInt32(8, 3000)
|
||||
responseWriter.writeMap(9, {})
|
||||
responseWriter.writeMap(10, {})
|
||||
return responseWriter.toBuffer()
|
||||
}
|
||||
|
||||
test('QIMEI x64hash128 与 SDK 向量一致', () => {
|
||||
assert.equal(x64hash128('', 31), '24700f9f1986800ab4fcc880530dd0ed')
|
||||
assert.equal(x64hash128('a', 31), 'f8b09930c1df727ef75677a1d6ae5bd6')
|
||||
@@ -182,6 +217,7 @@ test('已绑定账号和角色时直接使用当前绑定,不再发起换绑',
|
||||
test('内置 Connector 完成扫码、CK 兑换和一次性票据且不泄露 CK', async () => {
|
||||
const requests: Array<{ url: string; body: string; headers: Record<string, string> }> = []
|
||||
let stage = 0
|
||||
let profileCalls = 0
|
||||
let now = Date.now()
|
||||
const fetchImpl = (async (urlInput: string | URL | Request, init?: RequestInit) => {
|
||||
const url = String(urlInput)
|
||||
@@ -238,6 +274,18 @@ test('内置 Connector 完成扫码、CK 兑换和一次性票据且不泄露 CK
|
||||
headers: { 'content-type': 'application/json' },
|
||||
})
|
||||
}
|
||||
if (url.startsWith('https://cdnws.api.huya.com/')) {
|
||||
profileCalls += 1
|
||||
return new Response(
|
||||
buildHuyaProfileResponse({
|
||||
uid: 123456,
|
||||
nickname: '扫码资料昵称',
|
||||
avatar: 'https://example.com/avatar.jpg',
|
||||
passport: 'hy_123456',
|
||||
}),
|
||||
{ status: 200, headers: { 'content-type': 'application/octet-stream' } },
|
||||
)
|
||||
}
|
||||
throw new Error(`unexpected request: ${url}`)
|
||||
}) as typeof fetch
|
||||
|
||||
@@ -285,13 +333,14 @@ test('内置 Connector 完成扫码、CK 兑换和一次性票据且不泄露 CK
|
||||
const authenticated = await connector.getLoginSession(created.sessionId)
|
||||
assert.equal(authenticated.status, 'authenticated')
|
||||
assert.equal(authenticated.huyaUid, '123456')
|
||||
assert.equal(authenticated.nickname, '测试用户')
|
||||
assert.equal(authenticated.nickname, '扫码资料昵称')
|
||||
assert.equal(profileCalls, 1)
|
||||
assert.equal(JSON.stringify(authenticated).includes('secret-biztoken'), false)
|
||||
assert.equal(JSON.stringify(authenticated).includes('server-issued-cred'), false)
|
||||
|
||||
const credential = connector.exportSessionCredential(created.sessionId)
|
||||
assert.equal(credential.platformUid, '123456')
|
||||
assert.equal(credential.nickname, '测试用户')
|
||||
assert.equal(credential.nickname, '扫码资料昵称')
|
||||
assert.equal(
|
||||
credential.cookies.find((item) => item.name === 'udb_cred')?.value,
|
||||
'server-issued-cred',
|
||||
@@ -321,6 +370,7 @@ test('内置 Connector 完成扫码、CK 兑换和一次性票据且不泄露 CK
|
||||
|
||||
test('内置 Connector 手动发码并完成短信登录后保存账号 CK', async () => {
|
||||
const requests: Array<{ url: string; body: string }> = []
|
||||
let profileCalls = 0
|
||||
const fetchImpl = (async (urlInput: string | URL | Request, init?: RequestInit) => {
|
||||
const url = String(urlInput)
|
||||
const body = typeof init?.body === 'string' ? init.body : ''
|
||||
@@ -382,6 +432,17 @@ test('内置 Connector 手动发码并完成短信登录后保存账号 CK', asy
|
||||
headers: { 'content-type': 'application/json' },
|
||||
})
|
||||
}
|
||||
if (url.startsWith('https://cdnws.api.huya.com/')) {
|
||||
profileCalls += 1
|
||||
return new Response(
|
||||
buildHuyaProfileResponse({
|
||||
uid: 987654,
|
||||
nickname: '短信资料昵称',
|
||||
passport: 'hy_987654',
|
||||
}),
|
||||
{ status: 200, headers: { 'content-type': 'application/octet-stream' } },
|
||||
)
|
||||
}
|
||||
throw new Error(`unexpected request: ${url}`)
|
||||
}) as typeof fetch
|
||||
|
||||
@@ -428,11 +489,13 @@ test('内置 Connector 手动发码并完成短信登录后保存账号 CK', asy
|
||||
})
|
||||
assert.equal(authenticated.status, 'authenticated')
|
||||
assert.equal(authenticated.huyaUid, '987654')
|
||||
assert.equal(authenticated.nickname, '短信登录用户')
|
||||
assert.equal(authenticated.nickname, '短信资料昵称')
|
||||
assert.equal(profileCalls, 1)
|
||||
assert.equal(JSON.stringify(authenticated).includes('sms-session-secret'), false)
|
||||
|
||||
const credential = connector.exportSessionCredential(sent.session.sessionId)
|
||||
assert.equal(credential.platformUid, '987654')
|
||||
assert.equal(credential.nickname, '短信资料昵称')
|
||||
assert.equal(
|
||||
credential.cookies.find((item) => item.name === 'udb_cred')?.value,
|
||||
'server-issued-sms-cred',
|
||||
|
||||
@@ -421,6 +421,10 @@ function parseProfile(reader: TafReader): HuyaUserProfile {
|
||||
return { uid, nick, avatar, passport }
|
||||
}
|
||||
|
||||
function parseProfileResponse(reader: TafReader): HuyaUserProfile | null {
|
||||
return parseProfile(reader)
|
||||
}
|
||||
|
||||
function parseProfileBatch(reader: TafReader): HuyaUserProfileResponse {
|
||||
const profiles =
|
||||
reader.readList<HuyaUserProfile>(0, (itemReader, dataType) => {
|
||||
@@ -543,6 +547,26 @@ export class HuyaRpcClient {
|
||||
return response ? readWupStruct(response, parseProfileBatch) : null
|
||||
}
|
||||
|
||||
async getUserProfile(input: {
|
||||
uid: number
|
||||
cookie: string
|
||||
targetUid: number
|
||||
}): Promise<HuyaUserProfile | null> {
|
||||
const targetUid = Math.trunc(input.targetUid)
|
||||
if (!(targetUid > 0)) return null
|
||||
const response = await this.callRpc(
|
||||
'huyauserui',
|
||||
'getUserProfile',
|
||||
input.uid,
|
||||
input.cookie,
|
||||
(writer) => {
|
||||
this.writeActivityUser(writer, 0, input.cookie)
|
||||
writer.writeInt64(1, targetUid)
|
||||
},
|
||||
)
|
||||
return response ? readWupStruct(response, parseProfileResponse) : null
|
||||
}
|
||||
|
||||
private async callRpc(
|
||||
servant: string,
|
||||
func: string,
|
||||
|
||||
Reference in New Issue
Block a user