github.com/v2fly/v2ray-core/v4@v4.45.2/app/dns/config.go (about)

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