Skip to content

Commit e6ccbaf

Browse files
mrsharpobluntoshellscape
authored andcommitted
No longer generating ssl cert when one is already specified (#1036)
* No longer generating ssl cert when one is already specified * fixed spacing
1 parent 0b4729f commit e6ccbaf

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

lib/Server.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -366,37 +366,40 @@ function Server(compiler, options) {
366366
};
367367
}
368368

369-
// Use a self-signed certificate if no certificate was configured.
370-
// Cycle certs every 24 hours
371-
const certPath = path.join(__dirname, "../ssl/server.pem");
372-
let certExists = fs.existsSync(certPath);
373-
374-
if(certExists) {
375-
const certStat = fs.statSync(certPath);
376-
const certTtl = 1000 * 60 * 60 * 24;
377-
const now = new Date();
378-
379-
// cert is more than 30 days old, kill it with fire
380-
if((now - certStat.ctime) / certTtl > 30) {
381-
console.log("SSL Certificate is more than 30 days old. Removing.");
382-
del.sync([certPath], { force: true });
383-
certExists = false;
369+
let fakeCert;
370+
if(!options.https.key || !options.https.cert) {
371+
// Use a self-signed certificate if no certificate was configured.
372+
// Cycle certs every 24 hours
373+
const certPath = path.join(__dirname, "../ssl/server.pem");
374+
let certExists = fs.existsSync(certPath);
375+
376+
if(certExists) {
377+
const certStat = fs.statSync(certPath);
378+
const certTtl = 1000 * 60 * 60 * 24;
379+
const now = new Date();
380+
381+
// cert is more than 30 days old, kill it with fire
382+
if((now - certStat.ctime) / certTtl > 30) {
383+
console.log("SSL Certificate is more than 30 days old. Removing.");
384+
del.sync([certPath], { force: true });
385+
certExists = false;
386+
}
384387
}
385-
}
386388

387-
if(!certExists) {
388-
console.log("Generating SSL Certificate");
389-
const attrs = [{ name: "commonName", value: "localhost" }];
390-
const pems = selfsigned.generate(attrs, {
391-
algorithm: "sha256",
392-
days: 30,
393-
keySize: 2048
394-
});
389+
if(!certExists) {
390+
console.log("Generating SSL Certificate");
391+
const attrs = [{ name: "commonName", value: "localhost" }];
392+
const pems = selfsigned.generate(attrs, {
393+
algorithm: "sha256",
394+
days: 30,
395+
keySize: 2048
396+
});
395397

396-
fs.writeFileSync(certPath, pems.private + pems.cert, { encoding: "utf-8" });
398+
fs.writeFileSync(certPath, pems.private + pems.cert, { encoding: "utf-8" });
399+
}
400+
fakeCert = fs.readFileSync(certPath);
397401
}
398402

399-
const fakeCert = fs.readFileSync(certPath);
400403
options.https.key = options.https.key || fakeCert;
401404
options.https.cert = options.https.cert || fakeCert;
402405

0 commit comments

Comments
 (0)