dns_test.go 3.7 KB

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