diff --git a/lib/Server.js b/lib/Server.js index 1683ef8a39..1b3a0a5a00 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -366,37 +366,40 @@ function Server(compiler, options) { }; } - // Use a self-signed certificate if no certificate was configured. - // Cycle certs every 24 hours - const certPath = path.join(__dirname, "../ssl/server.pem"); - let certExists = fs.existsSync(certPath); - - if(certExists) { - const certStat = fs.statSync(certPath); - const certTtl = 1000 * 60 * 60 * 24; - const now = new Date(); - - // cert is more than 30 days old, kill it with fire - if((now - certStat.ctime) / certTtl > 30) { - console.log("SSL Certificate is more than 30 days old. Removing."); - del.sync([certPath], { force: true }); - certExists = false; + let fakeCert; + if(!options.https.key || !options.https.cert) { + // Use a self-signed certificate if no certificate was configured. + // Cycle certs every 24 hours + const certPath = path.join(__dirname, "../ssl/server.pem"); + let certExists = fs.existsSync(certPath); + + if(certExists) { + const certStat = fs.statSync(certPath); + const certTtl = 1000 * 60 * 60 * 24; + const now = new Date(); + + // cert is more than 30 days old, kill it with fire + if((now - certStat.ctime) / certTtl > 30) { + console.log("SSL Certificate is more than 30 days old. Removing."); + del.sync([certPath], { force: true }); + certExists = false; + } } - } - if(!certExists) { - console.log("Generating SSL Certificate"); - const attrs = [{ name: "commonName", value: "localhost" }]; - const pems = selfsigned.generate(attrs, { - algorithm: "sha256", - days: 30, - keySize: 2048 - }); + if(!certExists) { + console.log("Generating SSL Certificate"); + const attrs = [{ name: "commonName", value: "localhost" }]; + const pems = selfsigned.generate(attrs, { + algorithm: "sha256", + days: 30, + keySize: 2048 + }); - fs.writeFileSync(certPath, pems.private + pems.cert, { encoding: "utf-8" }); + fs.writeFileSync(certPath, pems.private + pems.cert, { encoding: "utf-8" }); + } + fakeCert = fs.readFileSync(certPath); } - const fakeCert = fs.readFileSync(certPath); options.https.key = options.https.key || fakeCert; options.https.cert = options.https.cert || fakeCert;