Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/eamuse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ 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 = (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 } });
Expand Down
35 changes: 33 additions & 2 deletions src/middlewares/EamuseMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -166,12 +188,21 @@ 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;
(info as any).proxy = Boolean(forwardedHost || forwardedProto);
}

try {
Expand Down
Loading