diff --git a/component/fakeip/pool.go b/component/fakeip/pool.go index 46fe5927..97d812ac 100644 --- a/component/fakeip/pool.go +++ b/component/fakeip/pool.go @@ -17,6 +17,7 @@ type Pool struct { offset uint32 mux sync.Mutex host *trie.DomainTrie + ipnet *net.IPNet cache *cache.LruCache } @@ -89,6 +90,11 @@ func (p *Pool) Gateway() net.IP { return uintToIP(p.gateway) } +// IPNet return raw ipnet +func (p *Pool) IPNet() *net.IPNet { + return p.ipnet +} + // PatchFrom clone cache from old pool func (p *Pool) PatchFrom(o *Pool) { o.cache.CloneTo(p.cache) @@ -141,6 +147,7 @@ func New(ipnet *net.IPNet, size int, host *trie.DomainTrie) (*Pool, error) { max: max, gateway: min - 1, host: host, + ipnet: ipnet, cache: cache.NewLRUCache(cache.WithSize(size * 2)), }, nil } diff --git a/component/resolver/enhancer.go b/component/resolver/enhancer.go index 48b49bea..c096f87a 100644 --- a/component/resolver/enhancer.go +++ b/component/resolver/enhancer.go @@ -10,6 +10,7 @@ type Enhancer interface { FakeIPEnabled() bool MappingEnabled() bool IsFakeIP(net.IP) bool + IsExistFakeIP(net.IP) bool FindHostByIP(net.IP) (string, bool) } @@ -37,6 +38,14 @@ func IsFakeIP(ip net.IP) bool { return false } +func IsExistFakeIP(ip net.IP) bool { + if mapper := DefaultHostMapper; mapper != nil { + return mapper.IsExistFakeIP(ip) + } + + return false +} + func FindHostByIP(ip net.IP) (string, bool) { if mapper := DefaultHostMapper; mapper != nil { return mapper.FindHostByIP(ip) diff --git a/dns/enhancer.go b/dns/enhancer.go index 5018affa..0b257005 100644 --- a/dns/enhancer.go +++ b/dns/enhancer.go @@ -21,7 +21,7 @@ func (h *ResolverEnhancer) MappingEnabled() bool { return h.mode == FAKEIP || h.mode == MAPPING } -func (h *ResolverEnhancer) IsFakeIP(ip net.IP) bool { +func (h *ResolverEnhancer) IsExistFakeIP(ip net.IP) bool { if !h.FakeIPEnabled() { return false } @@ -33,6 +33,18 @@ func (h *ResolverEnhancer) IsFakeIP(ip net.IP) bool { return false } +func (h *ResolverEnhancer) IsFakeIP(ip net.IP) bool { + if !h.FakeIPEnabled() { + return false + } + + if pool := h.fakePool; pool != nil { + return pool.IPNet().Contains(ip) && !pool.Gateway().Equal(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 { diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index b4441116..c645ce08 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -170,7 +170,7 @@ func handleUDPConn(packet *inbound.PacketAdapter) { // make a fAddr if requset ip is fakeip var fAddr net.Addr - if resolver.IsFakeIP(metadata.DstIP) { + if resolver.IsExistFakeIP(metadata.DstIP) { fAddr = metadata.UDPAddr() }