From bd4bc243315ab60b491b2e8abbf009eb7a74f52e Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 23 Jul 2019 16:00:56 +0800 Subject: [PATCH] remove lru cache --- cmd/tun2socks/main.go | 1 - cmd/tun2socks/main_fakedns.go | 3 +-- common/dns/fakedns/middleware.go | 2 +- common/dns/fakedns/server.go | 24 ++++++++---------------- common/dns/fakedns/utils.go | 19 +++++++++++++++++-- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/cmd/tun2socks/main.go b/cmd/tun2socks/main.go index 087da93..72f1a9c 100644 --- a/cmd/tun2socks/main.go +++ b/cmd/tun2socks/main.go @@ -50,7 +50,6 @@ type CmdArgs struct { FakeIPRange *string FakeDnsAddr *string FakeDnsHosts *string - DnsCacheSize *int ExceptionApps *string ExceptionSendThrough *string Stats *bool diff --git a/cmd/tun2socks/main_fakedns.go b/cmd/tun2socks/main_fakedns.go index 31c58e3..f5c71bc 100644 --- a/cmd/tun2socks/main_fakedns.go +++ b/cmd/tun2socks/main_fakedns.go @@ -13,11 +13,10 @@ func init() { args.FakeDnsAddr = flag.String("fakeDnsAddr", ":53", "listen address of fake DNS") args.FakeIPRange = flag.String("fakeIPRange", "198.18.0.1/16", "fake IP CIDR range for DNS") args.FakeDnsHosts = flag.String("fakeDnsHosts", "", "Hosts mapping, e.g. 'a.com=1.1.1.1,b.net=2.2.2.2'") - args.DnsCacheSize = flag.Int("dnsCacheSize", 100, "Size of LRU-Cache to store DNS msg") addPostFlagsInitFn(func() { if *args.EnableFakeDns { - fakeDnsServer, err := fakedns.NewServer(*args.FakeIPRange, *args.FakeDnsHosts, *args.DnsCacheSize) + fakeDnsServer, err := fakedns.NewServer(*args.FakeIPRange, *args.FakeDnsHosts) if err != nil { panic("create fake dns server error") } diff --git a/common/dns/fakedns/middleware.go b/common/dns/fakedns/middleware.go index 01e4369..be974ac 100644 --- a/common/dns/fakedns/middleware.go +++ b/common/dns/fakedns/middleware.go @@ -6,9 +6,9 @@ import ( "strings" 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" - cache "github.com/xjasonlyu/tun2socks/common/lru-cache" ) type handler func(w D.ResponseWriter, r *D.Msg) diff --git a/common/dns/fakedns/server.go b/common/dns/fakedns/server.go index 5084b3c..f2ba330 100644 --- a/common/dns/fakedns/server.go +++ b/common/dns/fakedns/server.go @@ -4,18 +4,19 @@ import ( "errors" "net" "strings" + "time" D "github.com/miekg/dns" + "github.com/xjasonlyu/tun2socks/common/cache" "github.com/xjasonlyu/tun2socks/common/fakeip" - cache "github.com/xjasonlyu/tun2socks/common/lru-cache" ) const ( dnsFakeTTL uint32 = 1 - dnsDefaultTTL uint32 = 600 + dnsDefaultTTL uint32 = 3600 ) -// var cacheDuration = time.Duration(dnsDefaultTTL) * time.Second +var cacheDuration = 10 * time.Minute type Server struct { *D.Server @@ -57,15 +58,15 @@ func (s *Server) StartServer(addr string) error { } func (s *Server) IPToHost(ip net.IP) (string, bool) { - c, ok := s.c.Peek(ip.String()) - if !ok { + c := s.c.Get(ip.String()) + if c == nil { return "", false } fqdn := c.(*D.Msg).Question[0].Name return strings.TrimRight(fqdn, "."), true } -func NewServer(fakeIPRange, hostsLine string, size int) (*Server, error) { +func NewServer(fakeIPRange, hostsLine string) (*Server, error) { _, ipnet, err := net.ParseCIDR(fakeIPRange) if err != nil { return nil, err @@ -75,16 +76,7 @@ func NewServer(fakeIPRange, hostsLine string, size int) (*Server, error) { return nil, err } - var cacheItem *cache.Cache - evictCallback := func(_ interface{}, value interface{}) { - msg := value.(*D.Msg).Copy() - q := msg.Question[0] - ip := msg.Answer[0].(*D.A).A - cacheItem.Remove("fakeip:" + q.String()) - cacheItem.Remove(ip.String()) - } - cacheItem = cache.New(size, evictCallback) - + cacheItem := cache.New(cacheDuration) hosts := lineToHosts(hostsLine) handler := newHandler(hosts, cacheItem, pool) diff --git a/common/dns/fakedns/utils.go b/common/dns/fakedns/utils.go index b170bc0..e8d5fef 100644 --- a/common/dns/fakedns/utils.go +++ b/common/dns/fakedns/utils.go @@ -1,12 +1,27 @@ package fakedns import ( + "time" + + "github.com/Dreamacro/clash/log" D "github.com/miekg/dns" - cache "github.com/xjasonlyu/tun2socks/common/lru-cache" + "github.com/xjasonlyu/tun2socks/common/cache" ) func putMsgToCache(c *cache.Cache, key string, msg *D.Msg) { - c.Add(key, msg.Copy()) + var ttl time.Duration + if len(msg.Answer) != 0 { + ttl = time.Duration(msg.Answer[0].Header().Ttl) * time.Second + } else if len(msg.Ns) != 0 { + ttl = time.Duration(msg.Ns[0].Header().Ttl) * time.Second + } else if len(msg.Extra) != 0 { + ttl = time.Duration(msg.Extra[0].Header().Ttl) * time.Second + } else { + log.Debugln("[DNS] response msg error: %#v", msg) + return + } + + c.Put(key, msg.Copy(), ttl) } func setMsgTTL(msg *D.Msg, ttl uint32) {