diff --git a/CHANGELOG.md b/CHANGELOG.md index 8627797..9542970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [0.0.1-alpha.20](https://github.com/DIG-Network/dig-content-server/compare/v0.0.1-alpha.19...v0.0.1-alpha.20) (2024-09-16) + + +### Bug Fixes + +* ssl cert ([a8b1f84](https://github.com/DIG-Network/dig-content-server/commit/a8b1f84592d2cb2b505a9f63af1e7b12c9eb36ca)) + ### [0.0.1-alpha.19](https://github.com/DIG-Network/dig-content-server/compare/v0.0.1-alpha.18...v0.0.1-alpha.19) (2024-09-16) diff --git a/package-lock.json b/package-lock.json index 407c9aa..ca5e858 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "dig-content-server", - "version": "0.0.1-alpha.19", + "version": "0.0.1-alpha.20", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dig-content-server", - "version": "0.0.1-alpha.19", + "version": "0.0.1-alpha.20", "license": "ISC", "dependencies": { "@dignetwork/dig-sdk": "^0.0.1-alpha.17", diff --git a/package.json b/package.json index 63e65cb..9ac862a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dig-content-server", - "version": "0.0.1-alpha.19", + "version": "0.0.1-alpha.20", "description": "", "type": "commonjs", "main": "./dist/index.js", diff --git a/src/app.ts b/src/app.ts index 0d5d18d..49e5960 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,19 +1,56 @@ -import express from "express"; -import { storeRoutes } from "./routes"; import cookieParser from "cookie-parser"; import cors from "cors"; +import express from "express"; +import https from "https"; +import fs from "fs"; +import path from "path"; +import { storeRoutes } from "./routes"; +import { Tls } from "chia-server-coin"; + +const caCertPath = path.join(__dirname, "ssl", "ca", "chia_ca.crt"); +const caKeyPath = path.join(__dirname, "ssl", "ca", "chia_ca.key"); + +const serverCertPath = path.join(__dirname, "ssl", "dig", "server.cert"); +const serverKeyPath = path.join(__dirname, "ssl", "dig", "server.key"); + +if (!fs.existsSync(caCertPath) || !fs.existsSync(caKeyPath)) { + throw new Error("CA certificate or key not found."); +} + +// Ensure the directory for server certificate and key exists +const serverDir = path.dirname(serverCertPath); +if (!fs.existsSync(serverDir)) { + fs.mkdirSync(serverDir, { recursive: true }); +} + +if (!fs.existsSync(serverCertPath) || !fs.existsSync(serverKeyPath)) { + // Ensure that the Tls class will generate certs correctly, signed by your CA. + new Tls(serverCertPath, serverKeyPath); + console.log("Server certificate and key generated successfully."); +} + +const caCert = fs.readFileSync(caCertPath); +const serverCert = fs.readFileSync(serverCertPath); +const serverKey = fs.readFileSync(serverKeyPath); const app = express(); -const PORT = process.env.PORT || 3000; +const PORT = Number(process.env.PORT) || 4161; +// Apply store routes app.use(cookieParser()); app.use(cors()); - app.use("/", storeRoutes); -app.use((req, res, next) => { - res.setHeader("Referrer-Policy", "same-origin"); - next(); -}); +const serverOptions = { + key: serverKey, + cert: serverCert, + ca: caCert, + requestCert: true, // Require client certificate + rejectUnauthorized: true, // Reject unauthorized clients +}; + +// Create the HTTPS server +const server = https.createServer(serverOptions, app); -export { app, PORT }; +// Export both the app and the server +export { app, server, PORT };