dns_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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/net"
  15. "github.com/xtls/xray-core/common/serial"
  16. "github.com/xtls/xray-core/core"
  17. feature_dns "github.com/xtls/xray-core/features/dns"
  18. "github.com/xtls/xray-core/proxy/freedom"
  19. "github.com/xtls/xray-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. 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 err != feature_dns.ErrEmptyResponse {
  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. }