util.go 12 KB

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