From 5f6d36d05b5bae569e5d620d5597e1f1af2243a1 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Mon, 7 Jan 2019 11:22:27 -0700 Subject: [PATCH] Add lines for enabling https --- .gitignore | 1 + server.js | 15 +++++++++++++++ settings.example.json | 5 ++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1155f4e..12d133e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ public/files/*.pdf public/files/*.json public/files/*.zip public/history/*.json +.well-known/ settings.json diff --git a/server.js b/server.js index 13309cb..8c3ca17 100644 --- a/server.js +++ b/server.js @@ -2,6 +2,7 @@ const path = require('path'); const fs = require('fs'); const express = require('express'); const http = require('http'); +const https = require('https'); const socketio = require('socket.io'); const helmet = require('helmet'); const bodyParser = require('body-parser'); @@ -13,10 +14,14 @@ const snarkdown = require('snarkdown'); const fecha = require('fecha'); const settings = require('./settings.json'); +const privateKey = settings.sslPrivateKey ? fs.readFileSync(settings.sslPrivateKey, 'utf8') : null; +const certificate = settings.sslCertificate ? fs.readFileSync(settings.sslCertificate, 'utf8') : null; +const ca = settings.sslCertificateAuthority ? fs.readFileSync(settings.sslCertificateAuthority, 'utf8') : null; function Server () { this.server = express(); this.http = http.Server(this.server); + this.https = privateKey && certificate ? https.createServer({ key: privateKey, cert: certificate, ca }, this.server) : null; this.io = socketio(this.http); this.fileLocation = path.resolve(settings.fileLocation); @@ -46,6 +51,11 @@ function Server () { this.server.use('/js', express.static(path.resolve('./node_modules/jquery/dist/'))); this.server.use('/js', express.static(path.resolve('./node_modules/socket.io-client/dist/'))); + // If a `.well-known` directory exists, allow it to be used for things like Let's Encrypt challenges + if (fs.existsSync(path.resolve('./.well-known'))) { + this.server.use('/.well-known', express.static(path.resolve('./.well-known'))); + } + this.server.get('/', (req, res) => { const html = this.generateHomePage(req); if (html) { @@ -421,6 +431,11 @@ Server.prototype.start = function () { this.http.listen((process.env.PORT || settings.port), () => { console.log('Started server on port ' + (process.env.PORT || settings.port)); }); + if (this.https) { + this.https.listen(443, () => { + console.log('Started SSL server on port 443'); + }); + } } Server.prototype.addBook = function (uploadData = {}, success = () => {}, error = () => {}) { diff --git a/settings.example.json b/settings.example.json index 77388d6..53fb8cf 100644 --- a/settings.example.json +++ b/settings.example.json @@ -9,5 +9,8 @@ "maxHistory": 0, "allowedFormats": [".epub", ".mobi", ".pdf"], "backupPassword": "password", - "hideVisitors": false + "hideVisitors": false, + "sslPrivateKey": null, + "sslCertificate": null, + "sslCertificateAuthority": null } \ No newline at end of file