main.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package main
  2. import (
  3. "errors"
  4. "log"
  5. "os"
  6. "os/exec"
  7. "strconv"
  8. "github.com/gravitl/netmaker/netclient/command"
  9. "github.com/gravitl/netmaker/netclient/config"
  10. "github.com/gravitl/netmaker/netclient/local"
  11. "github.com/urfave/cli/v2"
  12. )
  13. func main() {
  14. app := cli.NewApp()
  15. app.Name = "Netclient CLI"
  16. app.Usage = "Netmaker's netclient agent and CLI. Used to perform interactions with Netmaker server and set local WireGuard config."
  17. app.Version = "v0.7.3"
  18. cliFlags := []cli.Flag{
  19. &cli.StringFlag{
  20. Name: "network",
  21. Aliases: []string{"n"},
  22. EnvVars: []string{"NETCLIENT_NETWORK"},
  23. Value: "all",
  24. Usage: "Network to perform specified action against.",
  25. },
  26. &cli.StringFlag{
  27. Name: "password",
  28. Aliases: []string{"p"},
  29. EnvVars: []string{"NETCLIENT_PASSWORD"},
  30. Value: "",
  31. Usage: "Password for authenticating with netmaker.",
  32. },
  33. &cli.StringFlag{
  34. Name: "endpoint",
  35. Aliases: []string{"e"},
  36. EnvVars: []string{"NETCLIENT_ENDPOINT"},
  37. Value: "",
  38. Usage: "Reachable (usually public) address for WireGuard (not the private WG address).",
  39. },
  40. &cli.StringFlag{
  41. Name: "macaddress",
  42. Aliases: []string{"m"},
  43. EnvVars: []string{"NETCLIENT_MACADDRESS"},
  44. Value: "",
  45. Usage: "Mac Address for this machine. Used as a unique identifier within Netmaker network.",
  46. },
  47. &cli.StringFlag{
  48. Name: "publickey",
  49. Aliases: []string{"pubkey"},
  50. EnvVars: []string{"NETCLIENT_PUBLICKEY"},
  51. Value: "",
  52. Usage: "Public Key for WireGuard Interface.",
  53. },
  54. &cli.StringFlag{
  55. Name: "privatekey",
  56. Aliases: []string{"privkey"},
  57. EnvVars: []string{"NETCLIENT_PRIVATEKEY"},
  58. Value: "",
  59. Usage: "Private Key for WireGuard Interface.",
  60. },
  61. &cli.StringFlag{
  62. Name: "port",
  63. EnvVars: []string{"NETCLIENT_PORT"},
  64. Value: "",
  65. Usage: "Port for WireGuard Interface.",
  66. },
  67. &cli.IntFlag{
  68. Name: "keepalive",
  69. EnvVars: []string{"NETCLIENT_KEEPALIVE"},
  70. Value: 0,
  71. Usage: "Default PersistentKeepAlive for Peers in WireGuard Interface.",
  72. },
  73. &cli.StringFlag{
  74. Name: "operatingsystem",
  75. Aliases: []string{"os"},
  76. EnvVars: []string{"NETCLIENT_OS"},
  77. Value: "",
  78. Usage: "Identifiable name for machine within Netmaker network.",
  79. },
  80. &cli.StringFlag{
  81. Name: "name",
  82. EnvVars: []string{"NETCLIENT_NAME"},
  83. Value: "",
  84. Usage: "Identifiable name for machine within Netmaker network.",
  85. },
  86. &cli.StringFlag{
  87. Name: "localaddress",
  88. EnvVars: []string{"NETCLIENT_LOCALADDRESS"},
  89. Value: "",
  90. Usage: "Local address for machine. Can be used in place of Endpoint for machines on the same LAN.",
  91. },
  92. &cli.StringFlag{
  93. Name: "address",
  94. Aliases: []string{"a"},
  95. EnvVars: []string{"NETCLIENT_ADDRESS"},
  96. Value: "",
  97. Usage: "WireGuard address for machine within Netmaker network.",
  98. },
  99. &cli.StringFlag{
  100. Name: "addressIPv6",
  101. Aliases: []string{"a6"},
  102. EnvVars: []string{"NETCLIENT_ADDRESSIPV6"},
  103. Value: "",
  104. Usage: "WireGuard address for machine within Netmaker network.",
  105. },
  106. &cli.StringFlag{
  107. Name: "interface",
  108. Aliases: []string{"i"},
  109. EnvVars: []string{"NETCLIENT_INTERFACE"},
  110. Value: "",
  111. Usage: "WireGuard local network interface name.",
  112. },
  113. &cli.StringFlag{
  114. Name: "apiserver",
  115. EnvVars: []string{"NETCLIENT_API_SERVER"},
  116. Value: "",
  117. Usage: "Address + GRPC Port (e.g. 1.2.3.4:50051) of Netmaker server.",
  118. },
  119. &cli.StringFlag{
  120. Name: "grpcserver",
  121. EnvVars: []string{"NETCLIENT_GRPC_SERVER"},
  122. Value: "",
  123. Usage: "Address + API Port (e.g. 1.2.3.4:8081) of Netmaker server.",
  124. },
  125. &cli.StringFlag{
  126. Name: "key",
  127. Aliases: []string{"k"},
  128. EnvVars: []string{"NETCLIENT_ACCESSKEY"},
  129. Value: "",
  130. Usage: "Access Key for signing up machine with Netmaker server during initial 'add'.",
  131. },
  132. &cli.StringFlag{
  133. Name: "token",
  134. Aliases: []string{"t"},
  135. EnvVars: []string{"NETCLIENT_ACCESSTOKEN"},
  136. Value: "",
  137. Usage: "Access Token for signing up machine with Netmaker server during initial 'add'.",
  138. },
  139. &cli.StringFlag{
  140. Name: "localrange",
  141. EnvVars: []string{"NETCLIENT_LOCALRANGE"},
  142. Value: "",
  143. Usage: "Local Range if network is local, for instance 192.168.1.0/24.",
  144. },
  145. &cli.StringFlag{
  146. Name: "dnson",
  147. EnvVars: []string{"NETCLIENT_DNS"},
  148. Value: "yes",
  149. Usage: "Sets private dns if 'yes'. Ignores if 'no'. Will retrieve from network if unset.",
  150. },
  151. &cli.StringFlag{
  152. Name: "islocal",
  153. EnvVars: []string{"NETCLIENT_IS_LOCAL"},
  154. Value: "",
  155. Usage: "Sets endpoint to local address if 'yes'. Ignores if 'no'. Will retrieve from network if unset.",
  156. },
  157. &cli.StringFlag{
  158. Name: "isdualstack",
  159. EnvVars: []string{"NETCLIENT_IS_DUALSTACK"},
  160. Value: "",
  161. Usage: "Sets ipv6 address if 'yes'. Ignores if 'no'. Will retrieve from network if unset.",
  162. },
  163. &cli.StringFlag{
  164. Name: "udpholepunch",
  165. EnvVars: []string{"NETCLIENT_UDP_HOLEPUNCH"},
  166. Value: "",
  167. Usage: "Turns on udp holepunching if 'yes'. Ignores if 'no'. Will retrieve from network if unset.",
  168. },
  169. &cli.StringFlag{
  170. Name: "ipforwarding",
  171. EnvVars: []string{"NETCLIENT_IPFORWARDING"},
  172. Value: "on",
  173. Usage: "Sets ip forwarding on if 'on'. Ignores if 'off'. On by default.",
  174. },
  175. &cli.StringFlag{
  176. Name: "postup",
  177. EnvVars: []string{"NETCLIENT_POSTUP"},
  178. Value: "",
  179. Usage: "Sets PostUp command for WireGuard.",
  180. },
  181. &cli.StringFlag{
  182. Name: "postdown",
  183. EnvVars: []string{"NETCLIENT_POSTDOWN"},
  184. Value: "",
  185. Usage: "Sets PostDown command for WireGuard.",
  186. },
  187. &cli.StringFlag{
  188. Name: "daemon",
  189. EnvVars: []string{"NETCLIENT_DAEMON"},
  190. Value: "on",
  191. Usage: "Installs daemon if 'on'. Ignores if 'off'. On by default.",
  192. },
  193. &cli.StringFlag{
  194. Name: "roaming",
  195. EnvVars: []string{"NETCLIENT_ROAMING"},
  196. Value: "on",
  197. Usage: "Checks for IP changes if 'on'. Ignores if 'off'. On by default.",
  198. },
  199. }
  200. app.Commands = []*cli.Command{
  201. {
  202. Name: "join",
  203. Usage: "Join a Netmaker network.",
  204. Flags: cliFlags,
  205. Action: func(c *cli.Context) error {
  206. cfg, pvtKey, err := config.GetCLIConfig(c)
  207. if err != nil {
  208. return err
  209. }
  210. if cfg.Network == "all" {
  211. err = errors.New("No network provided.")
  212. return err
  213. }
  214. if cfg.Server.GRPCAddress == "" {
  215. err = errors.New("No server address provided.")
  216. return err
  217. }
  218. err = command.Join(cfg, pvtKey)
  219. return err
  220. },
  221. },
  222. {
  223. Name: "leave",
  224. Usage: "Leave a Netmaker network.",
  225. Flags: cliFlags,
  226. // the action, or code that will be executed when
  227. // we execute our `ns` command
  228. Action: func(c *cli.Context) error {
  229. cfg, _, err := config.GetCLIConfig(c)
  230. if err != nil {
  231. return err
  232. }
  233. err = command.Leave(cfg)
  234. return err
  235. },
  236. },
  237. {
  238. Name: "checkin",
  239. Usage: "Checks for local changes and then checks into the specified Netmaker network to ask about remote changes.",
  240. Flags: cliFlags,
  241. // the action, or code that will be executed when
  242. // we execute our `ns` command
  243. Action: func(c *cli.Context) error {
  244. cfg, _, err := config.GetCLIConfig(c)
  245. if err != nil {
  246. return err
  247. }
  248. err = command.CheckIn(cfg)
  249. return err
  250. },
  251. },
  252. {
  253. Name: "push",
  254. Usage: "Push configuration changes to server.",
  255. Flags: cliFlags,
  256. // the action, or code that will be executed when
  257. // we execute our `ns` command
  258. Action: func(c *cli.Context) error {
  259. cfg, _, err := config.GetCLIConfig(c)
  260. if err != nil {
  261. return err
  262. }
  263. err = command.Push(cfg)
  264. return err
  265. },
  266. },
  267. {
  268. Name: "pull",
  269. Usage: "Pull latest configuration and peers from server.",
  270. Flags: cliFlags,
  271. // the action, or code that will be executed when
  272. // we execute our `ns` command
  273. Action: func(c *cli.Context) error {
  274. cfg, _, err := config.GetCLIConfig(c)
  275. if err != nil {
  276. return err
  277. }
  278. err = command.Pull(cfg)
  279. return err
  280. },
  281. },
  282. {
  283. Name: "list",
  284. Usage: "Get list of networks.",
  285. Flags: cliFlags,
  286. // the action, or code that will be executed when
  287. // we execute our `ns` command
  288. Action: func(c *cli.Context) error {
  289. cfg, _, err := config.GetCLIConfig(c)
  290. if err != nil {
  291. return err
  292. }
  293. err = command.List(cfg)
  294. return err
  295. },
  296. },
  297. {
  298. Name: "uninstall",
  299. Usage: "Uninstall the netclient system service.",
  300. Flags: cliFlags,
  301. // the action, or code that will be executed when
  302. // we execute our `ns` command
  303. Action: func(c *cli.Context) error {
  304. err := command.Uninstall()
  305. return err
  306. },
  307. },
  308. }
  309. // start our application
  310. out, err := local.RunCmd("id -u")
  311. if err != nil {
  312. log.Fatal(out, err)
  313. }
  314. id, err := strconv.Atoi(string(out[:len(out)-1]))
  315. if err != nil {
  316. log.Fatal(err)
  317. }
  318. if id != 0 {
  319. log.Fatal("This program must be run with elevated privileges (sudo). This program installs a SystemD service and configures WireGuard and networking rules. Please re-run with sudo/root.")
  320. }
  321. _, err = exec.LookPath("wg")
  322. if err != nil {
  323. log.Println(err)
  324. log.Fatal("WireGuard not installed. Please install WireGuard (wireguard-tools) and try again.")
  325. }
  326. err = app.Run(os.Args)
  327. if err != nil {
  328. log.Fatal(err)
  329. }
  330. }