diff --git a/packages/vjudge/src/fetch.ts b/packages/vjudge/src/fetch.ts index 02c968b9..ca68bc97 100644 --- a/packages/vjudge/src/fetch.ts +++ b/packages/vjudge/src/fetch.ts @@ -47,4 +47,9 @@ export class BasicFetcher { if (this.fetchOptions.post?.headers) req = req.set(this.fetchOptions.post.headers); return this.account.proxy ? req.proxy(this.account.proxy) : req; } + + setCookie(cookie: string | string[]) { + if (typeof cookie === 'string') this.cookie = [cookie]; + else this.cookie = cookie; + } } diff --git a/packages/vjudge/src/providers/codeforces.ts b/packages/vjudge/src/providers/codeforces.ts index 0f143e82..251accba 100644 --- a/packages/vjudge/src/providers/codeforces.ts +++ b/packages/vjudge/src/providers/codeforces.ts @@ -126,7 +126,7 @@ export default class CodeforcesProvider extends BasicFetcher implements IBasicPr const cookie = res.header['set-cookie']; if (cookie) { await this.save({ cookie }); - this.cookie = cookie; + this.setCookie(cookie); } if (await this.loggedIn) { logger.success('Logged in'); diff --git a/packages/vjudge/src/providers/luogu.ts b/packages/vjudge/src/providers/luogu.ts index 05543637..77eae8ac 100644 --- a/packages/vjudge/src/providers/luogu.ts +++ b/packages/vjudge/src/providers/luogu.ts @@ -41,7 +41,10 @@ export default class LuoguProvider extends BasicFetcher implements IBasicProvide }, }, }); - setInterval(() => this.getCsrfToken('/user/setting'), 5 * 60 * 1000); + setInterval(() => { + this.ensureLogin(); + this.getCsrfToken('/user/setting'); + }, 5 * 60 * 1000); } async getCsrfToken(url: string) { @@ -60,7 +63,17 @@ export default class LuoguProvider extends BasicFetcher implements IBasicProvide return true; } logger.info('retry login'); - // TODO login; + const res = await this.post(`/api/auth/userPassLogin${this.account.query || ''}`) + .set('referer', 'https://www.luogu.com.cn/user/setting') + .send({ + username: this.account.handle, + password: this.account.password, + }); + if (res.headers['set-cookie']) this.setCookie(res.headers['set-cookie']); + if (await this.loggedIn) { + await this.getCsrfToken('/user/setting'); + return true; + } return false; }