fix: iface panic

https://github.com/MetaCubeX/mihomo/issues/1130
This commit is contained in:
wwqgtxx 2024-03-24 21:24:50 +08:00
parent d56a439a74
commit d2ae94f20b

View File

@ -40,17 +40,26 @@ func ResolveInterface(name string) (*Interface, error) {
ipNets := make([]netip.Prefix, 0, len(addrs))
for _, addr := range addrs {
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)
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)
}
}
r[iface.Name] = &Interface{
Index: iface.Index,