访问频率控制

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 { refresh } from '../../../services/refreshService';
import { checkPrivateChat, checkGuildChat, checkOtherChat, checkRoleName, checkGuildName } from "../../../services/sdkService"; import { checkPrivateChat, checkGuildChat, checkOtherChat, checkRoleName, checkGuildName } from "../../../services/sdkService";
import { resResult, checkWhiteList, genCode } from "../../../pubUtils/util"; 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 { UserModel } from "../../../db/User";
import { SERVER_DEBUG_MODE } from "../../../pubUtils/dicParam"; import { SERVER_DEBUG_MODE } from "../../../pubUtils/dicParam";
import { nowSeconds } from "../../../pubUtils/timeUtil"; import { nowSeconds } from "../../../pubUtils/timeUtil";
import { getServerMainten } from "../../../services/gmService"; import { getServerMainten } from "../../../services/gmService";
import { errlogger } from "../../../util/logger";
export function globalFilter(app: Application) { export function globalFilter(app: Application) {
return new Filter(app); return new Filter(app);
} }
var Filter = function(this: any, app: Application) { var Filter = function(this: any, app: Application) {
this.app = app; this.app = app;
this.accessTime = new Map<string, number>();
}; };
Filter.prototype.before = async function (routeRecord: RouteRecord, msg: any, session: FrontendOrBackendSession, next: HandlerCallback) { 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) console.log('*********** global before ip', ip)
let guildCode = session.get('guildCode'); let guildCode = session.get('guildCode');
let blockType = session.get('blockType'); 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.BLOCK) return next(new Error(), resResult(STATUS.BLOCKED));
if(blockType == BLOCK_TYPE.BAN) { if(blockType == BLOCK_TYPE.BAN) {
if([ if([
+3
View File
@@ -26,6 +26,9 @@ export const REFRESH_TIME = 5; // 统一一天刷新时间
export const REF_CIRCLE_MAIL_TIME = 3; // 统一一天刷新定期邮件时间 export const REF_CIRCLE_MAIL_TIME = 3; // 统一一天刷新定期邮件时间
export const PUSH_BATCH = 100; // 推送分批人数 export const PUSH_BATCH = 100; // 推送分批人数
export const PUSH_INTERVAL = 5 * 1000; // 分批时间,5秒一批 export const PUSH_INTERVAL = 5 * 1000; // 分批时间,5秒一批
export const ACCESS_FREQUENCY = 500; // 玩家访问频率,一个玩家一个接口间隔时间 单位ms
export enum TIME_OUTPUT_TYPE { export enum TIME_OUTPUT_TYPE {
DATE = 1, DATE = 1,
STAMP_10 = 2, STAMP_10 = 2,
+1
View File
@@ -12,6 +12,7 @@ export const STATUS = {
BLOCK_WORDS: { code: 9, simStr: '内容有不合法词汇' }, BLOCK_WORDS: { code: 9, simStr: '内容有不合法词汇' },
BLOCKED: { code: 10, simStr: '您已被封禁' }, BLOCKED: { code: 10, simStr: '您已被封禁' },
BANNED: { code: 11, simStr: '您已被禁言' }, BANNED: { code: 11, simStr: '您已被禁言' },
ACCESS_BUSY: { code: 12, simStr: '您的操作过于繁忙' },
GLOBAL_ERR: { code: 1003, simStr: '服务器内部错误' }, GLOBAL_ERR: { code: 1003, simStr: '服务器内部错误' },
// http请求 // http请求
REQUEST_TIME_OUT: { code: 2000, simStr: '请求超时' }, REQUEST_TIME_OUT: { code: 2000, simStr: '请求超时' },