dns_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package conf_test
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/xtls/xray-core/app/dns"
  6. "github.com/xtls/xray-core/common/net"
  7. . "github.com/xtls/xray-core/infra/conf"
  8. "google.golang.org/protobuf/proto"
  9. )
  10. func TestDNSConfigParsing(t *testing.T) {
  11. parserCreator := func() func(string) (proto.Message, error) {
  12. return func(s string) (proto.Message, error) {
  13. config := new(DNSConfig)
  14. if err := json.Unmarshal([]byte(s), config); err != nil {
  15. return nil, err
  16. }
  17. return config.Build()
  18. }
  19. }
  20. runMultiTestCase(t, []TestCase{
  21. {
  22. Input: `{
  23. "servers": [{
  24. "address": "8.8.8.8",
  25. "port": 5353,
  26. "skipFallback": true,
  27. "domains": ["domain:example.com"]
  28. }],
  29. "hosts": {
  30. "domain:example.com": "google.com",
  31. "example.com": "127.0.0.1",
  32. "keyword:google": ["8.8.8.8", "8.8.4.4"],
  33. "regexp:.*\\.com": "8.8.4.4",
  34. "www.example.org": ["127.0.0.1", "127.0.0.2"]
  35. },
  36. "clientIp": "10.0.0.1",
  37. "queryStrategy": "UseIPv4",
  38. "disableCache": true,
  39. "disableFallback": true
  40. }`,
  41. Parser: parserCreator(),
  42. Output: &dns.Config{
  43. NameServer: []*dns.NameServer{
  44. {
  45. Address: &net.Endpoint{
  46. Address: &net.IPOrDomain{
  47. Address: &net.IPOrDomain_Ip{
  48. Ip: []byte{8, 8, 8, 8},
  49. },
  50. },
  51. Network: net.Network_UDP,
  52. Port: 5353,
  53. },
  54. SkipFallback: true,
  55. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  56. {
  57. Type: dns.DomainMatchingType_Subdomain,
  58. Domain: "example.com",
  59. },
  60. },
  61. OriginalRules: []*dns.NameServer_OriginalRule{
  62. {
  63. Rule: "domain:example.com",
  64. Size: 1,
  65. },
  66. },
  67. },
  68. },
  69. StaticHosts: []*dns.Config_HostMapping{
  70. {
  71. Type: dns.DomainMatchingType_Subdomain,
  72. Domain: "example.com",
  73. ProxiedDomain: "google.com",
  74. },
  75. {
  76. Type: dns.DomainMatchingType_Full,
  77. Domain: "example.com",
  78. Ip: [][]byte{{127, 0, 0, 1}},
  79. },
  80. {
  81. Type: dns.DomainMatchingType_Keyword,
  82. Domain: "google",
  83. Ip: [][]byte{{8, 8, 8, 8}, {8, 8, 4, 4}},
  84. },
  85. {
  86. Type: dns.DomainMatchingType_Regex,
  87. Domain: ".*\\.com",
  88. Ip: [][]byte{{8, 8, 4, 4}},
  89. },
  90. {
  91. Type: dns.DomainMatchingType_Full,
  92. Domain: "www.example.org",
  93. Ip: [][]byte{{127, 0, 0, 1}, {127, 0, 0, 2}},
  94. },
  95. },
  96. ClientIp: []byte{10, 0, 0, 1},
  97. QueryStrategy: dns.QueryStrategy_USE_IP4,
  98. DisableCache: true,
  99. DisableFallback: true,
  100. },
  101. },
  102. })
  103. }