Skip to content

Commit

Permalink
add x-forward-proto
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis Fedoruk-Betschki committed Jan 9, 2025
1 parent ee04080 commit 9edadca
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function debouncePurgeCache() {
cachePurgeTimeout = setTimeout(async () => {
await purgeCache();
}, 10000);
}
}

app.set('trust proxy', true);

Expand Down Expand Up @@ -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(),
Expand All @@ -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) {
Expand Down Expand Up @@ -285,9 +286,8 @@ async function deleteOldCacheDirectories(): Promise<string> {
});

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, () => {
Expand Down

0 comments on commit 9edadca

Please sign in to comment.