attempt to use https

This commit is contained in:
Quentin Roussel
2024-03-27 23:55:53 +01:00
parent c239ab4487
commit c0c21aeeb8
8 changed files with 31 additions and 12 deletions

View File

@@ -1,3 +1,7 @@
#https dev certificates
csr.pem
key.pem
server.crt
# Logs
logs
*.log

8
traque-back/Makefile Normal file
View File

@@ -0,0 +1,8 @@
#Make a self signed certificate for development
keys: key.pem server.crt
key.pem csr.pem:
openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem
server.crt: key.pem csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out server.crt

View File

@@ -1,19 +1,26 @@
import { createServer } from "http";
import { createServer } from "https";
import { Server } from "socket.io";
import Game from "./game.js";
import { config } from "dotenv";
import { readFileSync } from "fs";
//extract admin password from .env file
config();
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD;
const HOST = process.env.HOST;
const PORT = process.env.PORT;
const httpServer = createServer();
//Password that socket clients will have to send to be able to send admin commands
const httpsServer = createServer({
key: readFileSync(process.env.SSL_KEY, 'utf-8'),
cert: readFileSync(process.env.SSL_CERT, 'utf-8')
});
httpsServer.listen(PORT, HOST, () => {
console.log(`Server running`);
});
//set cors to allow all origins
const io = new Server(httpServer, {
const io = new Server(httpsServer, {
cors: {
origin: "*",
methods: ["GET", "POST"]
@@ -195,7 +202,3 @@ io.of("player").on("connection", (socket) => {
});
});
});
httpServer.listen(PORT, HOST, () => {
console.log(`Server running`);
});