fix(remote_judger/luogu): sec cookie

This commit is contained in:
Baoshuo Ren 2023-02-03 11:35:43 +08:00
parent 5cfd8d5846
commit 4de24c44e0
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
4 changed files with 92 additions and 30 deletions

View File

@ -12,6 +12,7 @@
"crlf-normalize": "^1.0.18",
"fs-extra": "^11.1.0",
"jsdom": "^21.0.0",
"lodash.flattendeep": "^4.4.0",
"math-sum": "^2.0.0",
"reggol": "^1.3.4",
"superagent": "^8.0.6",
@ -21,6 +22,7 @@
"@types/fs-extra": "^11.0.1",
"@types/js-yaml": "^4.0.5",
"@types/jsdom": "^20.0.1",
"@types/lodash.flattendeep": "^4.4.7",
"@types/node": "^18.11.18",
"@types/superagent": "^4.1.16",
"@types/superagent-proxy": "^3.0.0",
@ -78,6 +80,21 @@
"@types/node": "*"
}
},
"node_modules/@types/lodash": {
"version": "4.14.191",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.191.tgz",
"integrity": "sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ==",
"dev": true
},
"node_modules/@types/lodash.flattendeep": {
"version": "4.4.7",
"resolved": "https://registry.npmjs.org/@types/lodash.flattendeep/-/lodash.flattendeep-4.4.7.tgz",
"integrity": "sha512-1h6GW/AeZw/Wej6uxrqgmdTDZX1yFS39lRsXYkg+3kWvOWWrlGCI6H7lXxlUHOzxDT4QeYGmgPpQ3BX9XevzOg==",
"dev": true,
"dependencies": {
"@types/lodash": "*"
}
},
"node_modules/@types/node": {
"version": "18.11.18",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz",
@ -790,6 +807,11 @@
"node": ">= 0.8.0"
}
},
"node_modules/lodash.flattendeep": {
"version": "4.4.0",
"resolved": "https://registry.npmjs.org/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz",
"integrity": "sha512-uHaJFihxmJcEX3kT4I23ABqKKalJ/zDrDg0lsFtc1h+3uw49SIJ5beyhx5ExVRti3AvKoOJngIj7xz3oylPdWQ=="
},
"node_modules/lru-cache": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz",
@ -1567,6 +1589,21 @@
"@types/node": "*"
}
},
"@types/lodash": {
"version": "4.14.191",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.191.tgz",
"integrity": "sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ==",
"dev": true
},
"@types/lodash.flattendeep": {
"version": "4.4.7",
"resolved": "https://registry.npmjs.org/@types/lodash.flattendeep/-/lodash.flattendeep-4.4.7.tgz",
"integrity": "sha512-1h6GW/AeZw/Wej6uxrqgmdTDZX1yFS39lRsXYkg+3kWvOWWrlGCI6H7lXxlUHOzxDT4QeYGmgPpQ3BX9XevzOg==",
"dev": true,
"requires": {
"@types/lodash": "*"
}
},
"@types/node": {
"version": "18.11.18",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz",
@ -2113,6 +2150,11 @@
"type-check": "~0.3.2"
}
},
"lodash.flattendeep": {
"version": "4.4.0",
"resolved": "https://registry.npmjs.org/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz",
"integrity": "sha512-uHaJFihxmJcEX3kT4I23ABqKKalJ/zDrDg0lsFtc1h+3uw49SIJ5beyhx5ExVRti3AvKoOJngIj7xz3oylPdWQ=="
},
"lru-cache": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz",

View File

@ -15,6 +15,7 @@
"crlf-normalize": "^1.0.18",
"fs-extra": "^11.1.0",
"jsdom": "^21.0.0",
"lodash.flattendeep": "^4.4.0",
"math-sum": "^2.0.0",
"reggol": "^1.3.4",
"superagent": "^8.0.6",
@ -24,6 +25,7 @@
"@types/fs-extra": "^11.0.1",
"@types/js-yaml": "^4.0.5",
"@types/jsdom": "^20.0.1",
"@types/lodash.flattendeep": "^4.4.7",
"@types/node": "^18.11.18",
"@types/superagent": "^4.1.16",
"@types/superagent-proxy": "^3.0.0",

