Skip to content

Commit 3c8ed5f

Browse files
authored
Merge pull request #69 from PatrikElfstrom/master
Add the possibility to set a custom domain
2 parents 68cb83d + 8233760 commit 3c8ed5f

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
strategy:
1111
matrix:
1212
os: [ubuntu-latest, macOS-latest]
13-
node: [10, 12, 14]
13+
node: [12, 14, 16]
1414

1515
steps:
1616
- uses: actions/checkout@v2

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ serve ~/myproj
4343
- `sudo` may be necessary.
4444
- If a static path is not provided the current directory content will be served.
4545
- You can change the **port** setting the `PORT` environmental variable: `PORT=4433 serve ~/myproj`. Specifying port number will also prevent http to https redirect.
46+
- You can change the **host** setting the `HOST` environmental variable: `HOST=example.com serve ~/myproj`.
4647

4748
### Binaries
4849
If you don't have Node.js installed just use a packaged version! Download it from the [release page](https://github.com/daquinoaldo/https-localhost/releases).

certs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ async function generate(appDataPath = CERT_PATH, customDomain = undefined) {
138138
}
139139

140140
async function getCerts(customDomain = undefined) {
141-
const domain = customDomain || "localhost"
141+
const domain = process.env.HOST || customDomain || "localhost"
142142
const certPath = process.env.CERT_PATH || CERT_PATH
143143
// check for updates if running as executable
144144
/* istanbul ignore if: cannot test pkg */

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const getCerts = require(path.resolve(__dirname, "certs.js")).getCerts
1414
/* CONFIGURE THE SERVER */
1515

1616
// SSL certificate
17-
const createServer = (domain = "localhost") => {
17+
const createServer = (domain = process.env.HOST || "localhost") => {
1818
// create a server with express
1919
const app = express()
2020

test/test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const assert = require("assert")
22
const fs = require("fs")
33
const http = require("http")
44
const https = require("https")
5+
const tls = require("tls")
6+
const net = require("net")
57

68
const sinon = require("sinon")
79

@@ -148,6 +150,33 @@ describe("Testing certs", function() {
148150
done()
149151
})()
150152
})
153+
154+
it("works with environment domain", function(done) {
155+
(async() => {
156+
// set the environment domain
157+
process.env.HOST = "192.168.0.1"
158+
159+
// Get the cert
160+
const appCerts = await app.getCerts()
161+
162+
// Configure the cert to be able to read it
163+
const secureContext = tls.createSecureContext({
164+
cert: appCerts.cert
165+
})
166+
const secureSocket = new tls.TLSSocket(new net.Socket(), {
167+
secureContext
168+
})
169+
170+
// Read the cert and parse out the domain
171+
const cert = secureSocket.getCertificate()
172+
const certDomain = cert.subjectaltname.split(":")[1]
173+
174+
// Compare the domains
175+
assert(certDomain === process.env.HOST)
176+
177+
done()
178+
})()
179+
})
151180
})
152181

153182
// TESTS MODULE

0 commit comments

Comments
 (0)