diff --git a/src/index.ts b/src/index.ts index 7e62c30..d3e59e8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,7 +38,7 @@ async function debouncePurgeCache() { cachePurgeTimeout = setTimeout(async () => { await purgeCache(); }, 10000); - } +} app.set('trust proxy', true); @@ -134,9 +134,9 @@ proxy.on('proxyReq', function (proxyReq, req, res, options) { const requestId = crypto.randomBytes(8).toString('hex'); (req as any).requestId = requestId; - const clientIp = req.headers['x-original-forwarded-for'] || - req.headers['x-forwarded-for'] || - req.connection.remoteAddress; + const clientIp = req.headers['x-original-forwarded-for'] || + req.headers['x-forwarded-for'] || + req.connection.remoteAddress; const logEntry = { timestamp: new Date().toISOString(), @@ -160,17 +160,18 @@ proxy.on('proxyReq', function (proxyReq, req, res, options) { proxyReq.setHeader('X-Proxy-Instance', `${proxyInstanceId}:${requestId}`); // Preserve the original client IP - const originalIp = req.headers['x-original-forwarded-for'] || - req.headers['x-forwarded-for'] || - req.connection.remoteAddress; - + const originalIp = req.headers['x-original-forwarded-for'] || + req.headers['x-forwarded-for'] || + req.connection.remoteAddress; + // Append our proxy's IP to the x-forwarded-for chain - const forwardedFor = req.headers['x-forwarded-for'] + const forwardedFor = req.headers['x-forwarded-for'] ? `${req.headers['x-forwarded-for']}, ${req.connection.remoteAddress}` : originalIp; proxyReq.setHeader('x-forwarded-for', forwardedFor as string); proxyReq.setHeader('x-real-ip', originalIp as string); + proxyReq.setHeader('x-forwarded-proto', 'https'); // Preserve original headers // Object.keys(req.headers).forEach(function (key) { @@ -285,9 +286,8 @@ async function deleteOldCacheDirectories(): Promise { }); const results = await Promise.allSettled(deletePromises); - return `Deleted ${ - results.filter((r) => r.status === 'fulfilled').length - } out of ${files.length} directories.`; + return `Deleted ${results.filter((r) => r.status === 'fulfilled').length + } out of ${files.length} directories.`; } app.listen(PORT, () => {