From f20e91fe4c77a73dc1f26b7cef6434863e956177 Mon Sep 17 00:00:00 2001 From: Fedor Borshev Date: Thu, 18 Nov 2021 22:50:46 +0300 Subject: [PATCH] Support for https --- Dockerfile | 7 +++++-- index.js | 29 ++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 45a0f68..ce9bb5c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,13 @@ FROM node:16-alpine -RUN apk update && apk --no-cache add dumb-init +RUN apk update && apk --no-cache add dumb-init openssl + +RUN openssl req -x509 -nodes -days 3650 -subj "/C=CA/ST=QC/O=Localhost Administration, Inc./CN=localhost" -addext \ + "subjectAltName=DNS:localhost" -newkey rsa:2048 -keyout /etc/ssl/private/express-selfsigned.key -out /etc/ssl/certs/express-selfsigned.crt; ADD . /srv WORKDIR /srv RUN npm ci ENTRYPOINT ["/usr/bin/dumb-init", "--"] -CMD node /srv/index.js \ No newline at end of file +CMD node /srv/index.js diff --git a/index.js b/index.js index 5772098..308f30b 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,30 @@ var express = require('express'); -var proxy = require('http-proxy-middleware'); +var fs = require('fs'); +var https = require('https'); +var createProxyMiddleware = require('http-proxy-middleware'); var morgan = require('morgan'); var app = express(); + app.use(morgan('combined')); -app.use( - '/', - proxy({ + +app.use('/', createProxyMiddleware({ target: process.env.TARGET, changeOrigin: true, followRedirects: true, - }), -); -app.listen(3000); + }) +) + +const port = process.env.PORT || 3000; + +if (process.env.SELF_SIGNED_TLS) { + var key = fs.readFileSync( '/etc/ssl/private/express-selfsigned.key' ); + var cert = fs.readFileSync( '/etc/ssl/certs/express-selfsigned.crt' ); + + https.createServer({key, cert}, app).listen(port); + console.log(`[HTTPS] Listening on 0.0.0.0:${port}...`); + console.log('Google chrome users, to allow https on localhost, go to chrome://flags/#allow-insecure-localhost'); +} else { + app.listen(port); + console.log(`Listening on 0.0.0.0:${port}...`); +}