Skip to content

Commit

Permalink
No longer generating ssl cert when one is already specified (#1036)
Browse files Browse the repository at this point in the history
* No longer generating ssl cert when one is already specified

* fixed spacing
  • Loading branch information
mrsharpoblunto authored and shellscape committed Aug 28, 2017
1 parent 0b4729f commit e6ccbaf
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit e6ccbaf

Please sign in to comment.