dns.go 2.3 KB

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