hosts.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package dns
  2. import (
  3. "context"
  4. "github.com/xtls/xray-core/common"
  5. "github.com/xtls/xray-core/common/errors"
  6. "github.com/xtls/xray-core/common/net"
  7. "github.com/xtls/xray-core/common/strmatcher"
  8. "github.com/xtls/xray-core/features"
  9. "github.com/xtls/xray-core/features/dns"
  10. )
  11. // StaticHosts represents static domain-ip mapping in DNS server.
  12. type StaticHosts struct {
  13. ips [][]net.Address
  14. matchers *strmatcher.MatcherGroup
  15. }
  16. // NewStaticHosts creates a new StaticHosts instance.
  17. func NewStaticHosts(hosts []*Config_HostMapping, legacy map[string]*net.IPOrDomain) (*StaticHosts, error) {
  18. g := new(strmatcher.MatcherGroup)
  19. sh := &StaticHosts{
  20. ips: make([][]net.Address, len(hosts)+len(legacy)+16),
  21. matchers: g,
  22. }
  23. if legacy != nil {
  24. features.PrintDeprecatedFeatureWarning("simple host mapping")
  25. for domain, ip := range legacy {
  26. matcher, err := strmatcher.Full.New(domain)
  27. common.Must(err)
  28. id := g.Add(matcher)
  29. address := ip.AsAddress()
  30. if address.Family().IsDomain() {
  31. return nil, errors.New("invalid domain address in static hosts: ", address.Domain()).AtWarning()
  32. }
  33. sh.ips[id] = []net.Address{address}
  34. }
  35. }
  36. for _, mapping := range hosts {
  37. matcher, err := toStrMatcher(mapping.Type, mapping.Domain)
  38. if err != nil {
  39. return nil, errors.New("failed to create domain matcher").Base(err)
  40. }
  41. id := g.Add(matcher)
  42. ips := make([]net.Address, 0, len(mapping.Ip)+1)
  43. switch {
  44. case len(mapping.ProxiedDomain) > 0:
  45. ips = append(ips, net.DomainAddress(mapping.ProxiedDomain))
  46. case len(mapping.Ip) > 0:
  47. for _, ip := range mapping.Ip {
  48. addr := net.IPAddress(ip)
  49. if addr == nil {
  50. return nil, errors.New("invalid IP address in static hosts: ", ip).AtWarning()
  51. }
  52. ips = append(ips, addr)
  53. }
  54. default:
  55. return nil, errors.New("neither IP address nor proxied domain specified for domain: ", mapping.Domain).AtWarning()
  56. }
  57. sh.ips[id] = ips
  58. }
  59. return sh, nil
  60. }
  61. func filterIP(ips []net.Address, option dns.IPOption) []net.Address {
  62. filtered := make([]net.Address, 0, len(ips))
  63. for _, ip := range ips {
  64. if (ip.Family().IsIPv4() && option.IPv4Enable) || (ip.Family().IsIPv6() && option.IPv6Enable) {
  65. filtered = append(filtered, ip)
  66. }
  67. }
  68. return filtered
  69. }
  70. func (h *StaticHosts) lookupInternal(domain string) []net.Address {
  71. var ips []net.Address
  72. for _, id := range h.matchers.Match(domain) {
  73. ips = append(ips, h.ips[id]...)
  74. }
  75. return ips
  76. }
  77. func (h *StaticHosts) lookup(domain string, option dns.IPOption, maxDepth int) []net.Address {
  78. switch addrs := h.lookupInternal(domain); {
  79. case len(addrs) == 0: // Not recorded in static hosts, return nil
  80. return nil
  81. case len(addrs) == 1 && addrs[0].Family().IsDomain(): // Try to unwrap domain
  82. errors.LogDebug(context.Background(), "found replaced domain: ", domain, " -> ", addrs[0].Domain(), ". Try to unwrap it")
  83. if maxDepth > 0 {
  84. unwrapped := h.lookup(addrs[0].Domain(), option, maxDepth-1)
  85. if unwrapped != nil {
  86. return unwrapped
  87. }
  88. }
  89. return addrs
  90. default: // IP record found, return a non-nil IP array
  91. return filterIP(addrs, option)
  92. }
  93. }
  94. // Lookup returns IP addresses or proxied domain for the given domain, if exists in this StaticHosts.
  95. func (h *StaticHosts) Lookup(domain string, option dns.IPOption) []net.Address {
  96. return h.lookup(domain, option, 5)
  97. }