server_test.go 21 KB

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