Skip to content

Commit e073a24

Browse files
committed
feat(logging): log requests in verbose mode
1 parent 4f81ff9 commit e073a24

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

config/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function listDomains() {
5757
async function processCLI() {
5858
const cli = getCLIOptions()
5959
const config = await readConfig()
60-
setupFrontline(config).start()
60+
setupFrontline({ ...config, verbose: cli.verbose }).start()
6161
}
6262

6363
function uninstallAll() {

config/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ async function readConfig() {
159159
for (let [domain, port] of Object.entries(settings.mappings)) {
160160
domain = normalizeDomain(domain)
161161
if (isValidDomain(domain) && isValidPort(port)) {
162-
result.mappings.set(domain.toLowerCase(), { port: convertPort(port) })
162+
result.mappings.set(domain, { port: convertPort(port) })
163163
}
164164
}
165165

frontline.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1+
const chalk = require('chalk')
12
const { createProxyServer } = require('http-proxy')
23
const { createServer } = require('https')
34
const { APP_NAME, findPortForMappedHost } = require('./config')
45

6+
const LOG_STAMP_FORMATTER = new Intl.DateTimeFormat([], { timeStyle: 'medium' })
7+
8+
function logRequest({ req, listeningPort, port }) {
9+
const { hostname, pathname } = new URL(req.url, `https://${req.headers.host}`)
10+
const frontlinePort = listeningPort === 443 ? '' : `:${listeningPort}`
11+
const stamp = LOG_STAMP_FORMATTER.format(new Date())
12+
13+
console.debug(
14+
chalk`{dim [${stamp}] ${req.method} https://}${hostname}{dim ${pathname}${frontlinePort} => port} {bold ${port}}`
15+
)
16+
}
17+
518
// Exported API: sets up an HTTPS server on the listening port and registers SSL
619
// configs and proxying for all known mappings. This does not start listening,
720
// but the resulting object has a `start()` method to do just that. Errors and
821
// successful launch both are automatically reported on the console.
9-
function setupFrontline({ listeningPort, mappings }) {
22+
function setupFrontline({ listeningPort, mappings, verbose }) {
1023
const frontline = createServer(proxyRequest)
1124

1225
// Here we register SSL configs (cert + key) for every mapped domain, so we
@@ -37,7 +50,9 @@ function setupFrontline({ listeningPort, mappings }) {
3750
// actionable reporting.
3851
function handleListenError(err) {
3952
if (err.code === 'EADDRINUSE') {
40-
console.error(`It looks like listening port ${listeningPort} is in use already. Check your existing services?`)
53+
console.error(
54+
`It looks like listening port ${listeningPort} is in use already. Check your existing services?`
55+
)
4156
} else {
4257
console.error(err)
4358
}
@@ -52,14 +67,15 @@ function setupFrontline({ listeningPort, mappings }) {
5267
return
5368
}
5469

55-
// TODO: log queries if config option 'verbose' is set
70+
if (verbose) {
71+
logRequest({ req, listeningPort, port })
72+
}
5673
proxy.web(req, res, { target: `http://localhost:${port}` }, (err) => {
5774
if (err.code === 'ECONNREFUSED') {
5875
const msg = `Could not connect to proxied port ${port}`
59-
res.writeHead(503, msg)
60-
res.end(msg)
76+
res.writeHead(503, msg).end(msg)
6177
} else {
62-
res.writeHead(500, err.message)
78+
res.writeHead(500, err.message).end(err.message)
6379
}
6480
})
6581
}
@@ -70,7 +86,9 @@ function setupFrontline({ listeningPort, mappings }) {
7086
`${APP_NAME} frontline listening on port ${frontline.address().port}`
7187
)
7288
console.log('Active port mappings:')
73-
console.table(Array.from(mappings).map(([domain, { port }]) => ({ domain, port })))
89+
console.table(
90+
Array.from(mappings).map(([domain, { port }]) => ({ domain, port }))
91+
)
7492
console.log('Hit Ctrl+C to stop')
7593
}
7694

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"homepage": "https://github.com/deliciousinsights/you-get-https#readme",
3232
"dependencies": {
3333
"aes256": "^1.0.4",
34+
"chalk": "^4.1.0",
3435
"devcert": "^1.1.3",
3536
"http-proxy": "^1.18.1",
3637
"json5": "^2.1.3",

0 commit comments

Comments
 (0)