main.go 5.8 KB

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