mirror of https://github.com/v2ray/v2ray-core
rename IPNet to IPNetTable
parent
fee3042239
commit
815019f6da
|
@ -180,11 +180,11 @@ func (v *CIDRMatcher) Apply(ctx context.Context) bool {
|
|||
}
|
||||
|
||||
type IPv4Matcher struct {
|
||||
ipv4net *v2net.IPNet
|
||||
ipv4net *v2net.IPNetTable
|
||||
onSource bool
|
||||
}
|
||||
|
||||
func NewIPv4Matcher(ipnet *v2net.IPNet, onSource bool) *IPv4Matcher {
|
||||
func NewIPv4Matcher(ipnet *v2net.IPNetTable, onSource bool) *IPv4Matcher {
|
||||
return &IPv4Matcher{
|
||||
ipv4net: ipnet,
|
||||
onSource: onSource,
|
||||
|
|
|
@ -17,7 +17,7 @@ func (r *Rule) Apply(ctx context.Context) bool {
|
|||
}
|
||||
|
||||
func cidrToCondition(cidr []*CIDR, source bool) (Condition, error) {
|
||||
ipv4Net := v2net.NewIPNet()
|
||||
ipv4Net := v2net.NewIPNetTable()
|
||||
ipv6Cond := NewAnyCondition()
|
||||
hasIpv6 := false
|
||||
|
||||
|
|
|
@ -5,17 +5,17 @@ import (
|
|||
"net"
|
||||
)
|
||||
|
||||
type IPNet struct {
|
||||
type IPNetTable struct {
|
||||
cache map[uint32]byte
|
||||
}
|
||||
|
||||
func NewIPNet() *IPNet {
|
||||
return &IPNet{
|
||||
func NewIPNetTable() *IPNetTable {
|
||||
return &IPNetTable{
|
||||
cache: make(map[uint32]byte, 1024),
|
||||
}
|
||||
}
|
||||
|
||||
func ipToUint32(ip net.IP) uint32 {
|
||||
func ipToUint32(ip IP) uint32 {
|
||||
value := uint32(0)
|
||||
for _, b := range []byte(ip) {
|
||||
value <<= 8
|
||||
|
@ -32,7 +32,7 @@ func ipMaskToByte(mask net.IPMask) byte {
|
|||
return value
|
||||
}
|
||||
|
||||
func (n *IPNet) Add(ipNet *net.IPNet) {
|
||||
func (n *IPNetTable) Add(ipNet *net.IPNet) {
|
||||
ipv4 := ipNet.IP.To4()
|
||||
if ipv4 == nil {
|
||||
// For now, we don't support IPv6
|
||||
|
@ -42,7 +42,7 @@ func (n *IPNet) Add(ipNet *net.IPNet) {
|
|||
n.AddIP(ipv4, mask)
|
||||
}
|
||||
|
||||
func (n *IPNet) AddIP(ip []byte, mask byte) {
|
||||
func (n *IPNetTable) AddIP(ip []byte, mask byte) {
|
||||
k := ipToUint32(ip)
|
||||
existing, found := n.cache[k]
|
||||
if !found || existing > mask {
|
||||
|
@ -50,7 +50,7 @@ func (n *IPNet) AddIP(ip []byte, mask byte) {
|
|||
}
|
||||
}
|
||||
|
||||
func (n *IPNet) Contains(ip net.IP) bool {
|
||||
func (n *IPNetTable) Contains(ip net.IP) bool {
|
||||
ipv4 := ip.To4()
|
||||
if ipv4 == nil {
|
||||
return false
|
||||
|
@ -77,6 +77,6 @@ func (n *IPNet) Contains(ip net.IP) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (n *IPNet) IsEmpty() bool {
|
||||
func (n *IPNetTable) IsEmpty() bool {
|
||||
return len(n.cache) == 0
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ func parseCIDR(str string) *net.IPNet {
|
|||
func TestIPNet(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
ipNet := NewIPNet()
|
||||
ipNet := NewIPNetTable()
|
||||
ipNet.Add(parseCIDR(("0.0.0.0/8")))
|
||||
ipNet.Add(parseCIDR(("10.0.0.0/8")))
|
||||
ipNet.Add(parseCIDR(("100.64.0.0/10")))
|
||||
|
@ -32,12 +32,12 @@ func TestIPNet(t *testing.T) {
|
|||
ipNet.Add(parseCIDR(("198.51.100.0/24")))
|
||||
ipNet.Add(parseCIDR(("203.0.113.0/24")))
|
||||
ipNet.Add(parseCIDR(("8.8.8.8/32")))
|
||||
assert.Bool(ipNet.Contains(net.ParseIP("192.168.1.1"))).IsTrue()
|
||||
assert.Bool(ipNet.Contains(net.ParseIP("192.0.0.0"))).IsTrue()
|
||||
assert.Bool(ipNet.Contains(net.ParseIP("192.0.1.0"))).IsFalse()
|
||||
assert.Bool(ipNet.Contains(net.ParseIP("0.1.0.0"))).IsTrue()
|
||||
assert.Bool(ipNet.Contains(net.ParseIP("1.0.0.1"))).IsFalse()
|
||||
assert.Bool(ipNet.Contains(net.ParseIP("8.8.8.7"))).IsFalse()
|
||||
assert.Bool(ipNet.Contains(net.ParseIP("8.8.8.8"))).IsTrue()
|
||||
assert.Bool(ipNet.Contains(net.ParseIP("2001:cdba::3257:9652"))).IsFalse()
|
||||
assert.Bool(ipNet.Contains(ParseIP("192.168.1.1"))).IsTrue()
|
||||
assert.Bool(ipNet.Contains(ParseIP("192.0.0.0"))).IsTrue()
|
||||
assert.Bool(ipNet.Contains(ParseIP("192.0.1.0"))).IsFalse()
|
||||
assert.Bool(ipNet.Contains(ParseIP("0.1.0.0"))).IsTrue()
|
||||
assert.Bool(ipNet.Contains(ParseIP("1.0.0.1"))).IsFalse()
|
||||
assert.Bool(ipNet.Contains(ParseIP("8.8.8.7"))).IsFalse()
|
||||
assert.Bool(ipNet.Contains(ParseIP("8.8.8.8"))).IsTrue()
|
||||
assert.Bool(ipNet.Contains(ParseIP("2001:cdba::3257:9652"))).IsFalse()
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ var ListenTCP = net.ListenTCP
|
|||
var ListenUDP = net.ListenUDP
|
||||
|
||||
var LookupIP = net.LookupIP
|
||||
var ParseIP = net.ParseIP
|
||||
|
||||
var SplitHostPort = net.SplitHostPort
|
||||
|
||||
|
@ -25,6 +26,11 @@ type UDPConn = net.UDPConn
|
|||
type UnixConn = net.UnixConn
|
||||
|
||||
type IP = net.IP
|
||||
type IPMask = net.IPMask
|
||||
type IPNet = net.IPNet
|
||||
|
||||
const IPv4len = net.IPv4len
|
||||
const IPv6len = net.IPv6len
|
||||
|
||||
type Error = net.Error
|
||||
type AddrError = net.AddrError
|
||||
|
|
Loading…
Reference in New Issue