Add proper logging

This commit is contained in:
D. Berge
2025-08-13 15:35:35 +02:00
parent 7205ec42a8
commit 84510e8dc9

View File

@@ -5,16 +5,17 @@ const cfg = require('../../../lib/config');
const jwt = require('../../../lib/jwt');
const user = require('../../../lib/db/user');
const ServerUser = require('../../../lib/db/user/User');
const { ERROR, WARNING, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
function parseIP(ip) {
if (!ip || typeof ip !== 'string') {
console.warn('Invalid IP input:', ip);
WARNING('Invalid IP input:', ip);
return null;
}
// Handle comma-separated X-Forwarded-For (e.g., "87.90.254.127,")
const cleanIp = ip.split(',')[0].trim();
if (!cleanIp) {
console.warn('Empty IP after parsing:', ip);
WARNING('Empty IP after parsing:', ip);
return null;
}
// Convert IPv6-mapped IPv4 (e.g., ::ffff:127.0.0.1 -> 127.0.0.1)
@@ -26,7 +27,7 @@ function parseIP(ip) {
function normalizeCIDR(range) {
if (!range || typeof range !== 'string') {
console.warn('Invalid CIDR range:', range);
WARNING('Invalid CIDR range:', range);
return null;
}
// If no /prefix, assume /32 for IPv4 or /128 for IPv6
@@ -36,7 +37,7 @@ function normalizeCIDR(range) {
const prefix = parsed.kind() === 'ipv4' ? 32 : 128;
return `${range}/${prefix}`;
} catch (err) {
console.warn(`Failed to parse bare IP ${range}:`, err.message);
WARNING(`Failed to parse bare IP ${range}:`, err.message);
return null;
}
}
@@ -45,9 +46,9 @@ function normalizeCIDR(range) {
async function authorisedIP(req, res) {
const ip = parseIP(req.ip || req.headers['x-forwarded-for'] || req.headers['x-real-ip']);
console.log('authorisedIP:', { ip, headers: req.headers }); // Debug
DEBUG('authorisedIP:', { ip, headers: req.headers }); // Debug
if (!ip) {
console.warn('No valid IP provided:', { ip, headers: req.headers });
WARNING('No valid IP provided:', { ip, headers: req.headers });
return false;
}
@@ -55,7 +56,7 @@ async function authorisedIP(req, res) {
try {
addr = ipaddr.parse(ip);
} catch (err) {
console.warn('Invalid IP:', ip, err.message);
WARNING('Invalid IP:', ip, err.message);
return false;
}
@@ -71,7 +72,7 @@ async function authorisedIP(req, res) {
const [rangeAddr, prefix] = ipaddr.parseCIDR(normalized);
i.$range = { addr: rangeAddr, prefix };
} catch (err) {
console.warn(`Invalid CIDR range ${i.ip}:`, err.message);
WARNING(`Invalid CIDR range ${i.ip}:`, err.message);
i.$range = null; // Skip invalid ranges
}
});
@@ -94,7 +95,7 @@ async function authorisedIP(req, res) {
return true;
}
} catch (err) {
console.warn(`Error checking range ${ipEntry.ip}:`, err.message);
WARNING(`Error checking range ${ipEntry.ip}:`, err.message);
continue;
}
}
@@ -103,9 +104,9 @@ async function authorisedIP(req, res) {
async function authorisedHost(req, res) {
const ip = parseIP(req.ip || req.headers['x-forwarded-for'] || req.headers['x-real-ip']);
console.log('authorisedHost:', { ip, headers: req.headers }); // Debug
DEBUG('authorisedHost:', { ip, headers: req.headers }); // Debug
if (!ip) {
console.warn('No valid IP for host check:', { ip, headers: req.headers });
WARNING('No valid IP for host check:', { ip, headers: req.headers });
return false;
}
@@ -127,7 +128,7 @@ async function authorisedHost(req, res) {
}
} catch (err) {
if (err.code !== 'ENODATA') {
console.error(`DNS error for host ${key}:`, err);
ERROR(`DNS error for host ${key}:`, err);
}
}
}