dns_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package dns_test
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/testassist"
  6. "io/fs"
  7. "os"
  8. "path/filepath"
  9. "testing"
  10. "google.golang.org/protobuf/runtime/protoiface"
  11. "github.com/v2fly/v2ray-core/v4/app/dns"
  12. "github.com/v2fly/v2ray-core/v4/common"
  13. "github.com/v2fly/v2ray-core/v4/common/net"
  14. "github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
  15. _ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/standard"
  16. dns2 "github.com/v2fly/v2ray-core/v4/infra/conf/synthetic/dns"
  17. )
  18. func init() {
  19. const (
  20. geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
  21. geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
  22. )
  23. wd, err := os.Getwd()
  24. common.Must(err)
  25. tempPath := filepath.Join(wd, "..", "..", "..", "..", "testing", "temp")
  26. geoipPath := filepath.Join(tempPath, "geoip.dat")
  27. geositePath := filepath.Join(tempPath, "geosite.dat")
  28. os.Setenv("v2ray.location.asset", tempPath)
  29. if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
  30. common.Must(os.MkdirAll(tempPath, 0o755))
  31. geoipBytes, err := common.FetchHTTPContent(geoipURL)
  32. common.Must(err)
  33. common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
  34. }
  35. if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) {
  36. common.Must(os.MkdirAll(tempPath, 0o755))
  37. geositeBytes, err := common.FetchHTTPContent(geositeURL)
  38. common.Must(err)
  39. common.Must(filesystem.WriteFile(geositePath, geositeBytes))
  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(dns2.DNSConfig)
  46. if err := json.Unmarshal([]byte(s), config); err != nil {
  47. return nil, err
  48. }
  49. return config.Build()
  50. }
  51. }
  52. testassist.RunMultiTestCase(t, []testassist.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.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. }