dns_test.go 24 KB

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