Skip to content

Commit d832388

Browse files
committed
dnsx: flow id in summary
1 parent fd3dbcd commit d832388

12 files changed

Lines changed: 84 additions & 69 deletions

File tree

intra/backend/dnsx_listener.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import (
1616
type DNSSummary struct {
1717
// dnscrypt, dns53, doh, odoh, dot, preset, fixed, etc.
1818
Type string
19+
// Flow ID that spawned this DNS query, if Origin is "tunnel".
20+
// Otherwise, it is randomly generated uint64 as hex.
21+
FID string
1922
// DNS Transport ID
2023
ID string
2124
// owner uid that sent this request. May be empty.
@@ -113,8 +116,8 @@ func (s *DNSSummary) String() string {
113116
if s == nil {
114117
return "<nil>"
115118
}
116-
return fmt.Sprintf("id: %s (%s by %s), t: %s, q: %s (do? %t), a: %s (cache? %t / ad? %t), code: %d, ttl: %d, by: %s / via: %s / relay: %s, status: %d, blocklists: %s / upstreamBlocks? %t, msg: %s, loc: %s",
117-
s.ID, s.Type, s.Origin, core.FmtSecsFloat(s.Latency), s.QName, s.DO, s.RData, s.Cached, s.AD, s.RCode, s.RTtl, s.Server, s.PID, s.RPID, s.Status, s.Blocklists, s.UpstreamBlocks, s.Msg, s.Region)
119+
return fmt.Sprintf("id: %s (fid: %s / %s by %s), t: %s, q: %s (do? %t), a: %s (cache? %t / ad? %t), code: %d, ttl: %d, by: %s / via: %s / relay: %s, status: %d, blocklists: %s / upstreamBlocks? %t, msg: %s, loc: %s",
120+
s.ID, s.FID, s.Type, s.Origin, core.FmtSecsFloat(s.Latency), s.QName, s.DO, s.RData, s.Cached, s.AD, s.RCode, s.RTtl, s.Server, s.PID, s.RPID, s.Status, s.Blocklists, s.UpstreamBlocks, s.Msg, s.Region)
118121
}
119122

120123
// DNSListener receives Summaries.
@@ -127,7 +130,7 @@ type DNSListener interface {
127130
// The listener may return DNSOpts to specify if another upstream should override that answer.
128131
// Another round of OnQuery is NOT called in this case, and OnResponse is called once after processing
129132
// DNSOpts returned by OnUpstreamAnswer if it has a non-empty TIDCSV (overriding the original TIDCSV).
130-
OnUpstreamAnswer(smm *DNSSummary, forPref *DNSOpts, unmodifiedipcsv string) *DNSOpts
133+
OnUpstreamAnswer(who string, smm *DNSSummary, forPref *DNSOpts, unmodifiedipcsv string) *DNSOpts
131134
// OnResponse is called when a DNS response is received. May be called twice for the same query,
132135
// for instance, when different options are requested through OnUpstreamAnswer.
133136
OnResponse(*DNSSummary)

intra/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,12 @@ func (h *baseHandler) dnsOverride(conn net.Conn, uid string, smm *FlowSummary) b
451451
// addr with zone information removed; see: netip.ParseAddrPort which h.resolver relies on
452452
// addr2 := &net.TCPAddr{IP: addr.IP, Port: addr.Port}
453453
// conn closed by the resolver
454+
fid := smm.ID // flow ID that spawned this DNS query
454455
core.Gx(h.proto+".dns", func() {
455456
// SocketSummary is not meant to be used by the listener; x.DNSSummary is
456457
// but call into PostFlow & OnSocketClosed anyway, to avoid ambiguities
457458
// on which sockets / sessions are still active.
458-
rx, tx, errs := h.resolver.Serve(h.proto, conn, uid)
459+
rx, tx, errs := h.resolver.Serve(h.proto, conn, uid, fid)
459460
smm.Rx = rx
460461
smm.Tx = tx
461462
// smm.Rtt

intra/core/typ.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ func loc(x any) uint64 {
3535
return 0
3636
}
3737

38+
// Rand64 returns a random 64-bit hex string (16 chars long).
39+
func Rand64() string {
40+
return strconv.FormatUint(rand.Uint64(), 16)
41+
}
42+
3843
// go.dev/play/p/jjI4XJZud4i
3944
func Loc[T comparable](x T) uint64 {
4045
// maphash will be compatible with moving gc
@@ -44,7 +49,7 @@ func Loc[T comparable](x T) uint64 {
4449
}
4550

4651
func LocStr[T comparable](x T) string {
47-
return strconv.FormatUint(Loc(x), 10)
52+
return strconv.FormatUint(Loc(x), 16)
4853
}
4954

5055
func HashStr(s string) uint64 {

intra/dns53/dot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ func (t *dot) Query(network string, q *dns.Msg, smm *x.DNSSummary) (ans *dns.Msg
354354
t.est.Add(smm.Latency)
355355

356356
if settings.Debug {
357-
log.V("dot: %s ech? %t; len(res): fro %s:%d a:%d/sz:%d/pad:%d, data: %s / status: %d, via: %s, err? %v",
358-
t.id, ech, smm.QName, smm.QType, xdns.Len(ans), xdns.Size(ans), xdns.EDNS0PadLen(ans), smm.RData, smm.Status, smm.PID, err)
357+
log.V("dot: %s (fid: %s) ech? %t; len(res): fro %s:%d a:%d/sz:%d/pad:%d, data: %s / status: %d, via: %s, err? %v",
358+
t.id, smm.FID, ech, smm.QName, smm.QType, xdns.Len(ans), xdns.Size(ans), xdns.EDNS0PadLen(ans), smm.RData, smm.Status, smm.PID, err)
359359
}
360360

361361
return

intra/dns53/upstream.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ func (t *transport) Query(network string, q *dns.Msg, smm *x.DNSSummary) (ans *d
320320
t.est.Add(smm.Latency)
321321

322322
if log.Debug {
323-
log.V("dns53: (%s) len(res): %d, data: %s, via: %s, err? %v",
324-
t.id, xdns.Len(ans), smm.RData, smm.PID, err)
323+
log.V("dns53: (%s) fid: %s; len(res): %d, data: %s, via: %s, err? %v",
324+
t.id, smm.FID, xdns.Len(ans), smm.RData, smm.PID, err)
325325
}
326326

327327
return ans, err

intra/dnscrypt/multiserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ func resolve(network string, data *dns.Msg, si *server, smm *x.DNSSummary) (ans
364364
}
365365

366366
if settings.Debug {
367-
log.V("dnscrypt: len(res): %d, data: %s, via: %s, err? %v",
368-
xdns.Len(ans), smm.RData, smm.PID, err)
367+
log.V("dnscrypt: (fid: %s) len(res): %d, data: %s, via: %s, err? %v",
368+
smm.FID, xdns.Len(ans), smm.RData, smm.PID, err)
369369
}
370370

371371
return // ans, err

intra/dnsx/alg.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,16 +1219,16 @@ func (t *dnsgateway) q(t1, t2 Transport, preset []netip.Addr, network, uid strin
12191219

12201220
if err != nil || ansin == nil {
12211221
if ansin == nil {
1222-
log.I("alg: abort no ans on %s+%s[%s]; self? %t synth? %t; qerr %v",
1223-
idstr(t1), idstr(t2), uid, uidself, synthAns, err)
1222+
log.I("alg: abort no ans on %s+%s[%s] (fid: %s); self? %t synth? %t; qerr %v",
1223+
idstr(t1), idstr(t2), uid, smm.FID, uidself, synthAns, err)
12241224
return nil, nil, core.JoinErr(err, errNoAnswer)
12251225
}
12261226
if !xdns.HasRcodeSuccess(ansin) {
12271227
return ansin, nil, err
12281228
}
12291229
if settings.Debug {
1230-
log.D("alg: for %s:%s err but ans ok: %d; do? %t, self? %t synth? %t; qerr %v",
1231-
qname(q), qtype(q), xdns.Len(ansin), hasdnssec, uidself, synthAns, err)
1230+
log.D("alg: for %s:%s (fid: %s) err but ans ok: %d; do? %t, self? %t synth? %t; qerr %v",
1231+
qname(ansin), qtype(ansin), smm.FID, xdns.Len(ansin), hasdnssec, uidself, synthAns, err)
12321232
}
12331233
}
12341234

intra/dnsx/cacher.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ func fillSummary(s *x.DNSSummary, out *x.DNSSummary) {
585585
out.PID = s.PID
586586
out.RPID = s.RPID
587587
}
588+
if len(out.FID) <= 0 {
589+
out.FID = s.FID
590+
}
588591
if out.Latency <= 0 {
589592
out.Latency = s.Latency
590593
}

intra/dnsx/plus.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ func (t *plus) forward(network string, q *dns.Msg, outSmm *x.DNSSummary, all ...
262262

263263
finalsmm = cursmm
264264

265-
loged(err != nil || failed)("plus: queried %s for %s:%d; data: %s [noans? %t], code: %d, err? %v",
266-
idstr(tr), qname, qtyp, finalsmm.RData, noans, finalsmm.RCode, err)
265+
loged(err != nil || failed)("plus: queried %s for %s:%d (fid: %s); data: %s [noans? %t], code: %d, err? %v",
266+
idstr(tr), qname, qtyp, outSmm.FID, finalsmm.RData, noans, finalsmm.RCode, err)
267267

268268
if err != nil || ans == nil {
269269
errs = core.JoinErr(errs, core.OneErr(err, errNoAnswer))

0 commit comments

Comments
 (0)