1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -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.

(cherry picked from commit c58a372d93)
This commit is contained in:
Loyalsoldier 2021-09-11 10:07:54 +08:00 committed by Shelikhoo
parent 389a0585f3
commit 41ae53e60d
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316

View File

@ -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)
}
}