From 2c0e0dcedc273e7460f28e04a63eccac68991974 Mon Sep 17 00:00:00 2001 From: martbost Date: Fri, 13 Nov 2026 16:02:08 -0600 Subject: [PATCH] Client IP: rightmost X-Forwarded-For (the one Coolify's proxy adds); cf-connecting-ip only with CF_PROXIED=1. The domain is not on Cloudflare, so a visitor could send a fake cf-connecting-ip and count as many unique viewers. Smoke 76/76 Co-Authored-By: Claude Opus 5.5 --- server.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 819d6be..e0f6cfd 100644 --- a/server.js +++ b/server.js @@ -61,7 +61,13 @@ function readRaw(req, max) { async function readBody(req) { const b = await readRaw(req, 32 * 1024); try { return b.length ? JSON.parse(b.toString('utf8')) : {}; } catch (e) { return {}; } } function cookies(req) { const o = {}; for (const p of (req.headers.cookie || '').split(';')) { const i = p.indexOf('='); if (i > 0) { try { o[p.slice(0, i).trim()] = decodeURIComponent(p.slice(i + 1).trim()); } catch (e) {} } } return o; } function cookie(name, val, maxAgeSec, httpOnly) { return name + '=' + encodeURIComponent(val) + '; Path=/; SameSite=Lax; Max-Age=' + maxAgeSec + (httpOnly ? '; HttpOnly' : '') + (C.isProd ? '; Secure' : ''); } -function ip(req) { return String(req.headers['cf-connecting-ip'] || (req.headers['x-forwarded-for'] || '').split(',')[0] || req.socket.remoteAddress || '').trim(); } +// Client IP. Behind Coolify's proxy the RIGHTMOST X-Forwarded-For entry is the one the proxy added; earlier entries and cf-connecting-ip +// are whatever the client sent (this domain is not on Cloudflare) and could fake many unique visitors. CF_PROXIED=1 only if Cloudflare proxies it. +function ip(req) { + if (process.env.CF_PROXIED === '1' && req.headers['cf-connecting-ip']) return String(req.headers['cf-connecting-ip']).trim(); + const xff = String(req.headers['x-forwarded-for'] || '').split(',').map(s => s.trim()).filter(Boolean); + return (xff.length ? xff[xff.length - 1] : String(req.socket.remoteAddress || '')).replace(/^::ffff:/, ''); +} const isBot = req => BOT_UA.test(req.headers['user-agent'] || '') || !req.headers['user-agent']; // visitor = IP + user agent (not a cookie: traffic-exchange frames drop cookies, and the gate must work there) function visitorKey(req) { return crypto.createHash('sha256').update(ip(req) + '|' + (req.headers['user-agent'] || '')).digest('hex').slice(0, 20); }