Verified Commit 940ed8c4 authored by Aral Balkan's avatar Aral Balkan
Browse files

Closes #14: you can now run multiple local HTTPS servers

parent 6f4c1800
......@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [6.0.0] - 2020-11-03
### Changed
- __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)
## [5.4.1] - 2020-07-11
### Changed
......
......@@ -69,8 +69,11 @@ Object.entries(os.networkInterfaces())
.map(addresses => addresses.address)).flat()
```
### Accessing your local machine from other devices on your local area network
### Multiple servers
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.
### Accessing your local machine from other devices on your local area network
You can access local servers via their IPv4 address over a local area network.
......
......@@ -128,14 +128,20 @@ class HttpServer {
// that don’t have this archaic restriction which is security theatre at best and a security
// vulnerability at worst in the global digital network age.
await new Promise((resolve, reject) => {
try {
this.server.listen(80, () => {
log(` ✨ ❨auto-encrypt-localhost❩ HTTP server is listening on port 80.`)
this.server.on('error', error => {
if (error.code === 'EADDRINUSE') {
console.log(' ❕ ❨auto-encrypt-localhost❩ Port 80 is busy; skipping http redirection server for this instance.')
resolve()
})
} catch (error) {
return
}
reject(error)
}
})
this.server.listen(80, (error) => {
log(` ✨ ❨auto-encrypt-localhost❩ HTTP server is listening on port 80.`)
resolve()
})
})
}
......
{
"name": "@small-tech/auto-encrypt-localhost",
"version": "5.4.1",
"version": "6.0.0",
"description": "Automatically provisions and installs locally-trusted TLS certificates for Node.js https servers (including Express.js, etc.) using mkcert.",
"keywords": [
"mkcert",
......
......@@ -92,5 +92,31 @@ test('certificate creation', async t => {
t.ok(fs.existsSync(path.join(customSettingsPath, 'localhost.pem')), '(Custom settings path) Local certificate exists.')
t.ok(fs.existsSync(path.join(customSettingsPath, 'localhost-key.pem')), '(Custom settings path) Local certificate private key exists.')
t.end()
server2.close(() => {
t.end()
})
})
test ('multiple servers', t => {
const server1Response = 'Server 1'
const server2Response = 'Server 2'
const server1 = AutoEncryptLocalhost.https.createServer((request, response) => { response.end(server1Response) })
server1.listen(443, () => {
const server2 = AutoEncryptLocalhost.https.createServer((request, response) => { response.end(server2Response) })
server2.listen(444, async () => {
const result1 = await downloadString('https://localhost')
const result2 = await downloadString('https://localhost:444')
t.strictEquals(result1, server1Response, 'Server 1 response is as expected.')
t.strictEquals(result2, server2Response, 'Server 2 response is as expected.')
server1.close(() => {
server2.close(() => {
t.end()
})
})
})
})
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment