From 9f1169b3e054a36686262d83db3763f30a329940 Mon Sep 17 00:00:00 2001 From: undefined Date: Sat, 8 Apr 2023 01:50:33 +0800 Subject: [PATCH] vjudge: codeforces: fix login check --- packages/vjudge/package.json | 2 +- packages/vjudge/src/fetch.ts | 5 +++-- packages/vjudge/src/model.ts | 5 ++++- packages/vjudge/src/providers/codeforces.ts | 18 +++++++++--------- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/vjudge/package.json b/packages/vjudge/package.json index be8af560a..fe5231d31 100644 --- a/packages/vjudge/package.json +++ b/packages/vjudge/package.json @@ -1,6 +1,6 @@ { "name": "@hydrooj/vjudge", - "version": "1.8.5", + "version": "1.8.6", "description": "Submit problems to remote oj", "main": "src/model.ts", "repository": "https://github.com/hydro-dev/Hydro.git", diff --git a/packages/vjudge/src/fetch.ts b/packages/vjudge/src/fetch.ts index ca68bc97a..470948230 100644 --- a/packages/vjudge/src/fetch.ts +++ b/packages/vjudge/src/fetch.ts @@ -33,10 +33,11 @@ export class BasicFetcher { } async html(url: string) { - const { text: html } = await this.get(url); + const { text: html, headers } = await this.get(url); const $dom = new JSDOM(html); $dom.window.html = html; - return $dom.window as DOMWindow & { html: string }; + $dom.window.headers = headers; + return $dom.window as DOMWindow & { html: string, headers: any }; } post(url: string) { diff --git a/packages/vjudge/src/model.ts b/packages/vjudge/src/model.ts index 48542d82e..94d678e9d 100644 --- a/packages/vjudge/src/model.ts +++ b/packages/vjudge/src/model.ts @@ -21,7 +21,10 @@ class AccountService { await coll.updateOne({ _id: account._id }, { $set: data }); }); this.problemLists = Set.union(this.api.entryProblemLists || ['main'], this.account.problemLists || []); - this.main().catch((e) => logger.error(`Error occured in ${account.type}/${account.handle}`, e)); + this.main().catch((e) => { + logger.error(`Error occured in ${account.type}/${account.handle}`); + console.error(e); + }); } async addProblemList(name: string) { diff --git a/packages/vjudge/src/providers/codeforces.ts b/packages/vjudge/src/providers/codeforces.ts index 7febcde40..7e2eba8bb 100644 --- a/packages/vjudge/src/providers/codeforces.ts +++ b/packages/vjudge/src/providers/codeforces.ts @@ -83,37 +83,37 @@ export default class CodeforcesProvider extends BasicFetcher implements IBasicPr } async getCsrfToken(url: string) { - const { document, html } = await this.html(url); + const { document, html, headers } = await this.html(url); if (document.body.children.length < 2 && html.length < 512) { - throw new Error(document.body.textContent); + throw new Error(document.body.textContent!); } - const ftaa = this.getCookie('70a7c28f3de'); - const bfaa = /_bfaa = "(.{32})"/.exec(html)?.[1] || this.getCookie('raa') || this.getCookie('bfaa'); + const ftaa = this.getCookie('70a7c28f3de') || 'n/a'; + const bfaa = this.getCookie('raa') || this.getCookie('bfaa') || 'n/a'; return [ ( document.querySelector('meta[name="X-Csrf-Token"]') || document.querySelector('input[name="csrf_token"]') )?.getAttribute('content'), - ftaa, bfaa, + ftaa, bfaa, headers, ]; } get loggedIn() { - return this.get('/enter').then((res) => { + return this.get('/').then((res) => { const html = res.text; - if (html.includes('Login into Codeforces')) return false; if (html.length < 1000 && html.includes('Redirecting...')) { logger.debug('Got a redirect', html); return false; } - return true; + return html.includes('header-bell__img'); }); } async ensureLogin() { if (await this.loggedIn) return true; logger.info('retry normal login'); - const [csrf, ftaa, bfaa] = await this.getCsrfToken('/enter'); + const [csrf, ftaa, bfaa, header] = await this.getCsrfToken('/enter'); + if (header['set-cookie']) this.setCookie(header['set-cookie']); const res = await this.post('/enter').send({ csrf_token: csrf, action: 'enter',