Skip to content

Commit

Permalink
Add disableFallbackIfMatch dns option
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai authored and xiaokangwang committed Sep 17, 2021
1 parent 97ef239 commit 00155ff
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 63 deletions.
80 changes: 46 additions & 34 deletions app/dns/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/dns/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ message Config {
QueryStrategy query_strategy = 9;

bool disableFallback = 10;
bool disableFallbackIfMatch = 11;
}
42 changes: 23 additions & 19 deletions app/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ import (
// DNS is a DNS rely server.
type DNS struct {
sync.Mutex
tag string
disableCache bool
disableFallback bool
ipOption *dns.IPOption
hosts *StaticHosts
clients []*Client
ctx context.Context
domainMatcher strmatcher.IndexMatcher
matcherInfos []*DomainMatcherInfo
tag string
disableCache bool
disableFallback bool
disableFallbackIfMatch bool
ipOption *dns.IPOption
hosts *StaticHosts
clients []*Client
ctx context.Context
domainMatcher strmatcher.IndexMatcher
matcherInfos []*DomainMatcherInfo
}

// DomainMatcherInfo contains information attached to index returned by Server.domainMatcher
Expand Down Expand Up @@ -135,15 +136,16 @@ func New(ctx context.Context, config *Config) (*DNS, error) {
}

return &DNS{
tag: tag,
hosts: hosts,
ipOption: ipOption,
clients: clients,
ctx: ctx,
domainMatcher: domainMatcher,
matcherInfos: matcherInfos,
disableCache: config.DisableCache,
disableFallback: config.DisableFallback,
tag: tag,
hosts: hosts,
ipOption: ipOption,
clients: clients,
ctx: ctx,
domainMatcher: domainMatcher,
matcherInfos: matcherInfos,
disableCache: config.DisableCache,
disableFallback: config.DisableFallback,
disableFallbackIfMatch: config.DisableFallbackIfMatch,
}, nil
}

Expand Down Expand Up @@ -264,6 +266,7 @@ func (s *DNS) sortClients(domain string) []*Client {
domainRules := []string{}

// Priority domain matching
hasMatch := false
for _, match := range s.domainMatcher.Match(domain) {
info := s.matcherInfos[match]
client := s.clients[info.clientIdx]
Expand All @@ -275,9 +278,10 @@ func (s *DNS) sortClients(domain string) []*Client {
clientUsed[info.clientIdx] = true
clients = append(clients, client)
clientNames = append(clientNames, client.Name())
hasMatch = true
}

if !s.disableFallback {
if !(s.disableFallback || s.disableFallbackIfMatch && hasMatch) {
// Default round-robin query
for idx, client := range s.clients {
if clientUsed[idx] || client.skipFallback {
Expand Down
22 changes: 12 additions & 10 deletions infra/conf/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,14 @@ var typeMap = map[router.Domain_Type]dns.DomainMatchingType{

// DNSConfig is a JSON serializable object for dns.Config.
type DNSConfig struct {
Servers []*NameServerConfig `json:"servers"`
Hosts map[string]*HostAddress `json:"hosts"`
ClientIP *cfgcommon.Address `json:"clientIp"`
Tag string `json:"tag"`
QueryStrategy string `json:"queryStrategy"`
DisableCache bool `json:"disableCache"`
DisableFallback bool `json:"disableFallback"`
Servers []*NameServerConfig `json:"servers"`
Hosts map[string]*HostAddress `json:"hosts"`
ClientIP *cfgcommon.Address `json:"clientIp"`
Tag string `json:"tag"`
QueryStrategy string `json:"queryStrategy"`
DisableCache bool `json:"disableCache"`
DisableFallback bool `json:"disableFallback"`
DisableFallbackIfMatch bool `json:"disableFallbackIfMatch"`
}

type HostAddress struct {
Expand Down Expand Up @@ -206,9 +207,10 @@ func (c *DNSConfig) Build() (*dns.Config, error) {
geoLoader := cfgEnv.GetGeoLoader()

config := &dns.Config{
Tag: c.Tag,
DisableCache: c.DisableCache,
DisableFallback: c.DisableFallback,
Tag: c.Tag,
DisableCache: c.DisableCache,
DisableFallback: c.DisableFallback,
DisableFallbackIfMatch: c.DisableFallbackIfMatch,
}

if c.ClientIP != nil {
Expand Down

0 comments on commit 00155ff

Please sign in to comment.