dns_test.go 3.9 KB

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