dns_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, 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("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. "domains": ["domain:example.com"]
  63. }],
  64. "hosts": {
  65. "example.com": "127.0.0.1",
  66. "domain:example.com": "google.com",
  67. "geosite:test": "10.0.0.1",
  68. "keyword:google": "8.8.8.8",
  69. "regexp:.*\\.com": "8.8.4.4"
  70. },
  71. "clientIp": "10.0.0.1"
  72. }`,
  73. Parser: parserCreator(),
  74. Output: &dns.Config{
  75. NameServer: []*dns.NameServer{
  76. {
  77. Address: &net.Endpoint{
  78. Address: &net.IPOrDomain{
  79. Address: &net.IPOrDomain_Ip{
  80. Ip: []byte{8, 8, 8, 8},
  81. },
  82. },
  83. Network: net.Network_UDP,
  84. Port: 5353,
  85. },
  86. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  87. {
  88. Type: dns.DomainMatchingType_Subdomain,
  89. Domain: "example.com",
  90. },
  91. },
  92. OriginalRules: []*dns.NameServer_OriginalRule{
  93. {
  94. Rule: "domain:example.com",
  95. Size: 1,
  96. },
  97. },
  98. },
  99. },
  100. StaticHosts: []*dns.Config_HostMapping{
  101. {
  102. Type: dns.DomainMatchingType_Subdomain,
  103. Domain: "example.com",
  104. ProxiedDomain: "google.com",
  105. },
  106. {
  107. Type: dns.DomainMatchingType_Full,
  108. Domain: "example.com",
  109. Ip: [][]byte{{127, 0, 0, 1}},
  110. },
  111. {
  112. Type: dns.DomainMatchingType_Full,
  113. Domain: "example.com",
  114. Ip: [][]byte{{10, 0, 0, 1}},
  115. },
  116. {
  117. Type: dns.DomainMatchingType_Keyword,
  118. Domain: "google",
  119. Ip: [][]byte{{8, 8, 8, 8}},
  120. },
  121. {
  122. Type: dns.DomainMatchingType_Regex,
  123. Domain: ".*\\.com",
  124. Ip: [][]byte{{8, 8, 4, 4}},
  125. },
  126. },
  127. ClientIp: []byte{10, 0, 0, 1},
  128. },
  129. },
  130. })
  131. }