Skip to content

Commit 400b289

Browse files
committed
generate ssl certs per instance
1 parent 662bc31 commit 400b289

File tree

5 files changed

+43
-54
lines changed

5 files changed

+43
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
/client/index.bundle.js
44
/client/sockjs.bundle.js
55
/coverage
6+
*.pem

lib/Server.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
"use strict";
22

3-
const fs = require("fs");
43
const chokidar = require("chokidar");
5-
const path = require("path");
6-
const webpackDevMiddleware = require("webpack-dev-middleware");
7-
const express = require("express");
84
const compress = require("compression");
9-
const sockjs = require("sockjs");
5+
const del = require("del");
6+
const express = require("express");
7+
const fs = require("fs");
108
const http = require("http");
11-
const spdy = require("spdy");
129
const httpProxyMiddleware = require("http-proxy-middleware");
1310
const serveIndex = require("serve-index");
1411
const historyApiFallback = require("connect-history-api-fallback");
12+
const path = require("path");
13+
const selfsigned = require("selfsigned");
14+
const sockjs = require("sockjs");
15+
const spdy = require("spdy");
1516
const webpack = require("webpack");
17+
const webpackDevMiddleware = require("webpack-dev-middleware");
18+
1619
const OptionsValidationError = require("./OptionsValidationError");
1720
const optionsSchema = require("./optionsSchema.json");
1821

@@ -360,8 +363,37 @@ function Server(compiler, options) {
360363
};
361364
}
362365

363-
// Use built-in self-signed certificate if no certificate was configured
364-
const fakeCert = fs.readFileSync(path.join(__dirname, "../ssl/server.pem"));
366+
// Use a self-signed certificate if no certificate was configured.
367+
// Cycle certs every 24 hours
368+
const certPath = path.join(__dirname, "../ssl/server.pem");
369+
let certExists = fs.existsSync(certPath);
370+
371+
if(certExists) {
372+
const certStat = fs.statSync(certPath);
373+
const certTtl = 1000 * 60 * 60 * 24;
374+
const now = new Date();
375+
376+
// cert is more than 30 days old, kill it with fire
377+
if((now - certStat.ctime) / certTtl > 30) {
378+
console.log("SSL Certificate is more than 30 days old. Removing.");
379+
del.sync([certPath], { force: true });
380+
certExists = false;
381+
}
382+
}
383+
384+
if(!certExists) {
385+
console.log("Generating SSL Certificate");
386+
const attrs = [{ name: "commonName", value: "localhost" }];
387+
const pems = selfsigned.generate(attrs, {
388+
algorithm: "sha256",
389+
days: 30,
390+
keySize: 2048
391+
});
392+
393+
fs.writeFileSync(certPath, pems.private + pems.cert, { encoding: "utf-8" });
394+
}
395+
396+
const fakeCert = fs.readFileSync(certPath);
365397
options.https.key = options.https.key || fakeCert;
366398
options.https.cert = options.https.cert || fakeCert;
367399

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
"chokidar": "^1.6.0",
1212
"compression": "^1.5.2",
1313
"connect-history-api-fallback": "^1.3.0",
14+
"del": "^3.0.0",
1415
"express": "^4.13.3",
1516
"html-entities": "^1.2.0",
1617
"http-proxy-middleware": "~0.17.4",
1718
"opn": "4.0.2",
1819
"portfinder": "^1.0.9",
20+
"selfsigned": "^1.9.1",
1921
"serve-index": "^1.7.2",
2022
"sockjs": "0.3.18",
2123
"sockjs-client": "1.1.2",

ssl/.gitkeep

Whitespace-only changes.

ssl/server.pem

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)