2020-09-17 02:48:42 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2022-04-11 16:31:04 +00:00
|
|
|
"net/netip"
|
2020-09-17 02:48:42 +00:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
|
|
|
"github.com/Dreamacro/clash/component/fakeip"
|
2021-10-18 13:08:27 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2020-09-17 02:48:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ResolverEnhancer struct {
|
2021-10-18 13:08:27 +00:00
|
|
|
mode C.DNSMode
|
2020-09-17 02:48:42 +00:00
|
|
|
fakePool *fakeip.Pool
|
2022-04-11 16:31:04 +00:00
|
|
|
mapping *cache.LruCache[netip.Addr, string]
|
2020-09-17 02:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ResolverEnhancer) FakeIPEnabled() bool {
|
2021-10-18 13:08:27 +00:00
|
|
|
return h.mode == C.DNSFakeIP
|
2020-09-17 02:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ResolverEnhancer) MappingEnabled() bool {
|
2021-10-18 13:08:27 +00:00
|
|
|
return h.mode == C.DNSFakeIP || h.mode == C.DNSMapping
|
2020-09-17 02:48:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
func (h *ResolverEnhancer) IsExistFakeIP(ip netip.Addr) bool {
|
2020-09-17 02:48:42 +00:00
|
|
|
if !h.FakeIPEnabled() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if pool := h.fakePool; pool != nil {
|
2022-04-19 17:52:51 +00:00
|
|
|
return pool.Exist(ip)
|
2020-09-17 02:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
func (h *ResolverEnhancer) IsFakeIP(ip netip.Addr) bool {
|
2020-10-17 04:52:43 +00:00
|
|
|
if !h.FakeIPEnabled() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if pool := h.fakePool; pool != nil {
|
2022-04-19 17:52:51 +00:00
|
|
|
return pool.IPNet().Contains(ip) && ip != pool.Gateway() && ip != pool.Broadcast()
|
2022-03-14 18:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
func (h *ResolverEnhancer) IsFakeBroadcastIP(ip netip.Addr) bool {
|
2022-03-14 18:43:40 +00:00
|
|
|
if !h.FakeIPEnabled() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if pool := h.fakePool; pool != nil {
|
2022-04-19 17:52:51 +00:00
|
|
|
return pool.Broadcast() == ip
|
2020-10-17 04:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
func (h *ResolverEnhancer) FindHostByIP(ip netip.Addr) (string, bool) {
|
2020-09-17 02:48:42 +00:00
|
|
|
if pool := h.fakePool; pool != nil {
|
2022-04-19 17:52:51 +00:00
|
|
|
if host, existed := pool.LookBack(ip); existed {
|
2020-09-17 02:48:42 +00:00
|
|
|
return host, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if mapping := h.mapping; mapping != nil {
|
2022-04-19 17:52:51 +00:00
|
|
|
if host, existed := h.mapping.Get(ip); existed {
|
2022-04-05 12:23:16 +00:00
|
|
|
return host, true
|
2020-09-17 02:48:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
func (h *ResolverEnhancer) InsertHostByIP(ip netip.Addr, host string) {
|
2022-03-30 15:54:52 +00:00
|
|
|
if mapping := h.mapping; mapping != nil {
|
2022-04-19 17:52:51 +00:00
|
|
|
h.mapping.Set(ip, host)
|
2022-03-30 15:54:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 21:55:08 +00:00
|
|
|
func (h *ResolverEnhancer) FlushFakeIP() error {
|
|
|
|
if h.fakePool != nil {
|
|
|
|
return h.fakePool.FlushFakeIP()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-17 02:48:42 +00:00
|
|
|
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 {
|
2021-10-11 12:48:58 +00:00
|
|
|
h.fakePool.CloneFrom(o.fakePool)
|
2020-09-17 02:48:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 08:47:47 +00:00
|
|
|
func (h *ResolverEnhancer) StoreFakePoolState() {
|
2022-03-22 17:05:43 +00:00
|
|
|
if h.fakePool != nil {
|
2022-04-12 21:55:08 +00:00
|
|
|
h.fakePool.StoreState()
|
2022-03-22 17:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 02:48:42 +00:00
|
|
|
func NewEnhancer(cfg Config) *ResolverEnhancer {
|
|
|
|
var fakePool *fakeip.Pool
|
2022-04-11 16:31:04 +00:00
|
|
|
var mapping *cache.LruCache[netip.Addr, string]
|
2020-09-17 02:48:42 +00:00
|
|
|
|
2021-10-18 13:08:27 +00:00
|
|
|
if cfg.EnhancedMode != C.DNSNormal {
|
2020-09-17 02:48:42 +00:00
|
|
|
fakePool = cfg.Pool
|
2022-04-11 16:31:04 +00:00
|
|
|
mapping = cache.NewLRUCache[netip.Addr, string](cache.WithSize[netip.Addr, string](4096), cache.WithStale[netip.Addr, string](true))
|
2020-09-17 02:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ResolverEnhancer{
|
|
|
|
mode: cfg.EnhancedMode,
|
|
|
|
fakePool: fakePool,
|
|
|
|
mapping: mapping,
|
|
|
|
}
|
|
|
|
}
|