diff --git a/common/dns/fakedns/middleware.go b/common/dns/fakedns/middleware.go index cfc3d18..45021e1 100644 --- a/common/dns/fakedns/middleware.go +++ b/common/dns/fakedns/middleware.go @@ -5,13 +5,15 @@ import ( "net" "strings" - trie "github.com/xjasonlyu/tun2socks/common/dns/domain-trie" D "github.com/miekg/dns" + "github.com/xjasonlyu/tun2socks/common/cache" + trie "github.com/xjasonlyu/tun2socks/common/domain-trie" + "github.com/xjasonlyu/tun2socks/common/fakeip" ) type handler func(w D.ResponseWriter, r *D.Msg) -func withFakeIP(cache *Cache, pool *Pool) handler { +func withFakeIP(cache *cache.Cache, pool *fakeip.Pool) handler { return func(w D.ResponseWriter, r *D.Msg) { q := r.Question[0] @@ -88,7 +90,7 @@ func withHost(hosts *trie.Trie, next handler) handler { } } -func strToHosts(str string) *trie.Trie { +func lineToHosts(str string) *trie.Trie { // trim `'` `"` ` ` char str = strings.Trim(str, "' \"") if str == "" { @@ -110,7 +112,7 @@ func strToHosts(str string) *trie.Trie { return tree } -func newHandler(hosts *trie.Trie, cache *Cache, pool *Pool) handler { +func newHandler(hosts *trie.Trie, cache *cache.Cache, pool *fakeip.Pool) handler { if hosts != nil { return withHost(hosts, withFakeIP(cache, pool)) } diff --git a/common/dns/fakedns/server.go b/common/dns/fakedns/server.go index 3f46e3d..c80256f 100644 --- a/common/dns/fakedns/server.go +++ b/common/dns/fakedns/server.go @@ -7,6 +7,8 @@ import ( "time" D "github.com/miekg/dns" + "github.com/xjasonlyu/tun2socks/common/cache" + "github.com/xjasonlyu/tun2socks/common/fakeip" ) var ( @@ -18,8 +20,7 @@ var ( type Server struct { *D.Server - p *Pool - c *Cache + c *cache.Cache h handler } @@ -65,31 +66,22 @@ func (s *Server) IPToHost(ip net.IP) string { return strings.TrimRight(fqdn, ".") } -func (s *Server) IsFakeIP(ip net.IP) bool { - c := ip2uint32(ip) - if c >= s.p.min && c <= s.p.max { - return true - } - return false -} - -func NewServer(fakeIPRange, hostsMap string) (*Server, error) { +func NewServer(fakeIPRange, hostsLine string) (*Server, error) { _, ipnet, err := net.ParseCIDR(fakeIPRange) if err != nil { return nil, err } - pool, err := NewPool(ipnet) + pool, err := fakeip.New(ipnet) if err != nil { return nil, err } - cache := NewCache(cacheDuration) - hosts := strToHosts(hostsMap) - handler := newHandler(hosts, cache, pool) + hosts := lineToHosts(hostsLine) + cacheItem := cache.New(cacheDuration) + handler := newHandler(hosts, cacheItem, pool) return &Server{ - p: pool, - c: cache, + c: cacheItem, h: handler, }, nil } diff --git a/common/dns/fakedns/util.go b/common/dns/fakedns/util.go index 5b12cbf..c21f6c0 100644 --- a/common/dns/fakedns/util.go +++ b/common/dns/fakedns/util.go @@ -6,9 +6,10 @@ import ( "time" D "github.com/miekg/dns" + "github.com/xjasonlyu/tun2socks/common/cache" ) -func putMsgToCache(c *Cache, key string, msg *D.Msg) { +func putMsgToCache(c *cache.Cache, key string, msg *D.Msg) { var ttl time.Duration if len(msg.Answer) != 0 { ttl = time.Duration(msg.Answer[0].Header().Ttl) * time.Second