dns_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package conf_test
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/fs"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "google.golang.org/protobuf/runtime/protoiface"
  10. "github.com/v2fly/v2ray-core/v4/app/dns"
  11. "github.com/v2fly/v2ray-core/v4/common"
  12. "github.com/v2fly/v2ray-core/v4/common/net"
  13. "github.com/v2fly/v2ray-core/v4/common/platform"
  14. "github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
  15. "github.com/v2fly/v2ray-core/v4/infra/conf"
  16. )
  17. func init() {
  18. wd, err := os.Getwd()
  19. common.Must(err)
  20. tempPath := filepath.Join(wd, "..", "..", "testing", "temp")
  21. os.Setenv("v2ray.location.asset", tempPath)
  22. geoipPath := platform.GetAssetLocation("geoip.dat")
  23. geositePath := platform.GetAssetLocation("geosite.dat")
  24. if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
  25. common.Must(os.MkdirAll(tempPath, 0755))
  26. geoipBytes, err := common.FetchHTTPContent(geoipURL)
  27. common.Must(err)
  28. common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
  29. }
  30. if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) {
  31. common.Must(os.MkdirAll(tempPath, 0755))
  32. geositeBytes, err := common.FetchHTTPContent(geositeURL)
  33. common.Must(err)
  34. common.Must(filesystem.WriteFile(geositePath, geositeBytes))
  35. }
  36. }
  37. func TestDNSConfigParsing(t *testing.T) {
  38. parserCreator := func() func(string) (protoiface.MessageV1, error) {
  39. return func(s string) (protoiface.MessageV1, error) {
  40. config := new(conf.DNSConfig)
  41. if err := json.Unmarshal([]byte(s), config); err != nil {
  42. return nil, err
  43. }
  44. return config.Build()
  45. }
  46. }
  47. runMultiTestCase(t, []TestCase{
  48. {
  49. Input: `{
  50. "servers": [{
  51. "address": "8.8.8.8",
  52. "clientIp": "10.0.0.1",
  53. "port": 5353,
  54. "skipFallback": true,
  55. "domains": ["domain:v2fly.org"]
  56. }],
  57. "hosts": {
  58. "v2fly.org": "127.0.0.1",
  59. "www.v2fly.org": ["1.2.3.4", "5.6.7.8"],
  60. "domain:example.com": "google.com",
  61. "geosite:test": ["127.0.0.1", "127.0.0.2"],
  62. "keyword:google": ["8.8.8.8", "8.8.4.4"],
  63. "regexp:.*\\.com": "8.8.4.4"
  64. },
  65. "clientIp": "10.0.0.1",
  66. "queryStrategy": "UseIPv4",
  67. "disableCache": true,
  68. "disableFallback": true
  69. }`,
  70. Parser: parserCreator(),
  71. Output: &dns.Config{
  72. NameServer: []*dns.NameServer{
  73. {
  74. Address: &net.Endpoint{
  75. Address: &net.IPOrDomain{
  76. Address: &net.IPOrDomain_Ip{
  77. Ip: []byte{8, 8, 8, 8},
  78. },
  79. },
  80. Network: net.Network_UDP,
  81. Port: 5353,
  82. },
  83. ClientIp: []byte{10, 0, 0, 1},
  84. SkipFallback: true,
  85. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  86. {
  87. Type: dns.DomainMatchingType_Subdomain,
  88. Domain: "v2fly.org",
  89. },
  90. },
  91. OriginalRules: []*dns.NameServer_OriginalRule{
  92. {
  93. Rule: "domain:v2fly.org",
  94. Size: 1,
  95. },
  96. },
  97. },
  98. },
  99. StaticHosts: []*dns.Config_HostMapping{
  100. {
  101. Type: dns.DomainMatchingType_Subdomain,
  102. Domain: "example.com",
  103. ProxiedDomain: "google.com",
  104. },
  105. {
  106. Type: dns.DomainMatchingType_Full,
  107. Domain: "test.example.com",
  108. Ip: [][]byte{{127, 0, 0, 1}, {127, 0, 0, 2}},
  109. },
  110. {
  111. Type: dns.DomainMatchingType_Keyword,
  112. Domain: "google",
  113. Ip: [][]byte{{8, 8, 8, 8}, {8, 8, 4, 4}},
  114. },
  115. {
  116. Type: dns.DomainMatchingType_Regex,
  117. Domain: ".*\\.com",
  118. Ip: [][]byte{{8, 8, 4, 4}},
  119. },
  120. {
  121. Type: dns.DomainMatchingType_Full,
  122. Domain: "v2fly.org",
  123. Ip: [][]byte{{127, 0, 0, 1}},
  124. },
  125. {
  126. Type: dns.DomainMatchingType_Full,
  127. Domain: "www.v2fly.org",
  128. Ip: [][]byte{{1, 2, 3, 4}, {5, 6, 7, 8}},
  129. },
  130. },
  131. ClientIp: []byte{10, 0, 0, 1},
  132. QueryStrategy: dns.QueryStrategy_USE_IP4,
  133. DisableCache: true,
  134. DisableFallback: true,
  135. },
  136. },
  137. })
  138. }