hosts_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package dns_test
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. . "github.com/xtls/xray-core/app/dns"
  6. "github.com/xtls/xray-core/common"
  7. "github.com/xtls/xray-core/common/matcher/domain"
  8. "github.com/xtls/xray-core/common/net"
  9. "github.com/xtls/xray-core/features/dns"
  10. )
  11. func TestStaticHosts(t *testing.T) {
  12. pb := []*Config_HostMapping{
  13. {
  14. Type: domain.MatchingType_Full,
  15. Domain: "example.com",
  16. Ip: [][]byte{
  17. {1, 1, 1, 1},
  18. },
  19. },
  20. {
  21. Type: domain.MatchingType_Full,
  22. Domain: "proxy.example.com",
  23. Ip: [][]byte{
  24. {1, 2, 3, 4},
  25. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  26. },
  27. ProxiedDomain: "another-proxy.example.com",
  28. },
  29. {
  30. Type: domain.MatchingType_Full,
  31. Domain: "proxy2.example.com",
  32. ProxiedDomain: "proxy.example.com",
  33. },
  34. {
  35. Type: domain.MatchingType_Subdomain,
  36. Domain: "example.cn",
  37. Ip: [][]byte{
  38. {2, 2, 2, 2},
  39. },
  40. },
  41. {
  42. Type: domain.MatchingType_Subdomain,
  43. Domain: "baidu.com",
  44. Ip: [][]byte{
  45. {127, 0, 0, 1},
  46. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  47. },
  48. },
  49. }
  50. hosts, err := NewStaticHosts(pb, nil)
  51. common.Must(err)
  52. {
  53. ips := hosts.Lookup("example.com", &dns.IPOption{
  54. IPv4Enable: true,
  55. IPv6Enable: true,
  56. })
  57. if len(ips) != 1 {
  58. t.Error("expect 1 IP, but got ", len(ips))
  59. }
  60. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
  61. t.Error(diff)
  62. }
  63. }
  64. {
  65. domain := hosts.Lookup("proxy.example.com", &dns.IPOption{
  66. IPv4Enable: true,
  67. IPv6Enable: false,
  68. })
  69. if len(domain) != 1 {
  70. t.Error("expect 1 domain, but got ", len(domain))
  71. }
  72. if diff := cmp.Diff(domain[0].Domain(), "another-proxy.example.com"); diff != "" {
  73. t.Error(diff)
  74. }
  75. }
  76. {
  77. domain := hosts.Lookup("proxy2.example.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.example.com"); diff != "" {
  85. t.Error(diff)
  86. }
  87. }
  88. {
  89. ips := hosts.Lookup("www.example.cn", &dns.IPOption{
  90. IPv4Enable: true,
  91. IPv6Enable: true,
  92. })
  93. if len(ips) != 1 {
  94. t.Error("expect 1 IP, but got ", len(ips))
  95. }
  96. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
  97. t.Error(diff)
  98. }
  99. }
  100. {
  101. ips := hosts.Lookup("baidu.com", &dns.IPOption{
  102. IPv4Enable: false,
  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(net.LocalHostIPv6.IP())); diff != "" {
  109. t.Error(diff)
  110. }
  111. }
  112. }