From b71011b3b9c80778059f166e3c0dcb46380193fe Mon Sep 17 00:00:00 2001 From: undefined Date: Sat, 27 Mar 2021 17:35:37 +0800 Subject: [PATCH] =?UTF-8?q?core:=20=E6=9B=B4=E6=96=B0=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E9=A2=91=E7=8E=87=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/hydrooj/package.json | 2 +- packages/hydrooj/src/handler/contest.ts | 4 ++-- packages/hydrooj/src/handler/discussion.ts | 6 +++--- packages/hydrooj/src/handler/home.ts | 2 +- packages/hydrooj/src/handler/homework.ts | 4 ++-- packages/hydrooj/src/handler/problem.ts | 3 ++- packages/hydrooj/src/handler/user.ts | 4 ++-- packages/hydrooj/src/model/builtin.ts | 1 + packages/hydrooj/src/service/server.ts | 5 +++-- 9 files changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/hydrooj/package.json b/packages/hydrooj/package.json index e10055135..0ce240b0e 100644 --- a/packages/hydrooj/package.json +++ b/packages/hydrooj/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "2.20.41", + "version": "2.20.43", "bin": "bin/hydrooj.js", "main": "dist/loader.js", "typings": "dist/loader.d.ts", diff --git a/packages/hydrooj/src/handler/contest.ts b/packages/hydrooj/src/handler/contest.ts index 624663876..a68338965 100644 --- a/packages/hydrooj/src/handler/contest.ts +++ b/packages/hydrooj/src/handler/contest.ts @@ -311,7 +311,7 @@ class ContestDetailProblemSubmitHandler extends ContestProblemHandler { @param('lang', Types.String) @param('code', Types.String) async post(domainId: string, tid: ObjectID, lang: string, code: string) { - await this.limitRate('add_record', 60, 100); + await this.limitRate('add_record', 60, 10); const rid = await record.add(domainId, this.pdoc.docId, this.user._id, lang, code, true, { type: document.TYPE_CONTEST, tid, @@ -339,7 +339,7 @@ class ContestCodeHandler extends ContestHandler { if (!this.user.hasPriv(PRIV.PRIV_READ_RECORD_CODE)) { this.checkPerm(PERM.PERM_READ_RECORD_CODE); } - this.limitRate('homework_code', 3600, 60); + await this.limitRate('homework_code', 3600, 60); const [tdoc, tsdocs] = await contest.getAndListStatus(domainId, tid); const rnames = {}; for (const tsdoc of tsdocs) { diff --git a/packages/hydrooj/src/handler/discussion.ts b/packages/hydrooj/src/handler/discussion.ts index 131d1f3e8..e32663bc4 100644 --- a/packages/hydrooj/src/handler/discussion.ts +++ b/packages/hydrooj/src/handler/discussion.ts @@ -159,7 +159,7 @@ class DiscussionCreateHandler extends DiscussionHandler { domainId: string, type: string, _name: string, title: string, content: string, highlight = false, pin = false, ) { - this.limitRate('add_discussion', 3600, 30); + await this.limitRate('add_discussion', 3600, 60); let name: ObjectID | string | number; if (ObjectID.isValid(_name)) name = new ObjectID(_name); else if (isSafeInteger(parseInt(_name, 10))) name = parseInt(_name, 10); @@ -220,7 +220,7 @@ class DiscussionDetailHandler extends DiscussionHandler { @param('content', Types.String, isContent) async postReply(domainId: string, did: ObjectID, content: string) { this.checkPerm(PERM.PERM_REPLY_DISCUSSION); - this.limitRate('add_discussion', 3600, 30); + await this.limitRate('add_discussion', 3600, 60); // Notify related users const replies: Drdoc[] = await discussion.getMultiReply(domainId, did).toArray(); const uids = Array.from(new Set(replies.map((drdoc) => drdoc.owner))); @@ -239,7 +239,7 @@ class DiscussionDetailHandler extends DiscussionHandler { @param('content', Types.String, isContent) async postTailReply(domainId: string, drid: ObjectID, content: string) { this.checkPerm(PERM.PERM_REPLY_DISCUSSION); - this.limitRate('add_discussion', 3600, 30); + await this.limitRate('add_discussion', 3600, 60); await discussion.addTailReply(domainId, drid, this.user._id, content, this.request.ip); this.back(); } diff --git a/packages/hydrooj/src/handler/home.ts b/packages/hydrooj/src/handler/home.ts index cae4762a3..9621e51cc 100644 --- a/packages/hydrooj/src/handler/home.ts +++ b/packages/hydrooj/src/handler/home.ts @@ -152,7 +152,7 @@ class HomeSecurityHandler extends Handler { @param('currentPassword', Types.String) @param('mail', Types.String, isEmail) async postChangeMail(domainId: string, current: string, email: string) { - this.limitRate('send_mail', 3600, 30); + await this.limitRate('send_mail', 3600, 30); this.user.checkPassword(current); const udoc = await user.getByEmail(domainId, email); if (udoc) throw new UserAlreadyExistError(email); diff --git a/packages/hydrooj/src/handler/homework.ts b/packages/hydrooj/src/handler/homework.ts index 8a3433edd..b77f2d243 100644 --- a/packages/hydrooj/src/handler/homework.ts +++ b/packages/hydrooj/src/handler/homework.ts @@ -177,7 +177,7 @@ class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler { @param('code', Types.String) @param('lang', Types.String) async post(domainId: string, tid: ObjectID, pid: number, code: string, lang: string) { - this.limitRate('add_record', 3600, 100); + await this.limitRate('add_record', 60, 5); const tsdoc = await contest.getStatus(domainId, tid, this.user._id, document.TYPE_HOMEWORK); if (!tsdoc.attend) throw new HomeworkNotAttendedError(tid); if (!contest.isOngoing(this.tdoc)) throw new HomeworkNotLiveError(tid); @@ -380,7 +380,7 @@ class HomeworkCodeHandler extends HomeworkHandler { @param('tid', Types.ObjectID) async get(domainId: string, tid: ObjectID) { this.checkPerm(PERM.PERM_READ_RECORD_CODE); - this.limitRate('homework_code', 3600, 60); + await this.limitRate('homework_code', 3600, 60); const [tdoc, tsdocs] = await contest.getAndListStatus( domainId, tid, document.TYPE_HOMEWORK, ); diff --git a/packages/hydrooj/src/handler/problem.ts b/packages/hydrooj/src/handler/problem.ts index 0239b017d..b2dc5634c 100644 --- a/packages/hydrooj/src/handler/problem.ts +++ b/packages/hydrooj/src/handler/problem.ts @@ -231,6 +231,7 @@ export class ProblemSubmitHandler extends ProblemDetailHandler { @param('lang', Types.String) @param('code', Types.String) async post(domainId: string, lang: string, code: string) { + await this.limitRate('add_record', 60, 5); const rid = await record.add(domainId, this.pdoc.docId, this.user._id, lang, code, true); const [rdoc] = await Promise.all([ record.get(domainId, rid), @@ -248,7 +249,7 @@ export class ProblemPretestHandler extends ProblemDetailHandler { @param('code', Types.String) @param('input', Types.String, true) async post(domainId: string, lang: string, code: string, input = '') { - this.limitRate('add_record', 3600, 100); + await this.limitRate('add_record', 60, 5); const rid = await record.add( domainId, this.pdoc.docId, this.user._id, lang, code, true, input, diff --git a/packages/hydrooj/src/handler/user.ts b/packages/hydrooj/src/handler/user.ts index 852746bc2..8b85615d0 100644 --- a/packages/hydrooj/src/handler/user.ts +++ b/packages/hydrooj/src/handler/user.ts @@ -67,7 +67,7 @@ class UserRegisterHandler extends Handler { async post(domainId: string, mail: string, phoneNumber: string) { if (mail) { if (await user.getByEmail('system', mail)) throw new UserAlreadyExistError(mail); - this.limitRate('send_mail', 3600, 30); + await this.limitRate('send_mail', 3600, 30); const t = await token.add( token.TYPE_REGISTRATION, system.get('session.unsaved_expire_seconds'), @@ -83,7 +83,7 @@ class UserRegisterHandler extends Handler { } else this.response.redirect = this.url('user_register_with_code', { code: t[0] }); } else if (phoneNumber) { if (!global.Hydro.lib.sendSms) throw new SystemError('Cannot send sms'); - this.limitRate('send_sms', 60, 3); + await this.limitRate('send_sms', 60, 3); const t = await token.add( token.TYPE_REGISTRATION, system.get('session.unsaved_expire_seconds'), diff --git a/packages/hydrooj/src/model/builtin.ts b/packages/hydrooj/src/model/builtin.ts index d5354358c..ace0e028c 100644 --- a/packages/hydrooj/src/model/builtin.ts +++ b/packages/hydrooj/src/model/builtin.ts @@ -231,6 +231,7 @@ export const PRIV = { PRIV_REJUDGE: 1 << 13, PRIV_VIEW_USER_SECRET: 1 << 14, PRIV_VIEW_JUDGE_STATISTICS: 1 << 15, + PRIV_UNLIMITED_RATE: 1 << 22, PRIV_ALL: -1, PRIV_DEFAULT: 0, diff --git a/packages/hydrooj/src/service/server.ts b/packages/hydrooj/src/service/server.ts index 19a537f63..c29ed602d 100644 --- a/packages/hydrooj/src/service/server.ts +++ b/packages/hydrooj/src/service/server.ts @@ -36,7 +36,7 @@ import * as system from '../model/system'; import blacklist from '../model/blacklist'; import token from '../model/token'; import * as opcount from '../model/opcount'; -import { PERM } from '../model/builtin'; +import { PERM, PRIV } from '../model/builtin'; const logger = new Logger('server'); export const app = new Koa(); @@ -274,6 +274,7 @@ export class HandlerCommon { extraTitleContent?: string; async limitRate(op: string, periodSecs: number, maxOperations: number) { + if (this.user && this.user.hasPriv(PRIV.PRIV_UNLIMITED_RATE)) return; await opcount.inc(op, this.request.ip, periodSecs, maxOperations); } @@ -459,7 +460,7 @@ export class Handler extends HandlerCommon { const [absoluteDomain, inferDomain] = await Promise.all([ domain.get(domainId), domain.getByHost(this.request.host), - this.limitRate('global', 10, 100), + this.limitRate('global', 10, 50), this.getSession(), this.getBdoc(), ]);