新增虎牙短信滑块自动验证

This commit is contained in:
yml
2026-11-14 02:18:38 +08:00
parent 2c2dc26a93
commit 3ea5dd5ce4
4 changed files with 281 additions and 16 deletions
@@ -85,6 +85,12 @@ export type HuyaChallengeSolution = {
points?: unknown
}
export type HuyaSliderDetection = {
distance: number
score: number
y: number
}
type ChallengeState = {
strategyUrl: string
page: string
@@ -184,6 +190,96 @@ function generateTrack(distance: number): number[][] {
return points
}
function edgeMap(data: Buffer, width: number, height: number, channels: number): Float32Array {
const gray = new Float32Array(width * height)
const at = (index: number) => gray[index] ?? 0
for (let index = 0, offset = 0; index < gray.length; index += 1, offset += channels) {
gray[index] =
0.299 * Number(data[offset] || 0) +
0.587 * Number(data[offset + 1] || 0) +
0.114 * Number(data[offset + 2] || 0)
}
const edges = new Float32Array(width * height)
for (let y = 1; y < height - 1; y += 1) {
for (let x = 1; x < width - 1; x += 1) {
const index = y * width + x
const gx =
-at(index - width - 1) -
2 * at(index - 1) -
at(index + width - 1) +
at(index - width + 1) +
2 * at(index + 1) +
at(index + width + 1)
const gy =
-at(index - width - 1) -
2 * at(index - width) -
at(index - width + 1) +
at(index + width - 1) +
2 * at(index + width) +
at(index + width + 1)
edges[index] = Math.hypot(gx, gy)
}
}
return edges
}
export async function detectHuyaSliderDistance(input: {
image: string
puzzleImage: string
puzzleY: number
puzzleWidth: number
}): Promise<HuyaSliderDetection | null> {
const background = await sharp(imageBuffer(input.image))
.grayscale()
.raw()
.toBuffer({ resolveWithObject: true })
const puzzle = await sharp(imageBuffer(input.puzzleImage))
.ensureAlpha()
.raw()
.toBuffer({ resolveWithObject: true })
if (background.info.width <= puzzle.info.width || background.info.height <= puzzle.info.height) {
return null
}
const backgroundEdges = edgeMap(background.data, background.info.width, background.info.height, 1)
const puzzleEdges = edgeMap(puzzle.data, puzzle.info.width, puzzle.info.height, 4)
const alpha = new Float32Array(puzzle.info.width * puzzle.info.height)
for (let index = 0; index < alpha.length; index += 1) {
alpha[index] = Number(puzzle.data[index * 4 + 3] || 0) / 255
}
const yCenter = Math.max(
0,
Math.min(background.info.height - puzzle.info.height, Math.round(input.puzzleY)),
)
const yStart = Math.max(1, yCenter - 8)
const yEnd = Math.min(background.info.height - puzzle.info.height - 1, yCenter + 8)
let best: HuyaSliderDetection | null = null
for (let x = 1; x <= background.info.width - puzzle.info.width - 1; x += 1) {
for (let y = yStart; y <= yEnd; y += 1) {
let dot = 0
let puzzleEnergy = 0
let backgroundEnergy = 0
for (let pieceY = 0; pieceY < puzzle.info.height; pieceY += 1) {
for (let pieceX = 0; pieceX < puzzle.info.width; pieceX += 1) {
const pieceIndex = pieceY * puzzle.info.width + pieceX
const mask = alpha[pieceIndex] ?? 0
if (mask <= 0.2) continue
const pieceEdge = (puzzleEdges[pieceIndex] ?? 0) * mask
const backgroundEdge =
backgroundEdges[(y + pieceY) * background.info.width + x + pieceX] ?? 0
dot += pieceEdge * backgroundEdge
puzzleEnergy += pieceEdge * pieceEdge
backgroundEnergy += backgroundEdge * backgroundEdge
}
}
if (puzzleEnergy <= 0 || backgroundEnergy <= 0) continue
const score = dot / Math.sqrt(puzzleEnergy * backgroundEnergy)
if (!best || score > best.score) best = { distance: x, score, y }
}
}
return best && best.score >= 0.7 ? best : null
}
function challengeTypeFromUrl(url: string): HuyaVerificationChallenge['type'] | 'qr' | 'sms' {
const lower = url.toLowerCase()
if (lower.includes('rotate_auth') || lower.includes('/auth/xz/')) return 'rotate'
@@ -490,6 +586,17 @@ export class HuyaSmsRole {
this.challengeState = null
}
async detectSliderDistance(): Promise<HuyaSliderDetection | null> {
const challenge = this.challengeState?.challenge
if (!challenge || challenge.type !== 'slider') return null
return detectHuyaSliderDistance({
image: challenge.image,
puzzleImage: challenge.puzzleImage,
puzzleY: challenge.puzzleY,
puzzleWidth: challenge.puzzleWidth,
})
}
async verifyCookie(): Promise<void> {
const response = await this.fetchImpl(`${UDB_BASE}/web/cookie/verify`, {
method: 'POST',