Skip to content

Commit

Permalink
feat: support cache no answer
Browse files Browse the repository at this point in the history
  • Loading branch information
jinliming2 committed Jun 6, 2021
1 parent d2740ca commit 26d3005
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ TXT = [ 'text' ]
| listen | `string[]` | ✔️ | | host and port to listen |
| timeout | `uint` | | `5` | timeout in seconds for each DNS request, 0 to disable |
| round_robin | `string` | | `'clock'` | upstream select round robin, can only be `'clock'`, `'random'`, `'wrandom'` or `'swrr'` |
| cache_no_answer | `uint` | | `0` | Cache response for specified seconds even if query returns with no specified answer or domain was not exists |
| no_cache | `boolean` | | `false` | disable global DNS result cache |
| custom_ecs | `string[]` | | | custom EDNS Subnet to override |
| fallback_no_ecs | `boolean` | | `false` | fallback to no_ecs=`true` when DNS request got no answer |
Expand Down
3 changes: 2 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type Client struct {

servers []*dns.Server

cacher *cache.Cache
cacher *cache.Cache
cacheNoAnswer uint32
}

func startDNSServer(server *dns.Server, logger *zap.SugaredLogger, results chan error) {
Expand Down
34 changes: 19 additions & 15 deletions client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,28 @@ func (client *Client) handlerFunc(w dns.ResponseWriter, r *dns.Msg, useTCP bool)
}
w.WriteMsg(response)

if client.cacher != nil && err == nil && response.Rcode == dns.RcodeSuccess && (len(response.Answer)+len(response.Ns)+len(response.Extra)) > 0 {
if client.cacher != nil && err == nil {
var minttl uint32
for _, answer := range response.Answer {
ttl := answer.Header().Ttl
if ttl > 0 && (minttl == 0 || ttl < minttl) {
minttl = ttl
if response.Rcode == dns.RcodeNameError || len(response.Answer)+len(response.Ns)+len(response.Extra) == 0 {
minttl = client.cacheNoAnswer
} else if response.Rcode == dns.RcodeSuccess {
for _, answer := range response.Answer {
ttl := answer.Header().Ttl
if ttl > 0 && (minttl == 0 || ttl < minttl) {
minttl = ttl
}
}
}
for _, ns := range response.Ns {
ttl := ns.Header().Ttl
if ttl > 0 && (minttl == 0 || ttl < minttl) {
minttl = ttl
for _, ns := range response.Ns {
ttl := ns.Header().Ttl
if ttl > 0 && (minttl == 0 || ttl < minttl) {
minttl = ttl
}
}
}
for _, extra := range response.Extra {
ttl := extra.Header().Ttl
if ttl > 0 && (minttl == 0 || ttl < minttl) {
minttl = ttl
for _, extra := range response.Extra {
ttl := extra.Header().Ttl
if ttl > 0 && (minttl == 0 || ttl < minttl) {
minttl = ttl
}
}
}
if minttl > 0 {
Expand Down
2 changes: 1 addition & 1 deletion client/newClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// NewClient returns a client with dnsClients
func NewClient(logger *zap.SugaredLogger, conf *config.Config) (client *Client, err error) {
timeout := time.Duration(*conf.Config.Timeout) * time.Second
client = &Client{logger: logger, timeout: timeout}
client = &Client{logger: logger, timeout: timeout, cacheNoAnswer: conf.Config.CacheNoAnswer}

switch conf.Config.RoundRobin {
case config.SelectorClock:
Expand Down
9 changes: 5 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ type typeCustomSpecified struct {
}

type typeGeneralConfig struct {
Listen []string `toml:"listen"`
Timeout *uint `toml:"timeout"` // seconds
RoundRobin Selectors `toml:"round_robin"` // default: clock
NoCache bool `toml:"no_cache"`
Listen []string `toml:"listen"`
Timeout *uint `toml:"timeout"` // seconds
RoundRobin Selectors `toml:"round_robin"` // default: clock
CacheNoAnswer uint32 `toml:"cache_no_answer"`
NoCache bool `toml:"no_cache"`
DNSSettings
}

Expand Down

0 comments on commit 26d3005

Please sign in to comment.