util.go 11 KB

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