utils: getIP formatting

This commit is contained in:
wukko 2024-01-31 16:47:18 +06:00
parent a375000ae9
commit 01240b6ca3

View file

@ -114,12 +114,13 @@ export function checkJSONPost(obj) {
export function getIP(req) { export function getIP(req) {
const strippedIP = req.ip.replace(/^::ffff:/, ''); const strippedIP = req.ip.replace(/^::ffff:/, '');
const ip = ipaddr.parse(strippedIP); const ip = ipaddr.parse(strippedIP);
if (ip.kind() === 'ipv4') if (ip.kind() === 'ipv4') {
return strippedIP; return strippedIP;
}
const PREFIX = 56; const prefix = 56;
const v6Bytes = ip.toByteArray(); const v6Bytes = ip.toByteArray();
v6Bytes.fill(0, PREFIX / 8); v6Bytes.fill(0, prefix / 8);
return ipaddr.fromByteArray(v6Bytes).toString(); return ipaddr.fromByteArray(v6Bytes).toString();
} }