|
1 | 1 | "use strict";
|
2 | 2 |
|
3 |
| -const fs = require("fs"); |
4 | 3 | const chokidar = require("chokidar");
|
5 |
| -const path = require("path"); |
6 |
| -const webpackDevMiddleware = require("webpack-dev-middleware"); |
7 |
| -const express = require("express"); |
8 | 4 | const compress = require("compression");
|
9 |
| -const sockjs = require("sockjs"); |
| 5 | +const del = require("del"); |
| 6 | +const express = require("express"); |
| 7 | +const fs = require("fs"); |
10 | 8 | const http = require("http");
|
11 |
| -const spdy = require("spdy"); |
12 | 9 | const httpProxyMiddleware = require("http-proxy-middleware");
|
13 | 10 | const serveIndex = require("serve-index");
|
14 | 11 | 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"); |
15 | 16 | const webpack = require("webpack");
|
| 17 | +const webpackDevMiddleware = require("webpack-dev-middleware"); |
| 18 | + |
16 | 19 | const OptionsValidationError = require("./OptionsValidationError");
|
17 | 20 | const optionsSchema = require("./optionsSchema.json");
|
18 | 21 |
|
@@ -360,8 +363,37 @@ function Server(compiler, options) {
|
360 | 363 | };
|
361 | 364 | }
|
362 | 365 |
|
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); |
365 | 397 | options.https.key = options.https.key || fakeCert;
|
366 | 398 | options.https.cert = options.https.cert || fakeCert;
|
367 | 399 |
|
|
0 commit comments