2024-08-25 09:12:39 +02:00
|
|
|
const ipBlockList = require("../utils/ipBlockList.js");
|
|
|
|
let blocklist = ipBlockList(process.serverConfig.blacklist);
|
|
|
|
|
|
|
|
module.exports = (req, res, logFacilities, config, next) => {
|
|
|
|
if (
|
|
|
|
blocklist.check(
|
|
|
|
req.socket.realRemoteAddress
|
|
|
|
? req.socket.realRemoteAddress
|
|
|
|
: req.socket.remoteAddress,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// Invoke 403 Forbidden error
|
|
|
|
res.error(403);
|
|
|
|
logFacilities.errmessage("Client is in the block list.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.commands = {
|
2024-08-25 09:25:06 +02:00
|
|
|
block: (ip, log, passCommand) => {
|
2024-08-25 09:12:39 +02:00
|
|
|
if (ip == undefined || JSON.stringify(ip) == "[]") {
|
|
|
|
log("Cannot block non-existent IP.");
|
|
|
|
} else {
|
|
|
|
for (var i = 0; i < ip.length; i++) {
|
|
|
|
if (ip[i] != "localhost" && ip[i].indexOf(":") == -1) {
|
|
|
|
ip[i] = "::ffff:" + ip[i];
|
|
|
|
}
|
|
|
|
if (!blocklist.check(ip[i])) {
|
|
|
|
blocklist.add(ip[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
process.config.blacklist = blocklist.raw;
|
|
|
|
log("IPs successfully blocked.");
|
2024-08-25 09:25:06 +02:00
|
|
|
passCommand(ip, log);
|
2024-08-25 09:12:39 +02:00
|
|
|
}
|
|
|
|
},
|
2024-08-25 09:25:06 +02:00
|
|
|
unblock: (ip, log, passCommand) => {
|
2024-08-25 09:12:39 +02:00
|
|
|
if (ip == undefined || JSON.stringify(ip) == "[]") {
|
|
|
|
log("Cannot unblock non-existent IP.");
|
|
|
|
} else {
|
|
|
|
for (var i = 0; i < ip.length; i++) {
|
|
|
|
if (ip[i].indexOf(":") == -1) {
|
|
|
|
ip[i] = "::ffff:" + ip[i];
|
|
|
|
}
|
|
|
|
blocklist.remove(ip[i]);
|
|
|
|
}
|
|
|
|
process.config.blacklist = blocklist.raw;
|
|
|
|
log("IPs successfully unblocked.");
|
2024-08-25 09:25:06 +02:00
|
|
|
passCommand(ip, log);
|
2024-08-25 09:12:39 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|