Skip to content

Commit becab6d

Browse files
rgoldfinger-quizletdaquinoaldo
authored andcommitted
Generate certificate for other domains (daquinoaldo#25)
You can create a certificate for additional domains with const app = require("https-localhost")("mydomain.com")
1 parent 0524415 commit becab6d

5 files changed

Lines changed: 37 additions & 25 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ app.listen(port)
7171
- If the **port** number is not provided, it will listen on 443.
7272
- To **redirect** the http traffic to https use `app.redirect()`.
7373
- You can serve **static files** with `app.serve(path)`.
74+
- You can create a certificate for additional domains with `require("https-localhost")("mydomain.com")`
7475

7576
**Tip:** consider installing it as a dev dependency: this is not a production tool!
7677
`npm i --save-dev https-localhost`

certs.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ function download(url, path) {
6969
}
7070

7171
// execute the binary executable to generate the certificates
72-
function mkcert(appDataPath, exe) {
72+
function mkcert(appDataPath, exe, domain) {
7373
const logPath = path.join(appDataPath, "mkcert.log")
7474
const errPath = path.join(appDataPath, "mkcert.err")
7575
// escape spaces in appDataPath (Mac OS)
7676
appDataPath = appDataPath.replace(" ", "\\ ")
7777
const exePath = path.join(appDataPath, exe)
78-
const crtPath = path.join(appDataPath, "localhost.crt")
79-
const keyPath = path.join(appDataPath, "localhost.key")
78+
const crtPath = path.join(appDataPath, domain + ".crt")
79+
const keyPath = path.join(appDataPath, domain + ".key")
8080
const cmd = exePath + " -install -cert-file " + crtPath +
81-
" -key-file " + keyPath + " localhost"
81+
" -key-file " + keyPath + " " + domain
8282
return new Promise((resolve, reject) => {
8383
console.log("Running mkcert to generate certificates...")
8484
exec(cmd, (err, stdout, stderr) => {
@@ -96,7 +96,8 @@ function mkcert(appDataPath, exe) {
9696
})
9797
}
9898

99-
async function generate(appDataPath = CERT_PATH) {
99+
async function generate(appDataPath = CERT_PATH, customDomain = undefined) {
100+
const domain = customDomain || "localhost"
100101
console.info("Generating certificates...")
101102
console.log("Certificates path: " + appDataPath +
102103
". Never modify nor share this files.")
@@ -114,23 +115,24 @@ async function generate(appDataPath = CERT_PATH) {
114115
// make binary executable
115116
fs.chmodSync(exePath, "0755")
116117
// execute the binary
117-
await mkcert(appDataPath, exe)
118+
await mkcert(appDataPath, exe, domain)
118119
console.log("Certificates generated, installed and trusted. Ready to go!")
119120
}
120121

121-
async function getCerts() {
122+
async function getCerts(customDomain = undefined) {
123+
const domain = customDomain || "localhost"
122124
const certPath = process.env.CERT_PATH || CERT_PATH
123125
// check for updates if running as executable
124126
/* istanbul ignore if: cannot test pkg */
125127
if (process.pkg) checkUpdates()
126128
// check if a reinstall is forced or needed by a mkcert update
127129
if (process.env.REINSTALL ||
128130
!fs.existsSync(path.join(certPath, getExe())))
129-
await generate(certPath)
131+
await generate(certPath, domain)
130132
try {
131133
return {
132-
key: fs.readFileSync(path.join(certPath, "localhost.key")),
133-
cert: fs.readFileSync(path.join(certPath, "localhost.crt"))
134+
key: fs.readFileSync(path.join(certPath, domain + ".key")),
135+
cert: fs.readFileSync(path.join(certPath, domain + ".crt"))
134136
}
135137
} catch (e) {
136138
/* istanbul ignore else: should never occur */
@@ -141,9 +143,9 @@ async function getCerts() {
141143
} else {
142144
// Missing certificates (first run)
143145
// generate the certificate
144-
await generate(CERT_PATH)
146+
await generate(CERT_PATH, domain)
145147
// recursive call
146-
return getCerts()
148+
return getCerts(domain)
147149
}
148150
}
149151
}

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const getCerts = require(path.resolve(__dirname, "certs.js")).getCerts
1212
/* CONFIGURE THE SERVER */
1313

1414
// SSL certificate
15-
const createServer = () => {
15+
const createServer = (domain = "localhost") => {
1616
// create a server with express
1717
const app = express()
1818

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

package-lock.json

Lines changed: 17 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "https-localhost",
3-
"version": "4.3.0",
3+
"version": "4.4.0",
44
"description": "HTTPS server running on localhost",
55
"main": "index.js",
66
"scripts": {
@@ -55,7 +55,7 @@
5555
"eslint": "^6.3.0",
5656
"eslint-config-standard": "^14.1.0",
5757
"eslint-plugin-import": "^2.18.2",
58-
"eslint-plugin-node": "^9.2.0",
58+
"eslint-plugin-node": "^10.0.0",
5959
"eslint-plugin-promise": "^4.2.1",
6060
"eslint-plugin-standard": "^4.0.1",
6161
"mocha": "^6.2.0",

0 commit comments

Comments
 (0)