Browse Source

remove lru cache

pull/15/head
Jason 5 years ago
parent
commit
bd4bc24331
  1. 1
      cmd/tun2socks/main.go
  2. 3
      cmd/tun2socks/main_fakedns.go
  3. 2
      common/dns/fakedns/middleware.go
  4. 24
      common/dns/fakedns/server.go
  5. 19
      common/dns/fakedns/utils.go

1
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

3
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")
}

2
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)

24
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)

19
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) {

Loading…
Cancel
Save