hosts.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package dns
  2. import (
  3. "v2ray.com/core/common"
  4. "v2ray.com/core/common/net"
  5. "v2ray.com/core/common/strmatcher"
  6. "v2ray.com/core/features"
  7. )
  8. // StaticHosts represents static domain-ip mapping in DNS server.
  9. type StaticHosts struct {
  10. ips [][]net.IP
  11. matchers *strmatcher.MatcherGroup
  12. }
  13. var typeMap = map[DomainMatchingType]strmatcher.Type{
  14. DomainMatchingType_Full: strmatcher.Full,
  15. DomainMatchingType_Subdomain: strmatcher.Domain,
  16. DomainMatchingType_Keyword: strmatcher.Substr,
  17. DomainMatchingType_Regex: strmatcher.Regex,
  18. }
  19. func toStrMatcher(t DomainMatchingType, domain string) (strmatcher.Matcher, error) {
  20. strMType, f := typeMap[t]
  21. if !f {
  22. return nil, newError("unknown mapping type", t).AtWarning()
  23. }
  24. matcher, err := strMType.New(domain)
  25. if err != nil {
  26. return nil, newError("failed to create str matcher").Base(err)
  27. }
  28. return matcher, nil
  29. }
  30. // NewStaticHosts creates a new StaticHosts instance.
  31. func NewStaticHosts(hosts []*Config_HostMapping, legacy map[string]*net.IPOrDomain) (*StaticHosts, error) {
  32. g := new(strmatcher.MatcherGroup)
  33. sh := &StaticHosts{
  34. ips: make([][]net.IP, len(hosts)+len(legacy)+16),
  35. matchers: g,
  36. }
  37. if legacy != nil {
  38. features.PrintDeprecatedFeatureWarning("simple host mapping")
  39. for domain, ip := range legacy {
  40. matcher, err := strmatcher.Full.New(domain)
  41. common.Must(err)
  42. id := g.Add(matcher)
  43. address := ip.AsAddress()
  44. if address.Family().IsDomain() {
  45. return nil, newError("ignoring domain address in static hosts: ", address.Domain()).AtWarning()
  46. }
  47. sh.ips[id] = []net.IP{address.IP()}
  48. }
  49. }
  50. for _, mapping := range hosts {
  51. matcher, err := toStrMatcher(mapping.Type, mapping.Domain)
  52. if err != nil {
  53. return nil, newError("failed to create domain matcher").Base(err)
  54. }
  55. id := g.Add(matcher)
  56. ips := make([]net.IP, len(mapping.Ip))
  57. for idx, ip := range mapping.Ip {
  58. ips[idx] = net.IP(ip)
  59. }
  60. sh.ips[id] = ips
  61. }
  62. return sh, nil
  63. }
  64. func filterIP(ips []net.IP, option IPOption) []net.IP {
  65. filtered := make([]net.IP, 0, len(ips))
  66. for _, ip := range ips {
  67. parsed := net.IPAddress(ip)
  68. if (parsed.Family().IsIPv4() && option.IPv4Enable) || (parsed.Family().IsIPv6() && option.IPv6Enable) {
  69. filtered = append(filtered, parsed.IP())
  70. }
  71. }
  72. if len(filtered) == 0 {
  73. return nil
  74. }
  75. return filtered
  76. }
  77. // LookupIP returns IP address for the given domain, if exists in this StaticHosts.
  78. func (h *StaticHosts) LookupIP(domain string, option IPOption) []net.IP {
  79. id := h.matchers.Match(domain)
  80. if id == 0 {
  81. return nil
  82. }
  83. return filterIP(h.ips[id], option)
  84. }