聊天:访问频率限制

This commit is contained in:
luying
2022-01-10 14:30:09 +08:00
parent 00c50ebe98
commit ac51a2302e
4 changed files with 47 additions and 17 deletions

View File

@@ -18,15 +18,27 @@ var Filter = function(this: any, app: Application) {
this.accessTime = new Map<string, number>();
};
Filter.prototype.checkFrequency = function(route: string, roleId: string) {
if(gameData.serverConst.PROTECT_API.indexOf(route) == -1) {
Filter.prototype.checkFrequency = function(route: string, msg: any, roleId: string) {
let protect = gameData.serverConst.PROTECT_API.find(cur => {
if(cur.route == route) {
let paramOK = true;
for(let key in cur.param) {
if(msg[key] != cur.param[key]) {
paramOK = false; break;
}
}
if(paramOK) return true;
}
return false;
});
if(!protect) {
return true;
}
let keyOfRouteWithUser = `${route}_${roleId}`;
let keyOfRouteWithUser = `${protect.id}_${roleId}`;
if(this.accessTime && this.accessTime.has(keyOfRouteWithUser)) {
console.log(this.accessTime.get(keyOfRouteWithUser) - Date.now());
if(Date.now() - this.accessTime.get(keyOfRouteWithUser) < gameData.serverConst.PROTECT_API_INTERVAL) {
errlogger.error(`${keyOfRouteWithUser} 间隔时间小于${gameData.serverConst.PROTECT_API_INTERVAL}ms${Date.now() - this.accessTime.get(keyOfRouteWithUser)}`);
if(Date.now() - this.accessTime.get(keyOfRouteWithUser) < protect.interval) {
errlogger.info(`${keyOfRouteWithUser} 间隔时间小于${protect.interval}ms${Date.now() - this.accessTime.get(keyOfRouteWithUser)}`);
return false;
}
}
@@ -54,7 +66,7 @@ Filter.prototype.before = async function (routeRecord: RouteRecord, msg: any, se
let guildCode = session.get('guildCode');
let blockType = session.get('blockType');
// 访问频率控制
if(!this.checkFrequency(routeRecord.route, roleId)) {
if(!this.checkFrequency(routeRecord.route, msg, roleId)) {
return next(new Error('globalFilter'), resResult(STATUS.ACCESS_BUSY));
}
if(!this.checkFunction(routeRecord.route)) {
@@ -67,6 +79,7 @@ Filter.prototype.before = async function (routeRecord: RouteRecord, msg: any, se
if([
'chat.chatHandler.sendPrivateMessage',
'chat.chatHandler.sendGroupMessage',
'battle.comBattleHandler.sendTeamMsg',
'battle.barrageHandler.sendBarrage',
'guild.guildHandler.sendMail',
'guild.guildHandler.setGuildInfo',
@@ -113,6 +126,12 @@ Filter.prototype.before = async function (routeRecord: RouteRecord, msg: any, se
break;
}
}
case 'battle.comBattleHandler.sendTeamMsg':
{
let result = await checkOtherChat(roleId, CHANNEL_PREFIX.TEAM, msg.content);
if(!result) hasBlockWords = true;
break;
}
case 'battle.barrageHandler.sendBarrage':
{
let result = await checkOtherChat(roleId, CHANNEL_PREFIX.WORLD, msg.content);

View File

@@ -12,7 +12,7 @@ export const STATUS = {
BLOCK_WORDS: { code: 9, simStr: '内容有不合法词汇' },
BLOCKED: { code: 10, simStr: '您已被封禁' },
BANNED: { code: 11, simStr: '您已被禁言' },
ACCESS_BUSY: { code: 12, simStr: '您的操作过于繁忙' },
ACCESS_BUSY: { code: 12, simStr: '您不久前发送过消息了,请稍等一下' },
ONLINE_USER_MAX: { code: 13, simStr: '服务器繁忙,请稍后再试' },
FUNCTION_CLOSE: { code: 14, simStr: '抱歉,该功能暂时关闭' },
GLOBAL_ERR: { code: 1003, simStr: '服务器内部错误' },

View File

@@ -2,11 +2,21 @@
import { readFileAndParse } from '../util'
import { FILENAME } from '../../consts'
interface ProtectApi {
// id
readonly id: number;
// 描述
readonly desc: string;
// 接口
readonly route: string;
// 参数
readonly param: any;
// 频率 毫秒
readonly interval: number;
}
export interface DicServerConst {
// 保护接口,间隔时间
readonly PROTECT_API_INTERVAL: number;
// 保护的接口
readonly PROTECT_API: string[];
readonly PROTECT_API: ProtectApi[];
// 某些api功能关闭
readonly API_IS_CLOSE: number;
// 关闭的接口

View File

@@ -1,13 +1,14 @@
{
"PROTECT_API_INTERVAL": 500,
"PROTECT_API": [
"chat.chatHandler.sendGroupMessage",
"chat.chatHandler.sendPrivateMessage",
"battle.barrageHandler.sendBarrage"
],
"API_IS_CLOSE": 0,
"CLOSE_APIS": [],
"CLOSE_LOGIN": 0,
"CLOSE_LOGIN_WHEN_ONLINE_MAX": 1,
"MAX_ONLINE_USER_COUNT": 5000
"MAX_ONLINE_USER_COUNT": 5000,
"PROTECT_API": [
{ "id": 1, "desc": "世界聊天", "route": "chat.chatHandler.sendGroupMessage", "param": { "channel": "world" }, "interval": 20000 },
{ "id": 2, "desc": "军团聊天", "route": "chat.chatHandler.sendGroupMessage", "param": { "channel": "guild" }, "interval": 5000 },
{ "id": 3, "desc": "组团聊天", "route": "battle.comBattleHandler.sendTeamMsg", "param": {}, "interval": 5000 },
{ "id": 4, "desc": "私聊", "route": "chat.chatHandler.sendPrivateMessage", "param": {}, "interval": 5000 },
{ "id": 5, "desc": "发送弹幕", "route": "battle.barrageHandler.sendBarrage", "param": {}, "interval": 5000 }
]
}