dns_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package conf_test
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/golang/protobuf/proto"
  8. "v2ray.com/core/app/dns"
  9. "v2ray.com/core/app/router"
  10. "v2ray.com/core/common"
  11. "v2ray.com/core/common/net"
  12. "v2ray.com/core/common/platform"
  13. "v2ray.com/core/common/platform/filesystem"
  14. . "v2ray.com/core/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:v2ray.com"]
  64. }],
  65. "hosts": {
  66. "v2ray.com": "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. }`,
  74. Parser: parserCreator(),
  75. Output: &dns.Config{
  76. NameServer: []*dns.NameServer{
  77. {
  78. Address: &net.Endpoint{
  79. Address: &net.IPOrDomain{
  80. Address: &net.IPOrDomain_Ip{
  81. Ip: []byte{8, 8, 8, 8},
  82. },
  83. },
  84. Network: net.Network_UDP,
  85. Port: 5353,
  86. },
  87. ClientIp: []byte{10, 0, 0, 1},
  88. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  89. {
  90. Type: dns.DomainMatchingType_Subdomain,
  91. Domain: "v2ray.com",
  92. },
  93. },
  94. OriginalRules: []*dns.NameServer_OriginalRule{
  95. {
  96. Rule: "domain:v2ray.com",
  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: "example.com",
  111. Ip: [][]byte{{10, 0, 0, 1}},
  112. },
  113. {
  114. Type: dns.DomainMatchingType_Keyword,
  115. Domain: "google",
  116. Ip: [][]byte{{8, 8, 8, 8}},
  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: "v2ray.com",
  126. Ip: [][]byte{{127, 0, 0, 1}},
  127. },
  128. },
  129. ClientIp: []byte{10, 0, 0, 1},
  130. },
  131. },
  132. })
  133. }