main.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gravitl/netmaker/netclient/functions"
  5. "golang.zx2c4.com/wireguard/wgctrl"
  6. nodepb "github.com/gravitl/netmaker/grpc"
  7. "flag"
  8. "os"
  9. "os/exec"
  10. "strconv"
  11. "strings"
  12. "log"
  13. )
  14. const (
  15. // name of the service
  16. name = "netclient"
  17. description = "Netmaker Daemon Service"
  18. )
  19. var password string
  20. var group string
  21. var server string
  22. var accesskey string
  23. var (
  24. wgclient *wgctrl.Client
  25. )
  26. var (
  27. wcclient nodepb.NodeServiceClient
  28. )
  29. func main() {
  30. tpassword := flag.String("p", "changeme", "This node's password for accessing the server regularly")
  31. taccesskey := flag.String("k", "badkey", "an access key generated by the server and used for one-time access (install only)")
  32. taccesstoken := flag.String("t", "badtoken", "an token generated by the server and used for one-time access (install only)")
  33. tserver := flag.String("s", "localhost:50051", "The location (including port) of the remote gRPC server.")
  34. tnetwork := flag.String("n", "nonetwork", "The node group you are attempting to join.")
  35. tnoauto := flag.Bool("na", false, "No auto mode. If true, netmclient will not be installed as a system service and you will have to retrieve updates manually via checkin command.")
  36. tnoforward := flag.Bool("nf", false, "No Forward mode. If true, netclient will not check for IP forwarding. This may break functionality")
  37. command := flag.String("c", "required", "The command to run")
  38. flag.Parse()
  39. getID := exec.Command("id", "-u")
  40. out, err := getID.Output()
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. id, err := strconv.Atoi(string(out[:len(out)-1]))
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. if id != 0 {
  49. 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.")
  50. }
  51. _, err = exec.LookPath("wg")
  52. if err != nil {
  53. log.Println(err)
  54. log.Fatal("WireGuard not installed. Please install WireGuard (wireguard-tools) and try again.")
  55. }
  56. switch *command {
  57. case "getport":
  58. portno, err := functions.GetFreePort(51821)
  59. fmt.Printf("Port Number: %v", portno)
  60. fmt.Println("")
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. case "required":
  65. fmt.Println("command flag 'c' is required. Pick one of |install|checkin|update|remove|")
  66. os.Exit(1)
  67. log.Fatal("Exiting")
  68. case "install":
  69. if *taccesstoken == "badtoken" && (*tnetwork == "nonetwork" || *tnetwork == "") {
  70. fmt.Println("Required, '-n'. No network provided. Exiting.")
  71. os.Exit(1)
  72. }
  73. if !*tnoforward {
  74. forward := exec.Command("sysctl", "net.ipv4.ip_forward")
  75. out, err := forward.Output()
  76. if err != nil {
  77. log.Fatal(err)
  78. }
  79. //s := strings.Split(string(out), " ", "\n")
  80. s := strings.Fields(string(out))
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. if s[2] != "1" {
  85. log.Fatal("It is recommended to enable IP Forwarding. Current status is: " + s[2] + ", but should be 1. if you would like to run without IP Forwarding, re-run with flag '-nf true'")
  86. }
  87. }
  88. fmt.Println("Beginning agent installation.")
  89. err := functions.Install(*taccesskey, *tpassword, *tserver, *tnetwork, *tnoauto, *taccesstoken)
  90. if err != nil {
  91. fmt.Println("Error installing: ", err)
  92. fmt.Println("Cleaning up (uninstall)")
  93. err = functions.Remove(*tnetwork)
  94. if err != nil {
  95. fmt.Println("Error uninstalling: ", err)
  96. fmt.Println("Wiping local.")
  97. err = functions.WipeLocal(*tnetwork)
  98. if err != nil {
  99. fmt.Println("Error removing artifacts: ", err)
  100. }
  101. err = functions.RemoveSystemDServices(*tnetwork)
  102. if err != nil {
  103. fmt.Println("Error removing services: ", err)
  104. }
  105. }
  106. os.Exit(1)
  107. }
  108. /*
  109. case "service-install":
  110. fmt.Println("Beginning service installation.")
  111. err := functions.ConfigureSystemD()
  112. if err != nil {
  113. fmt.Println("Error installing service: ", err)
  114. os.Exit(1)
  115. }
  116. case "service-uninstall":
  117. fmt.Println("Beginning service uninstall.")
  118. err := functions.RemoveSystemDServices()
  119. if err != nil {
  120. fmt.Println("Error installing service: ", err)
  121. os.Exit(1)
  122. }
  123. */
  124. case "checkin":
  125. if *tnetwork == "nonetwork" || *tnetwork == "" {
  126. fmt.Println("Required, '-n'. No network provided. Exiting.")
  127. os.Exit(1)
  128. }
  129. fmt.Println("Beginning node check in for group " + *tnetwork)
  130. err := functions.CheckIn(*tnetwork)
  131. if err != nil {
  132. fmt.Println("Error checking in: ", err)
  133. os.Exit(1)
  134. }
  135. case "remove":
  136. if *tnetwork == "nonetwork" || *tnetwork == "" {
  137. fmt.Println("Required, '-n'. No network provided. Exiting.")
  138. os.Exit(1)
  139. }
  140. fmt.Println("Beginning node cleanup.")
  141. err := functions.Remove(*tnetwork)
  142. if err != nil {
  143. /*
  144. fmt.Println("Error uninstalling: ", err)
  145. fmt.Println("Wiping local.")
  146. err = functions.WipeLocal()
  147. if err != nil {
  148. fmt.Println("Error removing artifacts: ", err)
  149. }
  150. err = functions.RemoveSystemDServices()
  151. if err != nil {
  152. fmt.Println("Error removing services: ", err)
  153. }
  154. */
  155. fmt.Println("Error deleting node: ", err)
  156. os.Exit(1)
  157. }
  158. default:
  159. fmt.Println("You must select from the following commands: install|remove|checkin", err)
  160. os.Exit(1)
  161. }
  162. fmt.Println("Command " + *command + " Executed Successfully")
  163. }