1
0
Fork 0
forked from svrjs/svrjs

Fix "block" and "unblock" commands

This commit is contained in:
Dorian Niemiec 2024-08-26 06:32:54 +02:00
parent a02e976dfe
commit 57bb2293b9
2 changed files with 81 additions and 38 deletions

View file

@ -1299,52 +1299,94 @@ function saveConfig() {
for (var i = 0; i < 3; i++) { for (var i = 0; i < 3; i++) {
try { try {
var configJSONobj = {}; var configJSONobj = {};
if (fs.existsSync(process.dirname + "/config.json")) configJSONobj = JSON.parse(fs.readFileSync(process.dirname + "/config.json").toString()); if (fs.existsSync(process.dirname + "/config.json"))
configJSONobj = JSON.parse(
fs.readFileSync(process.dirname + "/config.json").toString(),
);
if (configJSONobj.users === undefined) configJSONobj.users = []; if (configJSONobj.users === undefined) configJSONobj.users = [];
if (process.serverConfig.secure) { if (process.serverConfig.secure) {
if (configJSONobj.key === undefined) configJSONobj.key = "cert/key.key"; if (configJSONobj.key === undefined) configJSONobj.key = "cert/key.key";
if (configJSONobj.cert === undefined) configJSONobj.cert = "cert/cert.crt"; if (configJSONobj.cert === undefined)
configJSONobj.cert = "cert/cert.crt";
if (configJSONobj.sport === undefined) configJSONobj.sport = 443; if (configJSONobj.sport === undefined) configJSONobj.sport = 443;
if (configJSONobj.spubport === undefined) configJSONobj.spubport = 443; if (configJSONobj.spubport === undefined) configJSONobj.spubport = 443;
if (configJSONobj.sni === undefined) configJSONobj.sni = {}; if (configJSONobj.sni === undefined) configJSONobj.sni = {};
if (configJSONobj.enableOCSPStapling === undefined) configJSONobj.enableOCSPStapling = false; if (configJSONobj.enableOCSPStapling === undefined)
configJSONobj.enableOCSPStapling = false;
} }
if (configJSONobj.port === undefined) configJSONobj.port = 80; if (configJSONobj.port === undefined) configJSONobj.port = 80;
if (configJSONobj.pubport === undefined) configJSONobj.pubport = 80; if (configJSONobj.pubport === undefined) configJSONobj.pubport = 80;
if (configJSONobj.domain === undefined && configJSONobj.domian !== undefined) configJSONobj.domain = configJSONobj.domian; if (
configJSONobj.domain === undefined &&
configJSONobj.domian !== undefined
)
configJSONobj.domain = configJSONobj.domian;
delete configJSONobj.domian; delete configJSONobj.domian;
if (configJSONobj.page404 === undefined) configJSONobj.page404 = "404.html"; if (configJSONobj.page404 === undefined)
configJSONobj.page404 = "404.html";
configJSONobj.timestamp = process.serverConfig.timestamp; configJSONobj.timestamp = process.serverConfig.timestamp;
configJSONobj.blacklist = process.serverConfig.blacklist; configJSONobj.blacklist = process.serverConfig.blacklist;
if (configJSONobj.nonStandardCodes === undefined) configJSONobj.nonStandardCodes = []; if (configJSONobj.nonStandardCodes === undefined)
if (configJSONobj.enableCompression === undefined) configJSONobj.enableCompression = true; configJSONobj.nonStandardCodes = [];
if (configJSONobj.customHeaders === undefined) configJSONobj.customHeaders = {}; if (configJSONobj.enableCompression === undefined)
if (configJSONobj.enableHTTP2 === undefined) configJSONobj.enableHTTP2 = false; configJSONobj.enableCompression = true;
if (configJSONobj.enableLogging === undefined) configJSONobj.enableLogging = true; if (configJSONobj.customHeaders === undefined)
if (configJSONobj.enableDirectoryListing === undefined) configJSONobj.enableDirectoryListing = true; configJSONobj.customHeaders = {};
if (configJSONobj.enableDirectoryListingWithDefaultHead === undefined) configJSONobj.enableDirectoryListingWithDefaultHead = false; if (configJSONobj.enableHTTP2 === undefined)
if (configJSONobj.serverAdministratorEmail === undefined) configJSONobj.serverAdministratorEmail = "[no contact information]"; configJSONobj.enableHTTP2 = false;
if (configJSONobj.stackHidden === undefined) configJSONobj.stackHidden = false; if (configJSONobj.enableLogging === undefined)
if (configJSONobj.enableRemoteLogBrowsing === undefined) configJSONobj.enableRemoteLogBrowsing = false; configJSONobj.enableLogging = true;
if (configJSONobj.exposeServerVersion === undefined) configJSONobj.exposeServerVersion = true; if (configJSONobj.enableDirectoryListing === undefined)
if (configJSONobj.disableServerSideScriptExpose === undefined) configJSONobj.disableServerSideScriptExpose = true; configJSONobj.enableDirectoryListing = true;
if (configJSONobj.allowStatus === undefined) configJSONobj.allowStatus = true; if (configJSONobj.enableDirectoryListingWithDefaultHead === undefined)
configJSONobj.enableDirectoryListingWithDefaultHead = false;
if (configJSONobj.serverAdministratorEmail === undefined)
configJSONobj.serverAdministratorEmail = "[no contact information]";
if (configJSONobj.stackHidden === undefined)
configJSONobj.stackHidden = false;
if (configJSONobj.enableRemoteLogBrowsing === undefined)
configJSONobj.enableRemoteLogBrowsing = false;
if (configJSONobj.exposeServerVersion === undefined)
configJSONobj.exposeServerVersion = true;
if (configJSONobj.disableServerSideScriptExpose === undefined)
configJSONobj.disableServerSideScriptExpose = true;
if (configJSONobj.allowStatus === undefined)
configJSONobj.allowStatus = true;
if (configJSONobj.rewriteMap === undefined) configJSONobj.rewriteMap = []; if (configJSONobj.rewriteMap === undefined) configJSONobj.rewriteMap = [];
if (configJSONobj.dontCompress === undefined) configJSONobj.dontCompress = ["/.*\\.ipxe$/", "/.*\\.(?:jpe?g|png|bmp|tiff|jfif|gif|webp)$/", "/.*\\.(?:[id]mg|iso|flp)$/", "/.*\\.(?:zip|rar|bz2|[gb7x]z|lzma|tar)$/", "/.*\\.(?:mp[34]|mov|wm[av]|avi|webm|og[gv]|mk[va])$/"]; if (configJSONobj.dontCompress === undefined)
if (configJSONobj.enableIPSpoofing === undefined) configJSONobj.enableIPSpoofing = false; configJSONobj.dontCompress = [
"/.*\\.ipxe$/",
"/.*\\.(?:jpe?g|png|bmp|tiff|jfif|gif|webp)$/",
"/.*\\.(?:[id]mg|iso|flp)$/",
"/.*\\.(?:zip|rar|bz2|[gb7x]z|lzma|tar)$/",
"/.*\\.(?:mp[34]|mov|wm[av]|avi|webm|og[gv]|mk[va])$/",
];
if (configJSONobj.enableIPSpoofing === undefined)
configJSONobj.enableIPSpoofing = false;
if (configJSONobj.secure === undefined) configJSONobj.secure = false; if (configJSONobj.secure === undefined) configJSONobj.secure = false;
if (configJSONobj.disableNonEncryptedServer === undefined) configJSONobj.disableNonEncryptedServer = false; if (configJSONobj.disableNonEncryptedServer === undefined)
if (configJSONobj.disableToHTTPSRedirect === undefined) configJSONobj.disableToHTTPSRedirect = false; configJSONobj.disableNonEncryptedServer = false;
if (configJSONobj.enableETag === undefined) configJSONobj.enableETag = true; if (configJSONobj.disableToHTTPSRedirect === undefined)
if (configJSONobj.disableUnusedWorkerTermination === undefined) configJSONobj.disableUnusedWorkerTermination = false; configJSONobj.disableToHTTPSRedirect = false;
if (configJSONobj.rewriteDirtyURLs === undefined) configJSONobj.rewriteDirtyURLs = false; if (configJSONobj.enableETag === undefined)
configJSONobj.enableETag = true;
if (configJSONobj.disableUnusedWorkerTermination === undefined)
configJSONobj.disableUnusedWorkerTermination = false;
if (configJSONobj.rewriteDirtyURLs === undefined)
configJSONobj.rewriteDirtyURLs = false;
if (configJSONobj.errorPages === undefined) configJSONobj.errorPages = []; if (configJSONobj.errorPages === undefined) configJSONobj.errorPages = [];
if (configJSONobj.useWebRootServerSideScript === undefined) configJSONobj.useWebRootServerSideScript = true; if (configJSONobj.useWebRootServerSideScript === undefined)
if (configJSONobj.exposeModsInErrorPages === undefined) configJSONobj.exposeModsInErrorPages = true; configJSONobj.useWebRootServerSideScript = true;
if (configJSONobj.disableTrailingSlashRedirects === undefined) configJSONobj.disableTrailingSlashRedirects = false; if (configJSONobj.exposeModsInErrorPages === undefined)
if (configJSONobj.environmentVariables === undefined) configJSONobj.environmentVariables = {}; configJSONobj.exposeModsInErrorPages = true;
if (configJSONobj.allowDoubleSlashes === undefined) configJSONobj.allowDoubleSlashes = false; if (configJSONobj.disableTrailingSlashRedirects === undefined)
if (configJSONobj.optOutOfStatisticsServer === undefined) configJSONobj.optOutOfStatisticsServer = false; configJSONobj.disableTrailingSlashRedirects = false;
if (configJSONobj.environmentVariables === undefined)
configJSONobj.environmentVariables = {};
if (configJSONobj.allowDoubleSlashes === undefined)
configJSONobj.allowDoubleSlashes = false;
if (configJSONobj.optOutOfStatisticsServer === undefined)
configJSONobj.optOutOfStatisticsServer = false;
var configString = JSON.stringify(configJSONobj, null, 2) + "\n"; var configString = JSON.stringify(configJSONobj, null, 2) + "\n";
fs.writeFileSync(__dirname + "/config.json", configString); fs.writeFileSync(__dirname + "/config.json", configString);
@ -1760,7 +1802,7 @@ function start(init) {
} else { } else {
reqcounterKillReq = process.reqcounter; reqcounterKillReq = process.reqcounter;
} }
} else if (message[0] == "\x14") { } else if (line[0] == "\x14") {
// Discard unrecognized control messages // Discard unrecognized control messages
} else if ( } else if (
commands[line.split(" ")[0]] !== undefined && commands[line.split(" ")[0]] !== undefined &&

View file

@ -1,3 +1,4 @@
const cluster = require("../utils/clusterBunShim.js");
const ipBlockList = require("../utils/ipBlockList.js"); const ipBlockList = require("../utils/ipBlockList.js");
let blocklist = ipBlockList(process.serverConfig.blacklist); let blocklist = ipBlockList(process.serverConfig.blacklist);
@ -20,7 +21,7 @@ module.exports = (req, res, logFacilities, config, next) => {
module.exports.commands = { module.exports.commands = {
block: (ip, log, passCommand) => { block: (ip, log, passCommand) => {
if (ip == undefined || JSON.stringify(ip) == "[]") { if (ip == undefined || JSON.stringify(ip) == "[]") {
log("Cannot block non-existent IP."); if (!cluster.isPrimary === false) log("Cannot block non-existent IP.");
} else { } else {
for (var i = 0; i < ip.length; i++) { for (var i = 0; i < ip.length; i++) {
if (ip[i] != "localhost" && ip[i].indexOf(":") == -1) { if (ip[i] != "localhost" && ip[i].indexOf(":") == -1) {
@ -30,14 +31,14 @@ module.exports.commands = {
blocklist.add(ip[i]); blocklist.add(ip[i]);
} }
} }
process.config.blacklist = blocklist.raw; process.serverConfig.blacklist = blocklist.raw;
log("IPs successfully blocked."); if (!cluster.isPrimary === false) log("IPs successfully blocked.");
passCommand(ip, log); passCommand(ip, log);
} }
}, },
unblock: (ip, log, passCommand) => { unblock: (ip, log, passCommand) => {
if (ip == undefined || JSON.stringify(ip) == "[]") { if (ip == undefined || JSON.stringify(ip) == "[]") {
log("Cannot unblock non-existent IP."); if (!cluster.isPrimary === false) log("Cannot unblock non-existent IP.");
} else { } else {
for (var i = 0; i < ip.length; i++) { for (var i = 0; i < ip.length; i++) {
if (ip[i].indexOf(":") == -1) { if (ip[i].indexOf(":") == -1) {
@ -45,8 +46,8 @@ module.exports.commands = {
} }
blocklist.remove(ip[i]); blocklist.remove(ip[i]);
} }
process.config.blacklist = blocklist.raw; process.serverConfig.blacklist = blocklist.raw;
log("IPs successfully unblocked."); if (!cluster.isPrimary === false) log("IPs successfully unblocked.");
passCommand(ip, log); passCommand(ip, log);
} }
}, },