dns_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
  20. geositeFilePath := platform.GetAssetLocation("geosite.dat")
  21. geositeFile, err := os.OpenFile(geositeFilePath, os.O_CREATE|os.O_WRONLY, 0600)
  22. common.Must(err)
  23. defer geositeFile.Close()
  24. list := &router.GeoSiteList{
  25. Entry: []*router.GeoSite{
  26. {
  27. CountryCode: "TEST",
  28. Domain: []*router.Domain{
  29. {Type: router.Domain_Full, Value: "example.com"},
  30. },
  31. },
  32. },
  33. }
  34. listBytes, err := proto.Marshal(list)
  35. common.Must(err)
  36. common.Must2(geositeFile.Write(listBytes))
  37. }
  38. func TestDnsConfigParsing(t *testing.T) {
  39. geositePath := platform.GetAssetLocation("geosite.dat")
  40. defer func() {
  41. os.Remove(geositePath)
  42. }()
  43. parserCreator := func() func(string) (proto.Message, error) {
  44. return func(s string) (proto.Message, error) {
  45. config := new(DnsConfig)
  46. if err := json.Unmarshal([]byte(s), config); err != nil {
  47. return nil, err
  48. }
  49. return config.Build()
  50. }
  51. }
  52. runMultiTestCase(t, []TestCase{
  53. {
  54. Input: `{
  55. "servers": [{
  56. "address": "8.8.8.8",
  57. "port": 5353,
  58. "domains": ["domain:v2ray.com"]
  59. }],
  60. "hosts": {
  61. "v2ray.com": "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. }`,
  69. Parser: parserCreator(),
  70. Output: &dns.Config{
  71. NameServer: []*dns.NameServer{
  72. {
  73. Address: &net.Endpoint{
  74. Address: &net.IPOrDomain{
  75. Address: &net.IPOrDomain_Ip{
  76. Ip: []byte{8, 8, 8, 8},
  77. },
  78. },
  79. Network: net.Network_UDP,
  80. Port: 5353,
  81. },
  82. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  83. {
  84. Type: dns.DomainMatchingType_Subdomain,
  85. Domain: "v2ray.com",
  86. },
  87. },
  88. OriginalRules: []*dns.NameServer_OriginalRule{
  89. {
  90. Rule: "domain:v2ray.com",
  91. Size: 1,
  92. },
  93. },
  94. },
  95. },
  96. StaticHosts: []*dns.Config_HostMapping{
  97. {
  98. Type: dns.DomainMatchingType_Subdomain,
  99. Domain: "example.com",
  100. ProxiedDomain: "google.com",
  101. },
  102. {
  103. Type: dns.DomainMatchingType_Full,
  104. Domain: "example.com",
  105. Ip: [][]byte{{10, 0, 0, 1}},
  106. },
  107. {
  108. Type: dns.DomainMatchingType_Keyword,
  109. Domain: "google",
  110. Ip: [][]byte{{8, 8, 8, 8}},
  111. },
  112. {
  113. Type: dns.DomainMatchingType_Regex,
  114. Domain: ".*\\.com",
  115. Ip: [][]byte{{8, 8, 4, 4}},
  116. },
  117. {
  118. Type: dns.DomainMatchingType_Full,
  119. Domain: "v2ray.com",
  120. Ip: [][]byte{{127, 0, 0, 1}},
  121. },
  122. },
  123. ClientIp: []byte{10, 0, 0, 1},
  124. },
  125. },
  126. })
  127. }