防护:添加人数过多防护措施

This commit is contained in:
luying
2022-01-08 17:13:31 +08:00
parent f2c4be6443
commit 0f8a4a946e
18 changed files with 287 additions and 25 deletions
+4
View File
@@ -0,0 +1,4 @@
import proxy from 'egg-http-proxy-middleware';
module.exports = proxy;
+4
View File
@@ -289,6 +289,10 @@ export default class Auth extends Service {
public async checkRole(serverId: number) {
const ctx = this.ctx;
const { uid } = ctx;
let canLogin = await this.ctx.service.utils.validateCanLogin();
if(!canLogin) return this.ctx.service.utils.resResult(STATUS.ONLINE_USER_MAX);
const role = await RoleModel.findByUid(uid, serverId);
if (role) {
if(role.blockType == BLOCK_TYPE.BLOCK) {
+20
View File
@@ -2,6 +2,9 @@ import { Service } from 'egg';
import { resResult as pubResult } from '../pubUtils/util';
import { unlockFigure } from 'app/pubUtils/itemUtils';
import { RoleType } from '@db/Role';
import { gameData } from 'app/pubUtils/data';
import { RedisClient } from 'redis';
import { REDIS_KEY } from '@consts';
const csprng = require('csprng');
/**
* Utils Service
@@ -59,4 +62,21 @@ export default class Utils extends Service {
log.error(`${message}`);
}
}
// 检测是否可以登录
public async validateCanLogin() {
console.log('********* serverConst', gameData.serverConst)
if(gameData.serverConst.CLOSE_LOGIN == 1) return false;
if(gameData.serverConst.CLOSE_LOGIN_WHEN_ONLINE_MAX) {
let redisClient: RedisClient = this.ctx.app.context.redisClient;
let count = await redisClient.hlenAsync(REDIS_KEY.ONLINE_USERS);
console.log('********* count', count)
if(count >= gameData.serverConst.MAX_ONLINE_USER_COUNT) {
return false
}
}
return true;
}
}