diff --git a/app/router/condition_geoip.go b/app/router/condition_geoip.go index 43feaca5164..536dbf2e5ad 100644 --- a/app/router/condition_geoip.go +++ b/app/router/condition_geoip.go @@ -1,12 +1,7 @@ -//go:build !confonly -// +build !confonly - package router import ( "github.com/v2fly/v2ray-core/v4/app/router/routercommon" - "strconv" - "inet.af/netaddr" "github.com/v2fly/v2ray-core/v4/common/net" @@ -22,16 +17,15 @@ type GeoIPMatcher struct { func (m *GeoIPMatcher) Init(cidrs []*routercommon.CIDR) error { var builder4, builder6 netaddr.IPSetBuilder for _, cidr := range cidrs { - ip := net.IP(cidr.GetIp()) - ipStr := ip.String() + "/" + strconv.Itoa(int(cidr.GetPrefix())) - ipPrefix, err := netaddr.ParseIPPrefix(ipStr) - if err != nil { - return err + netaddrIP, ok := netaddr.FromStdIP(net.IP(cidr.GetIp())) + if !ok { + return newError("invalid IP address ", cidr) } - switch len(ip) { - case net.IPv4len: + ipPrefix := netaddr.IPPrefixFrom(netaddrIP, uint8(cidr.GetPrefix())) + switch { + case netaddrIP.Is4(): builder4.AddPrefix(ipPrefix) - case net.IPv6len: + case netaddrIP.Is6(): builder6.AddPrefix(ipPrefix) } }