mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2024-12-30 02:37:01 +00:00
remove lru cache
This commit is contained in:
@@ -50,7 +50,6 @@ type CmdArgs struct {
|
||||
FakeIPRange *string
|
||||
FakeDnsAddr *string
|
||||
FakeDnsHosts *string
|
||||
DnsCacheSize *int
|
||||
ExceptionApps *string
|
||||
ExceptionSendThrough *string
|
||||
Stats *bool
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user