cmd/cli: add QUERY/REPLY prefix to proxying log

So the log in INFO log is aligned, making it easier for human to
monitoring the log, either via console or running "tail" command.
This commit is contained in:
Cuong Manh Le
2023-12-18 16:56:07 +07:00
committed by Cuong Manh Le
parent 3023f33dff
commit 8d2cb6091e
+7 -13
View File
@@ -8,7 +8,6 @@ import (
"fmt" "fmt"
"net" "net"
"net/netip" "net/netip"
"os"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@@ -47,12 +46,6 @@ var privateUpstreamConfig = &ctrld.UpstreamConfig{
Timeout: 2000, Timeout: 2000,
} }
var hostName string
func init() {
hostName, _ = os.Hostname()
}
// proxyRequest contains data for proxying a DNS query to upstream. // proxyRequest contains data for proxying a DNS query to upstream.
type proxyRequest struct { type proxyRequest struct {
msg *dns.Msg msg *dns.Msg
@@ -68,6 +61,7 @@ type upstreamForResult struct {
matchedNetwork string matchedNetwork string
matchedRule string matchedRule string
matched bool matched bool
srcAddr string
} }
func (p *prog) serveDNS(listenerNum string) error { func (p *prog) serveDNS(listenerNum string) error {
@@ -104,9 +98,9 @@ func (p *prog) serveDNS(listenerNum string) error {
ci.ClientIDPref = p.cfg.Service.ClientIDPref ci.ClientIDPref = p.cfg.Service.ClientIDPref
stripClientSubnet(m) stripClientSubnet(m)
remoteAddr := spoofRemoteAddr(w.RemoteAddr(), ci) remoteAddr := spoofRemoteAddr(w.RemoteAddr(), ci)
fmtSrcToDest := fmtRemoteToLocal(listenerNum, ci.Hostname, remoteAddr.String(), w.LocalAddr().String()) fmtSrcToDest := fmtRemoteToLocal(listenerNum, ci.Hostname, remoteAddr.String())
t := time.Now() t := time.Now()
ctrld.Log(ctx, mainLog.Load().Info(), "%s received query: %s %s", fmtSrcToDest, dns.TypeToString[q.Qtype], domain) ctrld.Log(ctx, mainLog.Load().Info(), "QUERY: %s: %s %s", fmtSrcToDest, dns.TypeToString[q.Qtype], domain)
res := p.upstreamFor(ctx, listenerNum, listenerConfig, remoteAddr, ci.Mac, domain) res := p.upstreamFor(ctx, listenerNum, listenerConfig, remoteAddr, ci.Mac, domain)
var answer *dns.Msg var answer *dns.Msg
if !res.matched && listenerConfig.Restricted { if !res.matched && listenerConfig.Restricted {
@@ -207,7 +201,7 @@ func (p *prog) upstreamFor(ctx context.Context, defaultUpstreamNum string, lc *c
matchedNetwork := "no network" matchedNetwork := "no network"
matchedRule := "no rule" matchedRule := "no rule"
matched := false matched := false
res = &upstreamForResult{} res = &upstreamForResult{srcAddr: addr.String()}
defer func() { defer func() {
res.upstreams = upstreams res.upstreams = upstreams
@@ -510,7 +504,7 @@ func (p *prog) proxy(ctx context.Context, req *proxyRequest) *dns.Msg {
p.cache.Add(dnscache.NewKey(req.msg, upstreams[n]), dnscache.NewValue(answer, expired)) p.cache.Add(dnscache.NewKey(req.msg, upstreams[n]), dnscache.NewValue(answer, expired))
ctrld.Log(ctx, mainLog.Load().Debug(), "add cached response") ctrld.Log(ctx, mainLog.Load().Debug(), "add cached response")
} }
ctrld.Log(ctx, mainLog.Load().Info(), "%s -> %s replied: %s", upstreams[n], hostName, dns.RcodeToString[answer.Rcode]) ctrld.Log(ctx, mainLog.Load().Info(), "REPLY: %s -> %s (%s): %s", upstreams[n], req.ufr.srcAddr, req.ci.Hostname, dns.RcodeToString[answer.Rcode])
return answer return answer
} }
ctrld.Log(ctx, mainLog.Load().Error(), "all %v endpoints failed", upstreams) ctrld.Log(ctx, mainLog.Load().Error(), "all %v endpoints failed", upstreams)
@@ -572,8 +566,8 @@ func wildcardMatches(wildcard, domain string) bool {
return false return false
} }
func fmtRemoteToLocal(listenerNum, hostname, remote, local string) string { func fmtRemoteToLocal(listenerNum, hostname, remote string) string {
return fmt.Sprintf("%s (%s) -> listener.%s: %s:", remote, hostname, listenerNum, local) return fmt.Sprintf("%s (%s) -> listener.%s", remote, hostname, listenerNum)
} }
func requestID() string { func requestID() string {