dns.go 2.3 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 cmd
  14. import (
  15. "context"
  16. "time"
  17. "github.com/mudler/edgevpn/pkg/node"
  18. "github.com/mudler/edgevpn/pkg/services"
  19. "github.com/urfave/cli/v2"
  20. )
  21. func DNS() *cli.Command {
  22. return &cli.Command{
  23. Name: "dns",
  24. Usage: "Starts a local dns server",
  25. Description: `Start a local dns server which uses the blockchain to resolve addresses`,
  26. UsageText: "edgevpn dns",
  27. Flags: append(CommonFlags,
  28. &cli.StringFlag{
  29. Name: "listen",
  30. Usage: "DNS listening address. Empty to disable dns server",
  31. EnvVars: []string{"DNSADDRESS"},
  32. Value: "",
  33. },
  34. &cli.BoolFlag{
  35. Name: "dns-forwarder",
  36. Usage: "Enables dns forwarding",
  37. EnvVars: []string{"DNSFORWARD"},
  38. Value: true,
  39. },
  40. &cli.IntFlag{
  41. Name: "dns-cache-size",
  42. Usage: "DNS LRU cache size",
  43. EnvVars: []string{"DNSCACHESIZE"},
  44. Value: 200,
  45. },
  46. &cli.StringSliceFlag{
  47. Name: "dns-forward-server",
  48. Usage: "List of DNS forward server, e.g. 8.8.8.8:53, 192.168.1.1:53 ...",
  49. EnvVars: []string{"DNSFORWARDSERVER"},
  50. Value: cli.NewStringSlice("8.8.8.8:53", "1.1.1.1:53"),
  51. },
  52. ),
  53. Action: func(c *cli.Context) error {
  54. o, _, ll := cliToOpts(c)
  55. dns := c.String("listen")
  56. // Adds DNS Server
  57. o = append(o,
  58. services.DNS(ll, dns,
  59. c.Bool("dns-forwarder"),
  60. c.StringSlice("dns-forward-server"),
  61. c.Int("dns-cache-size"),
  62. )...)
  63. e, err := node.New(o...)
  64. if err != nil {
  65. return err
  66. }
  67. displayStart(ll)
  68. go handleStopSignals()
  69. ctx := context.Background()
  70. // Start the node to the network, using our ledger
  71. if err := e.Start(ctx); err != nil {
  72. return err
  73. }
  74. for {
  75. time.Sleep(1 * time.Second)
  76. }
  77. },
  78. }
  79. }