hosts.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // +build !confonly
  2. package dns
  3. import (
  4. "github.com/xtls/xray-core/v1/common"
  5. "github.com/xtls/xray-core/v1/common/net"
  6. "github.com/xtls/xray-core/v1/common/strmatcher"
  7. "github.com/xtls/xray-core/v1/features"
  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. var typeMap = map[DomainMatchingType]strmatcher.Type{
  15. DomainMatchingType_Full: strmatcher.Full,
  16. DomainMatchingType_Subdomain: strmatcher.Domain,
  17. DomainMatchingType_Keyword: strmatcher.Substr,
  18. DomainMatchingType_Regex: strmatcher.Regex,
  19. }
  20. func toStrMatcher(t DomainMatchingType, domain string) (strmatcher.Matcher, error) {
  21. strMType, f := typeMap[t]
  22. if !f {
  23. return nil, newError("unknown mapping type", t).AtWarning()
  24. }
  25. matcher, err := strMType.New(domain)
  26. if err != nil {
  27. return nil, newError("failed to create str matcher").Base(err)
  28. }
  29. return matcher, nil
  30. }
  31. // NewStaticHosts creates a new StaticHosts instance.
  32. func NewStaticHosts(hosts []*Config_HostMapping, legacy map[string]*net.IPOrDomain) (*StaticHosts, error) {
  33. g := new(strmatcher.MatcherGroup)
  34. sh := &StaticHosts{
  35. ips: make([][]net.Address, len(hosts)+len(legacy)+16),
  36. matchers: g,
  37. }
  38. if legacy != nil {
  39. features.PrintDeprecatedFeatureWarning("simple host mapping")
  40. for domain, ip := range legacy {
  41. matcher, err := strmatcher.Full.New(domain)
  42. common.Must(err)
  43. id := g.Add(matcher)
  44. address := ip.AsAddress()
  45. if address.Family().IsDomain() {
  46. return nil, newError("invalid domain address in static hosts: ", address.Domain()).AtWarning()
  47. }
  48. sh.ips[id] = []net.Address{address}
  49. }
  50. }
  51. for _, mapping := range hosts {
  52. matcher, err := toStrMatcher(mapping.Type, mapping.Domain)
  53. if err != nil {
  54. return nil, newError("failed to create domain matcher").Base(err)
  55. }
  56. id := g.Add(matcher)
  57. ips := make([]net.Address, 0, len(mapping.Ip)+1)
  58. switch {
  59. case len(mapping.Ip) > 0:
  60. for _, ip := range mapping.Ip {
  61. addr := net.IPAddress(ip)
  62. if addr == nil {
  63. return nil, newError("invalid IP address in static hosts: ", ip).AtWarning()
  64. }
  65. ips = append(ips, addr)
  66. }
  67. case len(mapping.ProxiedDomain) > 0:
  68. ips = append(ips, net.DomainAddress(mapping.ProxiedDomain))
  69. default:
  70. return nil, newError("neither IP address nor proxied domain specified for domain: ", mapping.Domain).AtWarning()
  71. }
  72. // Special handling for localhost IPv6. This is a dirty workaround as JSON config supports only single IP mapping.
  73. if len(ips) == 1 && ips[0] == net.LocalHostIP {
  74. ips = append(ips, net.LocalHostIPv6)
  75. }
  76. sh.ips[id] = ips
  77. }
  78. return sh, nil
  79. }
  80. func filterIP(ips []net.Address, option IPOption) []net.Address {
  81. filtered := make([]net.Address, 0, len(ips))
  82. for _, ip := range ips {
  83. if (ip.Family().IsIPv4() && option.IPv4Enable) || (ip.Family().IsIPv6() && option.IPv6Enable) {
  84. filtered = append(filtered, ip)
  85. }
  86. }
  87. if len(filtered) == 0 {
  88. return nil
  89. }
  90. return filtered
  91. }
  92. // LookupIP returns IP address for the given domain, if exists in this StaticHosts.
  93. func (h *StaticHosts) LookupIP(domain string, option IPOption) []net.Address {
  94. indices := h.matchers.Match(domain)
  95. if len(indices) == 0 {
  96. return nil
  97. }
  98. ips := []net.Address{}
  99. for _, id := range indices {
  100. ips = append(ips, h.ips[id]...)
  101. }
  102. if len(ips) == 1 && ips[0].Family().IsDomain() {
  103. return ips
  104. }
  105. return filterIP(ips, option)
  106. }