util.go 13 KB

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