config.go 2.1 KB

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