访问频率控制

This commit is contained in:
luying
2022-01-07 15:09:03 +08:00
parent cf7e223b85
commit 6aaebeb75b
3 changed files with 19 additions and 1 deletions
@@ -3,17 +3,19 @@ import {Application, RouteRecord, FrontendOrBackendSession, HandlerCallback, pin
import { refresh } from '../../../services/refreshService';
import { checkPrivateChat, checkGuildChat, checkOtherChat, checkRoleName, checkGuildName } from "../../../services/sdkService";
import { resResult, checkWhiteList, genCode } from "../../../pubUtils/util";
import { BLOCK_TYPE, CHANNEL_PREFIX, MSG_TYPE, STATUS } from "../../../consts";
import { ACCESS_FREQUENCY, BLOCK_TYPE, CHANNEL_PREFIX, MSG_TYPE, STATUS } from "../../../consts";
import { UserModel } from "../../../db/User";
import { SERVER_DEBUG_MODE } from "../../../pubUtils/dicParam";
import { nowSeconds } from "../../../pubUtils/timeUtil";
import { getServerMainten } from "../../../services/gmService";
import { errlogger } from "../../../util/logger";
export function globalFilter(app: Application) {
return new Filter(app);
}
var Filter = function(this: any, app: Application) {
this.app = app;
this.accessTime = new Map<string, number>();
};
Filter.prototype.before = async function (routeRecord: RouteRecord, msg: any, session: FrontendOrBackendSession, next: HandlerCallback) {
@@ -25,6 +27,18 @@ Filter.prototype.before = async function (routeRecord: RouteRecord, msg: any, se
console.log('*********** global before ip', ip)
let guildCode = session.get('guildCode');
let blockType = session.get('blockType');
// 访问频率控制
let keyOfRouteWithUser = `${routeRecord.route}_${roleId}`;
if(this.accessTime && this.accessTime.has(keyOfRouteWithUser)) {
console.log(this.accessTime.get(keyOfRouteWithUser) - Date.now());
if(Date.now() - this.accessTime.get(keyOfRouteWithUser) < ACCESS_FREQUENCY) {
errlogger.error(`${keyOfRouteWithUser} 间隔时间小于${ACCESS_FREQUENCY}ms,为 ${Date.now() - this.accessTime.get(keyOfRouteWithUser)}`);
// return next(new Error(), resResult(STATUS.ACCESS_BUSY));
}
}
if(!this.accessTime) this.accessTime = new Map<string, number>();
this.accessTime.set(keyOfRouteWithUser, Date.now());
// 玩家屏蔽
if(blockType == BLOCK_TYPE.BLOCK) return next(new Error(), resResult(STATUS.BLOCKED));
if(blockType == BLOCK_TYPE.BAN) {
if([