Skip to content

Commit

Permalink
feat: SSL/TLS for wss connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongbeom-Kim committed Nov 11, 2024
1 parent 17f49b3 commit 34b0a28
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions peerprep/backend/collab-service/src/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const express = require('express');
const WebSocket = require('ws');
const http = require('http');
const https = require('https');
const fs = require('fs');
const StaticServer = require('node-static').Server;
const ywsUtils = require('y-websocket/bin/utils');
const setupWSConnection = ywsUtils.setupWSConnection;
Expand All @@ -11,10 +13,29 @@ const { Server } = require("socket.io");
const cors = require("cors");
const gptRoutes = require('./routes/gptRoutes');
const dotenv = require('dotenv');

dotenv.config();
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ noServer: true });

let server;
if (process.env.SSL_KEY_PATH && process.env.SSL_CERT_PATH && process.env.SSL_CA_PATH) {
// Load SSL/TLS certificates from environment variables
const options = {
key: fs.readFileSync(process.env.SSL_KEY_PATH),
cert: fs.readFileSync(process.env.SSL_CERT_PATH),
ca: fs.readFileSync(process.env.SSL_CA_PATH)
};

// Create an HTTPS server
server = https.createServer(options, app);
console.log('HTTPS server created with SSL/TLS certificates.');
} else {
// Fallback to HTTP server if SSL/TLS environment variables are not set
const http = require('http');
server = http.createServer(app);
console.log('HTTP server created as SSL/TLS certificates are not provided.');
}

const wss = new WebSocket.Server({ server });

app.use(cors());
app.use(express.json());
Expand Down

0 comments on commit 34b0a28

Please sign in to comment.