Skip to content

Commit

Permalink
feat: dns caching
Browse files Browse the repository at this point in the history
  • Loading branch information
ohaiibuzzle committed Sep 8, 2024
1 parent a2993ac commit 0fd52dc
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
60 changes: 60 additions & 0 deletions dns/cache/dns_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cache

import (
"sync"
"time"

"github.com/xvzc/SpoofDPI/util"
)

type DNSCacheEntry struct {
ip string
expiry_timer *time.Timer
}

type DNSCache struct {
cacheMap map[string]DNSCacheEntry
cacheLock sync.RWMutex
}

var dnsCache *DNSCache
var once sync.Once

func GetCache() *DNSCache {
once.Do(func() {
dnsCache = &DNSCache{
cacheMap: make(map[string]DNSCacheEntry),
cacheLock: sync.RWMutex{},
}
})
return dnsCache
}

// interface function for the inner map basically.
func (d *DNSCache) Set(key string, value string) {
d.cacheLock.Lock()
defer d.cacheLock.Unlock()
if _, ok := d.cacheMap[key]; ok {
return
}

d.cacheMap[key] = DNSCacheEntry{
ip: value,
expiry_timer: time.AfterFunc(time.Duration(*&util.GetConfig().DnsCacheTTL)*time.Second, func() {
d.cacheLock.Lock()
defer d.cacheLock.Unlock()
delete(d.cacheMap, key)
}),
}
}

func (d *DNSCache) Get(key string) (string, bool) {
d.cacheLock.RLock()
defer d.cacheLock.RUnlock()

if value, ok := d.cacheMap[key]; ok {
return value.ip, true
}

return "", false
}
15 changes: 13 additions & 2 deletions dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/miekg/dns"
"github.com/xvzc/SpoofDPI/dns/cache"
"github.com/xvzc/SpoofDPI/dns/resolver"
"github.com/xvzc/SpoofDPI/util"
"github.com/xvzc/SpoofDPI/util/log"
Expand All @@ -23,6 +24,7 @@ type Resolver interface {
type Dns struct {
host string
port string
dnsCache *cache.DNSCache
systemClient Resolver
generalClient Resolver
dohClient Resolver
Expand All @@ -41,6 +43,7 @@ func NewDns(config *util.Config) *Dns {
return &Dns{
host: config.DnsAddr,
port: port,
dnsCache: cache.GetCache(),
systemClient: resolver.NewSystemResolver(),
generalClient: resolver.NewGeneralResolver(net.JoinHostPort(addr, port)),
dohClient: resolver.NewDOHResolver(addr),
Expand All @@ -56,6 +59,11 @@ func (d *Dns) ResolveHost(ctx context.Context, host string, enableDoh bool, useS
return ip.String(), nil
}

if r, ok := d.dnsCache.Get(host); ok {
logger.Debug().Msgf("found %s in cache", host)
return r, nil
}

clt := d.clientFactory(enableDoh, useSystemDns)
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
Expand All @@ -71,8 +79,11 @@ func (d *Dns) ResolveHost(ctx context.Context, host string, enableDoh bool, useS
}

if len(addrs) > 0 {
d := time.Since(t).Milliseconds()
logger.Debug().Msgf("resolved %s from %s in %d ms", addrs[0].String(), host, d)
elapsed := time.Since(t).Milliseconds()
logger.Debug().Msgf("resolved %s from %s in %d ms", addrs[0].String(), host, elapsed)

d.dnsCache.Set(host, addrs[0].String())

return addrs[0].String(), nil
}

Expand Down
2 changes: 2 additions & 0 deletions util/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Args struct {
DnsAddr string
DnsPort uint16
DnsIPv4Only bool
DnsCacheTTL uint16
EnableDoh bool
Debug bool
Silent bool
Expand Down Expand Up @@ -42,6 +43,7 @@ func ParseArgs() *Args {
uintNVar(&args.Port, "port", 8080, "port")
flag.StringVar(&args.DnsAddr, "dns-addr", "8.8.8.8", "dns address")
uintNVar(&args.DnsPort, "dns-port", 53, "port number for dns")
uintNVar(&args.DnsCacheTTL, "dns-cache-ttl", 300, "time-to-live in seconds for dns cache")
flag.BoolVar(&args.EnableDoh, "enable-doh", false, "enable 'dns-over-https'")
flag.BoolVar(&args.Debug, "debug", false, "enable debug output")
flag.BoolVar(&args.Silent, "silent", false, "do not show the banner and server information at start up")
Expand Down
2 changes: 2 additions & 0 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
DnsAddr string
DnsPort int
DnsIPv4Only bool
DnsCacheTTL int
EnableDoh bool
Debug bool
Silent bool
Expand All @@ -38,6 +39,7 @@ func (c *Config) Load(args *Args) {
c.DnsAddr = args.DnsAddr
c.DnsPort = int(args.DnsPort)
c.DnsIPv4Only = args.DnsIPv4Only
c.DnsCacheTTL = int(args.DnsCacheTTL)
c.Debug = args.Debug
c.EnableDoh = args.EnableDoh
c.Silent = args.Silent
Expand Down

0 comments on commit 0fd52dc

Please sign in to comment.