Skip to content

Commit 940ed8c

Browse files
committed
Closes #14: you can now run multiple local HTTPS servers
1 parent 6f4c180 commit 940ed8c

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [6.0.0] - 2020-11-03
9+
10+
### Changed
11+
12+
- __Breaking change:__ Running multiple servers at different HTTPS ports no longer results in an error due to port 80 being unavailable for the HTTP Server. However, know that only the first server will get the HTTP Server at port 80 that redirects HTTP calls to HTTPS and also serves your local root certificate authority public key. (#14)
13+
814
## [5.4.1] - 2020-07-11
915

1016
### Changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ Object.entries(os.networkInterfaces())
6969
.map(addresses => addresses.address)).flat()
7070
```
7171

72-
### Accessing your local machine from other devices on your local area network
72+
### Multiple servers
73+
74+
You are not limited to running your server on port 444. You can listen on any port you like and you can have multiple servers with the following caveat: the HTTP server that redirects HTTP calls to HTTPS and serves your local root certificate authority public key (see below) will only be created for the first server and then only if port 80 is free.
7375

76+
### Accessing your local machine from other devices on your local area network
7477

7578
You can access local servers via their IPv4 address over a local area network.
7679

lib/HttpServer.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,20 @@ class HttpServer {
128128
// that don’t have this archaic restriction which is security theatre at best and a security
129129
// vulnerability at worst in the global digital network age.
130130
await new Promise((resolve, reject) => {
131-
try {
132-
this.server.listen(80, () => {
133-
log(` ✨ ❨auto-encrypt-localhost❩ HTTP server is listening on port 80.`)
131+
132+
this.server.on('error', error => {
133+
if (error.code === 'EADDRINUSE') {
134+
console.log(' ❕ ❨auto-encrypt-localhost❩ Port 80 is busy; skipping http redirection server for this instance.')
134135
resolve()
135-
})
136-
} catch (error) {
136+
return
137+
}
137138
reject(error)
138-
}
139+
})
140+
141+
this.server.listen(80, (error) => {
142+
log(` ✨ ❨auto-encrypt-localhost❩ HTTP server is listening on port 80.`)
143+
resolve()
144+
})
139145
})
140146
}
141147

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@small-tech/auto-encrypt-localhost",
3-
"version": "5.4.1",
3+
"version": "6.0.0",
44
"description": "Automatically provisions and installs locally-trusted TLS certificates for Node.js https servers (including Express.js, etc.) using mkcert.",
55
"keywords": [
66
"mkcert",

test/index.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,31 @@ test('certificate creation', async t => {
9292
t.ok(fs.existsSync(path.join(customSettingsPath, 'localhost.pem')), '(Custom settings path) Local certificate exists.')
9393
t.ok(fs.existsSync(path.join(customSettingsPath, 'localhost-key.pem')), '(Custom settings path) Local certificate private key exists.')
9494

95-
t.end()
95+
server2.close(() => {
96+
t.end()
97+
})
98+
})
99+
100+
101+
test ('multiple servers', t => {
102+
const server1Response = 'Server 1'
103+
const server2Response = 'Server 2'
104+
const server1 = AutoEncryptLocalhost.https.createServer((request, response) => { response.end(server1Response) })
105+
server1.listen(443, () => {
106+
const server2 = AutoEncryptLocalhost.https.createServer((request, response) => { response.end(server2Response) })
107+
server2.listen(444, async () => {
108+
const result1 = await downloadString('https://localhost')
109+
const result2 = await downloadString('https://localhost:444')
110+
111+
t.strictEquals(result1, server1Response, 'Server 1 response is as expected.')
112+
t.strictEquals(result2, server2Response, 'Server 2 response is as expected.')
113+
114+
server1.close(() => {
115+
server2.close(() => {
116+
t.end()
117+
})
118+
})
119+
})
120+
})
121+
96122
})

0 commit comments

Comments
 (0)