hosts.go 2.7 KB

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