config.go 2.7 KB

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