From a51cc8075d9ba279306ead148cb5fdbbdee3ff28 Mon Sep 17 00:00:00 2001 From: Junxiao Shi Date: Tue, 10 Dec 2024 09:48:23 +0000 Subject: [PATCH] fix gateway detection --- hostinfo.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hostinfo.go b/hostinfo.go index 3dc620d..74364d9 100644 --- a/hostinfo.go +++ b/hostinfo.go @@ -34,13 +34,19 @@ func gatherHostInfo() (hi HostInfo, e error) { logEntry.Error("netlink.LinkByIndex error", zap.Error(e)) return hi, nil } + routes, e := nl.RouteList(link, unix.AF_INET6) if e != nil { logEntry.Error("netlink.RouteList error", zap.Error(e)) return hi, nil } + for _, route := range routes { - if route.Dst == nil { + maskLen := 0 + if route.Dst != nil { + maskLen, _ = route.Dst.Mask.Size() + } + if maskLen == 0 { hi.GatewayIP, _ = netip.AddrFromSlice(route.Gw) } }