From 4f467f943e1ab2bae2a01fa7cb4db4b6fc41f3ba Mon Sep 17 00:00:00 2001 From: Link33d Date: Tue, 13 May 2025 00:13:33 -0300 Subject: [PATCH] fix(server): prevent crash when SSL certificate is invalid --- .env.example | 4 ++++ src/main.ts | 10 +++++++++- src/utils/server-up.ts | 20 ++++++++++++-------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index 02eca6123..1a7b6d4ad 100644 --- a/.env.example +++ b/.env.example @@ -3,6 +3,10 @@ SERVER_PORT=8080 # Server URL - Set your application url SERVER_URL=http://localhost:8080 +# Paths to your SSL certificate files (used for HTTPS) +SSL_CONF_PRIVKEY=/path/to/cert.key +SSL_CONF_FULLCHAIN=/path/to/cert.crt + SENTRY_DSN= # Cors - * for all or set separate by commas - ex.: 'yourdomain1.com, yourdomain2.com' diff --git a/src/main.ts b/src/main.ts index 938d51d24..a027c011f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -128,7 +128,15 @@ async function bootstrap() { const httpServer = configService.get('SERVER'); ServerUP.app = app; - const server = ServerUP[httpServer.TYPE]; + let server = ServerUP[httpServer.TYPE]; + + if (server === null) { + logger.warn("SSL cert load failed — falling back to HTTP.") + logger.info("Ensure 'SSL_CONF_PRIVKEY' and 'SSL_CONF_FULLCHAIN' env vars point to valid certificate files."); + + httpServer.TYPE = "http" + server = ServerUP[httpServer.TYPE] + } eventManager.init(server); diff --git a/src/utils/server-up.ts b/src/utils/server-up.ts index 326b3be45..317c82cd8 100644 --- a/src/utils/server-up.ts +++ b/src/utils/server-up.ts @@ -12,14 +12,18 @@ export class ServerUP { } static get https() { - const { FULLCHAIN, PRIVKEY } = configService.get('SSL_CONF'); - return https.createServer( - { - cert: readFileSync(FULLCHAIN), - key: readFileSync(PRIVKEY), - }, - ServerUP.#app, - ); + try { + const { FULLCHAIN, PRIVKEY } = configService.get('SSL_CONF'); + return https.createServer( + { + cert: readFileSync(FULLCHAIN), + key: readFileSync(PRIVKEY), + }, + ServerUP.#app, + ); + } catch { + return null + } } static get http() {