Browse Source

use lru cache to store DNS

pull/15/head
Jason 5 years ago
parent
commit
c3c9517d56
  1. 3
      common/dns/fakedns/middleware.go
  2. 10
      common/dns/fakedns/server.go
  3. 12
      common/dns/fakedns/util.go
  4. 30
      common/lru-cache/cache.go

3
common/dns/fakedns/middleware.go

@ -6,9 +6,10 @@ import (
"strings" "strings"
D "github.com/miekg/dns" D "github.com/miekg/dns"
"github.com/xjasonlyu/tun2socks/common/cache"
trie "github.com/xjasonlyu/tun2socks/common/domain-trie" trie "github.com/xjasonlyu/tun2socks/common/domain-trie"
"github.com/xjasonlyu/tun2socks/common/fakeip" "github.com/xjasonlyu/tun2socks/common/fakeip"
cache "github.com/xjasonlyu/tun2socks/common/lru-cache"
// "github.com/xjasonlyu/tun2socks/common/cache"
) )
type handler func(w D.ResponseWriter, r *D.Msg) type handler func(w D.ResponseWriter, r *D.Msg)

10
common/dns/fakedns/server.go

@ -4,11 +4,11 @@ import (
"errors" "errors"
"net" "net"
"strings" "strings"
"time"
D "github.com/miekg/dns" D "github.com/miekg/dns"
"github.com/xjasonlyu/tun2socks/common/cache"
"github.com/xjasonlyu/tun2socks/common/fakeip" "github.com/xjasonlyu/tun2socks/common/fakeip"
cache "github.com/xjasonlyu/tun2socks/common/lru-cache"
// "github.com/xjasonlyu/tun2socks/common/cache"
) )
const ( const (
@ -16,7 +16,9 @@ const (
dnsDefaultTTL uint32 = 600 dnsDefaultTTL uint32 = 600
) )
var cacheDuration = time.Duration(dnsDefaultTTL) * time.Second // var cacheDuration = time.Duration(dnsDefaultTTL) * time.Second
var cacheSize = 100
type Server struct { type Server struct {
*D.Server *D.Server
@ -77,7 +79,7 @@ func NewServer(fakeIPRange, hostsLine string) (*Server, error) {
} }
hosts := lineToHosts(hostsLine) hosts := lineToHosts(hostsLine)
cacheItem := cache.New(cacheDuration) cacheItem := cache.New(cacheSize)
handler := newHandler(hosts, cacheItem, pool) handler := newHandler(hosts, cacheItem, pool)
return &Server{ return &Server{

12
common/dns/fakedns/util.go

@ -1,12 +1,13 @@
package fakedns package fakedns
import ( import (
"time"
D "github.com/miekg/dns" D "github.com/miekg/dns"
"github.com/xjasonlyu/tun2socks/common/cache"
cache "github.com/xjasonlyu/tun2socks/common/lru-cache"
// "github.com/xjasonlyu/tun2socks/common/cache"
) )
/*
func putMsgToCache(c *cache.Cache, key string, msg *D.Msg) { func putMsgToCache(c *cache.Cache, key string, msg *D.Msg) {
var ttl time.Duration var ttl time.Duration
if len(msg.Answer) != 0 { if len(msg.Answer) != 0 {
@ -21,6 +22,11 @@ func putMsgToCache(c *cache.Cache, key string, msg *D.Msg) {
c.Put(key, msg.Copy(), ttl) c.Put(key, msg.Copy(), ttl)
} }
*/
func putMsgToCache(c *cache.Cache, key string, msg *D.Msg) {
c.Put(key, msg.Copy())
}
func setMsgTTL(msg *D.Msg, ttl uint32) { func setMsgTTL(msg *D.Msg, ttl uint32) {
for _, answer := range msg.Answer { for _, answer := range msg.Answer {

30
common/lru-cache/cache.go

@ -0,0 +1,30 @@
package cache
import (
lru "github.com/hashicorp/golang-lru"
)
type Cache struct {
*lru.Cache
}
func (c *Cache) Purge() {
c.Cache.Purge()
}
func (c *Cache) Put(key interface{}, payload interface{}) {
_ = c.Cache.Add(key, payload)
}
func (c *Cache) Get(key interface{}) interface{} {
item, ok := c.Cache.Get(key)
if !ok {
return nil
}
return item
}
func New(size int) *Cache {
c, _ := lru.New(size)
return &Cache{c}
}
Loading…
Cancel
Save