mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
1cd2b8d8fb
Co-authored-by: Dreamacro <305009791@qq.com>
77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
package dns
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
|
"github.com/Dreamacro/clash/component/fakeip"
|
|
)
|
|
|
|
type ResolverEnhancer struct {
|
|
mode EnhancedMode
|
|
fakePool *fakeip.Pool
|
|
mapping *cache.LruCache
|
|
}
|
|
|
|
func (h *ResolverEnhancer) FakeIPEnabled() bool {
|
|
return h.mode == FAKEIP
|
|
}
|
|
|
|
func (h *ResolverEnhancer) MappingEnabled() bool {
|
|
return h.mode == FAKEIP || h.mode == MAPPING
|
|
}
|
|
|
|
func (h *ResolverEnhancer) IsFakeIP(ip net.IP) bool {
|
|
if !h.FakeIPEnabled() {
|
|
return false
|
|
}
|
|
|
|
if pool := h.fakePool; pool != nil {
|
|
return pool.Exist(ip)
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (h *ResolverEnhancer) FindHostByIP(ip net.IP) (string, bool) {
|
|
if pool := h.fakePool; pool != nil {
|
|
if host, existed := pool.LookBack(ip); existed {
|
|
return host, true
|
|
}
|
|
}
|
|
|
|
if mapping := h.mapping; mapping != nil {
|
|
if host, existed := h.mapping.Get(ip.String()); existed {
|
|
return host.(string), true
|
|
}
|
|
}
|
|
|
|
return "", false
|
|
}
|
|
|
|
func (h *ResolverEnhancer) PatchFrom(o *ResolverEnhancer) {
|
|
if h.mapping != nil && o.mapping != nil {
|
|
o.mapping.CloneTo(h.mapping)
|
|
}
|
|
|
|
if h.fakePool != nil && o.fakePool != nil {
|
|
h.fakePool.PatchFrom(o.fakePool)
|
|
}
|
|
}
|
|
|
|
func NewEnhancer(cfg Config) *ResolverEnhancer {
|
|
var fakePool *fakeip.Pool
|
|
var mapping *cache.LruCache
|
|
|
|
if cfg.EnhancedMode != NORMAL {
|
|
fakePool = cfg.Pool
|
|
mapping = cache.NewLRUCache(cache.WithSize(4096), cache.WithStale(true))
|
|
}
|
|
|
|
return &ResolverEnhancer{
|
|
mode: cfg.EnhancedMode,
|
|
fakePool: fakePool,
|
|
mapping: mapping,
|
|
}
|
|
}
|