dns_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright © 2022 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package services_test
  16. import (
  17. "context"
  18. "fmt"
  19. "time"
  20. "github.com/ipfs/go-log"
  21. "github.com/miekg/dns"
  22. . "github.com/onsi/ginkgo/v2"
  23. . "github.com/onsi/gomega"
  24. "github.com/mudler/edgevpn/pkg/blockchain"
  25. "github.com/mudler/edgevpn/pkg/logger"
  26. node "github.com/mudler/edgevpn/pkg/node"
  27. . "github.com/mudler/edgevpn/pkg/services"
  28. "github.com/mudler/edgevpn/pkg/types"
  29. )
  30. var _ = Describe("DNS service", func() {
  31. token := node.GenerateNewConnectionData().Base64()
  32. logg := logger.New(log.LevelDebug)
  33. l := node.Logger(logg)
  34. e2, _ := node.New(
  35. append(Alive(15*time.Second, 90*time.Minute, 15*time.Minute),
  36. node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)...)
  37. Context("DNS service", func() {
  38. It("Set DNS records and can resolve IPs", func() {
  39. ctx, cancel := context.WithCancel(context.Background())
  40. defer cancel()
  41. opts := DNS(logg, "127.0.0.1:19192", true, []string{"8.8.8.8:53"}, 10)
  42. opts = append(opts, node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)
  43. e, _ := node.New(opts...)
  44. e.Start(ctx)
  45. e2.Start(ctx)
  46. ll, _ := e2.Ledger()
  47. AnnounceDNSRecord(ctx, ll, 15*time.Second, `test.foo.`, types.DNS{
  48. dns.Type(dns.TypeA): "2.2.2.2",
  49. })
  50. searchDomain := func(d string) func() string {
  51. return func() string {
  52. var s string
  53. dnsMessage := new(dns.Msg)
  54. dnsMessage.SetQuestion(fmt.Sprintf("%s.", d), dns.TypeA)
  55. r, err := QueryDNS(ctx, dnsMessage, "127.0.0.1:19192")
  56. if r != nil {
  57. answers := r.Answer
  58. for _, a := range answers {
  59. s = a.String() + s
  60. }
  61. }
  62. if err != nil {
  63. fmt.Println(err)
  64. }
  65. return s
  66. }
  67. }
  68. Eventually(searchDomain("google.com"), 230*time.Second, 1*time.Second).Should(ContainSubstring("A"))
  69. // We hit the same record again, this time it's faster as there is a cache
  70. Eventually(searchDomain("google.com"), 1*time.Second, 1*time.Second).Should(ContainSubstring("A"))
  71. Eventually(searchDomain("test.foo"), 230*time.Second, 1*time.Second).Should(ContainSubstring("2.2.2.2"))
  72. })
  73. })
  74. })