util.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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/mudler/edgevpn/internal"
  21. "github.com/mudler/edgevpn/pkg/config"
  22. nodeConfig "github.com/mudler/edgevpn/pkg/config"
  23. "github.com/mudler/edgevpn/pkg/logger"
  24. node "github.com/mudler/edgevpn/pkg/node"
  25. "github.com/mudler/edgevpn/pkg/vpn"
  26. "github.com/urfave/cli"
  27. )
  28. var CommonFlags []cli.Flag = []cli.Flag{
  29. &cli.StringFlag{
  30. Name: "config",
  31. Usage: "Specify a path to a edgevpn config file",
  32. EnvVar: "EDGEVPNCONFIG",
  33. },
  34. &cli.StringFlag{
  35. Name: "timeout",
  36. Usage: "Specify a default timeout for connection stream",
  37. EnvVar: "EDGEVPNTIMEOUT",
  38. Value: "15s",
  39. },
  40. &cli.IntFlag{
  41. Name: "mtu",
  42. Usage: "Specify a mtu",
  43. EnvVar: "EDGEVPNMTU",
  44. Value: 1200,
  45. },
  46. &cli.BoolTFlag{
  47. Name: "bootstrap-iface",
  48. Usage: "Setup interface on startup (need privileges)",
  49. EnvVar: "EDGEVPNBOOTSTRAPIFACE",
  50. },
  51. &cli.IntFlag{
  52. Name: "packet-mtu",
  53. Usage: "Specify a mtu",
  54. EnvVar: "EDGEVPNPACKETMTU",
  55. Value: 1420,
  56. },
  57. &cli.IntFlag{
  58. Name: "channel-buffer-size",
  59. Usage: "Specify a channel buffer size",
  60. EnvVar: "EDGEVPNCHANNELBUFFERSIZE",
  61. Value: 0,
  62. },
  63. &cli.IntFlag{
  64. Name: "discovery-interval",
  65. Usage: "DHT discovery interval time",
  66. EnvVar: "EDGEVPNDHTINTERVAL",
  67. Value: 120,
  68. },
  69. &cli.IntFlag{
  70. Name: "ledger-announce-interval",
  71. Usage: "Ledger announce interval time",
  72. EnvVar: "EDGEVPNLEDGERINTERVAL",
  73. Value: 10,
  74. },
  75. &cli.StringFlag{
  76. Name: "autorelay-discovery-interval",
  77. Usage: "Autorelay discovery interval (Experimental. 0 to disable)",
  78. EnvVar: "EDGEVPNAUTORELAYDISCOVERYINTERVAL",
  79. Value: "0",
  80. },
  81. &cli.IntFlag{
  82. Name: "ledger-syncronization-interval",
  83. Usage: "Ledger syncronization interval time",
  84. EnvVar: "EDGEVPNLEDGERSYNCINTERVAL",
  85. Value: 10,
  86. },
  87. &cli.IntFlag{
  88. Name: "nat-ratelimit-global",
  89. Usage: "Rate limit global requests",
  90. EnvVar: "EDGEVPNNATRATELIMITGLOBAL",
  91. Value: 10,
  92. },
  93. &cli.IntFlag{
  94. Name: "nat-ratelimit-peer",
  95. Usage: "Rate limit perr requests",
  96. EnvVar: "EDGEVPNNATRATELIMITPEER",
  97. Value: 10,
  98. },
  99. &cli.IntFlag{
  100. Name: "nat-ratelimit-interval",
  101. Usage: "Rate limit interval",
  102. EnvVar: "EDGEVPNNATRATELIMITINTERVAL",
  103. Value: 60,
  104. },
  105. &cli.BoolTFlag{
  106. Name: "nat-ratelimit",
  107. Usage: "Changes the default rate limiting configured in helping other peers determine their reachability status",
  108. EnvVar: "EDGEVPNNATRATELIMIT",
  109. },
  110. &cli.IntFlag{
  111. Name: "max-connections",
  112. Usage: "Max connections",
  113. EnvVar: "EDGEVPNMAXCONNS",
  114. Value: 100,
  115. },
  116. &cli.StringFlag{
  117. Name: "ledger-state",
  118. Usage: "Specify a ledger state directory",
  119. EnvVar: "EDGEVPNLEDGERSTATE",
  120. },
  121. &cli.BoolTFlag{
  122. Name: "mdns",
  123. Usage: "Enable mDNS for peer discovery",
  124. EnvVar: "EDGEVPNMDNS",
  125. },
  126. &cli.BoolTFlag{
  127. Name: "autorelay",
  128. Usage: "Automatically act as a relay if the node can accept inbound connections",
  129. EnvVar: "EDGEVPNAUTORELAY",
  130. },
  131. &cli.BoolTFlag{
  132. Name: "autorelay-v1",
  133. Usage: "Enable autorelay v1 circuits",
  134. EnvVar: "EDGEVPNAUTORELAYV1",
  135. },
  136. &cli.IntFlag{
  137. Name: "concurrency",
  138. Usage: "Number of concurrent requests to serve",
  139. Value: runtime.NumCPU(),
  140. },
  141. &cli.BoolTFlag{
  142. Name: "holepunch",
  143. Usage: "Automatically try holepunching when possible",
  144. EnvVar: "EDGEVPNHOLEPUNCH",
  145. },
  146. &cli.BoolTFlag{
  147. Name: "natservice",
  148. Usage: "Tries to determine reachability status of nodes",
  149. EnvVar: "EDGEVPNNATSERVICE",
  150. },
  151. &cli.BoolTFlag{
  152. Name: "natmap",
  153. Usage: "Tries to open a port in the firewall via upnp",
  154. EnvVar: "EDGEVPNNATMAP",
  155. },
  156. &cli.BoolTFlag{
  157. Name: "dht",
  158. Usage: "Enable DHT for peer discovery",
  159. EnvVar: "EDGEVPNDHT",
  160. },
  161. &cli.BoolTFlag{
  162. Name: "low-profile",
  163. Usage: "Enable low profile. Lowers connections usage",
  164. EnvVar: "EDGEVPNLOWPROFILE",
  165. },
  166. &cli.BoolFlag{
  167. Name: "mplex-multiplexer",
  168. Usage: "Enable mplex multiplexer.",
  169. EnvVar: "EDGEVPNMPLEX",
  170. },
  171. &cli.BoolTFlag{
  172. Name: "low-profile-vpn",
  173. Usage: "Enable low profile on VPN",
  174. EnvVar: "EDGEVPNLOWPROFILEVPN",
  175. },
  176. &cli.IntFlag{
  177. Name: "max-streams",
  178. Usage: "Number of concurrent streams",
  179. Value: 100,
  180. EnvVar: "EDGEVPNMAXSTREAMS",
  181. },
  182. &cli.StringFlag{
  183. Name: "log-level",
  184. Usage: "Specify loglevel",
  185. EnvVar: "EDGEVPNLOGLEVEL",
  186. Value: "info",
  187. },
  188. &cli.StringFlag{
  189. Name: "libp2p-log-level",
  190. Usage: "Specify libp2p loglevel",
  191. EnvVar: "EDGEVPNLIBP2PLOGLEVEL",
  192. Value: "fatal",
  193. },
  194. &cli.StringSliceFlag{
  195. Name: "discovery-bootstrap-peers",
  196. Usage: "List of discovery peers to use",
  197. EnvVar: "EDGEVPNBOOTSTRAPPEERS",
  198. },
  199. &cli.StringSliceFlag{
  200. Name: "autorelay-static-peer",
  201. Usage: "List of autorelay static peers to use",
  202. EnvVar: "EDGEVPNAUTORELAYPEERS",
  203. },
  204. &cli.StringSliceFlag{
  205. Name: "blacklist",
  206. Usage: "List of peers/cidr to gate",
  207. EnvVar: "EDGEVPNBLACKLIST",
  208. },
  209. &cli.StringFlag{
  210. Name: "token",
  211. Usage: "Specify an edgevpn token in place of a config file",
  212. EnvVar: "EDGEVPNTOKEN",
  213. },
  214. &cli.StringFlag{
  215. Name: "limit-file",
  216. Usage: "Specify an limit config (json)",
  217. EnvVar: "LIMITFILE",
  218. },
  219. &cli.StringFlag{
  220. Name: "limit-scope",
  221. Usage: "Specify a limit scope",
  222. EnvVar: "LIMITSCOPE",
  223. Value: "system",
  224. },
  225. &cli.BoolFlag{
  226. Name: "limit-config",
  227. Usage: "Enable inline resource limit configuration",
  228. EnvVar: "LIMITCONFIG",
  229. },
  230. &cli.BoolFlag{
  231. Name: "limit-enable",
  232. Usage: "Enable resource manager. (Experimental) All options prefixed with limit requires resource manager to be enabled",
  233. EnvVar: "LIMITENABLE",
  234. },
  235. &cli.BoolFlag{
  236. Name: "limit-config-dynamic",
  237. Usage: "Enable dynamic resource limit configuration",
  238. EnvVar: "LIMITCONFIGDYNAMIC",
  239. },
  240. &cli.Int64Flag{
  241. Name: "limit-config-memory",
  242. Usage: "Memory resource limit configuration",
  243. EnvVar: "LIMITCONFIGMEMORY",
  244. Value: 128,
  245. },
  246. &cli.Float64Flag{
  247. Name: "limit-config-memory-fraction",
  248. Usage: "Fraction memory resource limit configuration (dynamic)",
  249. EnvVar: "LIMITCONFIGMEMORYFRACTION",
  250. Value: 10,
  251. },
  252. &cli.Int64Flag{
  253. Name: "limit-config-min-memory",
  254. Usage: "Minimum memory resource limit configuration (dynamic)",
  255. EnvVar: "LIMITCONFIGMINMEMORY",
  256. Value: 10,
  257. },
  258. &cli.Int64Flag{
  259. Name: "limit-config-max-memory",
  260. Usage: "Maximum memory resource limit configuration (dynamic)",
  261. EnvVar: "LIMITCONFIGMAXMEMORY",
  262. Value: 200,
  263. },
  264. &cli.IntFlag{
  265. Name: "limit-config-streams",
  266. Usage: "Streams resource limit configuration",
  267. EnvVar: "LIMITCONFIGSTREAMS",
  268. Value: 200,
  269. },
  270. &cli.IntFlag{
  271. Name: "limit-config-streams-inbound",
  272. Usage: "Inbound streams resource limit configuration",
  273. EnvVar: "LIMITCONFIGSTREAMSINBOUND",
  274. Value: 30,
  275. },
  276. &cli.IntFlag{
  277. Name: "limit-config-streams-outbound",
  278. Usage: "Outbound streams resource limit configuration",
  279. EnvVar: "LIMITCONFIGSTREAMSOUTBOUND",
  280. Value: 30,
  281. },
  282. &cli.IntFlag{
  283. Name: "limit-config-conn",
  284. Usage: "Connections resource limit configuration",
  285. EnvVar: "LIMITCONFIGCONNS",
  286. Value: 200,
  287. },
  288. &cli.IntFlag{
  289. Name: "limit-config-conn-inbound",
  290. Usage: "Inbound connections resource limit configuration",
  291. EnvVar: "LIMITCONFIGCONNSINBOUND",
  292. Value: 30,
  293. },
  294. &cli.IntFlag{
  295. Name: "limit-config-conn-outbound",
  296. Usage: "Outbound connections resource limit configuration",
  297. EnvVar: "LIMITCONFIGCONNSOUTBOUND",
  298. Value: 30,
  299. },
  300. &cli.IntFlag{
  301. Name: "limit-config-fd",
  302. Usage: "Max fd resource limit configuration",
  303. EnvVar: "LIMITCONFIGFD",
  304. Value: 30,
  305. },
  306. }
  307. func displayStart(ll *logger.Logger) {
  308. ll.Info(Copyright)
  309. ll.Infof("Version: %s commit: %s", internal.Version, internal.Commit)
  310. }
  311. func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
  312. var limitConfig *node.NetLimitConfig
  313. autorelayInterval, err := time.ParseDuration(c.String("autorelay-discovery-interval"))
  314. if err != nil {
  315. autorelayInterval = 0
  316. }
  317. if c.Bool("limit-config") {
  318. limitConfig = &node.NetLimitConfig{
  319. Dynamic: c.Bool("limit-config-dynamic"),
  320. Memory: c.Int64("limit-config-memory"),
  321. MinMemory: c.Int64("limit-config-min-memory"),
  322. MaxMemory: c.Int64("limit-config-max-memory"),
  323. MemoryFraction: c.Float64("limit-config-memory-fraction"),
  324. Streams: c.Int("limit-config-streams"),
  325. StreamsInbound: c.Int("limit-config-streams-inbound"),
  326. StreamsOutbound: c.Int("limit-config-streams-outbound"),
  327. Conns: c.Int("limit-config-conn"),
  328. ConnsInbound: c.Int("limit-config-conn-inbound"),
  329. ConnsOutbound: c.Int("limit-config-conn-outbound"),
  330. FD: c.Int("limit-config-fd"),
  331. }
  332. }
  333. nc := nodeConfig.Config{
  334. NetworkConfig: c.String("config"),
  335. NetworkToken: c.String("token"),
  336. Address: c.String("address"),
  337. Router: c.String("router"),
  338. Interface: c.String("interface"),
  339. Libp2pLogLevel: c.String("libp2p-log-level"),
  340. LogLevel: c.String("log-level"),
  341. LowProfile: c.Bool("low-profile"),
  342. VPNLowProfile: c.Bool("low-profile-vpn"),
  343. Blacklist: c.StringSlice("blacklist"),
  344. Concurrency: c.Int("concurrency"),
  345. FrameTimeout: c.String("timeout"),
  346. ChannelBufferSize: c.Int("channel-buffer-size"),
  347. InterfaceMTU: c.Int("mtu"),
  348. PacketMTU: c.Int("packet-mtu"),
  349. BootstrapIface: c.Bool("bootstrap-iface"),
  350. Ledger: config.Ledger{
  351. StateDir: c.String("ledger-state"),
  352. AnnounceInterval: time.Duration(c.Int("ledger-announce-interval")) * time.Second,
  353. SyncInterval: time.Duration(c.Int("ledger-syncronization-interval")) * time.Second,
  354. },
  355. NAT: config.NAT{
  356. Service: c.Bool("natservice"),
  357. Map: c.Bool("natmap"),
  358. RateLimit: c.Bool("nat-ratelimit"),
  359. RateLimitGlobal: c.Int("nat-ratelimit-global"),
  360. RateLimitPeer: c.Int("nat-ratelimit-peer"),
  361. RateLimitInterval: time.Duration(c.Int("nat-ratelimit-interval")) * time.Second,
  362. },
  363. Discovery: config.Discovery{
  364. BootstrapPeers: c.StringSlice("discovery-bootstrap-peers"),
  365. DHT: c.Bool("dht"),
  366. MDNS: c.Bool("mdns"),
  367. Interval: time.Duration(c.Int("discovery-interval")) * time.Second,
  368. },
  369. Connection: config.Connection{
  370. AutoRelay: c.Bool("autorelay"),
  371. RelayV1: c.Bool("autorelay-v1"),
  372. MaxConnections: c.Int("max-connections"),
  373. MaxStreams: c.Int("max-streams"),
  374. HolePunch: c.Bool("holepunch"),
  375. Mplex: c.Bool("mplex-multiplexer"),
  376. StaticRelays: c.StringSlice("autorelay-static-peer"),
  377. AutoRelayDiscoveryInterval: autorelayInterval,
  378. },
  379. Limit: config.ResourceLimit{
  380. Enable: c.Bool("limit-enable"),
  381. FileLimit: c.String("limit-file"),
  382. Scope: c.String("limit-scope"),
  383. MaxConns: c.Int("max-connections"), // Turn to 0 to use other way of limiting. Files take precedence
  384. LimitConfig: limitConfig,
  385. },
  386. }
  387. lvl, err := log.LevelFromString(nc.LogLevel)
  388. if err != nil {
  389. lvl = log.LevelError
  390. }
  391. llger := logger.New(lvl)
  392. nodeOpts, vpnOpts, err := nc.ToOpts(llger)
  393. if err != nil {
  394. llger.Fatal(err.Error())
  395. }
  396. return nodeOpts, vpnOpts, llger
  397. }