hosts_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package dns_test
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/google/go-cmp/cmp"
  6. . "github.com/xtls/xray-core/app/dns"
  7. "github.com/xtls/xray-core/app/router"
  8. "github.com/xtls/xray-core/common"
  9. "github.com/xtls/xray-core/common/net"
  10. "github.com/xtls/xray-core/features/dns"
  11. )
  12. func TestStaticHosts(t *testing.T) {
  13. pb := []*Config_HostMapping{
  14. {
  15. Type: DomainMatchingType_Subdomain,
  16. Domain: "lan",
  17. ProxiedDomain: "#3",
  18. },
  19. {
  20. Type: DomainMatchingType_Full,
  21. Domain: "example.com",
  22. Ip: [][]byte{
  23. {1, 1, 1, 1},
  24. },
  25. },
  26. {
  27. Type: DomainMatchingType_Full,
  28. Domain: "proxy.xray.com",
  29. Ip: [][]byte{
  30. {1, 2, 3, 4},
  31. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  32. },
  33. ProxiedDomain: "another-proxy.xray.com",
  34. },
  35. {
  36. Type: DomainMatchingType_Full,
  37. Domain: "proxy2.xray.com",
  38. ProxiedDomain: "proxy.xray.com",
  39. },
  40. {
  41. Type: DomainMatchingType_Subdomain,
  42. Domain: "example.cn",
  43. Ip: [][]byte{
  44. {2, 2, 2, 2},
  45. },
  46. },
  47. {
  48. Type: DomainMatchingType_Subdomain,
  49. Domain: "baidu.com",
  50. Ip: [][]byte{
  51. {127, 0, 0, 1},
  52. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  53. },
  54. },
  55. }
  56. hosts, err := NewStaticHosts(pb)
  57. common.Must(err)
  58. {
  59. _, err := hosts.Lookup("example.com.lan", dns.IPOption{})
  60. if dns.RCodeFromError(err) != 3 {
  61. t.Error(err)
  62. }
  63. }
  64. {
  65. ips, _ := hosts.Lookup("example.com", dns.IPOption{
  66. IPv4Enable: true,
  67. IPv6Enable: true,
  68. })
  69. if len(ips) != 1 {
  70. t.Error("expect 1 IP, but got ", len(ips))
  71. }
  72. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
  73. t.Error(diff)
  74. }
  75. }
  76. {
  77. domain, _ := hosts.Lookup("proxy.xray.com", dns.IPOption{
  78. IPv4Enable: true,
  79. IPv6Enable: false,
  80. })
  81. if len(domain) != 1 {
  82. t.Error("expect 1 domain, but got ", len(domain))
  83. }
  84. if diff := cmp.Diff(domain[0].Domain(), "another-proxy.xray.com"); diff != "" {
  85. t.Error(diff)
  86. }
  87. }
  88. {
  89. domain, _ := hosts.Lookup("proxy2.xray.com", dns.IPOption{
  90. IPv4Enable: true,
  91. IPv6Enable: false,
  92. })
  93. if len(domain) != 1 {
  94. t.Error("expect 1 domain, but got ", len(domain))
  95. }
  96. if diff := cmp.Diff(domain[0].Domain(), "another-proxy.xray.com"); diff != "" {
  97. t.Error(diff)
  98. }
  99. }
  100. {
  101. ips, _ := hosts.Lookup("www.example.cn", dns.IPOption{
  102. IPv4Enable: true,
  103. IPv6Enable: true,
  104. })
  105. if len(ips) != 1 {
  106. t.Error("expect 1 IP, but got ", len(ips))
  107. }
  108. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
  109. t.Error(diff)
  110. }
  111. }
  112. {
  113. ips, _ := hosts.Lookup("baidu.com", dns.IPOption{
  114. IPv4Enable: false,
  115. IPv6Enable: true,
  116. })
  117. if len(ips) != 1 {
  118. t.Error("expect 1 IP, but got ", len(ips))
  119. }
  120. if diff := cmp.Diff([]byte(ips[0].IP()), []byte(net.LocalHostIPv6.IP())); diff != "" {
  121. t.Error(diff)
  122. }
  123. }
  124. }
  125. func TestStaticHostsFromCache(t *testing.T) {
  126. sites := []*router.GeoSite{
  127. {
  128. CountryCode: "cloudflare-dns.com",
  129. Domain: []*router.Domain{
  130. {Type: router.Domain_Full, Value: "example.com"},
  131. },
  132. },
  133. {
  134. CountryCode: "geosite:cn",
  135. Domain: []*router.Domain{
  136. {Type: router.Domain_Domain, Value: "baidu.cn"},
  137. },
  138. },
  139. }
  140. deps := map[string][]string{
  141. "HOSTS": {"cloudflare-dns.com", "geosite:cn"},
  142. }
  143. hostIPs := map[string][]string{
  144. "cloudflare-dns.com": {"1.1.1.1"},
  145. "geosite:cn": {"2.2.2.2"},
  146. "_ORDER": {"cloudflare-dns.com", "geosite:cn"},
  147. }
  148. var buf bytes.Buffer
  149. err := router.SerializeGeoSiteList(sites, deps, hostIPs, &buf)
  150. common.Must(err)
  151. // Load matcher
  152. m, err := router.LoadGeoSiteMatcher(bytes.NewReader(buf.Bytes()), "HOSTS")
  153. common.Must(err)
  154. // Load hostIPs
  155. f := bytes.NewReader(buf.Bytes())
  156. hips, err := router.LoadGeoSiteHosts(f)
  157. common.Must(err)
  158. hosts, err := NewStaticHostsFromCache(m, hips)
  159. common.Must(err)
  160. {
  161. ips, _ := hosts.Lookup("example.com", dns.IPOption{IPv4Enable: true})
  162. if len(ips) != 1 || ips[0].String() != "1.1.1.1" {
  163. t.Error("failed to lookup example.com from cache")
  164. }
  165. }
  166. {
  167. ips, _ := hosts.Lookup("baidu.cn", dns.IPOption{IPv4Enable: true})
  168. if len(ips) != 1 || ips[0].String() != "2.2.2.2" {
  169. t.Error("failed to lookup baidu.cn from cache deps")
  170. }
  171. }
  172. }