2024-01-05 02:22:15 +01:00
|
|
|
const { Telemetry } = require("../../models/telemetry");
|
2024-06-21 22:38:50 +02:00
|
|
|
const { BackgroundService } = require("../BackgroundWorkers");
|
|
|
|
const { EncryptionManager } = require("../EncryptionManager");
|
2024-04-01 22:56:35 +02:00
|
|
|
const { CommunicationKey } = require("../comKey");
|
2024-01-05 02:22:15 +01:00
|
|
|
const setupTelemetry = require("../telemetry");
|
|
|
|
|
2024-07-20 02:40:22 +02:00
|
|
|
// Testing SSL? You can make a self signed certificate and point the ENVs to that location
|
|
|
|
// make a directory in server called 'sslcert' - cd into it
|
|
|
|
// - openssl genrsa -aes256 -passout pass:gsahdg -out server.pass.key 4096
|
|
|
|
// - openssl rsa -passin pass:gsahdg -in server.pass.key -out server.key
|
|
|
|
// - rm server.pass.key
|
|
|
|
// - openssl req -new -key server.key -out server.csr
|
|
|
|
// Update .env keys with the correct values and boot. These are temporary and not real SSL certs - only use for local.
|
|
|
|
// Test with https://localhost:3001/api/ping
|
|
|
|
// build and copy frontend to server/public with correct API_BASE and start server in prod model and all should be ok
|
2024-01-05 02:22:15 +01:00
|
|
|
function bootSSL(app, port = 3001) {
|
|
|
|
try {
|
|
|
|
console.log(
|
|
|
|
`\x1b[33m[SSL BOOT ENABLED]\x1b[0m Loading the certificate and key for HTTPS mode...`
|
|
|
|
);
|
|
|
|
const fs = require("fs");
|
|
|
|
const https = require("https");
|
|
|
|
const privateKey = fs.readFileSync(process.env.HTTPS_KEY_PATH);
|
|
|
|
const certificate = fs.readFileSync(process.env.HTTPS_CERT_PATH);
|
|
|
|
const credentials = { key: privateKey, cert: certificate };
|
2024-05-17 23:03:25 +02:00
|
|
|
const server = https.createServer(credentials, app);
|
2024-01-05 02:22:15 +01:00
|
|
|
|
2024-05-17 23:03:25 +02:00
|
|
|
server
|
2024-01-05 02:22:15 +01:00
|
|
|
.listen(port, async () => {
|
|
|
|
await setupTelemetry();
|
2024-04-01 22:56:35 +02:00
|
|
|
new CommunicationKey(true);
|
2024-06-21 22:38:50 +02:00
|
|
|
new EncryptionManager();
|
|
|
|
new BackgroundService().boot();
|
2024-01-05 02:22:15 +01:00
|
|
|
console.log(`Primary server in HTTPS mode listening on port ${port}`);
|
|
|
|
})
|
|
|
|
.on("error", catchSigTerms);
|
2024-05-17 23:03:25 +02:00
|
|
|
|
2024-07-20 02:40:22 +02:00
|
|
|
require("@mintplex-labs/express-ws").default(app, server);
|
2024-05-17 23:03:25 +02:00
|
|
|
return { app, server };
|
2024-01-05 02:22:15 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(
|
|
|
|
`\x1b[31m[SSL BOOT FAILED]\x1b[0m ${e.message} - falling back to HTTP boot.`,
|
|
|
|
{
|
|
|
|
ENABLE_HTTPS: process.env.ENABLE_HTTPS,
|
|
|
|
HTTPS_KEY_PATH: process.env.HTTPS_KEY_PATH,
|
|
|
|
HTTPS_CERT_PATH: process.env.HTTPS_CERT_PATH,
|
|
|
|
stacktrace: e.stack,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return bootHTTP(app, port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bootHTTP(app, port = 3001) {
|
|
|
|
if (!app) throw new Error('No "app" defined - crashing!');
|
|
|
|
|
|
|
|
app
|
|
|
|
.listen(port, async () => {
|
|
|
|
await setupTelemetry();
|
2024-04-01 22:56:35 +02:00
|
|
|
new CommunicationKey(true);
|
2024-06-21 22:38:50 +02:00
|
|
|
new EncryptionManager();
|
|
|
|
new BackgroundService().boot();
|
2024-01-05 02:22:15 +01:00
|
|
|
console.log(`Primary server in HTTP mode listening on port ${port}`);
|
|
|
|
})
|
|
|
|
.on("error", catchSigTerms);
|
2024-05-17 23:03:25 +02:00
|
|
|
|
|
|
|
return { app, server: null };
|
2024-01-05 02:22:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function catchSigTerms() {
|
|
|
|
process.once("SIGUSR2", function () {
|
|
|
|
Telemetry.flush();
|
|
|
|
process.kill(process.pid, "SIGUSR2");
|
|
|
|
});
|
|
|
|
process.on("SIGINT", function () {
|
|
|
|
Telemetry.flush();
|
|
|
|
process.kill(process.pid, "SIGINT");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
bootHTTP,
|
|
|
|
bootSSL,
|
|
|
|
};
|