From 460829e0be1d280a7f0ee7e7e2db4156836759ec Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Mon, 26 Aug 2024 10:14:21 +0200 Subject: [PATCH] Move code for sending the data to the statistics server to separate utility function --- src/index.js | 61 ++++--------------------------- src/utils/sendStatistics.js | 73 +++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 53 deletions(-) create mode 100644 src/utils/sendStatistics.js diff --git a/src/index.js b/src/index.js index 89398ae..2c51112 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ const logo = require("./res/logo.js"); const generateServerString = require("./utils/generateServerString.js"); const deleteFolderRecursive = require("./utils/deleteFolderRecursive.js"); const svrjsInfo = require("../svrjs.json"); -const { name, version, statisticsServerCollectEndpoint } = svrjsInfo; +const { name, version } = svrjsInfo; let inspector = undefined; try { @@ -211,6 +211,7 @@ const { calculateNetworkIPv4FromCidr, calculateBroadcastIPv4FromCidr, } = require("./utils/ipSubnetUtils.js"); +const sendStatistics = require("./utils/sendStatistics.js"); process.serverConfig = {}; 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.", ); } else { - const statisticsToSend = JSON.stringify({ - version: version, - runtime: process.isBun ? "Bun" : "Node.js", - runtimeVersion: process.isBun - ? process.versions.bun - : process.version, - mods: modInfos, + sendStatistics(modInfos, (err) => { + if (err) + serverconsole.locwarnmessage( + "There was a problem, when sending data to statistics server! Reason: " + + err.message, + ); }); - 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( - "There was a problem, when sending data to statistics server! Reason: " + - err.message, - ); - }); - statisticsRequest.end(statisticsToSend); } } }); diff --git a/src/utils/sendStatistics.js b/src/utils/sendStatistics.js new file mode 100644 index 0000000..b893d8c --- /dev/null +++ b/src/utils/sendStatistics.js @@ -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;