1
0
Fork 0
forked from svrjs/svrjs

Move code for sending the data to the statistics server to separate utility function

This commit is contained in:
Dorian Niemiec 2024-08-26 10:14:21 +02:00
parent 686ec4924e
commit 460829e0be
2 changed files with 81 additions and 53 deletions

View file

@ -7,7 +7,7 @@ const logo = require("./res/logo.js");
const generateServerString = require("./utils/generateServerString.js"); const generateServerString = require("./utils/generateServerString.js");
const deleteFolderRecursive = require("./utils/deleteFolderRecursive.js"); const deleteFolderRecursive = require("./utils/deleteFolderRecursive.js");
const svrjsInfo = require("../svrjs.json"); const svrjsInfo = require("../svrjs.json");
const { name, version, statisticsServerCollectEndpoint } = svrjsInfo; const { name, version } = svrjsInfo;
let inspector = undefined; let inspector = undefined;
try { try {
@ -211,6 +211,7 @@ const {
calculateNetworkIPv4FromCidr, calculateNetworkIPv4FromCidr,
calculateBroadcastIPv4FromCidr, calculateBroadcastIPv4FromCidr,
} = require("./utils/ipSubnetUtils.js"); } = require("./utils/ipSubnetUtils.js");
const sendStatistics = require("./utils/sendStatistics.js");
process.serverConfig = {}; process.serverConfig = {};
let configJSONRErr = undefined; let configJSONRErr = undefined;
@ -990,59 +991,13 @@ function listeningMessage() {
"Sending data to statistics server is disabled, because the server only supports HTTPS, and your Node.JS version doesn't have crypto support.", "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({ sendStatistics(modInfos, (err) => {
version: version, if (err)
runtime: process.isBun ? "Bun" : "Node.js",
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),
},
},
(res) => {
const statusCode = res.statusCode;
let data = "";
res.on("data", (chunk) => {
data += chunk.toString();
});
res.on("end", () => {
try {
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) {
serverconsole.locwarnmessage(
"There was a problem, when sending data to statistics server! Reason: " +
err.message,
);
}
});
},
);
statisticsRequest.on("error", (err) => {
serverconsole.locwarnmessage( serverconsole.locwarnmessage(
"There was a problem, when sending data to statistics server! Reason: " + "There was a problem, when sending data to statistics server! Reason: " +
err.message, err.message,
); );
}); });
statisticsRequest.end(statisticsToSend);
} }
} }
}); });

View file

@ -0,0 +1,73 @@
const generateServerString = require("./generateServerString.js");
const svrjsInfo = require("../svrjs.json");
const { version, statisticsServerCollectEndpoint } = svrjsInfo;
let crypto = {
__disabled__: null,
};
let https = {
createServer: function () {
throw new Error("Crypto support is not present");
},
connect: function () {
throw new Error("Crypto support is not present");
},
get: function () {
throw new Error("Crypto support is not present");
},
};
try {
crypto = require("crypto");
https = require("https");
} catch (err) {
// Can't load HTTPS
}
function sendStatistics(modInfos, callback) {
const statisticsToSend = JSON.stringify({
version: version,
runtime: process.isBun ? "Bun" : "Node.js",
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),
},
},
(res) => {
const statusCode = res.statusCode;
let data = "";
res.on("data", (chunk) => {
data += chunk.toString();
});
res.on("end", () => {
try {
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);
callback(null);
} catch (err) {
callback(err);
}
});
},
);
statisticsRequest.on("error", (err) => {
callback(err);
});
statisticsRequest.end(statisticsToSend);
}
module.exports = sendStatistics;