1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00

Fix: IP length not deterministic (#1267)

The length of IPv4 address in Go standard library could be 16
if it is in IPv6-mapped-IPv4 format.
This commit is contained in:
Loyalsoldier 2021-09-11 10:07:54 +08:00 committed by GitHub
parent 8a22e942c4
commit c58a372d93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,8 +4,6 @@
package router
import (
"strconv"
"inet.af/netaddr"
"github.com/v2fly/v2ray-core/v4/common/net"
@ -21,16 +19,15 @@ type GeoIPMatcher struct {
func (m *GeoIPMatcher) Init(cidrs []*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)
}
}