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

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

View File

@@ -25,9 +25,6 @@ export default class FooBoot implements IBoot {
}
// 如果gm使用的就是本机代理host不转发到target而只把path替换
if(this.app.config.httpProxy && this.app.config.httpProxy[`/web/${this.app.config.realEnv}/`]) {
this.app.config.httpProxy[`/web/${this.app.config.realEnv}/`].changeOrigin = false;
}
if(this.app.config.httpProxy && this.app.config.httpProxy[`/api/${this.app.config.realEnv}/`]) {
this.app.config.httpProxy[`/api/${this.app.config.realEnv}/`].changeOrigin = false;
}

View File

@@ -96,15 +96,6 @@ export default (appInfo: EggAppInfo) => {
return path.replace(`/api/${env}/`, '/api/')
}
}
httpProxy[`/web/${env}/`] = {
target: domain,
changeOrigin: true,
secure: true,
pathRewrite: function(path) {
console.log('proxy', path, path.replace(`/web/${env}/`, '/web/'))
return path.replace(`/web/${env}/`, '/web/')
}
}
}
config.proxy = httpProxy;

View File

@@ -24,7 +24,7 @@ export default (appInfo: EggAppInfo) => {
};
let regions = [ // 大区数据
{ id: 1, env: 'development', name: "测试", domain: 'http://127.0.0.1:7500' },
{ id: 1, env: 'development', name: "测试", domain: 'http://127.0.0.1:9000' },
];
config.regions = regions;
@@ -39,15 +39,6 @@ export default (appInfo: EggAppInfo) => {
return path.replace(`/api/${env}/`, '/api/')
}
}
httpProxy[`/web/${env}/`] = {
target: domain,
changeOrigin: true,
secure: true,
pathRewrite: function(path) {
console.log('proxy', path, path.replace(`/web/${env}/`, '/web/'))
return path.replace(`/web/${env}/`, '/web/')
}
}
}
config.proxy = httpProxy;

View File

