Skip to content

Commit d74105c

Browse files
committed
Generate certificate using mkcert
Switch to mkcert on all platform for the certificates generation. Windows support! :D Reached v3.0.0. Better testing and some minor improvement.
1 parent 58dde11 commit d74105c

13 files changed

Lines changed: 1590 additions & 849 deletions

.gitignore

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
.idea
66
*.iml
77

8-
# Node
8+
# Node & npm
99
node_modules
10-
11-
# Other
1210
coverage
13-
cert/CA.*
11+
.nyc_output
12+
13+
# Certificates
1414
cert/localhost.*
15-
cert/server.csr
15+
cert/mkcert-*

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
language: node_js
22
node_js:
33
- "node"
4-
after_script: "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
4+
after_success: npm run coveralls

cert/default.crt

Lines changed: 0 additions & 24 deletions
This file was deleted.

cert/default.key

Lines changed: 0 additions & 28 deletions
This file was deleted.

cert/defaultCA.pem

Lines changed: 0 additions & 19 deletions
This file was deleted.

cert/generate.js

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,73 @@
1+
#!/usr/bin/env node
2+
13
const exec = require("child_process").exec
4+
const fs = require("fs")
5+
const https = require("https")
6+
7+
const MKCERT_VERSION = "v1.3.0"
8+
9+
// get the executable name
10+
function getExe() {
11+
switch (process.platform) {
12+
case "darwin":
13+
return "mkcert-" + MKCERT_VERSION + "-darwin-amd64"
14+
case "linux":
15+
return "mkcert-" + MKCERT_VERSION + "-linux-amd64"
16+
case "windows":
17+
return "mkcert-" + MKCERT_VERSION + "-windows-amd64.exe"
18+
default:
19+
console.warn("Cannot generate the localhost certificate on your " +
20+
"platform. Please, consider contacting the developer if you can help.")
21+
process.exit(0)
22+
}
23+
}
224

3-
// noinspection FallThroughInSwitchStatementJS
4-
if (process.platform === "darwin" || process.platform === "linux") {
5-
console.log("\n----------------------------------------------\n" +
6-
"Please input your sudo password if required.\n" +
7-
"----------------------------------------------\n")
8-
exec("bash cert/generate.sh", (error, stdout, stderr) => {
9-
console.log(stdout)
10-
console.error(stderr)
11-
if (error !== null) console.error(`exec error: ${error}`)
25+
// download a binary file
26+
function download(url, path) {
27+
console.log("Downloading the mkcert executable...")
28+
const file = fs.createWriteStream(path)
29+
return new Promise(resolve => {
30+
function get(url, file) {
31+
https.get(url, (response) => {
32+
if (response.statusCode === 302) get(response.headers.location, file)
33+
else response.pipe(file).on("finish", resolve)
34+
})
35+
}
36+
get(url, file)
1237
})
13-
} else {
14-
console.warn("Cannot generate the localhost certificate on your " +
15-
"platform. Contact the developer if you can help.")
16-
process.exit(0)
1738
}
39+
40+
// execute the binary executable to generate the certificates
41+
function mkcert(path, exe) {
42+
return new Promise((resolve, reject) => {
43+
console.log("Running mkcert to generate certificates...")
44+
exec(path + exe + " -install -cert-file " + path + "localhost.crt " +
45+
"-key-file " + path + "localhost.key localhost", (err, stdout, stderr) => {
46+
console.log(stdout)
47+
console.error(stderr)
48+
if (err) reject(err)
49+
else resolve()
50+
})
51+
})
52+
}
53+
54+
async function main() {
55+
const url = "https://github.com/FiloSottile/mkcert/releases/download/" +
56+
MKCERT_VERSION + "/"
57+
const exe = getExe()
58+
const path = "cert/"
59+
// download the executable
60+
await download(url + exe, path + exe)
61+
// make binary executable
62+
fs.chmodSync(path + exe, "0755")
63+
// execute the binary
64+
await mkcert(path, exe)
65+
console.log("Certificates generated, installed and trusted. Ready to go!")
66+
}
67+
68+
// run as script
69+
if (require.main === module)
70+
try { main() } catch (err) { console.error("\nExec error: " + err) }
71+
72+
// export as module
73+
module.exports = main

cert/generate.sh

Lines changed: 0 additions & 38 deletions
This file was deleted.

cert/server.conf

Lines changed: 0 additions & 14 deletions
This file was deleted.

cert/x509.ext

Lines changed: 0 additions & 7 deletions
This file was deleted.

index.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,17 @@ try {
1818
key: fs.readFileSync(path.resolve(__dirname, "cert/localhost.key")),
1919
cert: fs.readFileSync(path.resolve(__dirname, "cert/localhost.crt"))
2020
}
21-
} catch (e) {
22-
// istanbul ignore next
23-
certOptions = {
24-
key: fs.readFileSync(path.resolve(__dirname, "cert/default.key")),
25-
cert: fs.readFileSync(path.resolve(__dirname, "cert/default.crt"))
26-
}
27-
// istanbul ignore next
28-
console.warn("Using the default certificate. " +
29-
"Validate it installing the defaultCA.pem certificate in the cert folder")
21+
} catch (e) /* istanbul ignore next: TODO, not so important */ {
22+
console.error("Cannot find the certificates. Try to reinstall the module.")
23+
process.exit(1)
3024
}
3125

3226
// create a server with express
3327
const app = express()
3428

3529
// override the default express listen method to use our server
36-
app.listen = function(port = process.env.PORT || 443) {
30+
app.listen = function(port = process.env.PORT ||
31+
/* istanbul ignore next: cannot be tested on Travis */ 443) {
3732
app.server = https.createServer(certOptions, app).listen(port)
3833
console.info("Server running on port " + port + ".")
3934
return app.server
@@ -47,7 +42,8 @@ app.set("json spaces", 0)
4742
/* SETUP USEFUL FUNCTIONS */
4843

4944
// redirect http to https, usage `app.redirect()`
50-
app.redirect = function(port = 80) {
45+
app.redirect = function(
46+
/* istanbul ignore next: cannot be tested on Travis */ port = 80) {
5147
app.http = http.createServer((req, res) => {
5248
res.writeHead(301, { Location: "https://" + req.headers["host"] + req.url })
5349
res.end()
@@ -56,16 +52,17 @@ app.redirect = function(port = 80) {
5652
}
5753

5854
// serve static content, usage `app.serve([path])`
59-
app.serve = function(path = process.cwd(), port = process.env.PORT || 443) {
55+
app.serve = function(path = process.cwd(), port = process.env.PORT ||
56+
/* istanbul ignore next: cannot be tested on Travis */ 443) {
6057
app.use(express.static(path))
61-
app.listen(port)
6258
console.info("Serving static path: " + path)
59+
app.listen(port)
6360
}
6461

65-
/* MAIN (running as script) */
62+
/* MAIN */
6663

6764
// usage: `serve [<path>]` or `node index.js [<path>]`
68-
// istanbul ignore if
65+
// istanbul ignore if: cannot be tested
6966
if (require.main === module) {
7067
// retrieve the static path from the process argv or use the cwd
7168
// 1st is node, 2nd is serve or index.js, 3rd (if exists) is the path

0 commit comments

Comments
 (0)