Skip to content

Commit

Permalink
fix: ssl check if folder exists before creating keys
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Mar 25, 2021
1 parent 00a6388 commit 79b4223
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/server/ssl.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
const path = require('path');
const os = require('os');
const {existsSync, readFileSync, readdirSync, writeFileSync} = require('fs');

const {
existsSync,
readFileSync,
readdirSync,
writeFileSync,
mkdirSync,
} = require('fs');
const chalk = require('chalk');
const ora = require('ora');
const selfsigned = require('selfsigned');

const spinner = ora(chalk.magenta('Creating SSL certificate...'));
const certFolder = path.resolve(os.homedir(), '.localhost_ssl/');

function makeCert(args) {
const name = args.name ? args.name : 'localhost';
spinner.start();
const attrs = [{name: 'commonName', value: name}];
const pems = selfsigned.generate(attrs, {days: 365});
makeCertFolder();
try {
const certFile = path.resolve(
os.homedir(),
'.localhost_ssl/',
`${name}.crt`
);
const keyFile = path.resolve(
os.homedir(),
'.localhost_ssl/',
`${name}.key`
);
const certFile = path.resolve(certFolder, `${name}.crt`);
const keyFile = path.resolve(certFolder, `${name}.key`);
writeFileSync(certFile, pems.cert);
writeFileSync(keyFile, pems.private);
console.log('\n');
console.log(chalk.green(`SSL Cert: ${certFile}`));
console.log(chalk.green(`SSL Key: ${keyFile}`));
console.log('\n');
spinner.succeed('SSL certificate created successfully!');
} catch (err) {
console.error(err);
Expand All @@ -38,34 +35,41 @@ function makeCert(args) {
}

function sslKeyCert() {
makeCertFolder();
const key = readFileSync(getSSLKeyPath());
const cert = readFileSync(getSSLCertPath());

return {key, cert};
}

function getSSLKeyPath() {
const fileName = findKey() || 'localhost.key';
const key = path.resolve(os.homedir(), '.localhost_ssl/', fileName);
const key = path.resolve(certFolder, fileName);
return existsSync(key) ? key : path.join(__dirname, './server.pem');
}

function getSSLCertPath() {
const fileName = findCert() || 'localhost.crt';
const cert = path.resolve(os.homedir(), '.localhost_ssl/', fileName);
const cert = path.resolve(certFolder, fileName);
return existsSync(cert) ? cert : path.join(__dirname, './server.pem');
}

function findCert() {
const files = readdirSync(path.resolve(os.homedir(), '.localhost_ssl'));
const files = readdirSync(certFolder);
return files.find((file) => file.includes('.crt') || file.includes('.cer'));
}

function findKey() {
const files = readdirSync(path.resolve(os.homedir(), '.localhost_ssl'));
const files = readdirSync(certFolder);
return files.find((file) => file.includes('.key') || file.includes('.pem'));
}

function makeCertFolder() {
if (!existsSync(certFolder)) {
console.log(chalk.green(`\nCreating a cert folder at: ${certFolder}`));
mkdirSync(certFolder);
}
}

module.exports = {
makeCert,
sslKeyCert,
Expand Down

0 comments on commit 79b4223

Please sign in to comment.