1
0
Fork 0
forked from svrjs/svrjs
This repository has been archived on 2024-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs/src/middleware/blocklist.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

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) => {
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:25:06 +02:00
unblock: (ip, log, passCommand) => {
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 12:37:14 +02:00
module.exports.proxySafe = true;