main.go 13 KB

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