dns_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package dns_test
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/fs"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "google.golang.org/protobuf/runtime/protoiface"
  10. "github.com/v2fly/v2ray-core/v5/app/dns"
  11. "github.com/v2fly/v2ray-core/v5/app/dns/fakedns"
  12. "github.com/v2fly/v2ray-core/v5/common"
  13. "github.com/v2fly/v2ray-core/v5/common/net"
  14. "github.com/v2fly/v2ray-core/v5/common/platform/filesystem"
  15. "github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon/testassist"
  16. _ "github.com/v2fly/v2ray-core/v5/infra/conf/geodata/standard"
  17. dns2 "github.com/v2fly/v2ray-core/v5/infra/conf/synthetic/dns"
  18. )
  19. func init() {
  20. const (
  21. geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
  22. geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
  23. )
  24. wd, err := os.Getwd()
  25. common.Must(err)
  26. tempPath := filepath.Join(wd, "..", "..", "..", "..", "testing", "temp")
  27. geoipPath := filepath.Join(tempPath, "geoip.dat")
  28. geositePath := filepath.Join(tempPath, "geosite.dat")
  29. os.Setenv("v2ray.location.asset", tempPath)
  30. if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
  31. common.Must(os.MkdirAll(tempPath, 0o755))
  32. geoipBytes, err := common.FetchHTTPContent(geoipURL)
  33. common.Must(err)
  34. common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
  35. }
  36. if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) {
  37. common.Must(os.MkdirAll(tempPath, 0o755))
  38. geositeBytes, err := common.FetchHTTPContent(geositeURL)
  39. common.Must(err)
  40. common.Must(filesystem.WriteFile(geositePath, geositeBytes))
  41. }
  42. }
  43. func TestDNSConfigParsing(t *testing.T) {
  44. parserCreator := func() func(string) (protoiface.MessageV1, error) {
  45. return func(s string) (protoiface.MessageV1, error) {
  46. config := new(dns2.DNSConfig)
  47. if err := json.Unmarshal([]byte(s), config); err != nil {
  48. return nil, err
  49. }
  50. return config.Build()
  51. }
  52. }
  53. testassist.RunMultiTestCase(t, []testassist.TestCase{
  54. {
  55. Input: `{
  56. "servers": [{
  57. "address": "8.8.8.8",
  58. "clientIp": "10.0.0.1",
  59. "port": 5353,
  60. "skipFallback": true,
  61. "domains": ["domain:v2fly.org"]
  62. }],
  63. "hosts": {
  64. "v2fly.org": "127.0.0.1",
  65. "www.v2fly.org": ["1.2.3.4", "5.6.7.8"],
  66. "domain:example.com": "google.com",
  67. "geosite:test": ["127.0.0.1", "127.0.0.2"],
  68. "keyword:google": ["8.8.8.8", "8.8.4.4"],
  69. "regexp:.*\\.com": "8.8.4.4"
  70. },
  71. "clientIp": "10.0.0.1",
  72. "queryStrategy": "UseIPv4",
  73. "disableCache": true,
  74. "disableFallback": true
  75. }`,
  76. Parser: parserCreator(),
  77. Output: &dns.Config{
  78. NameServer: []*dns.NameServer{
  79. {
  80. Address: &net.Endpoint{
  81. Address: &net.IPOrDomain{
  82. Address: &net.IPOrDomain_Ip{
  83. Ip: []byte{8, 8, 8, 8},
  84. },
  85. },
  86. Network: net.Network_UDP,
  87. Port: 5353,
  88. },
  89. ClientIp: []byte{10, 0, 0, 1},
  90. SkipFallback: true,
  91. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  92. {
  93. Type: dns.DomainMatchingType_Subdomain,
  94. Domain: "v2fly.org",
  95. },
  96. },
  97. OriginalRules: []*dns.NameServer_OriginalRule{
  98. {
  99. Rule: "domain:v2fly.org",
  100. Size: 1,
  101. },
  102. },
  103. },
  104. },
  105. StaticHosts: []*dns.HostMapping{
  106. {
  107. Type: dns.DomainMatchingType_Subdomain,
  108. Domain: "example.com",
  109. ProxiedDomain: "google.com",
  110. },
  111. {
  112. Type: dns.DomainMatchingType_Full,
  113. Domain: "test.example.com",
  114. Ip: [][]byte{{127, 0, 0, 1}, {127, 0, 0, 2}},
  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: "v2fly.org",
  129. Ip: [][]byte{{127, 0, 0, 1}},
  130. },
  131. {
  132. Type: dns.DomainMatchingType_Full,
  133. Domain: "www.v2fly.org",
  134. Ip: [][]byte{{1, 2, 3, 4}, {5, 6, 7, 8}},
  135. },
  136. },
  137. ClientIp: []byte{10, 0, 0, 1},
  138. QueryStrategy: dns.QueryStrategy_USE_IP4,
  139. DisableCache: true,
  140. DisableFallback: true,
  141. },
  142. },
  143. {
  144. Input: `{
  145. "servers": [{
  146. "address": "fakedns",
  147. "tag": "fake",
  148. "queryStrategy": "UseIPv6",
  149. "fallbackStrategy": "disabledIfAnyMatch",
  150. "fakedns": true
  151. }, {
  152. "address": "8.8.8.8",
  153. "port": 5353,
  154. "tag": "local",
  155. "clientIp": "10.0.0.1",
  156. "queryStrategy": "UseIP",
  157. "cacheStrategy": "enabled",
  158. "fallbackStrategy": "disabled",
  159. "domains": ["domain:v2fly.org"],
  160. "fakedns": ["198.19.0.0/16", "fc01::/18"]
  161. }],
  162. "hosts": {
  163. "v2fly.org": "127.0.0.1",
  164. "www.v2fly.org": ["1.2.3.4", "5.6.7.8"],
  165. "domain:example.com": "google.com",
  166. "geosite:test": ["127.0.0.1", "127.0.0.2"],
  167. "keyword:google": ["8.8.8.8", "8.8.4.4"],
  168. "regexp:.*\\.com": "8.8.4.4"
  169. },
  170. "fakedns": [
  171. { "ipPool": "198.18.0.0/16", "poolSize": 32768 },
  172. { "ipPool": "fc00::/18", "poolSize": 32768 }
  173. ],
  174. "tag": "global",
  175. "clientIp": "10.0.0.1",
  176. "queryStrategy": "UseIPv4",
  177. "cacheStrategy": "disabled",
  178. "fallbackStrategy": "enabled"
  179. }`,
  180. Parser: parserCreator(),
  181. Output: &dns.Config{
  182. NameServer: []*dns.NameServer{
  183. {
  184. Address: &net.Endpoint{
  185. Address: &net.IPOrDomain{
  186. Address: &net.IPOrDomain_Domain{
  187. Domain: "fakedns",
  188. },
  189. },
  190. Network: net.Network_UDP,
  191. },
  192. Tag: "fake",
  193. QueryStrategy: dns.QueryStrategy_USE_IP6.Enum(),
  194. FallbackStrategy: dns.FallbackStrategy_DisabledIfAnyMatch.Enum(),
  195. FakeDns: &fakedns.FakeDnsPoolMulti{
  196. Pools: []*fakedns.FakeDnsPool{},
  197. },
  198. },
  199. {
  200. Address: &net.Endpoint{
  201. Address: &net.IPOrDomain{
  202. Address: &net.IPOrDomain_Ip{
  203. Ip: []byte{8, 8, 8, 8},
  204. },
  205. },
  206. Network: net.Network_UDP,
  207. Port: 5353,
  208. },
  209. Tag: "local",
  210. ClientIp: []byte{10, 0, 0, 1},
  211. QueryStrategy: dns.QueryStrategy_USE_IP.Enum(),
  212. CacheStrategy: dns.CacheStrategy_CacheEnabled.Enum(),
  213. FallbackStrategy: dns.FallbackStrategy_Disabled.Enum(),
  214. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  215. {
  216. Type: dns.DomainMatchingType_Subdomain,
  217. Domain: "v2fly.org",
  218. },
  219. },
  220. OriginalRules: []*dns.NameServer_OriginalRule{
  221. {
  222. Rule: "domain:v2fly.org",
  223. Size: 1,
  224. },
  225. },
  226. FakeDns: &fakedns.FakeDnsPoolMulti{
  227. Pools: []*fakedns.FakeDnsPool{
  228. {IpPool: "198.19.0.0/16", LruSize: 65535},
  229. {IpPool: "fc01::/18", LruSize: 65535},
  230. },
  231. },
  232. },
  233. },
  234. StaticHosts: []*dns.HostMapping{
  235. {
  236. Type: dns.DomainMatchingType_Subdomain,
  237. Domain: "example.com",
  238. ProxiedDomain: "google.com",
  239. },
  240. {
  241. Type: dns.DomainMatchingType_Full,
  242. Domain: "test.example.com",
  243. Ip: [][]byte{{127, 0, 0, 1}, {127, 0, 0, 2}},
  244. },
  245. {
  246. Type: dns.DomainMatchingType_Keyword,
  247. Domain: "google",
  248. Ip: [][]byte{{8, 8, 8, 8}, {8, 8, 4, 4}},
  249. },
  250. {
  251. Type: dns.DomainMatchingType_Regex,
  252. Domain: ".*\\.com",
  253. Ip: [][]byte{{8, 8, 4, 4}},
  254. },
  255. {
  256. Type: dns.DomainMatchingType_Full,
  257. Domain: "v2fly.org",
  258. Ip: [][]byte{{127, 0, 0, 1}},
  259. },
  260. {
  261. Type: dns.DomainMatchingType_Full,
  262. Domain: "www.v2fly.org",
  263. Ip: [][]byte{{1, 2, 3, 4}, {5, 6, 7, 8}},
  264. },
  265. },
  266. FakeDns: &fakedns.FakeDnsPoolMulti{
  267. Pools: []*fakedns.FakeDnsPool{
  268. {IpPool: "198.18.0.0/16", LruSize: 32768},
  269. {IpPool: "fc00::/18", LruSize: 32768},
  270. },
  271. },
  272. Tag: "global",
  273. ClientIp: []byte{10, 0, 0, 1},
  274. QueryStrategy: dns.QueryStrategy_USE_IP4,
  275. CacheStrategy: dns.CacheStrategy_CacheDisabled,
  276. FallbackStrategy: dns.FallbackStrategy_Enabled,
  277. },
  278. },
  279. })
  280. }