From ce895a6716d698e2b315b331876c018a25e76c0c Mon Sep 17 00:00:00 2001 From: Talon Date: Fri, 30 Aug 2024 17:30:58 +0200 Subject: [PATCH] Prepare for auto self signed cert --- backend/package-lock.json | 29 +++++++++++++++++++++++++++++ backend/package.json | 1 + backend/src/config.ts | 5 ++++- backend/src/server.ts | 28 +++++++++++++++++++++++++++- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/backend/package-lock.json b/backend/package-lock.json index 0671f6f..abcbab0 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -19,6 +19,7 @@ "multer": "^1.4.5-lts.1", "ollama": "^0.5.8", "openai": "^4.56.0", + "selfsigned": "^2.4.1", "sharp": "^0.33.5", "tsx": "^4.18.0", "ws": "^8.18.0" @@ -834,6 +835,14 @@ "form-data": "^4.0.0" } }, + "node_modules/@types/node-forge": { + "version": "1.3.11", + "resolved": "https://registry.npmjs.org/@types/node-forge/-/node-forge-1.3.11.tgz", + "integrity": "sha512-FQx220y22OKNTqaByeBGqHWYz4cl94tpcxeFdvBo3wjG6XPBuZ0BNgNZRV5J5TFmmcsJ4IzsLkmGRiQbnYsBEQ==", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/qs": { "version": "6.9.15", "license": "MIT" @@ -1788,6 +1797,14 @@ } } }, + "node_modules/node-forge": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/node-forge/-/node-forge-1.3.1.tgz", + "integrity": "sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA==", + "engines": { + "node": ">= 6.13.0" + } + }, "node_modules/object-assign": { "version": "4.1.1", "license": "MIT", @@ -2015,6 +2032,18 @@ "version": "2.1.2", "license": "MIT" }, + "node_modules/selfsigned": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/selfsigned/-/selfsigned-2.4.1.tgz", + "integrity": "sha512-th5B4L2U+eGLq1TVh7zNRGBapioSORUeymIydxgFpwww9d2qyKvtuPU2jJuHvYAwwqi2Y596QBL3eEqcPEYL8Q==", + "dependencies": { + "@types/node-forge": "^1.3.0", + "node-forge": "^1" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/semver": { "version": "7.6.3", "license": "ISC", diff --git a/backend/package.json b/backend/package.json index a674d5d..58c4771 100644 --- a/backend/package.json +++ b/backend/package.json @@ -27,6 +27,7 @@ "multer": "^1.4.5-lts.1", "ollama": "^0.5.8", "openai": "^4.56.0", + "selfsigned": "^2.4.1", "sharp": "^0.33.5", "tsx": "^4.18.0", "ws": "^8.18.0" diff --git a/backend/src/config.ts b/backend/src/config.ts index 844b733..2afabb0 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -14,4 +14,7 @@ export const OPENAI_API_KEY= process.env["OPENAI_API_KEY"] || ""; export const OPENAI_MODEL = process.env["OPENAI_MODEL"] || "gpt-4o"; export const OLLAMA_URL= process.env["OLLAMA_URL"] || "http://localhost:11434"; export const OLLAMA_MODEL= process.env["OLLAMA_MODEL"] || "moondream"; -export const PORT = parseInt(process.env["PORT"]!) || 3000; \ No newline at end of file +export const PORT = parseInt(process.env["PORT"]!) || 3000; +export const USE_SSL = process.env["USE_SSL"] === "1" ? true : false; +export const SSL_KEY = process.env["SSL_KEY"] || ""; +export const SSL_CERT = process.env["SSL_CERT"] || ""; \ No newline at end of file diff --git a/backend/src/server.ts b/backend/src/server.ts index eea2bc5..58f8e1a 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -3,6 +3,7 @@ import { createServer } from "http"; import { WebSocket, WebSocketServer } from "ws"; import { attachEvents } from "./controllers/websocket-controller"; import { logger } from "./globals"; +import selfSigned from "selfsigned"; const PORT = process.env.PORT || 3000; @@ -26,4 +27,29 @@ wss.on('connection', (ws: WebSocket) => { server.listen(PORT, () => { logger.info(`Server is running on http://localhost:${PORT}`); -}); \ No newline at end of file +}); + +const getOrCreateCertificate = async () => { + if (process.env.USE_SSL === '1') { + if (!process.env.SSL_KEY || !process.env.SSL_CERT) { + return await createSelfSignedSSLCert(); + } + return { + key: process.env.SSL_KEY, + cert: process.env.SSL_CERT + }; + } + return null; +} + +const createSelfSignedSSLCert = async () => { + const selfsigned = await import('selfsigned'); + const pems = selfsigned.generate([{ name: 'Notebrook Self Signed Auto Generated Key', value: 'localhost' }], { + keySize: 2048, + days: 365 + }); + return { + key: pems.private, + cert: pems.cert + }; +} \ No newline at end of file