dns_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package conf_test
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/v2fly/v2ray-core/v4/app/dns"
  9. "github.com/v2fly/v2ray-core/v4/app/router"
  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. if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
  20. common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
  21. }
  22. geositeFilePath := filepath.Join(wd, "geosite.dat")
  23. os.Setenv("v2ray.location.asset", wd)
  24. geositeFile, err := os.OpenFile(geositeFilePath, os.O_CREATE|os.O_WRONLY, 0600)
  25. common.Must(err)
  26. defer geositeFile.Close()
  27. list := &router.GeoSiteList{
  28. Entry: []*router.GeoSite{
  29. {
  30. CountryCode: "TEST",
  31. Domain: []*router.Domain{
  32. {Type: router.Domain_Full, Value: "example.com"},
  33. },
  34. },
  35. },
  36. }
  37. listBytes, err := proto.Marshal(list)
  38. common.Must(err)
  39. common.Must2(geositeFile.Write(listBytes))
  40. }
  41. func TestDNSConfigParsing(t *testing.T) {
  42. geositePath := platform.GetAssetLocation("geosite.dat")
  43. defer func() {
  44. os.Remove(geositePath)
  45. os.Unsetenv("v2ray.location.asset")
  46. }()
  47. parserCreator := func() func(string) (proto.Message, error) {
  48. return func(s string) (proto.Message, error) {
  49. config := new(DNSConfig)
  50. if err := json.Unmarshal([]byte(s), config); err != nil {
  51. return nil, err
  52. }
  53. return config.Build()
  54. }
  55. }
  56. runMultiTestCase(t, []TestCase{
  57. {
  58. Input: `{
  59. "servers": [{
  60. "address": "8.8.8.8",
  61. "clientIp": "10.0.0.1",
  62. "port": 5353,
  63. "domains": ["domain:v2fly.org"]
  64. }],
  65. "hosts": {
  66. "v2fly.org": "127.0.0.1",
  67. "domain:example.com": "google.com",
  68. "geosite:test": "10.0.0.1",
  69. "keyword:google": "8.8.8.8",
  70. "regexp:.*\\.com": "8.8.4.4"
  71. },
  72. "clientIp": "10.0.0.1",
  73. "queryStrategy": "UseIPv4",
  74. "disableCache": true
  75. }`,
  76. Parser: parserCreator(),
  77. Output: &dns.Config{
  78. NameServer: []*dns.NameServer{
  79. {
  80. Address: &net.Endpoint{
  81. Address: &net.IPOrDomain{
  82. Address: &net.IPOrDomain_Ip{
  83. Ip: []byte{8, 8, 8, 8},
  84. },
  85. },
  86. Network: net.Network_UDP,
  87. Port: 5353,
  88. },
  89. ClientIp: []byte{10, 0, 0, 1},
  90. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  91. {
  92. Type: dns.DomainMatchingType_Subdomain,
  93. Domain: "v2fly.org",
  94. },
  95. },
  96. OriginalRules: []*dns.NameServer_OriginalRule{
  97. {
  98. Rule: "domain:v2fly.org",
  99. Size: 1,
  100. },
  101. },
  102. },
  103. },
  104. StaticHosts: []*dns.Config_HostMapping{
  105. {
  106. Type: dns.DomainMatchingType_Subdomain,
  107. Domain: "example.com",
  108. ProxiedDomain: "google.com",
  109. },
  110. {
  111. Type: dns.DomainMatchingType_Full,
  112. Domain: "example.com",
  113. Ip: [][]byte{{10, 0, 0, 1}},
  114. },
  115. {
  116. Type: dns.DomainMatchingType_Keyword,
  117. Domain: "google",
  118. Ip: [][]byte{{8, 8, 8, 8}},
  119. },
  120. {
  121. Type: dns.DomainMatchingType_Regex,
  122. Domain: ".*\\.com",
  123. Ip: [][]byte{{8, 8, 4, 4}},
  124. },
  125. {
  126. Type: dns.DomainMatchingType_Full,
  127. Domain: "v2fly.org",
  128. Ip: [][]byte{{127, 0, 0, 1}},
  129. },
  130. },
  131. ClientIp: []byte{10, 0, 0, 1},
  132. QueryStrategy: dns.QueryStrategy_USE_IP4,
  133. DisableCache: true,
  134. },
  135. },
  136. })
  137. }