dns_test.go 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. package dns_test
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/google/go-cmp/cmp"
  6. "github.com/miekg/dns"
  7. "google.golang.org/protobuf/types/known/anypb"
  8. core "github.com/v2fly/v2ray-core/v5"
  9. "github.com/v2fly/v2ray-core/v5/app/dispatcher"
  10. . "github.com/v2fly/v2ray-core/v5/app/dns"
  11. "github.com/v2fly/v2ray-core/v5/app/policy"
  12. "github.com/v2fly/v2ray-core/v5/app/proxyman"
  13. _ "github.com/v2fly/v2ray-core/v5/app/proxyman/outbound"
  14. "github.com/v2fly/v2ray-core/v5/app/router/routercommon"
  15. "github.com/v2fly/v2ray-core/v5/common"
  16. "github.com/v2fly/v2ray-core/v5/common/net"
  17. "github.com/v2fly/v2ray-core/v5/common/serial"
  18. "github.com/v2fly/v2ray-core/v5/common/strmatcher"
  19. feature_dns "github.com/v2fly/v2ray-core/v5/features/dns"
  20. "github.com/v2fly/v2ray-core/v5/proxy/freedom"
  21. "github.com/v2fly/v2ray-core/v5/testing/servers/udp"
  22. )
  23. type staticHandler struct{}
  24. func (*staticHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
  25. ans := new(dns.Msg)
  26. ans.Id = r.Id
  27. var clientIP net.IP
  28. opt := r.IsEdns0()
  29. if opt != nil {
  30. for _, o := range opt.Option {
  31. if o.Option() == dns.EDNS0SUBNET {
  32. subnet := o.(*dns.EDNS0_SUBNET)
  33. clientIP = subnet.Address
  34. }
  35. }
  36. }
  37. for _, q := range r.Question {
  38. switch {
  39. case q.Name == "google.com." && q.Qtype == dns.TypeA:
  40. if clientIP == nil {
  41. rr, _ := dns.NewRR("google.com. IN A 8.8.8.8")
  42. ans.Answer = append(ans.Answer, rr)
  43. } else {
  44. rr, _ := dns.NewRR("google.com. IN A 8.8.4.4")
  45. ans.Answer = append(ans.Answer, rr)
  46. }
  47. case q.Name == "api.google.com." && q.Qtype == dns.TypeA:
  48. rr, _ := dns.NewRR("api.google.com. IN A 8.8.7.7")
  49. ans.Answer = append(ans.Answer, rr)
  50. case q.Name == "v2.api.google.com." && q.Qtype == dns.TypeA:
  51. rr, _ := dns.NewRR("v2.api.google.com. IN A 8.8.7.8")
  52. ans.Answer = append(ans.Answer, rr)
  53. case q.Name == "facebook.com." && q.Qtype == dns.TypeA:
  54. rr, _ := dns.NewRR("facebook.com. IN A 9.9.9.9")
  55. ans.Answer = append(ans.Answer, rr)
  56. case q.Name == "ipv6.google.com." && q.Qtype == dns.TypeA:
  57. rr, err := dns.NewRR("ipv6.google.com. IN A 8.8.8.7")
  58. common.Must(err)
  59. ans.Answer = append(ans.Answer, rr)
  60. case q.Name == "ipv6.google.com." && q.Qtype == dns.TypeAAAA:
  61. rr, err := dns.NewRR("ipv6.google.com. IN AAAA 2001:4860:4860::8888")
  62. common.Must(err)
  63. ans.Answer = append(ans.Answer, rr)
  64. case q.Name == "notexist.google.com." && q.Qtype == dns.TypeAAAA:
  65. ans.MsgHdr.Rcode = dns.RcodeNameError
  66. case q.Name == "hostname." && q.Qtype == dns.TypeA:
  67. rr, _ := dns.NewRR("hostname. IN A 127.0.0.1")
  68. ans.Answer = append(ans.Answer, rr)
  69. case q.Name == "hostname.local." && q.Qtype == dns.TypeA:
  70. rr, _ := dns.NewRR("hostname.local. IN A 127.0.0.1")
  71. ans.Answer = append(ans.Answer, rr)
  72. case q.Name == "hostname.localdomain." && q.Qtype == dns.TypeA:
  73. rr, _ := dns.NewRR("hostname.localdomain. IN A 127.0.0.1")
  74. ans.Answer = append(ans.Answer, rr)
  75. case q.Name == "localhost." && q.Qtype == dns.TypeA:
  76. rr, _ := dns.NewRR("localhost. IN A 127.0.0.2")
  77. ans.Answer = append(ans.Answer, rr)
  78. case q.Name == "localhost-a." && q.Qtype == dns.TypeA:
  79. rr, _ := dns.NewRR("localhost-a. IN A 127.0.0.3")
  80. ans.Answer = append(ans.Answer, rr)
  81. case q.Name == "localhost-b." && q.Qtype == dns.TypeA:
  82. rr, _ := dns.NewRR("localhost-b. IN A 127.0.0.4")
  83. ans.Answer = append(ans.Answer, rr)
  84. case q.Name == "Mijia\\ Cloud." && q.Qtype == dns.TypeA:
  85. rr, _ := dns.NewRR("Mijia\\ Cloud. IN A 127.0.0.1")
  86. ans.Answer = append(ans.Answer, rr)
  87. case q.Name == "xn--vi8h.ws." /* 🍕.ws */ && q.Qtype == dns.TypeA:
  88. rr, err := dns.NewRR("xn--vi8h.ws. IN A 208.100.42.200")
  89. common.Must(err)
  90. ans.Answer = append(ans.Answer, rr)
  91. case q.Name == "xn--l8jaaa.com." /* ああああ.com */ && q.Qtype == dns.TypeA:
  92. rr, err := dns.NewRR("xn--l8jaaa.com. IN AAAA a:a:a:a::aaaa")
  93. common.Must(err)
  94. ans.Answer = append(ans.Answer, rr)
  95. }
  96. }
  97. w.WriteMsg(ans)
  98. }
  99. func TestUDPServerSubnet(t *testing.T) {
  100. port := udp.PickPort()
  101. dnsServer := dns.Server{
  102. Addr: "127.0.0.1:" + port.String(),
  103. Net: "udp",
  104. Handler: &staticHandler{},
  105. UDPSize: 1200,
  106. }
  107. go dnsServer.ListenAndServe()
  108. time.Sleep(time.Second)
  109. config := &core.Config{
  110. App: []*anypb.Any{
  111. serial.ToTypedMessage(&Config{
  112. NameServers: []*net.Endpoint{
  113. {
  114. Network: net.Network_UDP,
  115. Address: &net.IPOrDomain{
  116. Address: &net.IPOrDomain_Ip{
  117. Ip: []byte{127, 0, 0, 1},
  118. },
  119. },
  120. Port: uint32(port),
  121. },
  122. },
  123. ClientIp: []byte{7, 8, 9, 10},
  124. }),
  125. serial.ToTypedMessage(&dispatcher.Config{}),
  126. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  127. serial.ToTypedMessage(&policy.Config{}),
  128. },
  129. Outbound: []*core.OutboundHandlerConfig{
  130. {
  131. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  132. },
  133. },
  134. }
  135. v, err := core.New(config)
  136. common.Must(err)
  137. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  138. ips, err := client.LookupIP("google.com")
  139. if err != nil {
  140. t.Fatal("unexpected error: ", err)
  141. }
  142. if r := cmp.Diff(ips, []net.IP{{8, 8, 4, 4}}); r != "" {
  143. t.Fatal(r)
  144. }
  145. }
  146. func TestUDPServer(t *testing.T) {
  147. port := udp.PickPort()
  148. dnsServer := dns.Server{
  149. Addr: "127.0.0.1:" + port.String(),
  150. Net: "udp",
  151. Handler: &staticHandler{},
  152. UDPSize: 1200,
  153. }
  154. go dnsServer.ListenAndServe()
  155. time.Sleep(time.Second)
  156. config := &core.Config{
  157. App: []*anypb.Any{
  158. serial.ToTypedMessage(&Config{
  159. NameServers: []*net.Endpoint{
  160. {
  161. Network: net.Network_UDP,
  162. Address: &net.IPOrDomain{
  163. Address: &net.IPOrDomain_Ip{
  164. Ip: []byte{127, 0, 0, 1},
  165. },
  166. },
  167. Port: uint32(port),
  168. },
  169. },
  170. }),
  171. serial.ToTypedMessage(&dispatcher.Config{}),
  172. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  173. serial.ToTypedMessage(&policy.Config{}),
  174. },
  175. Outbound: []*core.OutboundHandlerConfig{
  176. {
  177. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  178. },
  179. },
  180. }
  181. v, err := core.New(config)
  182. common.Must(err)
  183. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  184. {
  185. ips, err := client.LookupIP("google.com")
  186. if err != nil {
  187. t.Fatal("unexpected error: ", err)
  188. }
  189. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
  190. t.Fatal(r)
  191. }
  192. }
  193. {
  194. ips, err := client.LookupIP("facebook.com")
  195. if err != nil {
  196. t.Fatal("unexpected error: ", err)
  197. }
  198. if r := cmp.Diff(ips, []net.IP{{9, 9, 9, 9}}); r != "" {
  199. t.Fatal(r)
  200. }
  201. }
  202. {
  203. _, err := client.LookupIP("notexist.google.com")
  204. if err == nil {
  205. t.Fatal("nil error")
  206. }
  207. if r := feature_dns.RCodeFromError(err); r != uint16(dns.RcodeNameError) {
  208. t.Fatal("expected NameError, but got ", r)
  209. }
  210. }
  211. {
  212. clientv6 := client.(feature_dns.IPv6Lookup)
  213. ips, err := clientv6.LookupIPv6("ipv4only.google.com")
  214. if err != feature_dns.ErrEmptyResponse {
  215. t.Fatal("error: ", err)
  216. }
  217. if len(ips) != 0 {
  218. t.Fatal("ips: ", ips)
  219. }
  220. }
  221. {
  222. ips, err := client.LookupIP(common.Must2(strmatcher.ToDomain("🍕.ws")).(string))
  223. if err != nil {
  224. t.Fatal("unexpected error: ", err)
  225. }
  226. if r := cmp.Diff(ips, []net.IP{{208, 100, 42, 200}}); r != "" {
  227. t.Fatal(r)
  228. }
  229. }
  230. {
  231. ips, err := client.LookupIP(common.Must2(strmatcher.ToDomain("ああああ.com")).(string))
  232. if err != nil {
  233. t.Fatal("unexpected error: ", err)
  234. }
  235. if r := cmp.Diff(ips, []net.IP{{0, 0xa, 0, 0xa, 0, 0xa, 0, 0xa, 0, 0, 0, 0, 0, 0, 0xaa, 0xaa}}); r != "" {
  236. t.Fatal(r)
  237. }
  238. }
  239. dnsServer.Shutdown()
  240. {
  241. ips, err := client.LookupIP("google.com")
  242. if err != nil {
  243. t.Fatal("unexpected error: ", err)
  244. }
  245. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
  246. t.Fatal(r)
  247. }
  248. }
  249. }
  250. func TestPrioritizedDomain(t *testing.T) {
  251. port := udp.PickPort()
  252. dnsServer := dns.Server{
  253. Addr: "127.0.0.1:" + port.String(),
  254. Net: "udp",
  255. Handler: &staticHandler{},
  256. UDPSize: 1200,
  257. }
  258. go dnsServer.ListenAndServe()
  259. time.Sleep(time.Second)
  260. config := &core.Config{
  261. App: []*anypb.Any{
  262. serial.ToTypedMessage(&Config{
  263. NameServers: []*net.Endpoint{
  264. {
  265. Network: net.Network_UDP,
  266. Address: &net.IPOrDomain{
  267. Address: &net.IPOrDomain_Ip{
  268. Ip: []byte{127, 0, 0, 1},
  269. },
  270. },
  271. Port: 9999, /* unreachable */
  272. },
  273. },
  274. NameServer: []*NameServer{
  275. {
  276. Address: &net.Endpoint{
  277. Network: net.Network_UDP,
  278. Address: &net.IPOrDomain{
  279. Address: &net.IPOrDomain_Ip{
  280. Ip: []byte{127, 0, 0, 1},
  281. },
  282. },
  283. Port: uint32(port),
  284. },
  285. PrioritizedDomain: []*NameServer_PriorityDomain{
  286. {
  287. Type: DomainMatchingType_Full,
  288. Domain: "google.com",
  289. },
  290. },
  291. },
  292. },
  293. }),
  294. serial.ToTypedMessage(&dispatcher.Config{}),
  295. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  296. serial.ToTypedMessage(&policy.Config{}),
  297. },
  298. Outbound: []*core.OutboundHandlerConfig{
  299. {
  300. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  301. },
  302. },
  303. }
  304. v, err := core.New(config)
  305. common.Must(err)
  306. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  307. startTime := time.Now()
  308. {
  309. ips, err := client.LookupIP("google.com")
  310. if err != nil {
  311. t.Fatal("unexpected error: ", err)
  312. }
  313. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
  314. t.Fatal(r)
  315. }
  316. }
  317. endTime := time.Now()
  318. if startTime.After(endTime.Add(time.Second * 2)) {
  319. t.Error("DNS query doesn't finish in 2 seconds.")
  320. }
  321. }
  322. func TestUDPServerIPv6(t *testing.T) {
  323. port := udp.PickPort()
  324. dnsServer := dns.Server{
  325. Addr: "127.0.0.1:" + port.String(),
  326. Net: "udp",
  327. Handler: &staticHandler{},
  328. UDPSize: 1200,
  329. }
  330. go dnsServer.ListenAndServe()
  331. time.Sleep(time.Second)
  332. config := &core.Config{
  333. App: []*anypb.Any{
  334. serial.ToTypedMessage(&Config{
  335. NameServers: []*net.Endpoint{
  336. {
  337. Network: net.Network_UDP,
  338. Address: &net.IPOrDomain{
  339. Address: &net.IPOrDomain_Ip{
  340. Ip: []byte{127, 0, 0, 1},
  341. },
  342. },
  343. Port: uint32(port),
  344. },
  345. },
  346. }),
  347. serial.ToTypedMessage(&dispatcher.Config{}),
  348. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  349. serial.ToTypedMessage(&policy.Config{}),
  350. },
  351. Outbound: []*core.OutboundHandlerConfig{
  352. {
  353. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  354. },
  355. },
  356. }
  357. v, err := core.New(config)
  358. common.Must(err)
  359. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  360. client6 := client.(feature_dns.IPv6Lookup)
  361. {
  362. ips, err := client6.LookupIPv6("ipv6.google.com")
  363. if err != nil {
  364. t.Fatal("unexpected error: ", err)
  365. }
  366. if r := cmp.Diff(ips, []net.IP{{32, 1, 72, 96, 72, 96, 0, 0, 0, 0, 0, 0, 0, 0, 136, 136}}); r != "" {
  367. t.Fatal(r)
  368. }
  369. }
  370. }
  371. func TestStaticHostDomain(t *testing.T) {
  372. port := udp.PickPort()
  373. dnsServer := dns.Server{
  374. Addr: "127.0.0.1:" + port.String(),
  375. Net: "udp",
  376. Handler: &staticHandler{},
  377. UDPSize: 1200,
  378. }
  379. go dnsServer.ListenAndServe()
  380. time.Sleep(time.Second)
  381. config := &core.Config{
  382. App: []*anypb.Any{
  383. serial.ToTypedMessage(&Config{
  384. NameServers: []*net.Endpoint{
  385. {
  386. Network: net.Network_UDP,
  387. Address: &net.IPOrDomain{
  388. Address: &net.IPOrDomain_Ip{
  389. Ip: []byte{127, 0, 0, 1},
  390. },
  391. },
  392. Port: uint32(port),
  393. },
  394. },
  395. StaticHosts: []*HostMapping{
  396. {
  397. Type: DomainMatchingType_Full,
  398. Domain: "example.com",
  399. ProxiedDomain: "google.com",
  400. },
  401. },
  402. }),
  403. serial.ToTypedMessage(&dispatcher.Config{}),
  404. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  405. serial.ToTypedMessage(&policy.Config{}),
  406. },
  407. Outbound: []*core.OutboundHandlerConfig{
  408. {
  409. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  410. },
  411. },
  412. }
  413. v, err := core.New(config)
  414. common.Must(err)
  415. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  416. {
  417. ips, err := client.LookupIP("example.com")
  418. if err != nil {
  419. t.Fatal("unexpected error: ", err)
  420. }
  421. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
  422. t.Fatal(r)
  423. }
  424. }
  425. dnsServer.Shutdown()
  426. }
  427. func TestIPMatch(t *testing.T) {
  428. port := udp.PickPort()
  429. dnsServer := dns.Server{
  430. Addr: "127.0.0.1:" + port.String(),
  431. Net: "udp",
  432. Handler: &staticHandler{},
  433. UDPSize: 1200,
  434. }
  435. go dnsServer.ListenAndServe()
  436. time.Sleep(time.Second)
  437. config := &core.Config{
  438. App: []*anypb.Any{
  439. serial.ToTypedMessage(&Config{
  440. NameServer: []*NameServer{
  441. // private dns, not match
  442. {
  443. Address: &net.Endpoint{
  444. Network: net.Network_UDP,
  445. Address: &net.IPOrDomain{
  446. Address: &net.IPOrDomain_Ip{
  447. Ip: []byte{127, 0, 0, 1},
  448. },
  449. },
  450. Port: uint32(port),
  451. },
  452. Geoip: []*routercommon.GeoIP{
  453. {
  454. CountryCode: "local",
  455. Cidr: []*routercommon.CIDR{
  456. {
  457. // inner ip, will not match
  458. Ip: []byte{192, 168, 11, 1},
  459. Prefix: 32,
  460. },
  461. },
  462. },
  463. },
  464. },
  465. // second dns, match ip
  466. {
  467. Address: &net.Endpoint{
  468. Network: net.Network_UDP,
  469. Address: &net.IPOrDomain{
  470. Address: &net.IPOrDomain_Ip{
  471. Ip: []byte{127, 0, 0, 1},
  472. },
  473. },
  474. Port: uint32(port),
  475. },
  476. Geoip: []*routercommon.GeoIP{
  477. {
  478. CountryCode: "test",
  479. Cidr: []*routercommon.CIDR{
  480. {
  481. Ip: []byte{8, 8, 8, 8},
  482. Prefix: 32,
  483. },
  484. },
  485. },
  486. {
  487. CountryCode: "test",
  488. Cidr: []*routercommon.CIDR{
  489. {
  490. Ip: []byte{8, 8, 8, 4},
  491. Prefix: 32,
  492. },
  493. },
  494. },
  495. },
  496. },
  497. },
  498. }),
  499. serial.ToTypedMessage(&dispatcher.Config{}),
  500. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  501. serial.ToTypedMessage(&policy.Config{}),
  502. },
  503. Outbound: []*core.OutboundHandlerConfig{
  504. {
  505. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  506. },
  507. },
  508. }
  509. v, err := core.New(config)
  510. common.Must(err)
  511. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  512. startTime := time.Now()
  513. {
  514. ips, err := client.LookupIP("google.com")
  515. if err != nil {
  516. t.Fatal("unexpected error: ", err)
  517. }
  518. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
  519. t.Fatal(r)
  520. }
  521. }
  522. endTime := time.Now()
  523. if startTime.After(endTime.Add(time.Second * 2)) {
  524. t.Error("DNS query doesn't finish in 2 seconds.")
  525. }
  526. }
  527. func TestLocalDomain(t *testing.T) {
  528. port := udp.PickPort()
  529. dnsServer := dns.Server{
  530. Addr: "127.0.0.1:" + port.String(),
  531. Net: "udp",
  532. Handler: &staticHandler{},
  533. UDPSize: 1200,
  534. }
  535. go dnsServer.ListenAndServe()
  536. time.Sleep(time.Second)
  537. config := &core.Config{
  538. App: []*anypb.Any{
  539. serial.ToTypedMessage(&Config{
  540. NameServers: []*net.Endpoint{
  541. {
  542. Network: net.Network_UDP,
  543. Address: &net.IPOrDomain{
  544. Address: &net.IPOrDomain_Ip{
  545. Ip: []byte{127, 0, 0, 1},
  546. },
  547. },
  548. Port: 9999, /* unreachable */
  549. },
  550. },
  551. NameServer: []*NameServer{
  552. {
  553. Address: &net.Endpoint{
  554. Network: net.Network_UDP,
  555. Address: &net.IPOrDomain{
  556. Address: &net.IPOrDomain_Ip{
  557. Ip: []byte{127, 0, 0, 1},
  558. },
  559. },
  560. Port: uint32(port),
  561. },
  562. PrioritizedDomain: []*NameServer_PriorityDomain{
  563. // Equivalent of dotless:localhost
  564. {Type: DomainMatchingType_Regex, Domain: "^[^.]*localhost[^.]*$"},
  565. },
  566. Geoip: []*routercommon.GeoIP{
  567. { // Will match localhost, localhost-a and localhost-b,
  568. CountryCode: "local",
  569. Cidr: []*routercommon.CIDR{
  570. {Ip: []byte{127, 0, 0, 2}, Prefix: 32},
  571. {Ip: []byte{127, 0, 0, 3}, Prefix: 32},
  572. {Ip: []byte{127, 0, 0, 4}, Prefix: 32},
  573. },
  574. },
  575. },
  576. },
  577. {
  578. Address: &net.Endpoint{
  579. Network: net.Network_UDP,
  580. Address: &net.IPOrDomain{
  581. Address: &net.IPOrDomain_Ip{
  582. Ip: []byte{127, 0, 0, 1},
  583. },
  584. },
  585. Port: uint32(port),
  586. },
  587. PrioritizedDomain: []*NameServer_PriorityDomain{
  588. // Equivalent of dotless: and domain:local
  589. {Type: DomainMatchingType_Regex, Domain: "^[^.]*$"},
  590. {Type: DomainMatchingType_Subdomain, Domain: "local"},
  591. {Type: DomainMatchingType_Subdomain, Domain: "localdomain"},
  592. },
  593. },
  594. },
  595. StaticHosts: []*HostMapping{
  596. {
  597. Type: DomainMatchingType_Full,
  598. Domain: "hostnamestatic",
  599. Ip: [][]byte{{127, 0, 0, 53}},
  600. },
  601. {
  602. Type: DomainMatchingType_Full,
  603. Domain: "hostnamealias",
  604. ProxiedDomain: "hostname.localdomain",
  605. },
  606. },
  607. }),
  608. serial.ToTypedMessage(&dispatcher.Config{}),
  609. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  610. serial.ToTypedMessage(&policy.Config{}),
  611. },
  612. Outbound: []*core.OutboundHandlerConfig{
  613. {
  614. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  615. },
  616. },
  617. }
  618. v, err := core.New(config)
  619. common.Must(err)
  620. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  621. startTime := time.Now()
  622. { // Will match dotless:
  623. ips, err := client.LookupIP("hostname")
  624. if err != nil {
  625. t.Fatal("unexpected error: ", err)
  626. }
  627. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 1}}); r != "" {
  628. t.Fatal(r)
  629. }
  630. }
  631. { // Will match domain:local
  632. ips, err := client.LookupIP("hostname.local")
  633. if err != nil {
  634. t.Fatal("unexpected error: ", err)
  635. }
  636. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 1}}); r != "" {
  637. t.Fatal(r)
  638. }
  639. }
  640. { // Will match static ip
  641. ips, err := client.LookupIP("hostnamestatic")
  642. if err != nil {
  643. t.Fatal("unexpected error: ", err)
  644. }
  645. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 53}}); r != "" {
  646. t.Fatal(r)
  647. }
  648. }
  649. { // Will match domain replacing
  650. ips, err := client.LookupIP("hostnamealias")
  651. if err != nil {
  652. t.Fatal("unexpected error: ", err)
  653. }
  654. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 1}}); r != "" {
  655. t.Fatal(r)
  656. }
  657. }
  658. { // Will match dotless:localhost, but not expectIPs: 127.0.0.2, 127.0.0.3, then matches at dotless:
  659. ips, err := client.LookupIP("localhost")
  660. if err != nil {
  661. t.Fatal("unexpected error: ", err)
  662. }
  663. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 2}}); r != "" {
  664. t.Fatal(r)
  665. }
  666. }
  667. { // Will match dotless:localhost, and expectIPs: 127.0.0.2, 127.0.0.3
  668. ips, err := client.LookupIP("localhost-a")
  669. if err != nil {
  670. t.Fatal("unexpected error: ", err)
  671. }
  672. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 3}}); r != "" {
  673. t.Fatal(r)
  674. }
  675. }
  676. { // Will match dotless:localhost, and expectIPs: 127.0.0.2, 127.0.0.3
  677. ips, err := client.LookupIP("localhost-b")
  678. if err != nil {
  679. t.Fatal("unexpected error: ", err)
  680. }
  681. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 4}}); r != "" {
  682. t.Fatal(r)
  683. }
  684. }
  685. { // Will match dotless:
  686. ips, err := client.LookupIP("Mijia Cloud")
  687. if err != nil {
  688. t.Fatal("unexpected error: ", err)
  689. }
  690. if r := cmp.Diff(ips, []net.IP{{127, 0, 0, 1}}); r != "" {
  691. t.Fatal(r)
  692. }
  693. }
  694. endTime := time.Now()
  695. if startTime.After(endTime.Add(time.Second * 2)) {
  696. t.Error("DNS query doesn't finish in 2 seconds.")
  697. }
  698. }
  699. func TestMultiMatchPrioritizedDomain(t *testing.T) {
  700. port := udp.PickPort()
  701. dnsServer := dns.Server{
  702. Addr: "127.0.0.1:" + port.String(),
  703. Net: "udp",
  704. Handler: &staticHandler{},
  705. UDPSize: 1200,
  706. }
  707. go dnsServer.ListenAndServe()
  708. time.Sleep(time.Second)
  709. config := &core.Config{
  710. App: []*anypb.Any{
  711. serial.ToTypedMessage(&Config{
  712. NameServers: []*net.Endpoint{
  713. {
  714. Network: net.Network_UDP,
  715. Address: &net.IPOrDomain{
  716. Address: &net.IPOrDomain_Ip{
  717. Ip: []byte{127, 0, 0, 1},
  718. },
  719. },
  720. Port: 9999, /* unreachable */
  721. },
  722. },
  723. NameServer: []*NameServer{
  724. {
  725. Address: &net.Endpoint{
  726. Network: net.Network_UDP,
  727. Address: &net.IPOrDomain{
  728. Address: &net.IPOrDomain_Ip{
  729. Ip: []byte{127, 0, 0, 1},
  730. },
  731. },
  732. Port: uint32(port),
  733. },
  734. PrioritizedDomain: []*NameServer_PriorityDomain{
  735. {
  736. Type: DomainMatchingType_Subdomain,
  737. Domain: "google.com",
  738. },
  739. },
  740. Geoip: []*routercommon.GeoIP{
  741. { // Will only match 8.8.8.8 and 8.8.4.4
  742. Cidr: []*routercommon.CIDR{
  743. {Ip: []byte{8, 8, 8, 8}, Prefix: 32},
  744. {Ip: []byte{8, 8, 4, 4}, Prefix: 32},
  745. },
  746. },
  747. },
  748. },
  749. {
  750. Address: &net.Endpoint{
  751. Network: net.Network_UDP,
  752. Address: &net.IPOrDomain{
  753. Address: &net.IPOrDomain_Ip{
  754. Ip: []byte{127, 0, 0, 1},
  755. },
  756. },
  757. Port: uint32(port),
  758. },
  759. PrioritizedDomain: []*NameServer_PriorityDomain{
  760. {
  761. Type: DomainMatchingType_Subdomain,
  762. Domain: "google.com",
  763. },
  764. },
  765. Geoip: []*routercommon.GeoIP{
  766. { // Will match 8.8.8.8 and 8.8.8.7, etc
  767. Cidr: []*routercommon.CIDR{
  768. {Ip: []byte{8, 8, 8, 7}, Prefix: 24},
  769. },
  770. },
  771. },
  772. },
  773. {
  774. Address: &net.Endpoint{
  775. Network: net.Network_UDP,
  776. Address: &net.IPOrDomain{
  777. Address: &net.IPOrDomain_Ip{
  778. Ip: []byte{127, 0, 0, 1},
  779. },
  780. },
  781. Port: uint32(port),
  782. },
  783. PrioritizedDomain: []*NameServer_PriorityDomain{
  784. {
  785. Type: DomainMatchingType_Subdomain,
  786. Domain: "api.google.com",
  787. },
  788. },
  789. Geoip: []*routercommon.GeoIP{
  790. { // Will only match 8.8.7.7 (api.google.com)
  791. Cidr: []*routercommon.CIDR{
  792. {Ip: []byte{8, 8, 7, 7}, Prefix: 32},
  793. },
  794. },
  795. },
  796. },
  797. {
  798. Address: &net.Endpoint{
  799. Network: net.Network_UDP,
  800. Address: &net.IPOrDomain{
  801. Address: &net.IPOrDomain_Ip{
  802. Ip: []byte{127, 0, 0, 1},
  803. },
  804. },
  805. Port: uint32(port),
  806. },
  807. PrioritizedDomain: []*NameServer_PriorityDomain{
  808. {
  809. Type: DomainMatchingType_Full,
  810. Domain: "v2.api.google.com",
  811. },
  812. },
  813. Geoip: []*routercommon.GeoIP{
  814. { // Will only match 8.8.7.8 (v2.api.google.com)
  815. Cidr: []*routercommon.CIDR{
  816. {Ip: []byte{8, 8, 7, 8}, Prefix: 32},
  817. },
  818. },
  819. },
  820. },
  821. },
  822. }),
  823. serial.ToTypedMessage(&dispatcher.Config{}),
  824. serial.ToTypedMessage(&proxyman.OutboundConfig{}),
  825. serial.ToTypedMessage(&policy.Config{}),
  826. },
  827. Outbound: []*core.OutboundHandlerConfig{
  828. {
  829. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  830. },
  831. },
  832. }
  833. v, err := core.New(config)
  834. common.Must(err)
  835. client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
  836. startTime := time.Now()
  837. { // Will match server 1,2 and server 1 returns expected ip
  838. ips, err := client.LookupIP("google.com")
  839. if err != nil {
  840. t.Fatal("unexpected error: ", err)
  841. }
  842. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
  843. t.Fatal(r)
  844. }
  845. }
  846. { // Will match server 1,2 and server 1 returns unexpected ip, then server 2 returns expected one
  847. clientv4 := client.(feature_dns.IPv4Lookup)
  848. ips, err := clientv4.LookupIPv4("ipv6.google.com")
  849. if err != nil {
  850. t.Fatal("unexpected error: ", err)
  851. }
  852. if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 7}}); r != "" {
  853. t.Fatal(r)
  854. }
  855. }
  856. { // Will match server 3,1,2 and server 3 returns expected one
  857. ips, err := client.LookupIP("api.google.com")
  858. if err != nil {
  859. t.Fatal("unexpected error: ", err)
  860. }
  861. if r := cmp.Diff(ips, []net.IP{{8, 8, 7, 7}}); r != "" {
  862. t.Fatal(r)
  863. }
  864. }
  865. { // Will match server 4,3,1,2 and server 4 returns expected one
  866. ips, err := client.LookupIP("v2.api.google.com")
  867. if err != nil {
  868. t.Fatal("unexpected error: ", err)
  869. }
  870. if r := cmp.Diff(ips, []net.IP{{8, 8, 7, 8}}); r != "" {
  871. t.Fatal(r)
  872. }
  873. }
  874. endTime := time.Now()
  875. if startTime.After(endTime.Add(time.Second * 2)) {
  876. t.Error("DNS query doesn't finish in 2 seconds.")
  877. }
  878. }