From 22b4e68a2ddf42b3942415b0ca1aea7b63441847 Mon Sep 17 00:00:00 2001 From: Vigilans Date: Sat, 15 Aug 2020 23:42:04 +0800 Subject: [PATCH] Log matched rules and DNS to use during priority matching 2. Dotless domain support in static host & modify first letter's captial in log --- app/dns/server.go | 11 +++++++++++ infra/conf/dns.go | 13 +++++++++++++ infra/conf/router.go | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/dns/server.go b/app/dns/server.go index 0a86f5c07..7a0ba8e9d 100644 --- a/app/dns/server.go +++ b/app/dns/server.go @@ -6,6 +6,7 @@ package dns import ( "context" + "fmt" "log" "net/url" "strings" @@ -370,6 +371,16 @@ func (s *Server) lookupIPInternal(domain string, option IPOption) ([]net.IP, err var matchedClient Client if s.domainMatcher != nil { indices := s.domainMatcher.Match(domain) + domainRules := []string{} + matchingDNS := []string{} + for _, idx := range indices { + info := s.matcherInfos[idx] + rule := s.domainRules[info.clientIdx][info.domainRuleIdx] + domainRules = append(domainRules, fmt.Sprintf("%s(DNS idx:%d)", rule, info.clientIdx)) + matchingDNS = append(matchingDNS, s.clients[info.clientIdx].Name()) + } + newError("domain ", domain, " matching following rules: ", domainRules).AtDebug().WriteToLog() + newError("domain ", domain, " uses following DNS first: ", matchingDNS).AtDebug().WriteToLog() for _, idx := range indices { clientIdx := int(s.matcherInfos[idx].clientIdx) matchedClient = s.clients[clientIdx] diff --git a/infra/conf/dns.go b/infra/conf/dns.go index 9e3713752..e9222a4c0 100644 --- a/infra/conf/dns.go +++ b/infra/conf/dns.go @@ -191,6 +191,19 @@ func (c *DnsConfig) Build() (*dns.Config, error) { mapping.Type = dns.DomainMatchingType_Full mapping.Domain = domain[5:] + mappings = append(mappings, mapping) + } else if strings.HasPrefix(domain, "dotless:") { + mapping := getHostMapping(addr) + mapping.Type = dns.DomainMatchingType_Regex + switch substr := domain[8:]; { + case substr == "": + mapping.Domain = "^[^.]*$" + case !strings.Contains(substr, "."): + mapping.Domain = "^[^.]*" + substr + "[^.]*$" + default: + return nil, newError("substr in dotless rule should not contain a dot: ", substr) + } + mappings = append(mappings, mapping) } else if strings.HasPrefix(domain, "ext:") { kv := strings.Split(domain[4:], ":") diff --git a/infra/conf/router.go b/infra/conf/router.go index 77549481e..e25077855 100644 --- a/infra/conf/router.go +++ b/infra/conf/router.go @@ -307,7 +307,7 @@ func parseDomainRule(domain string) ([]*router.Domain, error) { case !strings.Contains(substr, "."): domainRule.Value = "^[^.]*" + substr + "[^.]*$" default: - return nil, newError("Substr in dotless rule should not contain a dot: ", substr) + return nil, newError("substr in dotless rule should not contain a dot: ", substr) } default: domainRule.Type = router.Domain_Plain