|
|
@ -44,48 +44,49 @@ Blacklist.get = async function () {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blacklist.test = async function (clientIp) {
|
|
|
|
Blacklist.test = async function (clientIp) {
|
|
|
|
// Some handy test addresses
|
|
|
|
|
|
|
|
// clientIp = '2001:db8:85a3:0:0:8a2e:370:7334'; // IPv6
|
|
|
|
|
|
|
|
// clientIp = '127.0.15.1'; // IPv4
|
|
|
|
|
|
|
|
// clientIp = '127.0.15.1:3443'; // IPv4 with port strip port to not fail
|
|
|
|
|
|
|
|
if (!clientIp) {
|
|
|
|
if (!clientIp) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clientIp = clientIp.split(':').length === 2 ? clientIp.split(':')[0] : clientIp;
|
|
|
|
clientIp = clientIp.split(':').length === 2 ? clientIp.split(':')[0] : clientIp;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const rules = Blacklist._rules;
|
|
|
|
|
|
|
|
function checkCidrRange(clientIP) {
|
|
|
|
|
|
|
|
if (!rules.cidr.length) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
let addr;
|
|
|
|
let addr;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
addr = ipaddr.parse(clientIp);
|
|
|
|
addr = ipaddr.parse(clientIP);
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
winston.error(`[meta/blacklist] Error parsing client IP : ${clientIp}`);
|
|
|
|
winston.error(`[meta/blacklist] Error parsing client IP : ${clientIp}`);
|
|
|
|
throw err;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rules.cidr.some((subnet) => {
|
|
|
|
if (
|
|
|
|
|
|
|
|
!Blacklist._rules.ipv4.includes(clientIp) && // not explicitly specified in ipv4 list
|
|
|
|
|
|
|
|
!Blacklist._rules.ipv6.includes(clientIp) && // not explicitly specified in ipv6 list
|
|
|
|
|
|
|
|
!Blacklist._rules.cidr.some((subnet) => {
|
|
|
|
|
|
|
|
const cidr = ipaddr.parseCIDR(subnet);
|
|
|
|
const cidr = ipaddr.parseCIDR(subnet);
|
|
|
|
if (addr.kind() !== cidr[0].kind()) {
|
|
|
|
if (addr.kind() !== cidr[0].kind()) {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addr.match(cidr);
|
|
|
|
return addr.match(cidr);
|
|
|
|
}) // not in a blacklisted IPv4 or IPv6 cidr range
|
|
|
|
});
|
|
|
|
) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// To return test failure, pass back an error in callback
|
|
|
|
|
|
|
|
await plugins.hooks.fire('filter:blacklist.test', { ip: clientIp });
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
analytics.increment('blacklist');
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
if (rules.ipv4.includes(clientIp) ||
|
|
|
|
|
|
|
|
rules.ipv6.includes(clientIp) ||
|
|
|
|
|
|
|
|
checkCidrRange(clientIp)) {
|
|
|
|
const err = new Error('[[error:blacklisted-ip]]');
|
|
|
|
const err = new Error('[[error:blacklisted-ip]]');
|
|
|
|
err.code = 'blacklisted-ip';
|
|
|
|
err.code = 'blacklisted-ip';
|
|
|
|
|
|
|
|
|
|
|
|
analytics.increment('blacklist');
|
|
|
|
analytics.increment('blacklist');
|
|
|
|
throw err;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// To return test failure, throw an error in hook
|
|
|
|
|
|
|
|
await plugins.hooks.fire('filter:blacklist.test', { ip: clientIp });
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
analytics.increment('blacklist');
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blacklist.validate = function (rules) {
|
|
|
|
Blacklist.validate = function (rules) {
|
|
|
|