You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.8 KiB

11 months ago
  1. const { Telemetry } = require("../../models/telemetry");
  2. const { BackgroundService } = require("../BackgroundWorkers");
  3. const { EncryptionManager } = require("../EncryptionManager");
  4. const { CommunicationKey } = require("../comKey");
  5. const setupTelemetry = require("../telemetry");
  6. // Testing SSL? You can make a self signed certificate and point the ENVs to that location
  7. // make a directory in server called 'sslcert' - cd into it
  8. // - openssl genrsa -aes256 -passout pass:gsahdg -out server.pass.key 4096
  9. // - openssl rsa -passin pass:gsahdg -in server.pass.key -out server.key
  10. // - rm server.pass.key
  11. // - openssl req -new -key server.key -out server.csr
  12. // Update .env keys with the correct values and boot. These are temporary and not real SSL certs - only use for local.
  13. // Test with https://localhost:3001/api/ping
  14. // build and copy frontend to server/public with correct API_BASE and start server in prod model and all should be ok
  15. function bootSSL(app, port = 3001) {
  16. try {
  17. console.log(
  18. `\x1b[33m[SSL BOOT ENABLED]\x1b[0m Loading the certificate and key for HTTPS mode...`
  19. );
  20. const fs = require("fs");
  21. const https = require("https");
  22. const privateKey = fs.readFileSync(process.env.HTTPS_KEY_PATH);
  23. const certificate = fs.readFileSync(process.env.HTTPS_CERT_PATH);
  24. const credentials = { key: privateKey, cert: certificate };
  25. const server = https.createServer(credentials, app);
  26. server
  27. .listen(port, async () => {
  28. await setupTelemetry();
  29. new CommunicationKey(true);
  30. new EncryptionManager();
  31. new BackgroundService().boot();
  32. console.log(`Primary server in HTTPS mode listening on port ${port}`);
  33. })
  34. .on("error", catchSigTerms);
  35. require("@mintplex-labs/express-ws").default(app, server);
  36. return { app, server };
  37. } catch (e) {
  38. console.error(
  39. `\x1b[31m[SSL BOOT FAILED]\x1b[0m ${e.message} - falling back to HTTP boot.`,
  40. {
  41. ENABLE_HTTPS: process.env.ENABLE_HTTPS,
  42. HTTPS_KEY_PATH: process.env.HTTPS_KEY_PATH,
  43. HTTPS_CERT_PATH: process.env.HTTPS_CERT_PATH,
  44. stacktrace: e.stack,
  45. }
  46. );
  47. return bootHTTP(app, port);
  48. }
  49. }
  50. function bootHTTP(app, port = 3001) {
  51. if (!app) throw new Error('No "app" defined - crashing!');
  52. app
  53. .listen(port, async () => {
  54. await setupTelemetry();
  55. new CommunicationKey(true);
  56. new EncryptionManager();
  57. new BackgroundService().boot();
  58. console.log(`Primary server in HTTP mode listening on port ${port}`);
  59. })
  60. .on("error", catchSigTerms);
  61. return { app, server: null };
  62. }
  63. function catchSigTerms() {
  64. process.once("SIGUSR2", function () {
  65. Telemetry.flush();
  66. process.kill(process.pid, "SIGUSR2");
  67. });
  68. process.on("SIGINT", function () {
  69. Telemetry.flush();
  70. process.kill(process.pid, "SIGINT");
  71. });
  72. }
  73. module.exports = {
  74. bootHTTP,
  75. bootSSL,
  76. };