Browse Source

Log matched rules and DNS to use during priority matching
2. Dotless domain support in static host & modify first letter's captial in log

Vigilans 5 years ago
parent
commit
22b4e68a2d
3 changed files with 25 additions and 1 deletions
  1. 11 0
      app/dns/server.go
  2. 13 0
      infra/conf/dns.go
  3. 1 1
      infra/conf/router.go

+ 11 - 0
app/dns/server.go

@@ -6,6 +6,7 @@ package dns
 
 
 import (
 import (
 	"context"
 	"context"
+	"fmt"
 	"log"
 	"log"
 	"net/url"
 	"net/url"
 	"strings"
 	"strings"
@@ -370,6 +371,16 @@ func (s *Server) lookupIPInternal(domain string, option IPOption) ([]net.IP, err
 	var matchedClient Client
 	var matchedClient Client
 	if s.domainMatcher != nil {
 	if s.domainMatcher != nil {
 		indices := s.domainMatcher.Match(domain)
 		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 {
 		for _, idx := range indices {
 			clientIdx := int(s.matcherInfos[idx].clientIdx)
 			clientIdx := int(s.matcherInfos[idx].clientIdx)
 			matchedClient = s.clients[clientIdx]
 			matchedClient = s.clients[clientIdx]

+ 13 - 0
infra/conf/dns.go

@@ -192,6 +192,19 @@ func (c *DnsConfig) Build() (*dns.Config, error) {
 				mapping.Domain = domain[5:]
 				mapping.Domain = domain[5:]
 
 
 				mappings = append(mappings, mapping)
 				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:") {
 			} else if strings.HasPrefix(domain, "ext:") {
 				kv := strings.Split(domain[4:], ":")
 				kv := strings.Split(domain[4:], ":")
 				if len(kv) != 2 {
 				if len(kv) != 2 {

+ 1 - 1
infra/conf/router.go

@@ -307,7 +307,7 @@ func parseDomainRule(domain string) ([]*router.Domain, error) {
 		case !strings.Contains(substr, "."):
 		case !strings.Contains(substr, "."):
 			domainRule.Value = "^[^.]*" + substr + "[^.]*$"
 			domainRule.Value = "^[^.]*" + substr + "[^.]*$"
 		default:
 		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:
 	default:
 		domainRule.Type = router.Domain_Plain
 		domainRule.Type = router.Domain_Plain