Skip to content
Merged
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
45 changes: 39 additions & 6 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,48 @@ function log(level, msg, fields = {}) {
);
}

function headerValue(headers, name) {
const value = headers[name];
if (typeof value === "string") {
return value;
}
if (Array.isArray(value) && value.length > 0) {
return value[0];
}
return undefined;
}

function normalizeIp(ip) {
if (!ip) {
return undefined;
}
const trimmed = ip.trim().replace(/^\[|\]$/g, "");
if (trimmed.startsWith("::ffff:")) {
return trimmed.slice(7);
}
return trimmed || undefined;
}

function clientIp(req) {
const forwarded = req.headers["x-forwarded-for"];
if (typeof forwarded === "string" && forwarded.length > 0) {
return forwarded.split(",")[0].trim();
const forwardedFor = headerValue(req.headers, "x-forwarded-for");
if (forwardedFor) {
return normalizeIp(forwardedFor.split(",")[0]);
}

const realIp = headerValue(req.headers, "x-real-ip");
if (realIp) {
return normalizeIp(realIp);
}
if (Array.isArray(forwarded) && forwarded.length > 0) {
return forwarded[0].split(",")[0].trim();

const forwarded = headerValue(req.headers, "forwarded");
if (forwarded) {
const match = forwarded.match(/for=(?:"?\[?)([^\]";,]+)/i);
if (match) {
return normalizeIp(match[1]);
}
}
return req.socket.remoteAddress ?? undefined;

return normalizeIp(req.socket.remoteAddress);
}

const server = http.createServer(async (req, res) => {
Expand Down
Loading