Skip to content

Commit

Permalink
Merge 5eeca42 into b3ff163
Browse files Browse the repository at this point in the history
  • Loading branch information
rgoldfinger-quizlet committed Sep 5, 2019
2 parents b3ff163 + 5eeca42 commit f7b73fc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ app.listen(port)
- If the **port** number is not provided, it will listen on 443.
- To **redirect** the http traffic to https use `app.redirect()`.
- You can serve **static files** with `app.serve(path)`.
- You can create a certificate for additional domains with `require("https-localhost")("mydomain.com")`

**Tip:** consider installing it as a dev dependency: this is not a production tool!
`npm i --save-dev https-localhost`
Expand Down
21 changes: 11 additions & 10 deletions certs.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ function download(url, path) {
}

// execute the binary executable to generate the certificates
function mkcert(appDataPath, exe) {
function mkcert(appDataPath, exe, domain) {
const logPath = path.join(appDataPath, "mkcert.log")
const errPath = path.join(appDataPath, "mkcert.err")
// escape spaces in appDataPath (Mac OS)
appDataPath = appDataPath.replace(" ", "\\ ")
const exePath = path.join(appDataPath, exe)
const crtPath = path.join(appDataPath, "localhost.crt")
const keyPath = path.join(appDataPath, "localhost.key")
const crtPath = path.join(appDataPath, domain + ".crt")
const keyPath = path.join(appDataPath, domain + ".key")
const cmd = exePath + " -install -cert-file " + crtPath +
" -key-file " + keyPath + " localhost"
" -key-file " + keyPath + " " + domain
return new Promise((resolve, reject) => {
console.log("Running mkcert to generate certificates...")
exec(cmd, (err, stdout, stderr) => {
Expand All @@ -96,7 +96,8 @@ function mkcert(appDataPath, exe) {
})
}

async function generate(appDataPath = CERT_PATH) {
async function generate(appDataPath = CERT_PATH, customDomain = undefined) {
const domain = customDomain || "localhost"
console.info("Generating certificates...")
console.log("Certificates path: " + appDataPath +
". Never modify nor share this files.")
Expand All @@ -114,19 +115,19 @@ async function generate(appDataPath = CERT_PATH) {
// make binary executable
fs.chmodSync(exePath, "0755")
// execute the binary
await mkcert(appDataPath, exe)
await mkcert(appDataPath, exe, domain)
console.log("Certificates generated, installed and trusted. Ready to go!")
}

async function getCerts() {
async function getCerts(customDomain) {
const certPath = process.env.CERT_PATH || CERT_PATH
// check for updates if running as executable
/* istanbul ignore if: cannot test pkg */
if (process.pkg) checkUpdates()
// check if a reinstall is forced or needed by a mkcert update
if (process.env.REINSTALL ||
!fs.existsSync(path.join(certPath, getExe())))
await generate(certPath)
await generate(certPath, customDomain)
try {
return {
key: fs.readFileSync(path.join(certPath, "localhost.key")),
Expand All @@ -141,9 +142,9 @@ async function getCerts() {
} else {
// Missing certificates (first run)
// generate the certificate
await generate(CERT_PATH)
await generate(CERT_PATH, customDomain)
// recursive call
return getCerts()
return getCerts(customDomain)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getCerts = require(path.resolve(__dirname, "certs.js")).getCerts
/* CONFIGURE THE SERVER */

// SSL certificate
const createServer = () => {
const createServer = (domain = "localhost") => {
// create a server with express
const app = express()

Expand All @@ -22,7 +22,8 @@ const createServer = () => {
// override the default express listen method to use our server
app.listen = async function(port = process.env.PORT ||
/* istanbul ignore next: cannot be tested on Travis */ 443) {
app.server = https.createServer(await getCerts(), app).listen(port)
app.server = https.createServer(await getCerts(domain), app)
.listen(port)
console.info("Server running on port " + port + ".")
return app.server
}
Expand Down

0 comments on commit f7b73fc

Please sign in to comment.