hosts.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. )
  8. // StaticHosts represents static domain-ip mapping in DNS server.
  9. type StaticHosts struct {
  10. ips [][]net.Address
  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.Address, 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("invalid domain address in static hosts: ", address.Domain()).AtWarning()
  46. }
  47. sh.ips[id] = []net.Address{address}
  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.Address, 0, len(mapping.Ip)+1)
  57. switch {
  58. case len(mapping.Ip) > 0:
  59. for _, ip := range mapping.Ip {
  60. addr := net.IPAddress(ip)
  61. if addr == nil {
  62. return nil, newError("invalid IP address in static hosts: ", ip).AtWarning()
  63. }
  64. ips = append(ips, addr)
  65. }
  66. case len(mapping.ProxiedDomain) > 0:
  67. ips = append(ips, net.DomainAddress(mapping.ProxiedDomain))
  68. default:
  69. return nil, newError("neither IP address nor proxied domain specified for domain: ", mapping.Domain).AtWarning()
  70. }
  71. // Special handling for localhost IPv6. This is a dirty workaround as JSON config supports only single IP mapping.
  72. if len(ips) == 1 && ips[0] == net.LocalHostIP {
  73. ips = append(ips, net.LocalHostIPv6)
  74. }
  75. sh.ips[id] = ips
  76. }
  77. return sh, nil
  78. }
  79. func filterIP(ips []net.Address, option IPOption) []net.Address {
  80. filtered := make([]net.Address, 0, len(ips))
  81. for _, ip := range ips {
  82. if (ip.Family().IsIPv4() && option.IPv4Enable) || (ip.Family().IsIPv6() && option.IPv6Enable) {
  83. filtered = append(filtered, ip)
  84. }
  85. }
  86. if len(filtered) == 0 {
  87. return nil
  88. }
  89. return filtered
  90. }
  91. // LookupIP returns IP address for the given domain, if exists in this StaticHosts.
  92. func (h *StaticHosts) LookupIP(domain string, option IPOption) []net.Address {
  93. indices := h.matchers.Match(domain)
  94. if len(indices) == 0 {
  95. return nil
  96. }
  97. ips := []net.Address{}
  98. for _, id := range indices {
  99. ips = append(ips, h.ips[id]...)
  100. }
  101. if len(ips) == 1 && ips[0].Family().IsDomain() {
  102. return ips
  103. }
  104. return filterIP(ips, option)
  105. }