dns_test.go 4.0 KB

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