From a02e976dfe6a608dc38f7dd417191deb85b675bd Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Mon, 26 Aug 2024 06:26:22 +0200 Subject: [PATCH] Added configuration file saving. --- src/index.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 3211624..87105cb 100644 --- a/src/index.js +++ b/src/index.js @@ -1294,9 +1294,67 @@ function msgListener(message) { } } +// Save configuration file function saveConfig() { - // TODO: saveConfig() function - throw new Error("Configuration saving not yet implemented."); + for (var i = 0; i < 3; i++) { + try { + var configJSONobj = {}; + if (fs.existsSync(process.dirname + "/config.json")) configJSONobj = JSON.parse(fs.readFileSync(process.dirname + "/config.json").toString()); + if (configJSONobj.users === undefined) configJSONobj.users = []; + if (process.serverConfig.secure) { + if (configJSONobj.key === undefined) configJSONobj.key = "cert/key.key"; + if (configJSONobj.cert === undefined) configJSONobj.cert = "cert/cert.crt"; + if (configJSONobj.sport === undefined) configJSONobj.sport = 443; + if (configJSONobj.spubport === undefined) configJSONobj.spubport = 443; + if (configJSONobj.sni === undefined) configJSONobj.sni = {}; + if (configJSONobj.enableOCSPStapling === undefined) configJSONobj.enableOCSPStapling = false; + } + if (configJSONobj.port === undefined) configJSONobj.port = 80; + if (configJSONobj.pubport === undefined) configJSONobj.pubport = 80; + if (configJSONobj.domain === undefined && configJSONobj.domian !== undefined) configJSONobj.domain = configJSONobj.domian; + delete configJSONobj.domian; + if (configJSONobj.page404 === undefined) configJSONobj.page404 = "404.html"; + configJSONobj.timestamp = process.serverConfig.timestamp; + configJSONobj.blacklist = process.serverConfig.blacklist; + if (configJSONobj.nonStandardCodes === undefined) configJSONobj.nonStandardCodes = []; + if (configJSONobj.enableCompression === undefined) configJSONobj.enableCompression = true; + if (configJSONobj.customHeaders === undefined) configJSONobj.customHeaders = {}; + if (configJSONobj.enableHTTP2 === undefined) configJSONobj.enableHTTP2 = false; + if (configJSONobj.enableLogging === undefined) configJSONobj.enableLogging = true; + if (configJSONobj.enableDirectoryListing === undefined) configJSONobj.enableDirectoryListing = 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.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.enableIPSpoofing === undefined) configJSONobj.enableIPSpoofing = false; + if (configJSONobj.secure === undefined) configJSONobj.secure = false; + if (configJSONobj.disableNonEncryptedServer === undefined) configJSONobj.disableNonEncryptedServer = false; + if (configJSONobj.disableToHTTPSRedirect === undefined) configJSONobj.disableToHTTPSRedirect = 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.useWebRootServerSideScript === undefined) configJSONobj.useWebRootServerSideScript = true; + if (configJSONobj.exposeModsInErrorPages === undefined) configJSONobj.exposeModsInErrorPages = true; + if (configJSONobj.disableTrailingSlashRedirects === undefined) 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"; + fs.writeFileSync(__dirname + "/config.json", configString); + break; + } catch (err) { + if (i >= 2) throw err; + var now = Date.now(); + while (Date.now() - now < 2); + } + } } // Starting function