Skip to content

Commit

Permalink
Merge pull request #69 from PatrikElfstrom/master
Browse files Browse the repository at this point in the history
Add the possibility to set a custom domain
  • Loading branch information
daquinoaldo committed Sep 8, 2021
2 parents 68cb83d + 8233760 commit 3c8ed5f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]
node: [10, 12, 14]
node: [12, 14, 16]

steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -43,6 +43,7 @@ serve ~/myproj
- `sudo` may be necessary.
- If a static path is not provided the current directory content will be served.
- You can change the **port** setting the `PORT` environmental variable: `PORT=4433 serve ~/myproj`. Specifying port number will also prevent http to https redirect.
- You can change the **host** setting the `HOST` environmental variable: `HOST=example.com serve ~/myproj`.

### Binaries
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).
Expand Down
2 changes: 1 addition & 1 deletion certs.js
Expand Up @@ -138,7 +138,7 @@ async function generate(appDataPath = CERT_PATH, customDomain = undefined) {
}

async function getCerts(customDomain = undefined) {
const domain = customDomain || "localhost"
const domain = process.env.HOST || customDomain || "localhost"
const certPath = process.env.CERT_PATH || CERT_PATH
// check for updates if running as executable
/* istanbul ignore if: cannot test pkg */
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -14,7 +14,7 @@ const getCerts = require(path.resolve(__dirname, "certs.js")).getCerts
/* CONFIGURE THE SERVER */

// SSL certificate
const createServer = (domain = "localhost") => {
const createServer = (domain = process.env.HOST || "localhost") => {
// create a server with express
const app = express()

Expand Down
29 changes: 29 additions & 0 deletions test/test.js
Expand Up @@ -2,6 +2,8 @@ const assert = require("assert")
const fs = require("fs")
const http = require("http")
const https = require("https")
const tls = require("tls")
const net = require("net")

const sinon = require("sinon")

Expand Down Expand Up @@ -148,6 +150,33 @@ describe("Testing certs", function() {
done()
})()
})

it("works with environment domain", function(done) {
(async() => {
// set the environment domain
process.env.HOST = "192.168.0.1"

// Get the cert
const appCerts = await app.getCerts()

// Configure the cert to be able to read it
const secureContext = tls.createSecureContext({
cert: appCerts.cert
})
const secureSocket = new tls.TLSSocket(new net.Socket(), {
secureContext
})

// Read the cert and parse out the domain
const cert = secureSocket.getCertificate()
const certDomain = cert.subjectaltname.split(":")[1]

// Compare the domains
assert(certDomain === process.env.HOST)

done()
})()
})
})

// TESTS MODULE
Expand Down

0 comments on commit 3c8ed5f

Please sign in to comment.