github.com/xraypb/Xray-core@v1.8.1/app/dns/config.go (about)

     1  package dns
     2  
     3  import (
     4  	"github.com/xraypb/Xray-core/common/net"
     5  	"github.com/xraypb/Xray-core/common/strmatcher"
     6  	"github.com/xraypb/Xray-core/common/uuid"
     7  )
     8  
     9  var typeMap = map[DomainMatchingType]strmatcher.Type{
    10  	DomainMatchingType_Full:      strmatcher.Full,
    11  	DomainMatchingType_Subdomain: strmatcher.Domain,
    12  	DomainMatchingType_Keyword:   strmatcher.Substr,
    13  	DomainMatchingType_Regex:     strmatcher.Regex,
    14  }
    15  
    16  // References:
    17  // https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml
    18  // https://unix.stackexchange.com/questions/92441/whats-the-difference-between-local-home-and-lan
    19  var localTLDsAndDotlessDomains = []*NameServer_PriorityDomain{
    20  	{Type: DomainMatchingType_Regex, Domain: "^[^.]+$"}, // This will only match domains without any dot
    21  	{Type: DomainMatchingType_Subdomain, Domain: "local"},
    22  	{Type: DomainMatchingType_Subdomain, Domain: "localdomain"},
    23  	{Type: DomainMatchingType_Subdomain, Domain: "localhost"},
    24  	{Type: DomainMatchingType_Subdomain, Domain: "lan"},
    25  	{Type: DomainMatchingType_Subdomain, Domain: "home.arpa"},
    26  	{Type: DomainMatchingType_Subdomain, Domain: "example"},
    27  	{Type: DomainMatchingType_Subdomain, Domain: "invalid"},
    28  	{Type: DomainMatchingType_Subdomain, Domain: "test"},
    29  }
    30  
    31  var localTLDsAndDotlessDomainsRule = &NameServer_OriginalRule{
    32  	Rule: "geosite:private",
    33  	Size: uint32(len(localTLDsAndDotlessDomains)),
    34  }
    35  
    36  func toStrMatcher(t DomainMatchingType, domain string) (strmatcher.Matcher, error) {
    37  	strMType, f := typeMap[t]
    38  	if !f {
    39  		return nil, newError("unknown mapping type", t).AtWarning()
    40  	}
    41  	matcher, err := strMType.New(domain)
    42  	if err != nil {
    43  		return nil, newError("failed to create str matcher").Base(err)
    44  	}
    45  	return matcher, nil
    46  }
    47  
    48  func toNetIP(addrs []net.Address) ([]net.IP, error) {
    49  	ips := make([]net.IP, 0, len(addrs))
    50  	for _, addr := range addrs {
    51  		if addr.Family().IsIP() {
    52  			ips = append(ips, addr.IP())
    53  		} else {
    54  			return nil, newError("Failed to convert address", addr, "to Net IP.").AtWarning()
    55  		}
    56  	}
    57  	return ips, nil
    58  }
    59  
    60  func generateRandomTag() string {
    61  	id := uuid.New()
    62  	return "xray.system." + id.String()
    63  }