From b052f6bee93437b75f2642b7eaf7cb3a0e6dded4 Mon Sep 17 00:00:00 2001 From: LatoWolf Date: Tue, 26 May 2026 15:49:57 +0800 Subject: [PATCH 1/2] Add support for reverse proxy using header --- src/eamuse/index.ts | 5 +++-- src/middlewares/EamuseMiddleware.ts | 34 +++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/eamuse/index.ts b/src/eamuse/index.ts index a4ca47d..e5f4e58 100644 --- a/src/eamuse/index.ts +++ b/src/eamuse/index.ts @@ -92,8 +92,9 @@ export const services = (port: number, plugins: EamusePlugin[]) => { ], }; - const url = - port == 80 ? `http://${(info as any).host}` : `http://${(info as any).host}:${port}`; + const host = (info as any).host; + const protocol = (info as any).protocol || 'http'; + const url = `${protocol}://${host}`; for (const moduleName of coreModules) { services.item.push({ '@attr': { name: moduleName, url } }); diff --git a/src/middlewares/EamuseMiddleware.ts b/src/middlewares/EamuseMiddleware.ts index ab9f91c..e83d743 100644 --- a/src/middlewares/EamuseMiddleware.ts +++ b/src/middlewares/EamuseMiddleware.ts @@ -37,6 +37,28 @@ export interface EamuseInfo { model: string; } +function forwardedHeader(req: any, name: string) { + const value = req.headers?.[name]; + if (typeof value !== 'string') { + return ''; + } + return value.split(',')[0].trim(); +} + +function stripPort(host: string) { + if (!host) { + return host; + } + + if (host.startsWith('[')) { + const end = host.indexOf(']'); + return end >= 0 ? host.slice(1, end) : host; + } + + const colonIndex = host.lastIndexOf(':'); + return colonIndex > 0 ? host.slice(0, colonIndex) : host; +} + export const EamuseMiddleware: RequestHandler = async (req, res, next) => { res.set('X-Powered-By', 'Asphyxia'); @@ -166,12 +188,20 @@ export const EamuseRoute = (router: EamuseRootRouter): RequestHandler => { // HACK: give facility ip if (body.module == 'facility' && body.method == 'get') { - (info as any).ip = req.ip.includes(':') ? '127.0.0.1' : req.ip; + const forwardedFor = forwardedHeader(req, 'x-forwarded-for'); + const clientIp = forwardedFor || req.ip; + (info as any).ip = clientIp.includes(':') ? '127.0.0.1' : clientIp; } // HACK: give services host if (body.module == 'services' && body.method == 'get') { - (info as any).host = req.hostname; + const forwardedHost = forwardedHeader(req, 'x-forwarded-host'); + const forwardedProto = forwardedHeader(req, 'x-forwarded-proto'); + + (info as any).host = forwardedHost + ? stripPort(forwardedHost) + : req.get('host') || req.hostname; + (info as any).protocol = forwardedProto || req.protocol; } try { From 09eeb14cdc960aabc72a8d553b30f6bb79dc6692 Mon Sep 17 00:00:00 2001 From: LatoWolf Date: Tue, 26 May 2026 15:55:24 +0800 Subject: [PATCH 2/2] Reverse port detection when not using reverse proxy --- src/eamuse/index.ts | 6 +++++- src/middlewares/EamuseMiddleware.ts | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/eamuse/index.ts b/src/eamuse/index.ts index e5f4e58..e74d45c 100644 --- a/src/eamuse/index.ts +++ b/src/eamuse/index.ts @@ -94,7 +94,11 @@ export const services = (port: number, plugins: EamusePlugin[]) => { const host = (info as any).host; const protocol = (info as any).protocol || 'http'; - const url = `${protocol}://${host}`; + const url = (info as any).proxy + ? `${protocol}://${host}` + : port == 80 + ? `http://${host}` + : `http://${host}:${port}`; for (const moduleName of coreModules) { services.item.push({ '@attr': { name: moduleName, url } }); diff --git a/src/middlewares/EamuseMiddleware.ts b/src/middlewares/EamuseMiddleware.ts index e83d743..14a7325 100644 --- a/src/middlewares/EamuseMiddleware.ts +++ b/src/middlewares/EamuseMiddleware.ts @@ -202,6 +202,7 @@ export const EamuseRoute = (router: EamuseRootRouter): RequestHandler => { ? stripPort(forwardedHost) : req.get('host') || req.hostname; (info as any).protocol = forwardedProto || req.protocol; + (info as any).proxy = Boolean(forwardedHost || forwardedProto); } try {