util.go 11 KB

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