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) {
const ipRequest = doIpRequest(crypto.__disabled__ === undefined, {
host: "api64.ipify.org",
port: (crypto.__disabled__ !== undefined ? 80 : 443),
port: crypto.__disabled__ !== undefined ? 80 : 443,
path: "/",
headers: {
"User-Agent": generateServerString(true)
"User-Agent": generateServerString(true),
},
timeout: 5000
timeout: 5000,
});
if (crypto.__disabled__ === undefined) {
@ -538,9 +538,9 @@ if (host != "[offline]" || ifaceEx) {
port: 443,
path: "/",
headers: {
"User-Agent": generateServerString(true)
"User-Agent": generateServerString(true),
},
timeout: 5000
timeout: 5000,
});
}
} else {
@ -698,7 +698,9 @@ if (!disableMods) {
throw new Error(
"This version of " +
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");
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 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}$/));
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 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 sAccHost = host;
if (!listenToAny) accHost = listenAddress;
@ -859,72 +875,168 @@ function listeningMessage() {
serverconsole.locmessage("Started server at: ");
if (process.serverConfig.secure && (sListenToLocalhost || sListenToAny)) {
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 {
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") {
serverconsole.locmessage("* http://localhost" + (process.serverConfig.port == 80 ? "" : (":" + process.serverConfig.port)));
serverconsole.locmessage(
"* http://localhost" +
(process.serverConfig.port == 80
? ""
: ":" + process.serverConfig.port),
);
} else {
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 (!(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)));
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 (
!(
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(() => {
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 && process.serverConfig.disableNonEncryptedServer) && !listenToLocalhost) serverconsole.locmessage("* http://" + (pubip.indexOf(":") > -1 ? "[" + pubip + "]" : pubip) + (process.serverConfig.pubport == 80 ? "" : (":" + process.serverConfig.pubport)));
if (process.serverConfig.secure && !sListenToLocalhost)
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 (process.serverConfig.secure && !sListenToLocalhost) 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)));
if (process.serverConfig.secure && !sListenToLocalhost)
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
if (!process.serverConfig.optOutOfStatisticsServer) {
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 {
const statisticsToSend = JSON.stringify({
version: version,
runtime: process.isBun ? "Bun" : "Node.js",
runtimeVersion: process.isBun ? process.versions.bun : process.version,
mods: modInfos
runtimeVersion: process.isBun
? process.versions.bun
: process.version,
mods: modInfos,
});
const statisticsRequest = https.request(statisticsServerCollectEndpoint, {
method: "POST",
headers: {
"User-Agent": generateServerString(true),
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(statisticsToSend)
}
}, function (res) {
const statusCode = res.statusCode;
let data = "";
res.on("data", function (chunk) {
data += chunk.toString();
});
res.on("end", function () {
try {
let parsedJson = {};
const statisticsRequest = https.request(
statisticsServerCollectEndpoint,
{
method: "POST",
headers: {
"User-Agent": generateServerString(true),
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(statisticsToSend),
},
},
function (res) {
const statusCode = res.statusCode;
let data = "";
res.on("data", function (chunk) {
data += chunk.toString();
});
res.on("end", function () {
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) {
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) {
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);
}
@ -1379,7 +1491,9 @@ function SVRJSFork() {
newWorker.on("error", function (err) {
if (!exiting)
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,
);
});
@ -2431,4 +2545,4 @@ try {
setTimeout(function () {
process.exit(err.errno !== undefined ? err.errno : 1);
}, 10);
}
}