config.go 2.1 KB

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