Refine DNS strategies

This commit is contained in:
JimhHan 2021-04-09 23:36:36 +08:00
parent f4a048aa0c
commit 726a722019
No known key found for this signature in database
GPG key ID: 48D5D7CF95157AC5
21 changed files with 255 additions and 208 deletions

View file

@ -124,6 +124,7 @@ type DNSConfig struct {
ClientIP *Address `json:"clientIp"`
Tag string `json:"tag"`
QueryStrategy string `json:"queryStrategy"`
CacheStrategy string `json:"cacheStrategy"`
DisableCache bool `json:"disableCache"`
}
@ -142,8 +143,12 @@ func getHostMapping(addr *Address) *dns.Config_HostMapping {
// Build implements Buildable
func (c *DNSConfig) Build() (*dns.Config, error) {
config := &dns.Config{
Tag: c.Tag,
DisableCache: c.DisableCache,
Tag: c.Tag,
CacheStrategy: dns.CacheStrategy_Cache_ALL,
}
if c.DisableCache {
config.CacheStrategy = dns.CacheStrategy_Cache_DISABLE
}
if c.ClientIP != nil {
@ -163,6 +168,15 @@ func (c *DNSConfig) Build() (*dns.Config, error) {
config.QueryStrategy = dns.QueryStrategy_USE_IP6
}
switch strings.ToLower(c.CacheStrategy) {
case "noerror":
config.CacheStrategy = dns.CacheStrategy_Cache_NOERROR
case "all":
config.CacheStrategy = dns.CacheStrategy_Cache_ALL
case "disable", "none":
config.CacheStrategy = dns.CacheStrategy_Cache_DISABLE
}
for _, server := range c.Servers {
ns, err := server.Build()
if err != nil {

View file

@ -139,7 +139,7 @@ func TestDNSConfigParsing(t *testing.T) {
},
ClientIp: []byte{10, 0, 0, 1},
QueryStrategy: dns.QueryStrategy_USE_IP4,
DisableCache: true,
CacheStrategy: dns.CacheStrategy_Cache_DISABLE,
},
},
})