1
0
Fork 0
forked from svrjs/svrjs

Lint out the codebase

This commit is contained in:
Dorian Niemiec 2024-08-26 07:06:02 +02:00
parent 1fbede69e5
commit 39712a17f4

View file

@ -524,12 +524,12 @@ function doIpRequest(isHTTPS, options) {
if (host != "[offline]" || ifaceEx) { if (host != "[offline]" || ifaceEx) {
const ipRequest = doIpRequest(crypto.__disabled__ === undefined, { const ipRequest = doIpRequest(crypto.__disabled__ === undefined, {
host: "api64.ipify.org", host: "api64.ipify.org",
port: (crypto.__disabled__ !== undefined ? 80 : 443), port: crypto.__disabled__ !== undefined ? 80 : 443,
path: "/", path: "/",
headers: { headers: {
"User-Agent": generateServerString(true) "User-Agent": generateServerString(true),
}, },
timeout: 5000 timeout: 5000,
}); });
if (crypto.__disabled__ === undefined) { if (crypto.__disabled__ === undefined) {
@ -538,9 +538,9 @@ if (host != "[offline]" || ifaceEx) {
port: 443, port: 443,
path: "/", path: "/",
headers: { headers: {
"User-Agent": generateServerString(true) "User-Agent": generateServerString(true),
}, },
timeout: 5000 timeout: 5000,
}); });
} }
} else { } else {
@ -698,7 +698,9 @@ if (!disableMods) {
throw new Error( throw new Error(
"This version of " + "This version of " +
name + name +
' no longer supports "svrmodpack" library for ' + name + ' mods. Please consider using newer mods with .tar.gz format.', ' no longer supports "svrmodpack" library for ' +
name +
" mods. Please consider using newer mods with .tar.gz format.",
); );
} }
@ -846,10 +848,24 @@ function listeningMessage() {
process.send("\x12LISTEN"); process.send("\x12LISTEN");
return; return;
} }
const listenToLocalhost = (listenAddress && (listenAddress == "localhost" || listenAddress.match(/^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) || listenAddress.match(/^(?:0{0,4}:)+0{0,3}1$/))); const listenToLocalhost =
const listenToAny = (!listenAddress || listenAddress.match(/^0{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) || listenAddress.match(/^(?:0{0,4}:)+0{0,4}$/)); listenAddress &&
const sListenToLocalhost = (sListenAddress && (sListenAddress == "localhost" || sListenAddress.match(/^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) || sListenAddress.match(/^(?:0{0,4}:)+0{0,3}1$/))); (listenAddress == "localhost" ||
const sListenToAny = (!sListenAddress || sListenAddress.match(/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) || sListenAddress.match(/^(?:0{0,4}:)+0{0,4}$/)); listenAddress.match(/^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) ||
listenAddress.match(/^(?:0{0,4}:)+0{0,3}1$/));
const listenToAny =
!listenAddress ||
listenAddress.match(/^0{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) ||
listenAddress.match(/^(?:0{0,4}:)+0{0,4}$/);
const sListenToLocalhost =
sListenAddress &&
(sListenAddress == "localhost" ||
sListenAddress.match(/^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) ||
sListenAddress.match(/^(?:0{0,4}:)+0{0,3}1$/));
const sListenToAny =
!sListenAddress ||
sListenAddress.match(/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) ||
sListenAddress.match(/^(?:0{0,4}:)+0{0,4}$/);
let accHost = host; let accHost = host;
let sAccHost = host; let sAccHost = host;
if (!listenToAny) accHost = listenAddress; if (!listenToAny) accHost = listenAddress;
@ -859,72 +875,168 @@ function listeningMessage() {
serverconsole.locmessage("Started server at: "); serverconsole.locmessage("Started server at: ");
if (process.serverConfig.secure && (sListenToLocalhost || sListenToAny)) { if (process.serverConfig.secure && (sListenToLocalhost || sListenToAny)) {
if (typeof process.serverConfig.sport === "number") { if (typeof process.serverConfig.sport === "number") {
serverconsole.locmessage("* https://localhost" + (process.serverConfig.sport == 443 ? "" : (":" + process.serverConfig.sport))); serverconsole.locmessage(
"* https://localhost" +
(process.serverConfig.sport == 443
? ""
: ":" + process.serverConfig.sport),
);
} else { } else {
serverconsole.locmessage("* " + process.serverConfig.sport); // Unix socket or Windows named pipe serverconsole.locmessage("* " + process.serverConfig.sport); // Unix socket or Windows named pipe
} }
} }
if (!(process.serverConfig.secure && process.serverConfig.disableNonEncryptedServer) && (listenToLocalhost || listenToAny)) { if (
!(
process.serverConfig.secure &&
process.serverConfig.disableNonEncryptedServer
) &&
(listenToLocalhost || listenToAny)
) {
if (typeof process.serverConfig.port === "number") { if (typeof process.serverConfig.port === "number") {
serverconsole.locmessage("* http://localhost" + (process.serverConfig.port == 80 ? "" : (":" + process.serverConfig.port))); serverconsole.locmessage(
"* http://localhost" +
(process.serverConfig.port == 80
? ""
: ":" + process.serverConfig.port),
);
} else { } else {
serverconsole.locmessage("* " + process.serverConfig.port); // Unix socket or Windows named pipe serverconsole.locmessage("* " + process.serverConfig.port); // Unix socket or Windows named pipe
} }
} }
if (process.serverConfig.secure && typeof process.serverConfig.sport === "number" && !sListenToLocalhost && (!sListenToAny || (host != "" && host != "[offline]"))) serverconsole.locmessage("* https://" + (sAccHost.indexOf(":") > -1 ? "[" + sAccHost + "]" : sAccHost) + (process.serverConfig.sport == 443 ? "" : (":" + process.serverConfig.sport))); if (
if (!(process.serverConfig.secure && process.serverConfig.disableNonEncryptedServer) && !listenToLocalhost && (!listenToAny || (host != "" && host != "[offline]")) && typeof process.serverConfig.port === "number") serverconsole.locmessage("* http://" + (accHost.indexOf(":") > -1 ? "[" + accHost + "]" : accHost) + (process.serverConfig.port == 80 ? "" : (":" + process.serverConfig.port))); process.serverConfig.secure &&
typeof process.serverConfig.sport === "number" &&
!sListenToLocalhost &&
(!sListenToAny || (host != "" && host != "[offline]"))
)
serverconsole.locmessage(
"* https://" +
(sAccHost.indexOf(":") > -1 ? "[" + sAccHost + "]" : sAccHost) +
(process.serverConfig.sport == 443
? ""
: ":" + process.serverConfig.sport),
);
if (
!(
process.serverConfig.secure &&
process.serverConfig.disableNonEncryptedServer
) &&
!listenToLocalhost &&
(!listenToAny || (host != "" && host != "[offline]")) &&
typeof process.serverConfig.port === "number"
)
serverconsole.locmessage(
"* http://" +
(accHost.indexOf(":") > -1 ? "[" + accHost + "]" : accHost) +
(process.serverConfig.port == 80
? ""
: ":" + process.serverConfig.port),
);
ipStatusCallback(() => { ipStatusCallback(() => {
if (pubip != "") { if (pubip != "") {
if (process.serverConfig.secure && !sListenToLocalhost) serverconsole.locmessage("* https://" + (pubip.indexOf(":") > -1 ? "[" + pubip + "]" : pubip) + (process.serverConfig.spubport == 443 ? "" : (":" + process.serverConfig.spubport))); if (process.serverConfig.secure && !sListenToLocalhost)
if (!(process.serverConfig.secure && process.serverConfig.disableNonEncryptedServer) && !listenToLocalhost) serverconsole.locmessage("* http://" + (pubip.indexOf(":") > -1 ? "[" + pubip + "]" : pubip) + (process.serverConfig.pubport == 80 ? "" : (":" + process.serverConfig.pubport))); serverconsole.locmessage(
"* https://" +
(pubip.indexOf(":") > -1 ? "[" + pubip + "]" : pubip) +
(process.serverConfig.spubport == 443
? ""
: ":" + process.serverConfig.spubport),
);
if (
!(
process.serverConfig.secure &&
process.serverConfig.disableNonEncryptedServer
) &&
!listenToLocalhost
)
serverconsole.locmessage(
"* http://" +
(pubip.indexOf(":") > -1 ? "[" + pubip + "]" : pubip) +
(process.serverConfig.pubport == 80
? ""
: ":" + process.serverConfig.pubport),
);
} }
if (domain != "") { if (domain != "") {
if (process.serverConfig.secure && !sListenToLocalhost) serverconsole.locmessage("* https://" + domain + (spubport == 443 ? "" : (":" + spubport))); if (process.serverConfig.secure && !sListenToLocalhost)
if (!(process.serverConfig.secure && process.serverConfig.disableNonEncryptedServer) && !listenToLocalhost) serverconsole.locmessage("* http://" + domain + (process.serverConfig.pubport == 80 ? "" : (":" + process.serverConfig.pubport))); serverconsole.locmessage(
"* https://" + domain + (spubport == 443 ? "" : ":" + spubport),
);
if (
!(
process.serverConfig.secure &&
process.serverConfig.disableNonEncryptedServer
) &&
!listenToLocalhost
)
serverconsole.locmessage(
"* http://" +
domain +
(process.serverConfig.pubport == 80
? ""
: ":" + process.serverConfig.pubport),
);
} }
serverconsole.locmessage("For CLI help, you can type \"help\""); serverconsole.locmessage('For CLI help, you can type "help"');
// Code for sending data to a statistics server // Code for sending data to a statistics server
if (!process.serverConfig.optOutOfStatisticsServer) { if (!process.serverConfig.optOutOfStatisticsServer) {
if (crypto.__disabled__ !== undefined) { if (crypto.__disabled__ !== undefined) {
serverconsole.locwarnmessage("Sending data to statistics server is disabled, because the server only supports HTTPS, and your Node.JS version doesn't have crypto support."); serverconsole.locwarnmessage(
"Sending data to statistics server is disabled, because the server only supports HTTPS, and your Node.JS version doesn't have crypto support.",
);
} else { } else {
const statisticsToSend = JSON.stringify({ const statisticsToSend = JSON.stringify({
version: version, version: version,
runtime: process.isBun ? "Bun" : "Node.js", runtime: process.isBun ? "Bun" : "Node.js",
runtimeVersion: process.isBun ? process.versions.bun : process.version, runtimeVersion: process.isBun
mods: modInfos ? process.versions.bun
: process.version,
mods: modInfos,
}); });
const statisticsRequest = https.request(statisticsServerCollectEndpoint, { const statisticsRequest = https.request(
method: "POST", statisticsServerCollectEndpoint,
headers: { {
"User-Agent": generateServerString(true), method: "POST",
"Content-Type": "application/json", headers: {
"Content-Length": Buffer.byteLength(statisticsToSend) "User-Agent": generateServerString(true),
} "Content-Type": "application/json",
}, function (res) { "Content-Length": Buffer.byteLength(statisticsToSend),
const statusCode = res.statusCode; },
let data = ""; },
res.on("data", function (chunk) { function (res) {
data += chunk.toString(); const statusCode = res.statusCode;
}); let data = "";
res.on("end", function () { res.on("data", function (chunk) {
try { data += chunk.toString();
let parsedJson = {}; });
res.on("end", function () {
try { try {
parsedJson = JSON.parse(data); let parsedJson = {};
try {
parsedJson = JSON.parse(data);
} catch (err) {
throw new Error(
"JSON parse error (response parsing failed).",
);
}
if (parsedJson.status != statusCode)
throw new Error("Status code mismatch");
if (statusCode != 200) throw new Error(parsedJson.message);
} catch (err) { } catch (err) {
throw new Error("JSON parse error (response parsing failed)."); serverconsole.locwarnmessage(
"There was a problem, when sending data to statistics server! Reason: " +
err.message,
);
} }
if (parsedJson.status != statusCode) throw new Error("Status code mismatch"); });
if (statusCode != 200) throw new Error(parsedJson.message); },
} catch (err) { );
serverconsole.locwarnmessage("There was a problem, when sending data to statistics server! Reason: " + err.message);
}
});
});
statisticsRequest.on("error", function (err) { statisticsRequest.on("error", function (err) {
serverconsole.locwarnmessage("There was a problem, when sending data to statistics server! Reason: " + err.message); serverconsole.locwarnmessage(
"There was a problem, when sending data to statistics server! Reason: " +
err.message,
);
}); });
statisticsRequest.end(statisticsToSend); statisticsRequest.end(statisticsToSend);
} }
@ -1379,7 +1491,9 @@ function SVRJSFork() {
newWorker.on("error", function (err) { newWorker.on("error", function (err) {
if (!exiting) if (!exiting)
serverconsole.locwarnmessage( serverconsole.locwarnmessage(
"There was a problem when handling " + name + " worker! (from master process side) Reason: " + "There was a problem when handling " +
name +
" worker! (from master process side) Reason: " +
err.message, err.message,
); );
}); });
@ -2431,4 +2545,4 @@ try {
setTimeout(function () { setTimeout(function () {
process.exit(err.errno !== undefined ? err.errno : 1); process.exit(err.errno !== undefined ? err.errno : 1);
}, 10); }, 10);
} }