Skip to content

Commit

Permalink
Some attempts to resolve captcha issue
Browse files Browse the repository at this point in the history
  • Loading branch information
claabs committed Apr 1, 2021
1 parent 30791be commit 1d2f861
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 49 deletions.
33 changes: 20 additions & 13 deletions src/site/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,6 @@ router.use(
nocache()
);

router.use(
'/challenge',
proxy('https://assets.hcaptcha.com/captcha', {
proxyReqPathResolver: req => {
L.trace(req);
return req.originalUrl;
},
}),
nocache()
);

const insertTags = (data: string, url: string, component: string): string => {
if (url.includes(`${component}.html`)) {
let updatedData = data;
Expand Down Expand Up @@ -160,8 +149,7 @@ router.use(
return updatedReq;
},
// Updates URL in Akamai sensor_data body
proxyReqBodyDecorator: (bodyContent: Buffer, srcReq) => {
L.trace({ srcReq });
proxyReqBodyDecorator: (bodyContent: Buffer) => {
const body = bodyContent.toString();
const updatedBody = body.replace(
new RegExp(`https://${baseUrl.hostname}.*?,`, 'g'),
Expand Down Expand Up @@ -240,6 +228,24 @@ router.get(
nocache()
);

router.get(
'/challenge/grid/challenge.js',
asyncHandler(async (_req, res) => {
L.trace('incoming /challenge/grid/challenge.js request');
const resp = await got.get(`https://hcaptcha.com/challenge/grid/challenge.js`, {
followRedirect: true,
responseType: 'text',
});
const body = resp.body.replace(
new RegExp('https://assets.hcaptcha.com', 'g'),
`${baseUrl.origin}/assets`
);
res.header('content-type', resp.headers['content-type']);
res.status(200).send(body);
}),
nocache()
);

interface InitReq {
initData: InitData;
id: string;
Expand Down Expand Up @@ -309,6 +315,7 @@ router.post<any, any, CompleteBody, any>(
if (_abck) setCookie(email, '_abck', _abck);
const talon = new TalonSdk(email, req.headers['user-agent'], xsrfToken);
await talon.challengeComplete(session, timing);
await talon.sendBatchEvents();
const sessionData = assembleFinalCaptchaKey(session, initData, captchaResult);
await responseManualCaptcha({ id, sessionData });
const cookies = getCookies(email);
Expand Down
58 changes: 22 additions & 36 deletions src/site/talon-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ export default class TalonSdk {

private request: Got;

private eventBuffer: PhaserEvent[] = [];

constructor(email: string, userAgent: string, xsrfToken: string) {
this.L = logger.child({
user: email,
Expand All @@ -154,7 +156,6 @@ export default class TalonSdk {

private async sendPhaserEvent(
event: EventType,
referrerOrigin: string,
session?: PhaserSession,
oldTiming?: Timing[]
): Promise<Timing[]> {
Expand All @@ -172,21 +173,20 @@ export default class TalonSdk {
timing,
errors: [],
};
this.L.trace({ body, PHASER_F_ENDPOINT }, 'POST');
await this.request.post(PHASER_F_ENDPOINT, {
json: body,
headers: this.getHeaders(referrerOrigin),
});
this.L.trace({ body }, 'Add to event buffer');

this.eventBuffer.push(body);
return timing;
}

// TODO: Convert Talon SDK to batched events using batch endpoint
private async rawSendPhaserBatch(body: PhaserBatchBody, referrerOrigin: string): Promise<void> {
async sendBatchEvents(): Promise<void> {
const body = this.eventBuffer;
this.L.trace({ body, PHASER_BATCH_ENDPOINT }, 'POST');
await this.request.post(PHASER_BATCH_ENDPOINT, {
json: body,
headers: this.getHeaders(referrerOrigin),
headers: this.getHeaders('https://talon-website-prod.ak.epicgames.com'),
});
this.eventBuffer = [];
}

private getHeaders(referrerOrigin: string): Record<string, string> {
Expand All @@ -204,51 +204,35 @@ export default class TalonSdk {
}

async sdkLoad(): Promise<void> {
await this.sendPhaserEvent('sdk_load', 'https://www.epicgames.com');
await this.sendPhaserEvent('sdk_load');
}

async sdkInit(): Promise<Timing[]> {
return this.sendPhaserEvent('sdk_init', 'https://www.epicgames.com');
return this.sendPhaserEvent('sdk_init');
}

async sdkInitComplete(session: PhaserSession, oldTiming: Timing[]): Promise<Timing[]> {
return this.sendPhaserEvent(
'sdk_init_complete',
'https://www.epicgames.com',
session,
oldTiming
);
return this.sendPhaserEvent('sdk_init_complete', session, oldTiming);
}

async challengeReady(session: PhaserSession, oldTiming: Timing[]): Promise<Timing[]> {
return this.sendPhaserEvent('challenge_ready', 'https://www.epicgames.com', session, oldTiming);
return this.sendPhaserEvent('challenge_ready', session, oldTiming);
}

async challengeExecute(session: PhaserSession, oldTiming: Timing[]): Promise<Timing[]> {
return this.sendPhaserEvent(
'challenge_execute',
'https://www.epicgames.com',
session,
oldTiming
);
return this.sendPhaserEvent('challenge_execute', session, oldTiming);
}

async challengeOpened(session: PhaserSession, oldTiming: Timing[]): Promise<Timing[]> {
return this.sendPhaserEvent('challenge_opened', session, oldTiming);
}

async sdkExecute(session: PhaserSession, oldTiming: Timing[]): Promise<Timing[]> {
return this.sendPhaserEvent(
'sdk_execute',
'https://talon-website-prod.ak.epicgames.com',
session,
oldTiming
);
return this.sendPhaserEvent('sdk_execute', session, oldTiming);
}

async challengeComplete(session: PhaserSession, oldTiming: Timing[]): Promise<Timing[]> {
return this.sendPhaserEvent(
'challenge_complete',
'https://www.epicgames.com',
session,
oldTiming
);
return this.sendPhaserEvent('challenge_complete', session, oldTiming);
}

async initIp(): Promise<ClientIp> {
Expand Down Expand Up @@ -303,6 +287,8 @@ export default class TalonSdk {
blob = executeResp.arkose.data.blob;
}
timing = await this.challengeExecute(session, timing);
timing = await this.challengeOpened(session, timing);
this.sendBatchEvents();
return { session, timing, blob };
}
}
Expand Down

0 comments on commit 1d2f861

Please sign in to comment.