util.go 13 KB

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