github.com/igoogolx/clash@v1.19.8/dns/enhancer.go (about) 1 package dns 2 3 import ( 4 "net" 5 6 "github.com/igoogolx/clash/common/cache" 7 "github.com/igoogolx/clash/component/fakeip" 8 C "github.com/igoogolx/clash/constant" 9 ) 10 11 type ResolverEnhancer struct { 12 mode C.DNSMode 13 fakePool *fakeip.Pool 14 mapping *cache.LruCache 15 } 16 17 func (h *ResolverEnhancer) FakeIPEnabled() bool { 18 return h.mode == C.DNSFakeIP 19 } 20 21 func (h *ResolverEnhancer) MappingEnabled() bool { 22 return h.mode == C.DNSFakeIP || h.mode == C.DNSMapping 23 } 24 25 func (h *ResolverEnhancer) IsExistFakeIP(ip net.IP) bool { 26 if !h.FakeIPEnabled() { 27 return false 28 } 29 30 if pool := h.fakePool; pool != nil { 31 return pool.Exist(ip) 32 } 33 34 return false 35 } 36 37 func (h *ResolverEnhancer) IsFakeIP(ip net.IP) bool { 38 if !h.FakeIPEnabled() { 39 return false 40 } 41 42 if pool := h.fakePool; pool != nil { 43 return pool.IPNet().Contains(ip) && !pool.Gateway().Equal(ip) 44 } 45 46 return false 47 } 48 49 func (h *ResolverEnhancer) FindHostByIP(ip net.IP) (string, bool) { 50 if pool := h.fakePool; pool != nil { 51 if host, existed := pool.LookBack(ip); existed { 52 return host, true 53 } 54 } 55 56 if mapping := h.mapping; mapping != nil { 57 if host, existed := h.mapping.Get(ip.String()); existed { 58 return host.(string), true 59 } 60 } 61 62 return "", false 63 } 64 65 func (h *ResolverEnhancer) PatchFrom(o *ResolverEnhancer) { 66 if h.mapping != nil && o.mapping != nil { 67 o.mapping.CloneTo(h.mapping) 68 } 69 70 if h.fakePool != nil && o.fakePool != nil { 71 h.fakePool.CloneFrom(o.fakePool) 72 } 73 } 74 75 func NewEnhancer(cfg Config) *ResolverEnhancer { 76 var fakePool *fakeip.Pool 77 var mapping *cache.LruCache 78 79 if cfg.EnhancedMode != C.DNSNormal { 80 fakePool = cfg.Pool 81 mapping = cache.New(cache.WithSize(4096)) 82 } 83 84 return &ResolverEnhancer{ 85 mode: cfg.EnhancedMode, 86 fakePool: fakePool, 87 mapping: mapping, 88 } 89 }