dns_test.go 4.1 KB

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