Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer generating ssl cert when one is already specified #1036

Merged
merged 4 commits into from
Aug 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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