cmd/cli: simplify queryFromSelf

By using netmon.LocalAddresses instead of looping through interfaces
list manually.
This commit is contained in:
Cuong Manh Le authored and Cuong Manh Le committed 2024-10-08 22:08:48 +07:00
1 parent cb14992ddc
commit 5ac9d17bdf
1 file changed
+7 -17
+7 -17
View File
@@ -9,6 +9,7 @@ import (
"net" "net"
"net/netip" "net/netip"
"runtime" "runtime"
"slices"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@@ -16,7 +17,6 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"tailscale.com/net/netaddr"
"tailscale.com/net/netmon" "tailscale.com/net/netmon"
"tailscale.com/net/tsaddr" "tailscale.com/net/tsaddr"
@@ -968,25 +968,15 @@ func (p *prog) queryFromSelf(ip string) bool {
return val.(bool) return val.(bool)
} }
netIP := netip.MustParseAddr(ip) netIP := netip.MustParseAddr(ip)
ifaces, err := netmon.GetInterfaceList() regularIPs, loopbackIPs, err := netmon.LocalAddresses()
if err != nil { if err != nil {
mainLog.Load().Warn().Err(err).Msg("could not get interfaces list") mainLog.Load().Warn().Err(err).Msg("could not get local addresses")
return false return false
} }
for _, iface := range ifaces { for _, localIP := range slices.Concat(regularIPs, loopbackIPs) {
addrs, err := iface.Addrs() if localIP.Compare(netIP) == 0 {
if err != nil { p.queryFromSelfMap.Store(ip, true)
mainLog.Load().Warn().Err(err).Msgf("could not get interfaces addresses: %s", iface.Name) return true
continue
}
for _, a := range addrs {
switch v := a.(type) {
case *net.IPNet:
if pfx, ok := netaddr.FromStdIPNet(v); ok && pfx.Addr().Compare(netIP) == 0 {
p.queryFromSelfMap.Store(ip, true)
return true
}
}
} }
} }
p.queryFromSelfMap.Store(ip, false) p.queryFromSelfMap.Store(ip, false)