replace ipaddr.js

This commit is contained in:
Girish Ramakrishnan
2025-05-06 16:16:33 +02:00
parent c8976daf96
commit a7c6e36ec3
10 changed files with 186 additions and 34 deletions

View File

@@ -27,7 +27,7 @@ const assert = require('assert'),
constants = require('./constants.js'),
cron = require('./cron.js'),
fs = require('fs'),
ipaddr = require('ipaddr.js'),
ipaddr = require('./ipaddr.js'),
path = require('path'),
paths = require('./paths.js'),
safe = require('safetydance'),
@@ -74,20 +74,16 @@ async function setBlocklist(blocklist, auditSource) {
assert.strictEqual(typeof blocklist, 'string');
assert.strictEqual(typeof auditSource, 'object');
const parsedIp = ipaddr.process(auditSource.ip); // will demangle IPv4 mapped IPv6
let count = 0;
for (const line of blocklist.split('\n')) {
if (!line || line.startsWith('#')) continue;
const rangeOrIP = line.trim();
// this checks for IPv4 and IPv6
if (!ipaddr.isValid(rangeOrIP) && !ipaddr.isValidCIDR(rangeOrIP)) throw new BoxError(BoxError.BAD_FIELD, `${rangeOrIP} is not a valid IP or range`);
if (rangeOrIP.indexOf('/') === -1) {
if (auditSource.ip === rangeOrIP) throw new BoxError(BoxError.BAD_FIELD, `${rangeOrIP} includes client IP. Cannot block yourself`);
if (ipaddr.isEqual(rangeOrIP, auditSource.ip)) throw new BoxError(BoxError.BAD_FIELD, `IP ${rangeOrIP} is the client IP. Cannot block yourself`);
} else {
const parsedRange = ipaddr.parseCIDR(rangeOrIP); // returns [addr, range]
if (parsedRange[0].kind() === parsedIp.kind() && parsedIp.match(parsedRange)) throw new BoxError(BoxError.BAD_FIELD, `${rangeOrIP} includes client IP. Cannot block yourself`);
if (ipaddr.includes(rangeOrIP, auditSource.ip)) throw new BoxError(BoxError.BAD_FIELD, `range ${rangeOrIP} includes client IP. Cannot block yourself`);
}
++count;
}