This commit is contained in:
xy
2026-03-13 01:38:40 +00:00
parent e6e4f88257
commit 28855885cd
311 changed files with 89544 additions and 94350 deletions

View File

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