dns_test.go 4.0 KB

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