Refactor: fakeip pool use netip.Prefix, supports ipv6 range
This commit is contained in:
@ -2,6 +2,7 @@ package dns
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/netip"
|
||||
|
||||
"github.com/Dreamacro/clash/common/cache"
|
||||
"github.com/Dreamacro/clash/component/fakeip"
|
||||
@ -11,7 +12,7 @@ import (
|
||||
type ResolverEnhancer struct {
|
||||
mode C.DNSMode
|
||||
fakePool *fakeip.Pool
|
||||
mapping *cache.LruCache[string, string]
|
||||
mapping *cache.LruCache[netip.Addr, string]
|
||||
}
|
||||
|
||||
func (h *ResolverEnhancer) FakeIPEnabled() bool {
|
||||
@ -28,7 +29,7 @@ func (h *ResolverEnhancer) IsExistFakeIP(ip net.IP) bool {
|
||||
}
|
||||
|
||||
if pool := h.fakePool; pool != nil {
|
||||
return pool.Exist(ip)
|
||||
return pool.Exist(ipToAddr(ip))
|
||||
}
|
||||
|
||||
return false
|
||||
@ -39,8 +40,10 @@ func (h *ResolverEnhancer) IsFakeIP(ip net.IP) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
addr := ipToAddr(ip)
|
||||
|
||||
if pool := h.fakePool; pool != nil {
|
||||
return pool.IPNet().Contains(ip) && !pool.Gateway().Equal(ip) && !pool.Broadcast().Equal(ip)
|
||||
return pool.IPNet().Contains(addr) && addr != pool.Gateway() && addr != pool.Broadcast()
|
||||
}
|
||||
|
||||
return false
|
||||
@ -52,21 +55,22 @@ func (h *ResolverEnhancer) IsFakeBroadcastIP(ip net.IP) bool {
|
||||
}
|
||||
|
||||
if pool := h.fakePool; pool != nil {
|
||||
return pool.Broadcast().Equal(ip)
|
||||
return pool.Broadcast() == ipToAddr(ip)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (h *ResolverEnhancer) FindHostByIP(ip net.IP) (string, bool) {
|
||||
addr := ipToAddr(ip)
|
||||
if pool := h.fakePool; pool != nil {
|
||||
if host, existed := pool.LookBack(ip); existed {
|
||||
if host, existed := pool.LookBack(addr); existed {
|
||||
return host, true
|
||||
}
|
||||
}
|
||||
|
||||
if mapping := h.mapping; mapping != nil {
|
||||
if host, existed := h.mapping.Get(ip.String()); existed {
|
||||
if host, existed := h.mapping.Get(addr); existed {
|
||||
return host, true
|
||||
}
|
||||
}
|
||||
@ -76,7 +80,7 @@ func (h *ResolverEnhancer) FindHostByIP(ip net.IP) (string, bool) {
|
||||
|
||||
func (h *ResolverEnhancer) InsertHostByIP(ip net.IP, host string) {
|
||||
if mapping := h.mapping; mapping != nil {
|
||||
h.mapping.Set(ip.String(), host)
|
||||
h.mapping.Set(ipToAddr(ip), host)
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,11 +103,11 @@ func (h *ResolverEnhancer) FlushFakeIP() error {
|
||||
|
||||
func NewEnhancer(cfg Config) *ResolverEnhancer {
|
||||
var fakePool *fakeip.Pool
|
||||
var mapping *cache.LruCache[string, string]
|
||||
var mapping *cache.LruCache[netip.Addr, string]
|
||||
|
||||
if cfg.EnhancedMode != C.DNSNormal {
|
||||
fakePool = cfg.Pool
|
||||
mapping = cache.NewLRUCache[string, string](cache.WithSize[string, string](4096), cache.WithStale[string, string](true))
|
||||
mapping = cache.NewLRUCache[netip.Addr, string](cache.WithSize[netip.Addr, string](4096), cache.WithStale[netip.Addr, string](true))
|
||||
}
|
||||
|
||||
return &ResolverEnhancer{
|
||||
|
@ -21,7 +21,7 @@ type (
|
||||
middleware func(next handler) handler
|
||||
)
|
||||
|
||||
func withHosts(hosts *trie.DomainTrie[netip.Addr], mapping *cache.LruCache[string, string]) middleware {
|
||||
func withHosts(hosts *trie.DomainTrie[netip.Addr], mapping *cache.LruCache[netip.Addr, string]) middleware {
|
||||
return func(next handler) handler {
|
||||
return func(ctx *context.DNSContext, r *D.Msg) (*D.Msg, error) {
|
||||
q := r.Question[0]
|
||||
@ -57,7 +57,7 @@ func withHosts(hosts *trie.DomainTrie[netip.Addr], mapping *cache.LruCache[strin
|
||||
}
|
||||
|
||||
if mapping != nil {
|
||||
mapping.SetWithExpire(ip.Unmap().String(), host, time.Now().Add(time.Second*10))
|
||||
mapping.SetWithExpire(ip, host, time.Now().Add(time.Second*10))
|
||||
}
|
||||
|
||||
ctx.SetType(context.DNSTypeHost)
|
||||
@ -70,7 +70,7 @@ func withHosts(hosts *trie.DomainTrie[netip.Addr], mapping *cache.LruCache[strin
|
||||
}
|
||||
}
|
||||
|
||||
func withMapping(mapping *cache.LruCache[string, string]) middleware {
|
||||
func withMapping(mapping *cache.LruCache[netip.Addr, string]) middleware {
|
||||
return func(next handler) handler {
|
||||
return func(ctx *context.DNSContext, r *D.Msg) (*D.Msg, error) {
|
||||
q := r.Question[0]
|
||||
@ -101,7 +101,7 @@ func withMapping(mapping *cache.LruCache[string, string]) middleware {
|
||||
continue
|
||||
}
|
||||
|
||||
mapping.SetWithExpire(ip.String(), host, time.Now().Add(time.Second*time.Duration(ttl)))
|
||||
mapping.SetWithExpire(ipToAddr(ip), host, time.Now().Add(time.Second*time.Duration(ttl)))
|
||||
}
|
||||
|
||||
return msg, nil
|
||||
@ -131,7 +131,7 @@ func withFakeIP(fakePool *fakeip.Pool) middleware {
|
||||
rr := &D.A{}
|
||||
rr.Hdr = D.RR_Header{Name: q.Name, Rrtype: D.TypeA, Class: D.ClassINET, Ttl: dnsDefaultTTL}
|
||||
ip := fakePool.Lookup(host)
|
||||
rr.A = ip
|
||||
rr.A = ip.AsSlice()
|
||||
msg := r.Copy()
|
||||
msg.Answer = []D.RR{rr}
|
||||
|
||||
|
17
dns/util.go
17
dns/util.go
@ -5,6 +5,7 @@ import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/Dreamacro/clash/common/cache"
|
||||
@ -114,6 +115,22 @@ func msgToIP(msg *D.Msg) []net.IP {
|
||||
return ips
|
||||
}
|
||||
|
||||
func ipToAddr(ip net.IP) netip.Addr {
|
||||
if ip == nil {
|
||||
return netip.Addr{}
|
||||
}
|
||||
|
||||
l := len(ip)
|
||||
|
||||
if l == 4 {
|
||||
return netip.AddrFrom4(*(*[4]byte)(ip))
|
||||
} else if l == 16 {
|
||||
return netip.AddrFrom16(*(*[16]byte)(ip))
|
||||
} else {
|
||||
return netip.Addr{}
|
||||
}
|
||||
}
|
||||
|
||||
type wrapPacketConn struct {
|
||||
net.PacketConn
|
||||
rAddr net.Addr
|
||||
|
Reference in New Issue
Block a user