github.com/imannamdari/v2ray-core/v5@v5.0.5/app/dns/config.go (about)

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