aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/app.ts12
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/app.ts b/lib/app.ts
index b2c3c1a7..e349f1ee 100644
--- a/lib/app.ts
+++ b/lib/app.ts
@@ -95,8 +95,16 @@ setInterval(resolveUntrusted, 30000).unref();
app.addHook("onRequest", async (req) => {
const peer = (req.socket.remoteAddress || "").replace(/^::ffff:/, "");
- if (!untrusted.has(peer)) return;
- req.headers["cf-connecting-ip"] = peer;
+ // The onion service points at lb, not at us, so the peer is usually lb.
+ // lb (Caddy, no trusted_proxies) overwrites x-forwarded-for with its own
+ // peer, so a tor address there means the request came in over the onion.
+ const xff = req.headers["x-forwarded-for"];
+ const hop = (Array.isArray(xff) ? xff.at(-1) : xff?.split(",").at(-1))
+ ?.trim()
+ .replace(/^::ffff:/, "");
+ const viaTor = untrusted.has(peer) || (hop && untrusted.has(hop));
+ if (!viaTor) return;
+ req.headers["cf-connecting-ip"] = untrusted.has(peer) ? peer : hop;
delete req.headers["x-forwarded-for"];
delete req.headers["rate-limit-by"];
});