chore: netip.Prefix should not using pointer
This commit is contained in:
parent
a08c894f41
commit
429cd3a098
9 changed files with 40 additions and 40 deletions
|
@ -15,7 +15,7 @@ func LookupLocalAddrFromIfaceName(ifaceName string, network string, destination
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var addr *netip.Prefix
|
var addr netip.Prefix
|
||||||
switch network {
|
switch network {
|
||||||
case "udp4", "tcp4":
|
case "udp4", "tcp4":
|
||||||
addr, err = ifaceObj.PickIPv4Addr(destination)
|
addr, err = ifaceObj.PickIPv4Addr(destination)
|
||||||
|
|
|
@ -36,7 +36,7 @@ type Pool struct {
|
||||||
cycle bool
|
cycle bool
|
||||||
mux sync.Mutex
|
mux sync.Mutex
|
||||||
host *trie.DomainTrie[struct{}]
|
host *trie.DomainTrie[struct{}]
|
||||||
ipnet *netip.Prefix
|
ipnet netip.Prefix
|
||||||
store store
|
store store
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ func (p *Pool) Broadcast() netip.Addr {
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPNet return raw ipnet
|
// IPNet return raw ipnet
|
||||||
func (p *Pool) IPNet() *netip.Prefix {
|
func (p *Pool) IPNet() netip.Prefix {
|
||||||
return p.ipnet
|
return p.ipnet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,7 +153,7 @@ func (p *Pool) restoreState() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
IPNet *netip.Prefix
|
IPNet netip.Prefix
|
||||||
Host *trie.DomainTrie[struct{}]
|
Host *trie.DomainTrie[struct{}]
|
||||||
|
|
||||||
// Size sets the maximum number of entries in memory
|
// Size sets the maximum number of entries in memory
|
||||||
|
@ -171,7 +171,7 @@ func New(options Options) (*Pool, error) {
|
||||||
hostAddr = options.IPNet.Masked().Addr()
|
hostAddr = options.IPNet.Masked().Addr()
|
||||||
gateway = hostAddr.Next()
|
gateway = hostAddr.Next()
|
||||||
first = gateway.Next().Next().Next() // default start with 198.18.0.4
|
first = gateway.Next().Next().Next() // default start with 198.18.0.4
|
||||||
last = nnip.UnMasked(*options.IPNet)
|
last = nnip.UnMasked(options.IPNet)
|
||||||
)
|
)
|
||||||
|
|
||||||
if !options.IPNet.IsValid() || !first.IsValid() || !first.Less(last) {
|
if !options.IPNet.IsValid() || !first.IsValid() || !first.Less(last) {
|
||||||
|
|
|
@ -51,7 +51,7 @@ func createCachefileStore(options Options) (*Pool, string, error) {
|
||||||
func TestPool_Basic(t *testing.T) {
|
func TestPool_Basic(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.0/28")
|
ipnet := netip.MustParsePrefix("192.168.0.0/28")
|
||||||
pools, tempfile, err := createPools(Options{
|
pools, tempfile, err := createPools(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
@ -79,7 +79,7 @@ func TestPool_Basic(t *testing.T) {
|
||||||
func TestPool_BasicV6(t *testing.T) {
|
func TestPool_BasicV6(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("2001:4860:4860::8888/118")
|
ipnet := netip.MustParsePrefix("2001:4860:4860::8888/118")
|
||||||
pools, tempfile, err := createPools(Options{
|
pools, tempfile, err := createPools(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
@ -107,7 +107,7 @@ func TestPool_BasicV6(t *testing.T) {
|
||||||
func TestPool_Case_Insensitive(t *testing.T) {
|
func TestPool_Case_Insensitive(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.1/29")
|
ipnet := netip.MustParsePrefix("192.168.0.1/29")
|
||||||
pools, tempfile, err := createPools(Options{
|
pools, tempfile, err := createPools(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
@ -128,7 +128,7 @@ func TestPool_Case_Insensitive(t *testing.T) {
|
||||||
func TestPool_CycleUsed(t *testing.T) {
|
func TestPool_CycleUsed(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.16/28")
|
ipnet := netip.MustParsePrefix("192.168.0.16/28")
|
||||||
pools, tempfile, err := createPools(Options{
|
pools, tempfile, err := createPools(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
@ -152,7 +152,7 @@ func TestPool_Skip(t *testing.T) {
|
||||||
tree := trie.New[struct{}]()
|
tree := trie.New[struct{}]()
|
||||||
tree.Insert("example.com", struct{}{})
|
tree.Insert("example.com", struct{}{})
|
||||||
pools, tempfile, err := createPools(Options{
|
pools, tempfile, err := createPools(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
Host: tree,
|
Host: tree,
|
||||||
})
|
})
|
||||||
|
@ -168,7 +168,7 @@ func TestPool_Skip(t *testing.T) {
|
||||||
func TestPool_MaxCacheSize(t *testing.T) {
|
func TestPool_MaxCacheSize(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.1/24")
|
ipnet := netip.MustParsePrefix("192.168.0.1/24")
|
||||||
pool, _ := New(Options{
|
pool, _ := New(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 2,
|
Size: 2,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -183,7 +183,7 @@ func TestPool_MaxCacheSize(t *testing.T) {
|
||||||
func TestPool_DoubleMapping(t *testing.T) {
|
func TestPool_DoubleMapping(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.1/24")
|
ipnet := netip.MustParsePrefix("192.168.0.1/24")
|
||||||
pool, _ := New(Options{
|
pool, _ := New(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 2,
|
Size: 2,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -213,7 +213,7 @@ func TestPool_DoubleMapping(t *testing.T) {
|
||||||
func TestPool_Clone(t *testing.T) {
|
func TestPool_Clone(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.1/24")
|
ipnet := netip.MustParsePrefix("192.168.0.1/24")
|
||||||
pool, _ := New(Options{
|
pool, _ := New(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 2,
|
Size: 2,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -223,7 +223,7 @@ func TestPool_Clone(t *testing.T) {
|
||||||
assert.True(t, last == netip.AddrFrom4([4]byte{192, 168, 0, 5}))
|
assert.True(t, last == netip.AddrFrom4([4]byte{192, 168, 0, 5}))
|
||||||
|
|
||||||
newPool, _ := New(Options{
|
newPool, _ := New(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 2,
|
Size: 2,
|
||||||
})
|
})
|
||||||
newPool.CloneFrom(pool)
|
newPool.CloneFrom(pool)
|
||||||
|
@ -236,7 +236,7 @@ func TestPool_Clone(t *testing.T) {
|
||||||
func TestPool_Error(t *testing.T) {
|
func TestPool_Error(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.1/31")
|
ipnet := netip.MustParsePrefix("192.168.0.1/31")
|
||||||
_, err := New(Options{
|
_, err := New(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -246,7 +246,7 @@ func TestPool_Error(t *testing.T) {
|
||||||
func TestPool_FlushFileCache(t *testing.T) {
|
func TestPool_FlushFileCache(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.1/28")
|
ipnet := netip.MustParsePrefix("192.168.0.1/28")
|
||||||
pools, tempfile, err := createPools(Options{
|
pools, tempfile, err := createPools(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
@ -278,7 +278,7 @@ func TestPool_FlushFileCache(t *testing.T) {
|
||||||
func TestPool_FlushMemoryCache(t *testing.T) {
|
func TestPool_FlushMemoryCache(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.1/28")
|
ipnet := netip.MustParsePrefix("192.168.0.1/28")
|
||||||
pool, _ := New(Options{
|
pool, _ := New(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: ipnet,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
type Interface struct {
|
type Interface struct {
|
||||||
Index int
|
Index int
|
||||||
Name string
|
Name string
|
||||||
Addrs []*netip.Prefix
|
Addrs []netip.Prefix
|
||||||
HardwareAddr net.HardwareAddr
|
HardwareAddr net.HardwareAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ func ResolveInterface(name string) (*Interface, error) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ipNets := make([]*netip.Prefix, 0, len(addrs))
|
ipNets := make([]netip.Prefix, 0, len(addrs))
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
ipNet := addr.(*net.IPNet)
|
ipNet := addr.(*net.IPNet)
|
||||||
ip, _ := netip.AddrFromSlice(ipNet.IP)
|
ip, _ := netip.AddrFromSlice(ipNet.IP)
|
||||||
|
@ -59,7 +59,7 @@ func ResolveInterface(name string) (*Interface, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pf := netip.PrefixFrom(ip, ones)
|
pf := netip.PrefixFrom(ip, ones)
|
||||||
ipNets = append(ipNets, &pf)
|
ipNets = append(ipNets, pf)
|
||||||
}
|
}
|
||||||
|
|
||||||
r[iface.Name] = &Interface{
|
r[iface.Name] = &Interface{
|
||||||
|
@ -89,27 +89,27 @@ func FlushCache() {
|
||||||
interfaces.Reset()
|
interfaces.Reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iface *Interface) PickIPv4Addr(destination netip.Addr) (*netip.Prefix, error) {
|
func (iface *Interface) PickIPv4Addr(destination netip.Addr) (netip.Prefix, error) {
|
||||||
return iface.pickIPAddr(destination, func(addr *netip.Prefix) bool {
|
return iface.pickIPAddr(destination, func(addr netip.Prefix) bool {
|
||||||
return addr.Addr().Is4()
|
return addr.Addr().Is4()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iface *Interface) PickIPv6Addr(destination netip.Addr) (*netip.Prefix, error) {
|
func (iface *Interface) PickIPv6Addr(destination netip.Addr) (netip.Prefix, error) {
|
||||||
return iface.pickIPAddr(destination, func(addr *netip.Prefix) bool {
|
return iface.pickIPAddr(destination, func(addr netip.Prefix) bool {
|
||||||
return addr.Addr().Is6()
|
return addr.Addr().Is6()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (iface *Interface) pickIPAddr(destination netip.Addr, accept func(addr *netip.Prefix) bool) (*netip.Prefix, error) {
|
func (iface *Interface) pickIPAddr(destination netip.Addr, accept func(addr netip.Prefix) bool) (netip.Prefix, error) {
|
||||||
var fallback *netip.Prefix
|
var fallback netip.Prefix
|
||||||
|
|
||||||
for _, addr := range iface.Addrs {
|
for _, addr := range iface.Addrs {
|
||||||
if !accept(addr) {
|
if !accept(addr) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if fallback == nil && !addr.Addr().IsLinkLocalUnicast() {
|
if !fallback.IsValid() && !addr.Addr().IsLinkLocalUnicast() {
|
||||||
fallback = addr
|
fallback = addr
|
||||||
|
|
||||||
if !destination.IsValid() {
|
if !destination.IsValid() {
|
||||||
|
@ -122,8 +122,8 @@ func (iface *Interface) pickIPAddr(destination netip.Addr, accept func(addr *net
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if fallback == nil {
|
if !fallback.IsValid() {
|
||||||
return nil, ErrAddrNotFound
|
return netip.Prefix{}, ErrAddrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return fallback, nil
|
return fallback, nil
|
||||||
|
|
|
@ -122,7 +122,7 @@ type DNS struct {
|
||||||
type FallbackFilter struct {
|
type FallbackFilter struct {
|
||||||
GeoIP bool `yaml:"geoip"`
|
GeoIP bool `yaml:"geoip"`
|
||||||
GeoIPCode string `yaml:"geoip-code"`
|
GeoIPCode string `yaml:"geoip-code"`
|
||||||
IPCIDR []*netip.Prefix `yaml:"ipcidr"`
|
IPCIDR []netip.Prefix `yaml:"ipcidr"`
|
||||||
Domain []string `yaml:"domain"`
|
Domain []string `yaml:"domain"`
|
||||||
GeoSite []*router.DomainMatcher `yaml:"geosite"`
|
GeoSite []*router.DomainMatcher `yaml:"geosite"`
|
||||||
}
|
}
|
||||||
|
@ -1148,15 +1148,15 @@ func parseNameServerPolicy(nsPolicy map[string]any, ruleProviders map[string]pro
|
||||||
return policy, nil
|
return policy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFallbackIPCIDR(ips []string) ([]*netip.Prefix, error) {
|
func parseFallbackIPCIDR(ips []string) ([]netip.Prefix, error) {
|
||||||
var ipNets []*netip.Prefix
|
var ipNets []netip.Prefix
|
||||||
|
|
||||||
for idx, ip := range ips {
|
for idx, ip := range ips {
|
||||||
ipnet, err := netip.ParsePrefix(ip)
|
ipnet, err := netip.ParsePrefix(ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("DNS FallbackIP[%d] format error: %s", idx, err.Error())
|
return nil, fmt.Errorf("DNS FallbackIP[%d] format error: %s", idx, err.Error())
|
||||||
}
|
}
|
||||||
ipNets = append(ipNets, &ipnet)
|
ipNets = append(ipNets, ipnet)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ipNets, nil
|
return ipNets, nil
|
||||||
|
@ -1224,7 +1224,7 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
|
||||||
IPv6: cfg.IPv6,
|
IPv6: cfg.IPv6,
|
||||||
EnhancedMode: cfg.EnhancedMode,
|
EnhancedMode: cfg.EnhancedMode,
|
||||||
FallbackFilter: FallbackFilter{
|
FallbackFilter: FallbackFilter{
|
||||||
IPCIDR: []*netip.Prefix{},
|
IPCIDR: []netip.Prefix{},
|
||||||
GeoSite: []*router.DomainMatcher{},
|
GeoSite: []*router.DomainMatcher{},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -1298,7 +1298,7 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
|
||||||
}
|
}
|
||||||
|
|
||||||
pool, err := fakeip.New(fakeip.Options{
|
pool, err := fakeip.New(fakeip.Options{
|
||||||
IPNet: &fakeIPRange,
|
IPNet: fakeIPRange,
|
||||||
Size: 1000,
|
Size: 1000,
|
||||||
Host: host,
|
Host: host,
|
||||||
Persistence: rawCfg.Profile.StoreFakeIP,
|
Persistence: rawCfg.Profile.StoreFakeIP,
|
||||||
|
|
|
@ -26,7 +26,7 @@ type dhcpClient struct {
|
||||||
ifaceInvalidate time.Time
|
ifaceInvalidate time.Time
|
||||||
dnsInvalidate time.Time
|
dnsInvalidate time.Time
|
||||||
|
|
||||||
ifaceAddr *netip.Prefix
|
ifaceAddr netip.Prefix
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
clients []dnsClient
|
clients []dnsClient
|
||||||
err error
|
err error
|
||||||
|
|
|
@ -45,7 +45,7 @@ func (gf *geoipFilter) Match(ip netip.Addr) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ipnetFilter struct {
|
type ipnetFilter struct {
|
||||||
ipnet *netip.Prefix
|
ipnet netip.Prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
func (inf *ipnetFilter) Match(ip netip.Addr) bool {
|
func (inf *ipnetFilter) Match(ip netip.Addr) bool {
|
||||||
|
|
|
@ -398,7 +398,7 @@ type NameServer struct {
|
||||||
type FallbackFilter struct {
|
type FallbackFilter struct {
|
||||||
GeoIP bool
|
GeoIP bool
|
||||||
GeoIPCode string
|
GeoIPCode string
|
||||||
IPCIDR []*netip.Prefix
|
IPCIDR []netip.Prefix
|
||||||
Domain []string
|
Domain []string
|
||||||
GeoSite []*router.DomainMatcher
|
GeoSite []*router.DomainMatcher
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ func WithIPCIDRNoResolve(noResolve bool) IPCIDROption {
|
||||||
|
|
||||||
type IPCIDR struct {
|
type IPCIDR struct {
|
||||||
*Base
|
*Base
|
||||||
ipnet *netip.Prefix
|
ipnet netip.Prefix
|
||||||
adapter string
|
adapter string
|
||||||
isSourceIP bool
|
isSourceIP bool
|
||||||
noResolveIP bool
|
noResolveIP bool
|
||||||
|
@ -63,7 +63,7 @@ func NewIPCIDR(s string, adapter string, opts ...IPCIDROption) (*IPCIDR, error)
|
||||||
|
|
||||||
ipcidr := &IPCIDR{
|
ipcidr := &IPCIDR{
|
||||||
Base: &Base{},
|
Base: &Base{},
|
||||||
ipnet: &ipnet,
|
ipnet: ipnet,
|
||||||
adapter: adapter,
|
adapter: adapter,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue