util.go 13 KB

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