dns_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package services_test
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. "github.com/ipfs/go-log"
  19. "github.com/miekg/dns"
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. "github.com/mudler/edgevpn/pkg/blockchain"
  23. "github.com/mudler/edgevpn/pkg/logger"
  24. node "github.com/mudler/edgevpn/pkg/node"
  25. . "github.com/mudler/edgevpn/pkg/services"
  26. "github.com/mudler/edgevpn/pkg/types"
  27. )
  28. var _ = Describe("DNS service", func() {
  29. token := node.GenerateNewConnectionData().Base64()
  30. logg := logger.New(log.LevelDebug)
  31. l := node.Logger(logg)
  32. e2, _ := node.New(
  33. append(Alive(15*time.Second, 90*time.Minute, 15*time.Minute),
  34. node.FromBase64(true, true, token,nil, nil), node.WithStore(&blockchain.MemoryStore{}), l)...)
  35. Context("DNS service", func() {
  36. It("Set DNS records and can resolve IPs", func() {
  37. ctx, cancel := context.WithCancel(context.Background())
  38. defer cancel()
  39. opts := DNS(logg, "127.0.0.1:19192", true, []string{"8.8.8.8:53"}, 10)
  40. opts = append(opts, node.FromBase64(true, true, token,nil, nil), node.WithStore(&blockchain.MemoryStore{}), l)
  41. e, _ := node.New(opts...)
  42. e.Start(ctx)
  43. e2.Start(ctx)
  44. ll, _ := e2.Ledger()
  45. AnnounceDNSRecord(ctx, ll, 60*time.Second, `test.foo.`, types.DNS{
  46. dns.Type(dns.TypeA): "2.2.2.2",
  47. })
  48. searchDomain := func(d string) func() string {
  49. return func() string {
  50. var s string
  51. dnsMessage := new(dns.Msg)
  52. dnsMessage.SetQuestion(fmt.Sprintf("%s.", d), dns.TypeA)
  53. r, err := QueryDNS(ctx, dnsMessage, "127.0.0.1:19192")
  54. if r != nil {
  55. answers := r.Answer
  56. for _, a := range answers {
  57. s = a.String() + s
  58. }
  59. }
  60. if err != nil {
  61. fmt.Println(err)
  62. }
  63. return s
  64. }
  65. }
  66. Eventually(searchDomain("google.com"), 230*time.Second, 1*time.Second).Should(ContainSubstring("A"))
  67. // We hit the same record again, this time it's faster as there is a cache
  68. Eventually(searchDomain("google.com"), 1*time.Second, 1*time.Second).Should(ContainSubstring("A"))
  69. Eventually(searchDomain("test.foo"), 230*time.Second, 1*time.Second).Should(ContainSubstring("2.2.2.2"))
  70. })
  71. })
  72. })