访问频率控制

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

View File

@@ -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([

View File

@@ -26,6 +26,9 @@ export const REFRESH_TIME = 5; // 统一一天刷新时间
export const REF_CIRCLE_MAIL_TIME = 3; // 统一一天刷新定期邮件时间
export const PUSH_BATCH = 100; // 推送分批人数
export const PUSH_INTERVAL = 5 * 1000; // 分批时间5秒一批
export const ACCESS_FREQUENCY = 500; // 玩家访问频率,一个玩家一个接口间隔时间 单位ms
export enum TIME_OUTPUT_TYPE {
DATE = 1,
STAMP_10 = 2,

View File

@@ -12,6 +12,7 @@ export const STATUS = {
BLOCK_WORDS: { code: 9, simStr: '内容有不合法词汇' },
BLOCKED: { code: 10, simStr: '您已被封禁' },
BANNED: { code: 11, simStr: '您已被禁言' },
ACCESS_BUSY: { code: 12, simStr: '您的操作过于繁忙' },
GLOBAL_ERR: { code: 1003, simStr: '服务器内部错误' },
// http请求
REQUEST_TIME_OUT: { code: 2000, simStr: '请求超时' },