完善虎牙短信登录与图形安全验证
This commit is contained in:
@@ -368,9 +368,9 @@ export async function createSemiAutoMock(
|
||||
platform === 'weibo'
|
||||
? '真实登录:使用微博绑定手机号接收验证码登录,或使用微博 App 扫码登录。登录信息仅用于本次订单履约。'
|
||||
: connectorMode === 'mock'
|
||||
? 'Mock 登录:点击获取二维码后,页面轮询两次会自动模拟登录成功。'
|
||||
: '真实登录:点击获取二维码后,请使用虎牙 App 扫码并在手机上确认登录。',
|
||||
...(platform === 'weibo' ? { loginMethods: ['sms', 'qr'] } : {}),
|
||||
? 'Mock 登录:使用手机号发送验证码后输入 123456,或点击获取二维码模拟登录。'
|
||||
: '真实登录:使用虎牙绑定手机号接收验证码登录,或使用虎牙 App 扫码登录。',
|
||||
loginMethods: ['sms', 'qr'],
|
||||
browserTargets: [
|
||||
{
|
||||
key: platform,
|
||||
|
||||
@@ -9,8 +9,10 @@ import {
|
||||
exportMockSessionCredential,
|
||||
getMockBinding,
|
||||
getMockLoginSession,
|
||||
loginMockHuyaSms,
|
||||
resetMockSessions,
|
||||
revokeMockSession,
|
||||
sendMockHuyaSmsCode,
|
||||
setMockBindingStatus,
|
||||
setMockLoginStatus,
|
||||
startMockBinding,
|
||||
@@ -87,3 +89,27 @@ test('Mock Connector 支持控制状态、撤销会话和重置', () => {
|
||||
assert.equal(reset.cleared, 1)
|
||||
assert.throws(() => getMockLoginSession(second.sessionId), /不存在或已失效/)
|
||||
})
|
||||
|
||||
test('Mock Connector 支持手动虎牙短信发码与验证码登录', () => {
|
||||
const sent = sendMockHuyaSmsCode({
|
||||
taskId: 303,
|
||||
orderNo: 'MOCK-SA-SMS',
|
||||
mobile: '13800138000',
|
||||
})
|
||||
assert.equal(sent.status, 'waiting_code')
|
||||
assert.equal(sent.qrContent, '')
|
||||
assert.equal(sent.hasCredential, false)
|
||||
assert.throws(
|
||||
() => loginMockHuyaSms({ sessionId: sent.sessionId, mobile: '13800138000', code: '000000' }),
|
||||
/验证码错误或已过期/,
|
||||
)
|
||||
|
||||
const authenticated = loginMockHuyaSms({
|
||||
sessionId: sent.sessionId,
|
||||
mobile: '13800138000',
|
||||
code: '123456',
|
||||
})
|
||||
assert.equal(authenticated.status, 'authenticated')
|
||||
assert.equal(authenticated.hasCredential, true)
|
||||
assert.ok(authenticated.huyaUid)
|
||||
})
|
||||
|
||||
@@ -40,6 +40,8 @@ type MockSession = {
|
||||
loginExpiresAtMs: number
|
||||
credentialExpiresAtMs: number
|
||||
qrContent: string
|
||||
smsPhone: string
|
||||
smsCode: string
|
||||
huyaUid: string
|
||||
nickname: string
|
||||
hasCredential: boolean
|
||||
@@ -144,6 +146,8 @@ function bindingView(session: MockSession): ConnectorBindingSession {
|
||||
function markAuthenticated(session: MockSession) {
|
||||
session.loginStatus = 'authenticated'
|
||||
session.hasCredential = true
|
||||
session.smsPhone = ''
|
||||
session.smsCode = ''
|
||||
session.huyaUid = buildMockUid(session.sessionId)
|
||||
session.nickname = 'Mock虎牙用户'
|
||||
session.credentialExpiresAtMs = Date.now() + CREDENTIAL_TTL_MS
|
||||
@@ -174,6 +178,8 @@ export function createMockLoginSession(input: {
|
||||
loginExpiresAtMs: now + LOGIN_QR_TTL_MS,
|
||||
credentialExpiresAtMs: 0,
|
||||
qrContent: `mock-huya-login://${sessionId}`,
|
||||
smsPhone: '',
|
||||
smsCode: '',
|
||||
huyaUid: '',
|
||||
nickname: '',
|
||||
hasCredential: false,
|
||||
@@ -190,6 +196,78 @@ export function createMockLoginSession(input: {
|
||||
return loginView(session)
|
||||
}
|
||||
|
||||
export function sendMockHuyaSmsCode(input: {
|
||||
taskId?: unknown
|
||||
orderNo?: unknown
|
||||
mobile?: unknown
|
||||
}): ConnectorLoginSession {
|
||||
const mobile = String(input.mobile || '').replace(/\D/g, '')
|
||||
if (!/^1\d{10}$/.test(mobile)) {
|
||||
throw createHttpError('请输入正确的手机号', {
|
||||
statusCode: 400,
|
||||
errorCode: 'huya_sms_mobile_invalid',
|
||||
})
|
||||
}
|
||||
const sessionId = crypto.randomUUID()
|
||||
const now = Date.now()
|
||||
const session: MockSession = {
|
||||
sessionId,
|
||||
taskId: Number(input.taskId || 0),
|
||||
orderNo: String(input.orderNo || '').trim(),
|
||||
createdAtMs: now,
|
||||
loginStatus: 'waiting_code',
|
||||
loginPolls: 0,
|
||||
loginExpiresAtMs: now + LOGIN_QR_TTL_MS,
|
||||
credentialExpiresAtMs: 0,
|
||||
qrContent: '',
|
||||
smsPhone: mobile,
|
||||
smsCode: '123456',
|
||||
huyaUid: '',
|
||||
nickname: '',
|
||||
hasCredential: false,
|
||||
bindingStatus: 'not_started',
|
||||
bindingPolls: 0,
|
||||
bindingQrExpiresAtMs: 0,
|
||||
bindGameAccount: '',
|
||||
bindGameRole: '',
|
||||
bindGameChannel: '',
|
||||
message: 'Mock 短信验证码已发送,请输入 123456',
|
||||
tickets: new Map(),
|
||||
}
|
||||
sessions.set(sessionId, session)
|
||||
return loginView(session)
|
||||
}
|
||||
|
||||
export function loginMockHuyaSms(input: {
|
||||
sessionId?: unknown
|
||||
mobile?: unknown
|
||||
code?: unknown
|
||||
}): ConnectorLoginSession {
|
||||
const session = requireSession(input.sessionId)
|
||||
if (session.loginStatus === 'authenticated') return loginView(session)
|
||||
if (session.loginStatus !== 'waiting_code') {
|
||||
throw createHttpError('Mock 短信登录会话状态无效', {
|
||||
statusCode: 409,
|
||||
errorCode: 'mock_huya_sms_session_invalid',
|
||||
})
|
||||
}
|
||||
const mobile = String(input.mobile || '').replace(/\D/g, '')
|
||||
if (mobile !== session.smsPhone) {
|
||||
throw createHttpError('手机号与发送验证码时不一致', {
|
||||
statusCode: 409,
|
||||
errorCode: 'huya_sms_phone_mismatch',
|
||||
})
|
||||
}
|
||||
if (String(input.code || '').trim() !== session.smsCode) {
|
||||
throw createHttpError('验证码错误或已过期', {
|
||||
statusCode: 409,
|
||||
errorCode: 'huya_sms_login_failed',
|
||||
})
|
||||
}
|
||||
markAuthenticated(session)
|
||||
return loginView(session)
|
||||
}
|
||||
|
||||
export function getMockLoginSession(sessionIdInput: unknown): ConnectorLoginSession {
|
||||
const session = requireSession(sessionIdInput)
|
||||
if (session.loginStatus === 'waiting_scan' || session.loginStatus === 'scanned') {
|
||||
@@ -371,7 +449,11 @@ export function setMockLoginStatus(
|
||||
): ConnectorLoginSession {
|
||||
const session = requireSession(sessionIdInput)
|
||||
const status = String(statusInput || '').trim() as MockLoginStatus
|
||||
if (!['waiting_scan', 'scanned', 'authenticated', 'expired', 'failed'].includes(status)) {
|
||||
if (
|
||||
!['waiting_code', 'waiting_scan', 'scanned', 'authenticated', 'expired', 'failed'].includes(
|
||||
status,
|
||||
)
|
||||
) {
|
||||
throw createHttpError('Mock 登录状态无效', {
|
||||
statusCode: 400,
|
||||
errorCode: 'mock_semi_auto_login_status_invalid',
|
||||
|
||||
Reference in New Issue
Block a user