2024-08-26 10:14:21 +02:00
|
|
|
const generateServerString = require("./generateServerString.js");
|
2024-08-26 13:32:21 +02:00
|
|
|
const svrjsInfo = require("../../svrjs.json");
|
2024-08-26 10:14:21 +02:00
|
|
|
const { version, statisticsServerCollectEndpoint } = svrjsInfo;
|
|
|
|
|
|
|
|
let crypto = {
|
|
|
|
__disabled__: null,
|
|
|
|
};
|
|
|
|
let https = {
|
2024-08-27 10:54:08 +02:00
|
|
|
createServer: () => {
|
2024-08-26 10:14:21 +02:00
|
|
|
throw new Error("Crypto support is not present");
|
|
|
|
},
|
2024-08-27 10:54:08 +02:00
|
|
|
connect: () => {
|
2024-08-26 10:14:21 +02:00
|
|
|
throw new Error("Crypto support is not present");
|
|
|
|
},
|
2024-08-27 10:54:08 +02:00
|
|
|
get: () => {
|
2024-08-26 10:14:21 +02:00
|
|
|
throw new Error("Crypto support is not present");
|
|
|
|
},
|
|
|
|
};
|
|
|
|
try {
|
2024-08-27 11:28:24 +02:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2024-08-26 10:14:21 +02:00
|
|
|
crypto = require("crypto");
|
|
|
|
https = require("https");
|
2024-08-27 11:28:24 +02:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2024-08-26 10:14:21 +02:00
|
|
|
} 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);
|
2024-08-27 11:28:24 +02:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2024-08-26 10:14:21 +02:00
|
|
|
} 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;
|