dns_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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("geosite.dat")); err != nil && os.IsNotExist(err) {
  20. common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "release", "config", "geosite.dat")))
  21. }
  22. geositeFilePath := platform.GetAssetLocation("geosite.dat")
  23. geositeFile, err := os.OpenFile(geositeFilePath, os.O_CREATE|os.O_WRONLY, 0600)
  24. common.Must(err)
  25. defer geositeFile.Close()
  26. list := &router.GeoSiteList{
  27. Entry: []*router.GeoSite{
  28. {
  29. CountryCode: "TEST",
  30. Domain: []*router.Domain{
  31. {Type: router.Domain_Full, Value: "example.com"},
  32. },
  33. },
  34. },
  35. }
  36. listBytes, err := proto.Marshal(list)
  37. common.Must(err)
  38. common.Must2(geositeFile.Write(listBytes))
  39. }
  40. func TestDnsConfigParsing(t *testing.T) {
  41. geositePath := platform.GetAssetLocation("geosite.dat")
  42. defer func() {
  43. os.Remove(geositePath)
  44. }()
  45. parserCreator := func() func(string) (proto.Message, error) {
  46. return func(s string) (proto.Message, error) {
  47. config := new(DnsConfig)
  48. if err := json.Unmarshal([]byte(s), config); err != nil {
  49. return nil, err
  50. }
  51. return config.Build()
  52. }
  53. }
  54. runMultiTestCase(t, []TestCase{
  55. {
  56. Input: `{
  57. "servers": [{
  58. "address": "8.8.8.8",
  59. "port": 5353,
  60. "domains": ["domain:v2ray.com"]
  61. }],
  62. "hosts": {
  63. "v2ray.com": "127.0.0.1",
  64. "domain:example.com": "google.com",
  65. "geosite:test": "10.0.0.1",
  66. "keyword:google": "8.8.8.8",
  67. "regexp:.*\\.com": "8.8.4.4"
  68. },
  69. "clientIp": "10.0.0.1"
  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. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  85. {
  86. Type: dns.DomainMatchingType_Subdomain,
  87. Domain: "v2ray.com",
  88. },
  89. },
  90. OriginalRules: []*dns.NameServer_OriginalRule{
  91. {
  92. Rule: "domain:v2ray.com",
  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: "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: "v2ray.com",
  122. Ip: [][]byte{{127, 0, 0, 1}},
  123. },
  124. },
  125. ClientIp: []byte{10, 0, 0, 1},
  126. },
  127. },
  128. })
  129. }