Skip to content

Commit

Permalink
Fix: IP length not deterministic
Browse files Browse the repository at this point in the history
The length of IPv4 address in Go standard library could be 16
if it is in IPv6-mapped-IPv4 format.
  • Loading branch information
Loyalsoldier committed Sep 11, 2021
1 parent 8a22e94 commit 97338cb
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions app/router/condition_geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package router

import (
"strconv"

"inet.af/netaddr"

"github.com/v2fly/v2ray-core/v4/common/net"
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 97338cb

Please sign in to comment.