main.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. "fmt"
  19. "net"
  20. "os"
  21. "path/filepath"
  22. "time"
  23. "github.com/mudler/edgevpn/api"
  24. edgevpn "github.com/mudler/edgevpn/pkg/node"
  25. "github.com/mudler/edgevpn/pkg/services"
  26. "github.com/mudler/edgevpn/pkg/vpn"
  27. "github.com/urfave/cli"
  28. )
  29. const Copyright string = ` edgevpn Copyright (C) 2021-2022 Ettore Di Giacinto
  30. This program comes with ABSOLUTELY NO WARRANTY.
  31. This is free software, and you are welcome to redistribute it
  32. under certain conditions.`
  33. func MainFlags() []cli.Flag {
  34. basedir, _ := os.UserHomeDir()
  35. if basedir == "" {
  36. basedir = os.TempDir()
  37. }
  38. return append([]cli.Flag{
  39. &cli.IntFlag{
  40. Name: "key-otp-interval",
  41. Usage: "Tweaks default otp interval (in seconds) when generating new tokens",
  42. Value: 9000,
  43. },
  44. &cli.BoolFlag{
  45. Name: "g",
  46. Usage: "Generates a new configuration and prints it on screen",
  47. },
  48. &cli.BoolFlag{
  49. Name: "b",
  50. Usage: "Encodes the new config in base64, so it can be used as a token",
  51. },
  52. &cli.BoolFlag{
  53. Name: "api",
  54. Usage: "Starts also the API daemon locally for inspecting the network status",
  55. EnvVar: "API",
  56. },
  57. &cli.StringFlag{
  58. Name: "api-listen",
  59. Value: ":8080",
  60. Usage: "API listening port",
  61. EnvVar: "APILISTEN",
  62. },
  63. &cli.BoolFlag{
  64. Name: "dhcp",
  65. Usage: "Enables p2p ip negotiation (experimental)",
  66. EnvVar: "DHCP",
  67. },
  68. &cli.StringFlag{
  69. Name: "lease-dir",
  70. Value: filepath.Join(basedir, ".edgevpn", "leases"),
  71. Usage: "DHCP leases directory",
  72. EnvVar: "DHCPLEASEDIR",
  73. },
  74. &cli.StringFlag{
  75. Name: "address",
  76. Usage: "VPN virtual address",
  77. EnvVar: "ADDRESS",
  78. Value: "10.1.0.1/24",
  79. },
  80. &cli.StringFlag{
  81. Name: "dns",
  82. Usage: "DNS listening address. Empty to disable dns server",
  83. EnvVar: "DNSADDRESS",
  84. Value: "",
  85. },
  86. &cli.BoolTFlag{
  87. Name: "dns-forwarder",
  88. Usage: "Enables dns forwarding",
  89. EnvVar: "DNSFORWARD",
  90. },
  91. &cli.IntFlag{
  92. Name: "dns-cache-size",
  93. Usage: "DNS LRU cache size",
  94. EnvVar: "DNSCACHESIZE",
  95. Value: 200,
  96. },
  97. &cli.IntFlag{
  98. Name: "aliveness-healthcheck-interval",
  99. Usage: "Healthcheck interval",
  100. EnvVar: "HEALTHCHECKINTERVAL",
  101. Value: 120,
  102. },
  103. &cli.IntFlag{
  104. Name: "aliveness-healthcheck-scrub-interval",
  105. Usage: "Healthcheck scrub interval",
  106. EnvVar: "HEALTHCHECKSCRUBINTERVAL",
  107. Value: 600,
  108. },
  109. &cli.IntFlag{
  110. Name: "aliveness-healthcheck-max-interval",
  111. Usage: "Healthcheck max interval. Threshold after a node is determined offline",
  112. EnvVar: "HEALTHCHECKMAXINTERVAL",
  113. Value: 900,
  114. },
  115. &cli.StringSliceFlag{
  116. Name: "dns-forward-server",
  117. Usage: "List of DNS forward server, e.g. 8.8.8.8:53, 192.168.1.1:53 ...",
  118. EnvVar: "DNSFORWARDSERVER",
  119. Value: &cli.StringSlice{"8.8.8.8:53", "1.1.1.1:53"},
  120. },
  121. &cli.StringFlag{
  122. Name: "router",
  123. Usage: "Sends all packets to this node",
  124. EnvVar: "ROUTER",
  125. },
  126. &cli.StringFlag{
  127. Name: "interface",
  128. Usage: "Interface name",
  129. Value: "edgevpn0",
  130. EnvVar: "IFACE",
  131. }}, CommonFlags...)
  132. }
  133. func Main() func(c *cli.Context) error {
  134. return func(c *cli.Context) error {
  135. if c.Bool("g") {
  136. // Generates a new config and exit
  137. newData := edgevpn.GenerateNewConnectionData(c.Int("key-otp-interval"))
  138. if c.Bool("b") {
  139. fmt.Print(newData.Base64())
  140. } else {
  141. fmt.Println(newData.YAML())
  142. }
  143. os.Exit(0)
  144. }
  145. o, vpnOpts, ll := cliToOpts(c)
  146. o = append(o,
  147. services.Alive(
  148. time.Duration(c.Int("aliveness-healthcheck-interval"))*time.Second,
  149. time.Duration(c.Int("aliveness-healthcheck-scrub-interval"))*time.Second,
  150. time.Duration(c.Int("aliveness-healthcheck-max-interval"))*time.Second)...)
  151. if c.Bool("dhcp") {
  152. // Adds DHCP server
  153. address, _, err := net.ParseCIDR(c.String("address"))
  154. if err != nil {
  155. return err
  156. }
  157. nodeOpts, vO := vpn.DHCP(ll, 15*time.Minute, c.String("lease-dir"), address.String())
  158. o = append(o, nodeOpts...)
  159. vpnOpts = append(vpnOpts, vO...)
  160. }
  161. dns := c.String("dns")
  162. if dns != "" {
  163. // Adds DNS Server
  164. o = append(o,
  165. services.DNS(dns,
  166. c.Bool("dns-forwarder"),
  167. c.StringSlice("dns-forward-server"),
  168. c.Int("dns-cache-size"),
  169. )...)
  170. }
  171. opts, err := vpn.Register(vpnOpts...)
  172. if err != nil {
  173. return err
  174. }
  175. e := edgevpn.New(append(o, opts...)...)
  176. displayStart(ll)
  177. ctx := context.Background()
  178. if c.Bool("api") {
  179. go api.API(ctx, c.String("api-listen"), 5*time.Second, 20*time.Second, e)
  180. }
  181. return e.Start(ctx)
  182. }
  183. }