@@ -486,6 +486,7 @@ export const FILENAME = {
FILTER_WORDS: 'filterWords',
DIC_GUILD_WISH_REWARD: 'dic_army_wishReward',
DIC_API: 'dic_api',
DIC_SERVER_CONST: 'server_const',
}
export const WAR_RELATE_TABLES = [

View File

@@ -13,6 +13,7 @@ export const STATUS = {
BLOCKED: { code: 10, simStr: '您已被封禁' },
BANNED: { code: 11, simStr: '您已被禁言' },
ACCESS_BUSY: { code: 12, simStr: '您的操作过于繁忙' },
ONLINE_USER_MAX: { code: 13, simStr: '服务器繁忙,请稍后再试' },
GLOBAL_ERR: { code: 1003, simStr: '服务器内部错误' },
// http请求
REQUEST_TIME_OUT: { code: 2000, simStr: '请求超时' },

View File

@@ -97,6 +97,7 @@ import { dicPvpDifficultRatio, loadPvpDifficultRatio } from './dictionary/DicPvp
import { dicWhiteIp, loadWhiteIp } from './dictionary/DicWhiteIp';
import { dicGuildWishReward, loadGuildWishReward } from './dictionary/DicGuildWishReward';
import { dicApiById, dicApiByUrl, loadApi } from './dictionary/DicApi';
import { dicServerConst, loadServerConst } from './dictionary/DicServerConst';
import { pick } from "underscore";
import _ = require("underscore");
@@ -246,6 +247,7 @@ export const gameData = {
guildWishReward: dicGuildWishReward,
apiById: dicApiById,
apiByUrl: dicApiByUrl,
serverConst: dicServerConst,
};
// 在此提供一些原先在gamedata中提供的方法以便更方便获取gameData数据
@@ -995,6 +997,7 @@ function loadDatas() {
treatTaskGroup();
loadGuildWishReward();
loadApi();
loadServerConst();
}
// 重载dicParam

View File

@@ -0,0 +1,29 @@
// 服务器名
import { readFileAndParse } from '../util'
import { FILENAME } from '../../consts'
export interface DicServerConst {
// 保护接口,间隔时间
readonly PROTECT_API_INTERVAL: number;
// 保护的接口
readonly PROTECT_API: string[];
// 这些api功能关闭
readonly API_IS_CLOSE: number;
// 关闭的接口
readonly CLOSE_APIS: string[];
// 是否直接关掉登录
readonly CLOSE_LOGIN: number;
// 是否当检测在线玩家过多是关掉登录
readonly CLOSE_LOGIN_WHEN_ONLINE_MAX: number;
// 最大在线人数
readonly MAX_ONLINE_USER_COUNT: number;
}
export const dicServerConst: DicServerConst = {} as DicServerConst;
export function loadServerConst() {
let json = readFileAndParse(FILENAME.DIC_SERVER_CONST);
for(let key in json) {
dicServerConst[key] = json[key];
}
json = undefined;
}

View File

@@ -74,6 +74,8 @@ declare module 'redis' {
publishAsync(channel: string, message: string): Promise<number>;
// 订阅给定的一个或多个频道的信息
subscribeAsync(...channels: string[]): Promise<number>;
// 返回hash字段数量
hlenAsync(key: string): Promise<number>;
}
export interface Multi extends Commands<Multi> {
execAsync(...args: any[]): Promise<any>;

View File

@@ -0,0 +1,15 @@
{
"PROTECT_API_INTERVAL": 500,
"PROTECT_API": [
"chat.chatHandler.sendGroupMessage",
"chat.chatHandler.sendPrivateMessage",
"battle.barrageHandler.sendBarrage"
],
"API_IS_CLOSE": 1,
"CLOSE_APIS": [
"chat.chatHandler.sendGroupMessage"
],
"CLOSE_LOGIN": 0,
"CLOSE_LOGIN_WHEN_ONLINE_MAX": 1,
"MAX_ONLINE_USER_COUNT": 5000
}

View File

@@ -25,6 +25,10 @@ export default class FooBoot implements IBoot {
if(this.app.config.env == 'local') {
this.app.config.realEnv = 'development';
}
// 如果gm使用的就是本机代理host不转发到target而只把path替换
if(this.app.config.httpProxy && this.app.config.httpProxy[`/web/${this.app.config.realEnv}/`]) {
this.app.config.httpProxy[`/web/${this.app.config.realEnv}/`].changeOrigin = false;
}
this.connectThinkingData(this.app);
}

View File

@@ -0,0 +1,4 @@
import proxy from 'egg-http-proxy-middleware';
module.exports = proxy;

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) {

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;
}
}

View File

@@ -18,7 +18,7 @@ export default (appInfo: EggAppInfo) => {
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',
};
// add your egg config in here
config.middleware = [ 'parmsDecode', 'getIp' ];
config.middleware = [ 'parmsDecode', 'getIp', 'proxy' ];
config.mongoose = {
url: 'mongodb://dbop:zyzdbopbantu@dds-8vbdb47c6fb58a541.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vbdb47c6fb58a542.mongodb.zhangbei.rds.aliyuncs.com:3717/zyz?replicaSet=mgset-500808098', // 内网
@@ -75,6 +75,31 @@ export default (appInfo: EggAppInfo) => {
sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
};
let regions = [ // 大区数据
{ env: 'stable', name: "常山少年", domain: 'http://zyz_gm.trgame.cn' },
{ env: 'alpha', name: "测试服", domain: 'http://pinus_gm_test.trgame.cn' },
{ env: 'dev', name: "开发服", domain: 'http://zyzdev_gm.trgame.cn' },
// 37测试服组
{ env: 'sq1', name: "37测试1服", domain: 'http://gm-sgzyz.37wan.com' },
];
config.regions = regions;
let httpProxy: any = {};
for(let { env, domain } of regions) {
httpProxy[`/web/${env}/`] = {
target: domain,
changeOrigin: true,
secure: true,
pathRewrite: function(path) {
console.log('proxy', path, path.replace(`/web/${env}/`, '/web/'))
return path.replace(`/web/${env}/`, '/web/')
}
}
}
config.proxy = httpProxy;
// the return config will combines to EggAppConfig
return {
...config,

View File

@@ -16,7 +16,24 @@ export default (appInfo: EggAppInfo) => {
url: '127.0.0.1', // 内网
pw: ''
};
let regions = [ // 大区数据
{ id: 1, env: 'development', name: "测试", domain: 'http://127.0.0.1:9000' },
];
config.regions = regions;
let httpProxy: any = {};
for(let { env, domain } of regions) {
httpProxy[`/web/${env}/`] = {
target: domain,
changeOrigin: true,
secure: true,
pathRewrite: function(path) {
console.log('proxy', path, path.replace(`/web/${env}/`, '/web/'))
return path.replace(`/web/${env}/`, '/web/')
}
}
}
config.proxy = httpProxy;
config.decodeParm = true;
// the return config will combines to EggAppConfig

View File

@@ -491,6 +491,14 @@
"resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-1.8.0.tgz",
"integrity": "sha512-2aoSC4UUbHDj2uCsCxcG/vRMXey/m17bC7UwitVm5hn22nI8O8Y9iDpA76Orc+DWkQ4zZrOKEshCqR/jSuXAHA=="
},
"@types/http-proxy": {
"version": "1.17.8",
"resolved": "https://registry.npmjs.org/@types/http-proxy/-/http-proxy-1.17.8.tgz",
"integrity": "sha512-5kPLG5BKpWYkw/LVOGWpiq3nEVqxiN32rTgI53Sk12/xHFQ2rG3ehI9IO+O3W2QoKeyB92dJkoka8SUm6BX1pA==",
"requires": {
"@types/node": "*"
}
},
"@types/istanbul-lib-coverage": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.3.tgz",
@@ -591,8 +599,7 @@
"@types/node": {
"version": "7.10.12",
"resolved": "https://registry.npmjs.org/@types/node/-/node-7.10.12.tgz",
"integrity": "sha512-SieeKQUj5NbRKZtRH/6T3H2XQCraXqSEc+gABTXNtL4uwPtXmoZEaHJ3WJylfwF3vJzj5ha6Aq8h9dbKFBINiQ==",
"dev": true
"integrity": "sha512-SieeKQUj5NbRKZtRH/6T3H2XQCraXqSEc+gABTXNtL4uwPtXmoZEaHJ3WJylfwF3vJzj5ha6Aq8h9dbKFBINiQ=="
},
"@types/power-assert": {
"version": "1.5.3",
@@ -4240,6 +4247,62 @@
"utility": "^1.13.1"
}
},
"egg-http-proxy-middleware": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/egg-http-proxy-middleware/-/egg-http-proxy-middleware-1.0.3.tgz",
"integrity": "sha512-mS5MGg8sBNMaWGieJ8DJ/+f51dQTl303v7wvxSM+w4yE5AsQx8Lh9pIY2PpBk/OvvPBHpwEuMNU23tSH7qQcQQ==",
"requires": {
"http-proxy-middleware": "^1.0.4",
"is-glob": "^4.0.1",
"koa-connect": "^2.1.0",
"micromatch": "^4.0.2"
},
"dependencies": {
"braces": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz",
"integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==",
"requires": {
"fill-range": "^7.0.1"
}
},
"fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
"integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==",
"requires": {
"to-regex-range": "^5.0.1"
}
},
"is-number": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
"integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng=="
},
"micromatch": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.4.tgz",
"integrity": "sha512-pRmzw/XUcwXGpD9aI9q/0XOwLNygjETJ8y0ao0wdqprrzDa4YnxLcz7fQRZr8voh8V10kGhABbNcHVk5wHgWwg==",
"requires": {
"braces": "^3.0.1",
"picomatch": "^2.2.3"
}
},
"picomatch": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz",
"integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA=="
},
"to-regex-range": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",
"integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==",
"requires": {
"is-number": "^7.0.0"
}
}
}
},
"egg-i18n": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/egg-i18n/-/egg-i18n-2.1.1.tgz",
@@ -5425,6 +5488,11 @@
"through": "~2.3.1"
}
},
"eventemitter3": {
"version": "4.0.7",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz",
"integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw=="
},
"execa": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/execa/-/execa-1.0.0.tgz",
@@ -5692,6 +5760,11 @@
"resolved": "https://registry.npmjs.org/flatted/-/flatted-2.0.2.tgz",
"integrity": "sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA=="
},
"follow-redirects": {
"version": "1.14.6",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.6.tgz",
"integrity": "sha512-fhUl5EwSJbbl8AR+uYL2KQDxLkdSjZGR36xy46AO7cOMTrCMON6Sa28FmAnC2tRTDbd/Uuzz3aJBv7EBN7JH8A=="
},
"for-in": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",
@@ -6181,6 +6254,16 @@
"toidentifier": "1.0.0"
}
},
"http-proxy": {
"version": "1.18.1",
"resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.1.tgz",
"integrity": "sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==",
"requires": {
"eventemitter3": "^4.0.0",
"follow-redirects": "^1.0.0",
"requires-port": "^1.0.0"
}
},
"http-proxy-agent": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-2.1.0.tgz",
@@ -6205,6 +6288,63 @@
}
}
},
"http-proxy-middleware": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/http-proxy-middleware/-/http-proxy-middleware-1.3.1.tgz",
"integrity": "sha512-13eVVDYS4z79w7f1+NPllJtOQFx/FdUW4btIvVRMaRlUY9VGstAbo5MOhLEuUgZFRHn3x50ufn25zkj/boZnEg==",
"requires": {
"@types/http-proxy": "^1.17.5",
"http-proxy": "^1.18.1",
"is-glob": "^4.0.1",
"is-plain-obj": "^3.0.0",
"micromatch": "^4.0.2"
},
"dependencies": {
"braces": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz",
"integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==",
"requires": {
"fill-range": "^7.0.1"
}
},
"fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
"integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==",
"requires": {
"to-regex-range": "^5.0.1"
}
},
"is-number": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
"integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng=="
},
"micromatch": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.4.tgz",
"integrity": "sha512-pRmzw/XUcwXGpD9aI9q/0XOwLNygjETJ8y0ao0wdqprrzDa4YnxLcz7fQRZr8voh8V10kGhABbNcHVk5wHgWwg==",
"requires": {
"braces": "^3.0.1",
"picomatch": "^2.2.3"
}
},
"picomatch": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz",
"integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA=="
},
"to-regex-range": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",
"integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==",
"requires": {
"is-number": "^7.0.0"
}
}
}
},
"http-signature": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",
@@ -6673,6 +6813,11 @@
"integrity": "sha1-PkcprB9f3gJc19g6iW2rn09n2w8=",
"dev": true
},
"is-plain-obj": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-3.0.0.tgz",
"integrity": "sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA=="
},
"is-plain-object": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.4.tgz",
@@ -7006,6 +7151,11 @@
"any-promise": "^1.1.0"
}
},
"koa-connect": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/koa-connect/-/koa-connect-2.1.0.tgz",
"integrity": "sha512-O9pcFafHk0oQsBevlbTBlB9co+2RUQJ4zCzu3qJPmGlGoeEZkne+7gWDkecqDPSbCtED6LmhlQladxs6NjOnMQ=="
},
"koa-convert": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/koa-convert/-/koa-convert-1.2.0.tgz",
@@ -9865,6 +10015,11 @@
"resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-1.0.1.tgz",
"integrity": "sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE="
},
"requires-port": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
"integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8="
},
"resolve": {
"version": "1.17.0",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz",

View File

@@ -37,6 +37,7 @@
"egg": "^2.6.1",
"egg-alinode": "^2.0.1",
"egg-cors": "^2.2.3",
"egg-http-proxy-middleware": "^1.0.3",
"egg-scripts": "^2.6.0",
"egg-view-nunjucks": "^2.2.0",
"egg-xtransit": "^1.2.2",

View File

@@ -6,6 +6,7 @@ import ExportCheckMainten from '../../../app/middleware/checkMainten';
import ExportGetIp from '../../../app/middleware/getIp';
import ExportGmTokenParser from '../../../app/middleware/gmTokenParser';
import ExportParmsDecode from '../../../app/middleware/parmsDecode';
import ExportProxy from '../../../app/middleware/proxy';
import ExportTokenParser from '../../../app/middleware/tokenParser';
declare module 'egg' {
@@ -14,6 +15,7 @@ declare module 'egg' {
getIp: typeof ExportGetIp;
gmTokenParser: typeof ExportGmTokenParser;
parmsDecode: typeof ExportParmsDecode;
proxy: typeof ExportProxy;
tokenParser: typeof ExportTokenParser;
}
}