dns_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/xtls/xray-core/app/dns"
  9. "github.com/xtls/xray-core/app/router"
  10. "github.com/xtls/xray-core/common"
  11. "github.com/xtls/xray-core/common/net"
  12. "github.com/xtls/xray-core/common/platform"
  13. "github.com/xtls/xray-core/common/platform/filesystem"
  14. . "github.com/xtls/xray-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, "..", "..", "resources", "geoip.dat")))
  21. }
  22. geositeFilePath := filepath.Join(wd, "geosite.dat")
  23. os.Setenv("xray.location.asset", wd)
  24. geositeFile, err := os.OpenFile(geositeFilePath, os.O_CREATE|os.O_WRONLY, 0o600)
  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("xray.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. "port": 5353,
  62. "skipFallback": true,
  63. "domains": ["domain:example.com"]
  64. }],
  65. "hosts": {
  66. "domain:example.com": "google.com",
  67. "example.com": "127.0.0.1",
  68. "keyword:google": ["8.8.8.8", "8.8.4.4"],
  69. "regexp:.*\\.com": "8.8.4.4",
  70. "www.example.org": ["127.0.0.1", "127.0.0.2"]
  71. },
  72. "clientIp": "10.0.0.1",
  73. "queryStrategy": "UseIPv4",
  74. "disableCache": true,
  75. "disableFallback": true
  76. }`,
  77. Parser: parserCreator(),
  78. Output: &dns.Config{
  79. NameServer: []*dns.NameServer{
  80. {
  81. Address: &net.Endpoint{
  82. Address: &net.IPOrDomain{
  83. Address: &net.IPOrDomain_Ip{
  84. Ip: []byte{8, 8, 8, 8},
  85. },
  86. },
  87. Network: net.Network_UDP,
  88. Port: 5353,
  89. },
  90. SkipFallback: true,
  91. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  92. {
  93. Type: dns.DomainMatchingType_Subdomain,
  94. Domain: "example.com",
  95. },
  96. },
  97. OriginalRules: []*dns.NameServer_OriginalRule{
  98. {
  99. Rule: "domain:example.com",
  100. Size: 1,
  101. },
  102. },
  103. },
  104. },
  105. StaticHosts: []*dns.Config_HostMapping{
  106. {
  107. Type: dns.DomainMatchingType_Subdomain,
  108. Domain: "example.com",
  109. ProxiedDomain: "google.com",
  110. },
  111. {
  112. Type: dns.DomainMatchingType_Full,
  113. Domain: "example.com",
  114. Ip: [][]byte{{127, 0, 0, 1}},
  115. },
  116. {
  117. Type: dns.DomainMatchingType_Keyword,
  118. Domain: "google",
  119. Ip: [][]byte{{8, 8, 8, 8}, {8, 8, 4, 4}},
  120. },
  121. {
  122. Type: dns.DomainMatchingType_Regex,
  123. Domain: ".*\\.com",
  124. Ip: [][]byte{{8, 8, 4, 4}},
  125. },
  126. {
  127. Type: dns.DomainMatchingType_Full,
  128. Domain: "www.example.org",
  129. Ip: [][]byte{{127, 0, 0, 1}, {127, 0, 0, 2}},
  130. },
  131. },
  132. ClientIp: []byte{10, 0, 0, 1},
  133. QueryStrategy: dns.QueryStrategy_USE_IP4,
  134. DisableCache: true,
  135. DisableFallback: true,
  136. },
  137. },
  138. })
  139. }