View File

@ -4,7 +4,7 @@ import proxy from 'superagent-proxy';
import Logger from '../utils/logger';
import { IBasicProvider, RemoteAccount, USER_AGENT } from '../interface';
import sleep from '../utils/sleep';
import flattenDeep from '../utils/flattenDeep';
import flattenDeep from 'lodash.flattendeep';
proxy(superagent);
const logger = new Logger('remote/luogu');
@ -106,6 +106,23 @@ export default class LuoguProvider implements IBasicProvider {
return req;
}
async safeGet(url: string) {
const res = await this.get(url);
if (res.text.startsWith('<html><script>document.location.reload()')) {
const sec = this.getCookie.call(
{ cookie: res.header['set-cookie'] },
'sec'
);
this.setCookie('sec', sec);
logger.debug('sec', sec);
return await this.get(url);
}
return res;
}
post(url: string) {
logger.debug('post', url, this.cookie);
@ -125,8 +142,21 @@ export default class LuoguProvider implements IBasicProvider {
return req;
}
getCookie(target: string) {
return this.cookie
.find(i => i.startsWith(`${target}=`))
?.split('=')[1]
?.split(';')[0];
}
setCookie(target: string, value: string) {
this.cookie = this.cookie.filter(i => !i.startsWith(`${target}=`));
this.cookie.push(`${target}=${value}`);
}
async getCsrfToken(url: string) {
const { text: html } = await this.get(url);
let { text: html } = await this.safeGet(url);
const $dom = new JSDOM(html);
this.csrf = $dom.window.document
@ -137,7 +167,7 @@ export default class LuoguProvider implements IBasicProvider {
}
get loggedIn() {
return this.get('/user/setting?_contentOnly=1').then(
return this.safeGet('/user/setting?_contentOnly=1').then(
({ body }) => body.currentTemplate !== 'AuthLogin'
);
}
@ -208,17 +238,15 @@ export default class LuoguProvider implements IBasicProvider {
}
async waitForSubmission(problem_id: string, id: string, next, end) {
const done = {};
let fail = 0;
let count = 0;
let finished = 0;
while (count < 120 && fail < 5) {
await sleep(1500);
count++;
try {
const { body } = await this.get(`/record/${id}?_contentOnly=1`);
const { body } = await this.safeGet(`/record/${id}?_contentOnly=1`);
const data = body.currentData.record;
if (
@ -227,41 +255,36 @@ export default class LuoguProvider implements IBasicProvider {
) {
return await end({
error: true,
id,
id: 'R' + id,
status: 'Compile Error',
message: data.detail.compileResult.message,
});
}
logger.info('Fetched with length', JSON.stringify(body).length);
const total = flattenDeep(body.currentData.testCaseGroup).length;
// TODO sorted
const total = flattenDeep(
Object.entries(body.currentData.testCaseGroup || {}).map(o => o[1])
).length;
if (!data.detail.judgeResult?.subtasks) continue;
for (const key in data.detail.judgeResult.subtasks) {
const subtask = data.detail.judgeResult.subtasks[key];
for (const cid in subtask.testCases || {}) {
if (done[`${subtask.id}.${cid}`]) continue;
finished++;
done[`${subtask.id}.${cid}`] = true;
await next({
status: `Judging (${(finished / total) * 100})`,
status: `Judging (${
data.detail.judgeResult?.finishedCaseCount || '?'
}/${total})`,
});
}
}
if (data.status < 2) continue;
logger.info('RecordID:', id, 'done');
// TODO calc total status
return await end({
id: 'R' + id,
status: STATUS_MAP[data.status],
score: data.score,
score:
STATUS_MAP[data.status] === 'Accepted'
? 100
: (data.score / data.problem.fullScore) * 100,
time: data.time,
memory: data.memory,
});
@ -274,6 +297,7 @@ export default class LuoguProvider implements IBasicProvider {
return await end({
error: true,
id: 'R' + id,
status: 'Judgment Failed',
message: 'Failed to fetch submission details.',
});

View File

@ -1,6 +0,0 @@
const flattenDeep = arr =>
Array.isArray(arr)
? arr.reduce((a, b) => a.concat(flattenDeep(b)), [])
: [arr];
export default flattenDeep;