util.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. "time"
  18. "github.com/ipfs/go-log"
  19. "github.com/libp2p/go-libp2p"
  20. "github.com/mudler/edgevpn/internal"
  21. "github.com/mudler/edgevpn/pkg/blockchain"
  22. "github.com/mudler/edgevpn/pkg/discovery"
  23. "github.com/mudler/edgevpn/pkg/edgevpn"
  24. "github.com/mudler/edgevpn/pkg/logger"
  25. "github.com/peterbourgon/diskv"
  26. "github.com/songgao/water"
  27. "github.com/urfave/cli"
  28. )
  29. var CommonFlags []cli.Flag = []cli.Flag{
  30. &cli.StringFlag{
  31. Name: "config",
  32. Usage: "Specify a path to a edgevpn config file",
  33. EnvVar: "EDGEVPNCONFIG",
  34. },
  35. &cli.StringFlag{
  36. Name: "timeout",
  37. Usage: "Specify a default timeout for connection stream",
  38. EnvVar: "EDGEVPNTIMEOUT",
  39. Value: "15s",
  40. },
  41. &cli.IntFlag{
  42. Name: "mtu",
  43. Usage: "Specify a mtu",
  44. EnvVar: "EDGEVPNMTU",
  45. Value: 1200,
  46. },
  47. &cli.IntFlag{
  48. Name: "discovery-interval",
  49. Usage: "DHT discovery interval time",
  50. EnvVar: "EDGEVPNDHTINTERVAL",
  51. Value: 120,
  52. },
  53. &cli.IntFlag{
  54. Name: "ledger-announce-interval",
  55. Usage: "Ledger announce interval time",
  56. EnvVar: "EDGEVPNLEDGERINTERVAL",
  57. Value: 10,
  58. },
  59. &cli.IntFlag{
  60. Name: "ledger-syncronization-interval",
  61. Usage: "Ledger syncronization interval time",
  62. EnvVar: "EDGEVPNLEDGERSYNCINTERVAL",
  63. Value: 10,
  64. },
  65. &cli.IntFlag{
  66. Name: "nat-ratelimit-global",
  67. Usage: "Rate limit global requests",
  68. EnvVar: "EDGEVPNNATRATELIMITGLOBAL",
  69. Value: 10,
  70. },
  71. &cli.IntFlag{
  72. Name: "nat-ratelimit-peer",
  73. Usage: "Rate limit perr requests",
  74. EnvVar: "EDGEVPNNATRATELIMITPEER",
  75. Value: 10,
  76. },
  77. &cli.IntFlag{
  78. Name: "nat-ratelimit-interval",
  79. Usage: "Rate limit interval",
  80. EnvVar: "EDGEVPNNATRATELIMITINTERVAL",
  81. Value: 60,
  82. },
  83. &cli.BoolTFlag{
  84. Name: "nat-ratelimit",
  85. Usage: "Changes the default rate limiting configured in helping other peers determine their reachability status",
  86. EnvVar: "EDGEVPNNATRATELIMIT",
  87. },
  88. &cli.StringFlag{
  89. Name: "ledger-state",
  90. Usage: "Specify a ledger state directory",
  91. EnvVar: "EDGEVPNLEDGERSTATE",
  92. },
  93. &cli.BoolTFlag{
  94. Name: "mdns",
  95. Usage: "Enable mDNS for peer discovery",
  96. EnvVar: "EDGEVPNMDNS",
  97. },
  98. &cli.BoolTFlag{
  99. Name: "autorelay",
  100. Usage: "Automatically act as a relay if the node can accept inbound connections",
  101. EnvVar: "EDGEVPNAUTORELAY",
  102. },
  103. &cli.BoolTFlag{
  104. Name: "holepunch",
  105. Usage: "Automatically try holepunching when possible",
  106. EnvVar: "EDGEVPNHOLEPUNCH",
  107. },
  108. &cli.BoolTFlag{
  109. Name: "natservice",
  110. Usage: "Tries to determine reachability status of nodes",
  111. EnvVar: "EDGEVPNNATSERVICE",
  112. },
  113. &cli.BoolTFlag{
  114. Name: "natmap",
  115. Usage: "Tries to open a port in the firewall via upnp",
  116. EnvVar: "EDGEVPNNATMAP",
  117. },
  118. &cli.BoolTFlag{
  119. Name: "dht",
  120. Usage: "Enable DHT for peer discovery",
  121. EnvVar: "EDGEVPNDHT",
  122. },
  123. &cli.StringFlag{
  124. Name: "log-level",
  125. Usage: "Specify loglevel",
  126. EnvVar: "EDGEVPNLOGLEVEL",
  127. Value: "info",
  128. },
  129. &cli.StringFlag{
  130. Name: "libp2p-log-level",
  131. Usage: "Specify libp2p loglevel",
  132. EnvVar: "EDGEVPNLIBP2PLOGLEVEL",
  133. Value: "fatal",
  134. },
  135. &cli.StringSliceFlag{
  136. Name: "discovery-bootstrap-peers",
  137. Usage: "List of discovery peers to use",
  138. EnvVar: "EDGEVPNBOOTSTRAPPEERS",
  139. },
  140. &cli.StringFlag{
  141. Name: "token",
  142. Usage: "Specify an edgevpn token in place of a config file",
  143. EnvVar: "EDGEVPNTOKEN",
  144. }}
  145. func displayStart(e *edgevpn.EdgeVPN) {
  146. e.Logger().Info(Copyright)
  147. e.Logger().Infof("Version: %s commit: %s", internal.Version, internal.Commit)
  148. }
  149. func cliToOpts(c *cli.Context) []edgevpn.Option {
  150. config := c.String("config")
  151. address := c.String("address")
  152. router := c.String("router")
  153. iface := c.String("interface")
  154. logLevel := c.String("log-level")
  155. libp2plogLevel := c.String("libp2p-log-level")
  156. dht, mDNS := c.Bool("dht"), c.Bool("mdns")
  157. ledgerState := c.String("ledger-state")
  158. addrsList := discovery.AddrList{}
  159. peers := c.StringSlice("discovery-bootstrap-peers")
  160. lvl, err := log.LevelFromString(logLevel)
  161. if err != nil {
  162. lvl = log.LevelError
  163. }
  164. llger := logger.New(lvl)
  165. libp2plvl, err := log.LevelFromString(libp2plogLevel)
  166. if err != nil {
  167. libp2plvl = log.LevelFatal
  168. }
  169. token := c.String("token")
  170. if config == "" &&
  171. token == "" {
  172. llger.Fatal("EDGEVPNCONFIG or EDGEVPNTOKEN not supplied. At least a config file is required")
  173. }
  174. for _, p := range peers {
  175. if err := addrsList.Set(p); err != nil {
  176. llger.Fatal("Failed reading bootstrap peer list", err.Error())
  177. }
  178. }
  179. opts := []edgevpn.Option{
  180. edgevpn.WithDiscoveryInterval(time.Duration(c.Int("discovery-interval")) * time.Second),
  181. edgevpn.WithLedgerAnnounceTime(time.Duration(c.Int("ledger-announce-interval")) * time.Second),
  182. edgevpn.WithLedgerInterval(time.Duration(c.Int("ledger-syncronization-interval")) * time.Second),
  183. edgevpn.Logger(llger),
  184. edgevpn.WithDiscoveryBootstrapPeers(addrsList),
  185. edgevpn.LibP2PLogLevel(libp2plvl),
  186. edgevpn.WithInterfaceMTU(c.Int("mtu")),
  187. edgevpn.WithPacketMTU(1420),
  188. edgevpn.WithInterfaceAddress(address),
  189. edgevpn.WithRouterAddress(router),
  190. edgevpn.WithInterfaceName(iface),
  191. edgevpn.WithTimeout(c.String("timeout")),
  192. edgevpn.WithInterfaceType(water.TUN),
  193. edgevpn.NetLinkBootstrap(true),
  194. edgevpn.FromBase64(mDNS, dht, token),
  195. edgevpn.FromYaml(mDNS, dht, config),
  196. }
  197. libp2pOpts := []libp2p.Option{libp2p.UserAgent("edgevpn")}
  198. if c.Bool("autorelay") {
  199. libp2pOpts = append(libp2pOpts, libp2p.EnableAutoRelay())
  200. }
  201. if c.Bool("nat-ratelimit") {
  202. libp2pOpts = append(libp2pOpts, libp2p.AutoNATServiceRateLimit(
  203. c.Int("nat-ratelimit-global"),
  204. c.Int("nat-ratelimit-peer"),
  205. time.Duration(c.Int("nat-ratelimit-interval"))*time.Second,
  206. ))
  207. }
  208. if c.Bool("holepunch") {
  209. libp2pOpts = append(libp2pOpts, libp2p.EnableHolePunching())
  210. }
  211. if c.Bool("natservice") {
  212. libp2pOpts = append(libp2pOpts, libp2p.EnableNATService())
  213. }
  214. if c.Bool("natmap") {
  215. libp2pOpts = append(libp2pOpts, libp2p.NATPortMap())
  216. }
  217. opts = append(opts, edgevpn.WithLibp2pOptions(libp2pOpts...))
  218. if ledgerState != "" {
  219. opts = append(opts, edgevpn.WithStore(blockchain.NewDiskStore(diskv.New(diskv.Options{
  220. BasePath: ledgerState,
  221. CacheSizeMax: uint64(50), // 50MB
  222. }))))
  223. } else {
  224. opts = append(opts, edgevpn.WithStore(&blockchain.MemoryStore{}))
  225. }
  226. return opts
  227. }