Skip to content

Commit

Permalink
Fix: missing fake-ip record should return error
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Oct 17, 2020
1 parent 8c3e2a7 commit d3bb4c6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions component/fakeip/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Pool struct {
offset uint32
mux sync.Mutex
host *trie.DomainTrie
ipnet *net.IPNet
cache *cache.LruCache
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions component/resolver/enhancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Enhancer interface {
FakeIPEnabled() bool
MappingEnabled() bool
IsFakeIP(net.IP) bool
IsExistFakeIP(net.IP) bool
FindHostByIP(net.IP) (string, bool)
}

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion dns/enhancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit d3bb4c6

Please sign in to comment.