From d2ae94f20b70a23f82fc74d529bbe4ef3fd43e1d Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sun, 24 Mar 2024 21:24:50 +0800 Subject: [PATCH] fix: iface panic https://github.com/MetaCubeX/mihomo/issues/1130 --- component/iface/iface.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/component/iface/iface.go b/component/iface/iface.go index dd932b46..2fd36861 100644 --- a/component/iface/iface.go +++ b/component/iface/iface.go @@ -40,16 +40,25 @@ func ResolveInterface(name string) (*Interface, error) { ipNets := make([]netip.Prefix, 0, len(addrs)) for _, addr := range addrs { - ipNet := addr.(*net.IPNet) - ip, _ := netip.AddrFromSlice(ipNet.IP) - - ones, bits := ipNet.Mask.Size() - if bits == 32 { + var pf netip.Prefix + switch addr.(type) { + case *net.IPNet: + ipNet := addr.(*net.IPNet) + ip, _ := netip.AddrFromSlice(ipNet.IP) + ones, bits := ipNet.Mask.Size() + if bits == 32 { + ip = ip.Unmap() + } + pf = netip.PrefixFrom(ip, ones) + case *net.IPAddr: + ipNet := addr.(*net.IPAddr) + ip, _ := netip.AddrFromSlice(ipNet.IP) ip = ip.Unmap() + pf = netip.PrefixFrom(ip, ip.BitLen()) + } + if pf.IsValid() { + ipNets = append(ipNets, pf) } - - pf := netip.PrefixFrom(ip, ones) - ipNets = append(ipNets, pf) } r[iface.Name] = &Interface{