init
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import { STATUS } from '@consts';
|
||||
import { ServerlistModel } from '@db/Serverlist';
|
||||
import { nowSeconds } from 'app/pubUtils/timeUtil';
|
||||
import { checkWhiteList } from 'app/pubUtils/sysUtil';
|
||||
import { nowSeconds } from '@pubUtils/timeUtil';
|
||||
import { checkWhiteList } from '@pubUtils/sysUtil';
|
||||
import { RoleModel } from '@db/Role';
|
||||
import { Context } from 'egg';
|
||||
|
||||
module.exports = () => {
|
||||
return async function checkMainten(ctx, next) {
|
||||
return async function checkMainten(ctx: Context, next: () => Promise<any>) {
|
||||
const { serverId, version } = ctx.request.body;
|
||||
if (serverId) {
|
||||
let server = await ServerlistModel.findByServerId(serverId);
|
||||
|
||||
@@ -2,6 +2,14 @@ import c2k = require('koa-connect');
|
||||
import { createProxyMiddleware, Options as ContextOptions } from 'http-proxy-middleware';
|
||||
import * as micromatch from 'micromatch';
|
||||
import * as isGlob from 'is-glob';
|
||||
import { Context } from 'egg';
|
||||
|
||||
// 扩展Request接口,添加rawBody属性
|
||||
declare module 'egg' {
|
||||
interface Request {
|
||||
rawBody: string;
|
||||
}
|
||||
}
|
||||
|
||||
function match(context: string, path: string): boolean {
|
||||
// single path
|
||||
@@ -19,7 +27,7 @@ export interface Options {
|
||||
}
|
||||
|
||||
export default function (options: Options) {
|
||||
return async (ctx, next) => {
|
||||
return async (ctx: Context, next: () => Promise<any>) => {
|
||||
for (const context of Object.keys(options)) {
|
||||
if (match(context, ctx.path)) {
|
||||
const contextOptions: ContextOptions = options[context];
|
||||
@@ -41,7 +49,7 @@ export default function (options: Options) {
|
||||
return proxyReq;
|
||||
},
|
||||
}) as any
|
||||
)(ctx, next);
|
||||
)(ctx as any, next);
|
||||
}
|
||||
}
|
||||
await next();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Context } from 'egg';
|
||||
|
||||
module.exports = () => {
|
||||
return async function parmsDecode(ctx: Context, next) {
|
||||
return async function parmsDecode(ctx: Context, next: () => Promise<any>) {
|
||||
|
||||
let clientIp = null;
|
||||
if (ctx.header['x-forwarded-for'] && (typeof ctx.header['x-forwarded-for'] == 'string')) {
|
||||
|
||||
@@ -2,17 +2,19 @@ import { GMUserModel } from '@db/GMUser';
|
||||
import { GMGroupModel } from '@db/GMGroup'
|
||||
import { GMRecordModel } from '@db/GMRecord'
|
||||
import { GM_API_TYPE, STATUS } from '@consts';
|
||||
import { gameData } from 'app/pubUtils/data';
|
||||
import { gameData } from '@pubUtils/data';
|
||||
import { Context } from 'egg';
|
||||
|
||||
module.exports = () => {
|
||||
return async function tokenParser(ctx, next) {
|
||||
return async function tokenParser(ctx: Context, next: () => Promise<any>) {
|
||||
|
||||
if (!ctx.request.headers || !ctx.request.headers.token) {
|
||||
console.error('token not found');
|
||||
ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
|
||||
return;
|
||||
}
|
||||
const user = await GMUserModel.getGmAccountByToken(ctx.request.headers.token);
|
||||
const token = typeof ctx.request.headers.token === 'string' ? ctx.request.headers.token : ctx.request.headers.token[0];
|
||||
const user = await GMUserModel.getGmAccountByToken(token);
|
||||
if (!user) {
|
||||
console.error('token invalid');
|
||||
ctx.body = ctx.service.utils.resResult(STATUS.TOKEN_ERR);
|
||||
|
||||
@@ -1,39 +1,41 @@
|
||||
import { genCode } from 'app/pubUtils/util';
|
||||
import { MsgEncrypt } from "app/pubUtils/sysUtil";
|
||||
import { genCode } from '../../../shared/pubUtils/util';
|
||||
import { MsgEncrypt } from "../../../shared/pubUtils/sysUtil";
|
||||
import { Context } from 'egg';
|
||||
|
||||
module.exports = options => {
|
||||
return async function parmsDecode(ctx: Context, next) {
|
||||
module.exports = (options: any) => {
|
||||
return async function parmsDecode(ctx: Context, next: () => Promise<any>) {
|
||||
let url = ctx.request.url;
|
||||
ctx.logcode = genCode(10);
|
||||
if(url.indexOf("/dev") == 0 || url.indexOf("/web") == 0 || url.indexOf("/cb") == 0) {
|
||||
if (url.indexOf("/dev") == 0 || url.indexOf("/web") == 0 || url.indexOf("/cb") == 0) {
|
||||
ctx.service.utils.log('INFO', `[${ctx.request.url}] [${ctx.logcode}] request: ${JSON.stringify(ctx.request.body)}`);
|
||||
await next();
|
||||
ctx.service.utils.log('INFO', `[${ctx.request.url}] [${ctx.logcode}] res: ${JSON.stringify(ctx.body)}`)
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (options.threshold && ctx.length < options.threshold) return;
|
||||
const reqBody = ctx.request.body;
|
||||
const reqHeader = ctx.request.header;
|
||||
|
||||
console.log(ctx.app.config.decodeParm)
|
||||
if(ctx.app.config.decodeParm == false) {
|
||||
if (ctx.app.config.decodeParm == false) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!reqBody.data) return;
|
||||
|
||||
let msgEncrypt = new MsgEncrypt({ encodeK: reqHeader['k'], encodeV: reqHeader['v'] });
|
||||
const k = typeof reqHeader['k'] === 'string' ? reqHeader['k'] : reqHeader['k'][0];
|
||||
const v = typeof reqHeader['v'] === 'string' ? reqHeader['v'] : reqHeader['v'][0];
|
||||
let msgEncrypt = new MsgEncrypt({ encodeK: k, encodeV: v });
|
||||
|
||||
console.log(`encode str ${msgEncrypt.encryptMsg(reqBody)}`);
|
||||
|
||||
try {
|
||||
let decryptResult = msgEncrypt.decryptMsg(reqBody.data);
|
||||
if(!decryptResult) throw new Error('params parse err');
|
||||
if (!decryptResult) throw new Error('params parse err');
|
||||
|
||||
ctx.request.body = decryptResult;
|
||||
console.log('req body', ctx.request.body);
|
||||
console.log('req body:', ctx.request.body);
|
||||
ctx.service.utils.log('INFO', `[${ctx.request.url}] [${ctx.logcode}] request: ${JSON.stringify(ctx.request.body)}`)
|
||||
} catch (e) {
|
||||
console.error('parms parse err');
|
||||
@@ -41,9 +43,9 @@ module.exports = options => {
|
||||
|
||||
}
|
||||
|
||||
try{
|
||||
try {
|
||||
await next();
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
ctx.service.utils.log('ERROR', `[${ctx.request.url}] [${ctx.logcode}] err: ${(<Error>e).stack}`);
|
||||
throw e;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
import { RegionModel } from '@db/Region';
|
||||
import { ServerlistModel } from '@db/Serverlist';
|
||||
import proxy from './egg-proxy';
|
||||
import { Context } from 'egg';
|
||||
|
||||
interface ProxyOptions {
|
||||
[key: string]: {
|
||||
target: string;
|
||||
changeOrigin: boolean;
|
||||
secure: boolean;
|
||||
pathRewrite?: (path: string) => string;
|
||||
headers?: { [key: string]: string };
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = () => {
|
||||
return async function (ctx, next) {
|
||||
return async function (ctx: Context, next: () => Promise<any>) {
|
||||
if(!ctx.app.config.envToHost) {
|
||||
let envToHost = new Map<string, string>();
|
||||
let regions = await RegionModel.getAllRegion();
|
||||
@@ -16,13 +27,13 @@ module.exports = () => {
|
||||
await getNewHost(ctx);
|
||||
}
|
||||
|
||||
let options = {};
|
||||
let options: ProxyOptions = {};
|
||||
for(let [env, webHost] of ctx.app.config.envToHost) {
|
||||
options[`/web/${env}/`] = {
|
||||
target: webHost,
|
||||
changeOrigin: true,
|
||||
secure: true,
|
||||
pathRewrite: function(path) {
|
||||
pathRewrite: function(path: string) {
|
||||
console.log('proxy', path, path.replace(`/web/${env}/`, '/web/'))
|
||||
return path.replace(`/web/${env}/`, '/web/')
|
||||
}
|
||||
@@ -37,7 +48,7 @@ module.exports = () => {
|
||||
}
|
||||
if(!!ctx.app.config.sidToHost.get(sid)) {
|
||||
options[url] = {
|
||||
target: ctx.app.config.sidToHost.get(sid),
|
||||
target: ctx.app.config.sidToHost.get(sid) as string,
|
||||
changeOrigin: true,
|
||||
secure: true,
|
||||
headers: { "is-proxy": "true" }
|
||||
@@ -50,20 +61,20 @@ module.exports = () => {
|
||||
};
|
||||
};
|
||||
|
||||
async function getNewHost(ctx) {
|
||||
async function getNewHost(ctx: Context) {
|
||||
let envToHost = ctx.app.config.envToHost||new Map();
|
||||
let sidToHost = new Map<string, string>();
|
||||
let servers = await ServerlistModel.getAllServerList();
|
||||
for(let { id, env, isMain } of servers) {
|
||||
let webHost = envToHost.get(env);
|
||||
sidToHost.set(id.toString(), webHost);
|
||||
if(isMain) sidToHost.set('main', webHost);
|
||||
sidToHost.set(id.toString(), webHost as string);
|
||||
if(isMain) sidToHost.set('main', webHost as string);
|
||||
}
|
||||
if(!sidToHost.has('main') && servers.length > 0) sidToHost.set('main', envToHost.get(servers[0].env));
|
||||
if(!sidToHost.has('main') && servers.length > 0) sidToHost.set('main', envToHost.get(servers[0].env) as string);
|
||||
ctx.app.config.sidToHost = sidToHost;
|
||||
}
|
||||
|
||||
function getProxyUrl(url: string) {
|
||||
function getProxyUrl(url: string): string | undefined {
|
||||
const urls = [
|
||||
'/cb/treatusername',
|
||||
'/cb/treatguildname',
|
||||
@@ -80,9 +91,10 @@ module.exports = () => {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function getServerId(url: string, ctx: any) {
|
||||
function getServerId(url: string, ctx: Context): string {
|
||||
switch(url) {
|
||||
case '/cb/treatusername':
|
||||
case '/cb/treatguildname':
|
||||
@@ -99,5 +111,7 @@ module.exports = () => {
|
||||
return ctx.query? ctx.query.dsid.toString(): ctx.request.body.dsid.toString();
|
||||
case '/cb/getrolebyuid':
|
||||
return 'main';
|
||||
default:
|
||||
return 'main';
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
import { STATUS } from '@consts';
|
||||
import { UserModel } from '@db/User';
|
||||
import { Context } from 'egg';
|
||||
|
||||
module.exports = () => {
|
||||
return async function tokenParser(ctx, next) {
|
||||
return async function tokenParser(ctx: Context, next: () => Promise<any>) {
|
||||
|
||||
if (!ctx.request.body || !ctx.request.body.token) {
|
||||
console.error('token not found');
|
||||
|
||||
Reference in New Issue
Block a user