util.go 7.6 KB

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