添加全局rate_limit,更换ui节点接口

This commit is contained in:
undefined committed 2020-09-05 22:17:21 +08:00
1 parent 02c1866963
commit 03dc8903e2
22 files changed
+231 -216

No files matched your search

+2 -1
View File
@@ -1,4 +1,4 @@
import superagent from 'superagent';
import superagent, { SuperAgentRequest } from 'superagent';
import fs from 'fs';
async function _download(url: string, path: string, retry: number) {
@@ -11,6 +11,7 @@ async function _download(url: string, path: string, retry: number) {
return path;
}
function download(url: string): SuperAgentRequest
function download(url: string, path?: string, retry = 3) {
if (path) return _download(url, path, retry);
return superagent.get(url).retry(retry);
-71
View File
@@ -1,71 +0,0 @@
import assert from 'assert';
import superagent from 'superagent';
import download from './download';
import { ValidationError, RemoteOnlineJudgeError } from '../error';
const RE_SYZOJ = /https?:\/\/([a-zA-Z0-9.]+)\/problem\/([0-9]+)\/?/i;
export async function syzoj(url: string, handler: any) {
assert(url.match(RE_SYZOJ), new ValidationError('url'));
if (!url.endsWith('/')) url += '/';
const [, host, pid] = RE_SYZOJ.exec(url);
const res = await superagent.get(`${url}export`);
assert(res.status === 200, new RemoteOnlineJudgeError('Cannot connect to target server'));
assert(res.body.success, new RemoteOnlineJudgeError((res.body.error || {}).message));
const p = res.body.obj;
const content = [];
if (p.description) {
content.push(
handler.translate('problem.import.problem_description'),
p.description,
);
}
if (p.input_format) {
content.push(
handler.translate('problem.import.input_format'),
p.input_format,
);
}
if (p.output_format) {
content.push(
handler.translate('problem.import.output_format'),
p.output_format,
);
}
if (p.hint) {
content.push(
handler.translate('problem.import.hint'),
p.hint,
);
}
if (p.limit_and_hint) {
content.push(
handler.translate('problem.import.limit_and_hint'),
p.limit_and_hint,
);
}
if (p.have_additional_file) {
content.push(
handler.translate('problem.import.additional_file'),
`${url}download/additional_file`,
);
}
const pdoc = {
title: p.title,
content: content.join(' \n'),
owner: handler.user._id,
from: url,
pid: `${host}_${pid}`,
tags: p.tags,
config: {
time: p.time_limit,
memory: p.memory_limit * 1024,
filename: p.file_io_input_name,
type: p.type,
},
};
const r = download(`${url}testdata/download`);
return [pdoc, r];
}
global.Hydro.lib['import.syzoj'] = syzoj;
-57
View File
@@ -1,57 +0,0 @@
import { isNull } from 'lodash';
import { PERM, PRIV } from '../model/builtin';
global.Hydro.ui.nav = [];
const trueChecker = () => true;
const Checker = (perm: bigint, priv: number, checker: Function = trueChecker) => (handler) => (
checker(handler)
&& (perm ? handler.user.hasPerm(perm) : true)
&& (priv ? handler.user.hasPriv(priv) : true)
);
const Item = (
name: string, args: any, prefix: string,
...permPrivChecker: Array<number | bigint | Function>
) => {
let _priv: number;
let _perm: bigint;
let checker: Function = trueChecker;
for (const item of permPrivChecker) {
if (item instanceof Object && !isNull(item)) {
if (item instanceof Array) {
if (typeof item[0] === 'number') {
// @ts-ignore
_priv = item;
} else if (typeof item[0] === 'bigint') {
// @ts-ignore
_perm = item;
}
} else if (typeof item.call !== 'undefined') {
checker = item;
}
} else if (typeof item === 'number') {
_priv = item;
} else if (typeof item === 'bigint') {
_perm = item;
}
}
checker = Checker(_perm, _priv, checker);
global.Hydro.ui.nav.push({
name, args: args || {}, prefix, checker,
});
};
Item('homepage', null, 'homepage');
Item('problem_main', null, 'problem', PERM.PERM_VIEW_PROBLEM);
Item('training_main', null, 'training', PERM.PERM_VIEW_TRAINING);
Item('homework_main', null, 'homework', PERM.PERM_VIEW_HOMEWORK);
Item('discussion_main', null, 'discussion', PERM.PERM_VIEW_DISCUSSION);
Item('contest_main', null, 'contest', PERM.PERM_VIEW_CONTEST);
Item('record_main', null, 'record');
Item('ranking', null, 'ranking', PERM.PERM_VIEW_RANKING);
Item('domain_dashboard', null, 'domain', PERM.PERM_EDIT_DOMAIN);
Item('manage_dashboard', null, 'manage', PRIV.PRIV_EDIT_SYSTEM);
global.Hydro.lib.nav = Item;
export = Item;
+67
View File
@@ -0,0 +1,67 @@
import { Dictionary, isNull } from 'lodash';
import { PERM, PRIV } from '../model/builtin';
const trueChecker = () => true;
const Checker = (perm: bigint, priv: number, checker: Function = trueChecker) => (handler) => (
checker(handler)
&& (perm ? handler.user.hasPerm(perm) : true)
&& (priv ? handler.user.hasPriv(priv) : true)
);
const buildChecker = (...permPrivChecker: Array<number | bigint | Function>) => {
let _priv: number;
let _perm: bigint;
let checker: Function = trueChecker;
for (const item of permPrivChecker) {
if (item instanceof Object && !isNull(item)) {
if (item instanceof Array) {
if (typeof item[0] === 'number') {
// @ts-ignore
_priv = item;
} else if (typeof item[0] === 'bigint') {
// @ts-ignore
_perm = item;
}
} else if (typeof item.call !== 'undefined') {
checker = item;
}
} else if (typeof item === 'number') {
_priv = item;
} else if (typeof item === 'bigint') {
_perm = item;
}
}
return Checker(_perm, _priv, checker);
};
export const Nav = (
name: string, args: Dictionary<any> = {}, prefix: string,
...permPrivChecker: Array<number | bigint | Function>
) => {
global.Hydro.ui.nodes.nav.push({
name, args: args || {}, prefix, checker: buildChecker(...permPrivChecker),
});
};
export const ProblemAdd = (
name: string, args: Dictionary<any> = {}, icon = 'add', text = 'Create Problem',
) => {
global.Hydro.ui.nodes.problem_add.push({
name, args, icon, text,
});
};
Nav('homepage', {}, 'homepage');
Nav('problem_main', {}, 'problem', PERM.PERM_VIEW_PROBLEM);
Nav('training_main', {}, 'training', PERM.PERM_VIEW_TRAINING);
Nav('homework_main', {}, 'homework', PERM.PERM_VIEW_HOMEWORK);
Nav('discussion_main', {}, 'discussion', PERM.PERM_VIEW_DISCUSSION);
Nav('contest_main', {}, 'contest', PERM.PERM_VIEW_CONTEST);
Nav('record_main', {}, 'record');
Nav('ranking', {}, 'ranking', PERM.PERM_VIEW_RANKING);
Nav('domain_dashboard', {}, 'domain', PERM.PERM_EDIT_DOMAIN);
Nav('manage_dashboard', {}, 'manage', PRIV.PRIV_EDIT_SYSTEM);
ProblemAdd('problem_create');
ProblemAdd('problem_import_syzoj', {}, 'copy', 'Import From SYZOJ');
global.Hydro.ui.Nav = Nav;
global.Hydro.ui.ProblemAdd = ProblemAdd;