hosts.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.Ip) > 0:
  44. for _, ip := range mapping.Ip {
  45. addr := net.IPAddress(ip)
  46. if addr == nil {
  47. return nil, newError("invalid IP address in static hosts: ", ip).AtWarning()
  48. }
  49. ips = append(ips, addr)
  50. }
  51. case len(mapping.ProxiedDomain) > 0:
  52. ips = append(ips, net.DomainAddress(mapping.ProxiedDomain))
  53. default:
  54. return nil, newError("neither IP address nor proxied domain specified for domain: ", mapping.Domain).AtWarning()
  55. }
  56. // Special handling for localhost IPv6. This is a dirty workaround as JSON config supports only single IP mapping.
  57. if len(ips) == 1 && ips[0] == net.LocalHostIP {
  58. ips = append(ips, net.LocalHostIPv6)
  59. }
  60. sh.ips[id] = ips
  61. }
  62. return sh, nil
  63. }
  64. func filterIP(ips []net.Address, option dns.IPOption) []net.Address {
  65. filtered := make([]net.Address, 0, len(ips))
  66. for _, ip := range ips {
  67. if (ip.Family().IsIPv4() && option.IPv4Enable) || (ip.Family().IsIPv6() && option.IPv6Enable) {
  68. filtered = append(filtered, ip)
  69. }
  70. }
  71. return filtered
  72. }
  73. func (h *StaticHosts) lookupInternal(domain string) []net.Address {
  74. var ips []net.Address
  75. for _, id := range h.matchers.Match(domain) {
  76. ips = append(ips, h.ips[id]...)
  77. }
  78. return ips
  79. }
  80. func (h *StaticHosts) lookup(domain string, option dns.IPOption, maxDepth int) []net.Address {
  81. switch addrs := h.lookupInternal(domain); {
  82. case len(addrs) == 0: // Not recorded in static hosts, return nil
  83. return nil
  84. case len(addrs) == 1 && addrs[0].Family().IsDomain(): // Try to unwrap domain
  85. if maxDepth > 0 {
  86. unwrapped := h.lookup(addrs[0].Domain(), option, maxDepth-1)
  87. if unwrapped != nil {
  88. return unwrapped
  89. }
  90. }
  91. return addrs
  92. default: // IP record found, return a non-nil IP array
  93. return filterIP(addrs, option)
  94. }
  95. }
  96. // Lookup returns IP addresses or proxied domain for the given domain, if exists in this StaticHosts.
  97. func (h *StaticHosts) Lookup(domain string, option dns.IPOption) []net.Address {
  98. return h.lookup(domain, option, 5)
  99. }