forked from fossabot/clash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: split enhanced mode instance (#936)
Co-authored-by: Dreamacro <[email protected]>
- Loading branch information
Showing
8 changed files
with
228 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package resolver | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
var DefaultHostMapper Enhancer | ||
|
||
type Enhancer interface { | ||
FakeIPEnabled() bool | ||
MappingEnabled() bool | ||
IsFakeIP(net.IP) bool | ||
FindHostByIP(net.IP) (string, bool) | ||
} | ||
|
||
func FakeIPEnabled() bool { | ||
if mapper := DefaultHostMapper; mapper != nil { | ||
return mapper.FakeIPEnabled() | ||
} | ||
|
||
return false | ||
} | ||
|
||
func MappingEnabled() bool { | ||
if mapper := DefaultHostMapper; mapper != nil { | ||
return mapper.MappingEnabled() | ||
} | ||
|
||
return false | ||
} | ||
|
||
func IsFakeIP(ip net.IP) bool { | ||
if mapper := DefaultHostMapper; mapper != nil { | ||
return mapper.IsFakeIP(ip) | ||
} | ||
|
||
return false | ||
} | ||
|
||
func FindHostByIP(ip net.IP) (string, bool) { | ||
if mapper := DefaultHostMapper; mapper != nil { | ||
return mapper.FindHostByIP(ip) | ||
} | ||
|
||
return "", false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.