Prepare for auto self signed cert

main
Talon 2024-08-30 17:30:58 +02:00
parent e384b6fcc3
commit ce895a6716
4 changed files with 61 additions and 2 deletions

View File

@ -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",

View File

@ -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"

View File

@ -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;
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"] || "";

View File

@ -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}`);
});
});
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
};
}