From 3056a98bbdcec3308a3b6f74cdcd9f0d9b11922a Mon Sep 17 00:00:00 2001 From: Maisem Ali Date: Tue, 19 Sep 2023 14:50:13 -0700 Subject: [PATCH] net/tstun: add better logging of natV4Config It might as well have been spewing out gibberish. This adds a nicer output format for us to be able to read and identify whats going on. Sample output ``` natV4Config{nativeAddr: 100.83.114.95, listenAddrs: [10.32.80.33], dstMasqAddrs: [10.32.80.33: 407 peers]} ``` Fixes tailscale/corp#14650 Signed-off-by: Maisem Ali --- net/tstun/wrap.go | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/net/tstun/wrap.go b/net/tstun/wrap.go index a5daa5070..ab0a4a3db 100644 --- a/net/tstun/wrap.go +++ b/net/tstun/wrap.go @@ -545,6 +545,44 @@ type natV4Config struct { dstAddrToPeerKeyMapper *table.RoutingTable } +func (c *natV4Config) String() string { + if c == nil { + return "" + } + var b strings.Builder + b.WriteString("natV4Config{") + fmt.Fprintf(&b, "nativeAddr: %v, ", c.nativeAddr) + fmt.Fprint(&b, "listenAddrs: [") + + i := 0 + c.listenAddrs.Range(func(k netip.Addr, _ struct{}) bool { + if i > 0 { + b.WriteString(", ") + } + b.WriteString(k.String()) + i++ + return true + }) + count := map[netip.Addr]int{} + c.dstMasqAddrs.Range(func(_ key.NodePublic, v netip.Addr) bool { + count[v]++ + return true + }) + + i = 0 + b.WriteString("], dstMasqAddrs: [") + for k, v := range count { + if i > 0 { + b.WriteString(", ") + } + fmt.Fprintf(&b, "%v: %v peers", k, v) + i++ + } + b.WriteString("]}") + + return b.String() +} + // mapDstIP returns the destination IP to use for a packet to dst. // If dst is not one of the listen addresses, it is returned as-is, // otherwise the native address is returned. @@ -635,7 +673,7 @@ func (t *Wrapper) SetWGConfig(wcfg *wgcfg.Config) { cfg := natV4ConfigFromWGConfig(wcfg) old := t.natV4Config.Swap(cfg) if !reflect.DeepEqual(old, cfg) { - t.logf("nat config: %+v", cfg) + t.logf("nat config: %v", cfg) } }