dns_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "domain:example.com": "google.com",
  63. "geosite:test": "10.0.0.1",
  64. "keyword:google": "8.8.8.8",
  65. "regexp:.*\\.com": "8.8.4.4"
  66. },
  67. "clientIp": "10.0.0.1",
  68. "queryStrategy": "UseIPv4",
  69. "disableCache": true,
  70. "disableFallback": true
  71. }`,
  72. Parser: parserCreator(),
  73. Output: &dns.Config{
  74. NameServer: []*dns.NameServer{
  75. {
  76. Address: &net.Endpoint{
  77. Address: &net.IPOrDomain{
  78. Address: &net.IPOrDomain_Ip{
  79. Ip: []byte{8, 8, 8, 8},
  80. },
  81. },
  82. Network: net.Network_UDP,
  83. Port: 5353,
  84. },
  85. ClientIp: []byte{10, 0, 0, 1},
  86. SkipFallback: true,
  87. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  88. {
  89. Type: dns.DomainMatchingType_Subdomain,
  90. Domain: "v2fly.org",
  91. },
  92. },
  93. OriginalRules: []*dns.NameServer_OriginalRule{
  94. {
  95. Rule: "domain:v2fly.org",
  96. Size: 1,
  97. },
  98. },
  99. },
  100. },
  101. StaticHosts: []*dns.Config_HostMapping{
  102. {
  103. Type: dns.DomainMatchingType_Subdomain,
  104. Domain: "example.com",
  105. ProxiedDomain: "google.com",
  106. },
  107. {
  108. Type: dns.DomainMatchingType_Full,
  109. Domain: "test.example.com",
  110. Ip: [][]byte{{10, 0, 0, 1}},
  111. },
  112. {
  113. Type: dns.DomainMatchingType_Keyword,
  114. Domain: "google",
  115. Ip: [][]byte{{8, 8, 8, 8}},
  116. },
  117. {
  118. Type: dns.DomainMatchingType_Regex,
  119. Domain: ".*\\.com",
  120. Ip: [][]byte{{8, 8, 4, 4}},
  121. },
  122. {
  123. Type: dns.DomainMatchingType_Full,
  124. Domain: "v2fly.org",
  125. Ip: [][]byte{{127, 0, 0, 1}},
  126. },
  127. },
  128. ClientIp: []byte{10, 0, 0, 1},
  129. QueryStrategy: dns.QueryStrategy_USE_IP4,
  130. DisableCache: true,
  131. DisableFallback: true,
  132. },
  133. },
  134. })
  135. }