Refactor: metadata use netip.Addr

This commit is contained in:
yaling888
2022-04-20 01:52:51 +08:00
committed by adlyq
parent 6c4791480e
commit 7ca1a03d73
45 changed files with 374 additions and 346 deletions

View File

@ -2,7 +2,7 @@ package common
import (
"errors"
"net"
"net/netip"
"strings"
C "github.com/Dreamacro/clash/constant"
@ -54,17 +54,17 @@ func FindNetwork(params []string) C.NetWork {
return C.ALLNet
}
func FindSourceIPs(params []string) []*net.IPNet {
var ips []*net.IPNet
func FindSourceIPs(params []string) []*netip.Prefix {
var ips []*netip.Prefix
for _, p := range params {
if p == noResolve || len(p) < 7 {
continue
}
_, ipnet, err := net.ParseCIDR(p)
ipnet, err := netip.ParsePrefix(p)
if err != nil {
continue
}
ips = append(ips, ipnet)
ips = append(ips, &ipnet)
}
if len(ips) > 0 {

View File

@ -26,7 +26,7 @@ func (g *GEOIP) RuleType() C.RuleType {
func (g *GEOIP) Match(metadata *C.Metadata) bool {
ip := metadata.DstIP
if ip == nil {
if !ip.IsValid() {
return false
}
@ -39,10 +39,10 @@ func (g *GEOIP) Match(metadata *C.Metadata) bool {
resolver.IsFakeBroadcastIP(ip)
}
if !C.GeodataMode {
record, _ := mmdb.Instance().Country(ip)
record, _ := mmdb.Instance().Country(ip.AsSlice())
return strings.EqualFold(record.Country.IsoCode, g.country)
}
return g.geoIPMatcher.Match(ip)
return g.geoIPMatcher.Match(ip.AsSlice())
}
func (g *GEOIP) Adapter() string {

View File

@ -1,7 +1,7 @@
package common
import (
"net"
"net/netip"
C "github.com/Dreamacro/clash/constant"
)
@ -22,7 +22,7 @@ func WithIPCIDRNoResolve(noResolve bool) IPCIDROption {
type IPCIDR struct {
*Base
ipnet *net.IPNet
ipnet *netip.Prefix
adapter string
isSourceIP bool
noResolveIP bool
@ -40,7 +40,7 @@ func (i *IPCIDR) Match(metadata *C.Metadata) bool {
if i.isSourceIP {
ip = metadata.SrcIP
}
return ip != nil && i.ipnet.Contains(ip)
return ip.IsValid() && i.ipnet.Contains(ip)
}
func (i *IPCIDR) Adapter() string {
@ -56,14 +56,14 @@ func (i *IPCIDR) ShouldResolveIP() bool {
}
func NewIPCIDR(s string, adapter string, opts ...IPCIDROption) (*IPCIDR, error) {
_, ipnet, err := net.ParseCIDR(s)
ipnet, err := netip.ParsePrefix(s)
if err != nil {
return nil, errPayload
}
ipcidr := &IPCIDR{
Base: &Base{},
ipnet: ipnet,
ipnet: &ipnet,
adapter: adapter,
}

View File

@ -13,7 +13,7 @@ type ipcidrStrategy struct {
}
func (i *ipcidrStrategy) Match(metadata *C.Metadata) bool {
return i.trie != nil && i.trie.IsContain(metadata.DstIP)
return i.trie != nil && i.trie.IsContain(metadata.DstIP.AsSlice())
}
func (i *ipcidrStrategy) Count() int {