hosts.go 3.2 KB

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