From 7155d6c54aed9e5bf8a2159ebe4d1b9d35525316 Mon Sep 17 00:00:00 2001 From: Miroslav Pejic Date: Sat, 22 Mar 2025 19:49:07 +0100 Subject: [PATCH] [call-me] - add httpolyglot --- .env.template | 3 +-- app/server.js | 37 +++++++++++++------------------------ app/ssl/README.md | 8 +------- package.json | 5 +++-- 4 files changed, 18 insertions(+), 35 deletions(-) diff --git a/.env.template b/.env.template index fbd31c8..7652ebc 100644 --- a/.env.template +++ b/.env.template @@ -1,7 +1,6 @@ # Server -SSL=false # true or false -DOMAIN=localhost +HOST= # Default http://localhost:port PORT=8000 # Host diff --git a/app/server.js b/app/server.js index b65a9e8..cfc7e93 100755 --- a/app/server.js +++ b/app/server.js @@ -5,8 +5,7 @@ const dotenv = require('dotenv').config(); const express = require('express'); const cors = require('cors'); const fs = require('fs'); -const http = require('http'); -const https = require('https'); +const httpolyglot = require('httpolyglot'); const socketIO = require('socket.io'); const axios = require('axios'); const helmet = require('helmet'); @@ -95,32 +94,22 @@ const corsOptions = { const app = express(); // Server configurations -const domain = process.env.DOMAIN || 'localhost'; -const isHttps = process.env.SSL === 'true'; const port = process.env.PORT || 4000; -const host = `http${isHttps ? 's' : ''}://${domain}:${port}`; +const host = process.env.HOST || `http://localhost:${port}`; const apiDocs = host + config.apiBasePath + '/docs'; -// This server -let server; +// Define paths to the SSL key and certificate files +const keyPath = path.join(__dirname, 'ssl/key.pem'); +const certPath = path.join(__dirname, 'ssl/cert.pem'); -// Load self-signed certificates if HTTPS is enabled -if (isHttps) { - try { - const options = { - key: fs.readFileSync(path.join(__dirname, '/ssl/key.pem'), 'utf-8'), - cert: fs.readFileSync(path.join(__dirname, '/ssl/cert.pem'), 'utf-8'), - }; - // Create HTTPS server using Express - server = https.createServer(options, app); - } catch (err) { - log.error('Error loading certificates:', err); - process.exit(1); // Exit the process if certificates cannot be loaded - } -} else { - // Create HTTP server using Express - server = http.createServer(app); -} +// Read SSL key and certificate files securely +const options = { + key: fs.readFileSync(keyPath, 'utf-8'), + cert: fs.readFileSync(certPath, 'utf-8'), +}; + +// Server both http and https +const server = httpolyglot.createServer(options, app); // Create WebSocket server using Socket.io on top of HTTP server const io = socketIO(server); diff --git a/app/ssl/README.md b/app/ssl/README.md index 2025dbb..fc2d7b3 100644 --- a/app/ssl/README.md +++ b/app/ssl/README.md @@ -2,7 +2,7 @@ ![call-me-https](https.png) -1. Generate a [self-signed certificate](https://en.wikipedia.org/wiki/Self-signed_certificate) +Generate a [self-signed certificate](https://en.wikipedia.org/wiki/Self-signed_certificate) ```bash # install openssl 4 ubuntu @@ -19,9 +19,3 @@ rm csr.pem # https://www.sslchecker.com/certdecoder ``` - -2. Expose `server.js` on `https` using the `self-signed certificate`, edit the `.env` file - -```bash -SSL=true -``` diff --git a/package.json b/package.json index e5fdbdb..2b0de14 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "call-me", - "version": "1.0.77", + "version": "1.0.78", "description": "Your Go-To for Instant Video Calls", "author": "Miroslav Pejic - miroslav.pejic.85@gmail.com", "license": "AGPLv3", @@ -19,12 +19,13 @@ "lint": "npx prettier --write ." }, "dependencies": { - "axios": "^1.8.3", + "axios": "^1.8.4", "colors": "^1.4.0", "cors": "^2.8.5", "dotenv": "^16.4.7", "express": "^4.21.2", "helmet": "^8.1.0", + "httpolyglot": "0.1.2", "js-yaml": "4.1.0", "socket.io": "^4.8.1", "swagger-ui-express": "5.0.1"