hosts.go 3.2 KB

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