main.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 network 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. tname := flag.String("name", "noname", "give the node a name at runtime")
  34. tserver := flag.String("s", "localhost:50051", "The location (including port) of the remote gRPC server.")
  35. tnetwork := flag.String("n", "nonetwork", "The node network you are attempting to join.")
  36. tnoauto := flag.Bool("na", false, "No auto mode. If true, netclient will not be installed as a system service and you will have to retrieve updates manually via checkin command.")
  37. tdnsoff := flag.Bool("dnsoff", false, "No dns mode. If true, netclient will not alter system dns.")
  38. tnoforward := flag.Bool("nf", false, "No Forward mode. If true, netclient will not check for IP forwarding. This may break functionality")
  39. command := flag.String("c", "required", "The command to run")
  40. flag.Parse()
  41. getID := exec.Command("id", "-u")
  42. out, err := getID.Output()
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. id, err := strconv.Atoi(string(out[:len(out)-1]))
  47. if err != nil {
  48. log.Fatal(err)
  49. }
  50. if id != 0 {
  51. 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.")
  52. }
  53. _, err = exec.LookPath("wg")
  54. if err != nil {
  55. log.Println(err)
  56. log.Fatal("WireGuard not installed. Please install WireGuard (wireguard-tools) and try again.")
  57. }
  58. switch *command {
  59. case "getport":
  60. portno, err := functions.GetFreePort(51821)
  61. fmt.Printf("Port Number: %v", portno)
  62. fmt.Println("")
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. case "required":
  67. fmt.Println("command flag 'c' is required. Pick one of |install|checkin|update|remove|")
  68. os.Exit(1)
  69. log.Fatal("Exiting")
  70. case "install":
  71. if *taccesstoken == "badtoken" && (*tnetwork == "nonetwork" || *tnetwork == "") {
  72. fmt.Println("Required, '-n'. No network provided. Exiting.")
  73. os.Exit(1)
  74. }
  75. if !*tnoforward {
  76. forward := exec.Command("sysctl", "net.ipv4.ip_forward")
  77. out, err := forward.Output()
  78. if err != nil {
  79. log.Fatal(err)
  80. }
  81. //s := strings.Split(string(out), " ", "\n")
  82. s := strings.Fields(string(out))
  83. if err != nil {
  84. log.Fatal(err)
  85. }
  86. if s[2] != "1" {
  87. 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'")
  88. }
  89. }
  90. fmt.Println("Beginning agent installation.")
  91. err := functions.Install(*taccesskey, *tpassword, *tserver, *tnetwork, *tnoauto, *taccesstoken, *tname, *tdnsoff)
  92. if err != nil {
  93. fmt.Println("Error encountered while installing.")
  94. if !strings.Contains(err.Error(), "ALREADY_INSTALLED") {
  95. fmt.Println("Error installing: ", err)
  96. fmt.Println("Cleaning up (uninstall)")
  97. err = functions.Remove(*tnetwork)
  98. if err != nil {
  99. fmt.Println("Error uninstalling: ", err)
  100. fmt.Println("Wiping local.")
  101. err = functions.WipeLocal(*tnetwork)
  102. if err != nil {
  103. fmt.Println("Error removing artifacts: ", err)
  104. }
  105. err = functions.RemoveSystemDServices(*tnetwork)
  106. if err != nil {
  107. fmt.Println("Error removing services: ", err)
  108. }
  109. }
  110. os.Exit(1)
  111. } else {
  112. fmt.Println(err.Error())
  113. os.Exit(1)
  114. }
  115. }
  116. /*
  117. case "service-install":
  118. fmt.Println("Beginning service installation.")
  119. err := functions.ConfigureSystemD()
  120. if err != nil {
  121. fmt.Println("Error installing service: ", err)
  122. os.Exit(1)
  123. }
  124. case "service-uninstall":
  125. fmt.Println("Beginning service uninstall.")
  126. err := functions.RemoveSystemDServices()
  127. if err != nil {
  128. fmt.Println("Error installing service: ", err)
  129. os.Exit(1)
  130. }
  131. */
  132. case "checkin":
  133. if *tnetwork == "nonetwork" || *tnetwork == "" {
  134. fmt.Println("Required, '-n'. No network provided. Exiting.")
  135. os.Exit(1)
  136. }
  137. fmt.Println("Beginning node check in for network " + *tnetwork)
  138. err := functions.CheckIn(*tnetwork)
  139. if err != nil {
  140. fmt.Println("Error checking in: ", err)
  141. os.Exit(1)
  142. }
  143. case "remove":
  144. if *tnetwork == "nonetwork" || *tnetwork == "" {
  145. fmt.Println("Required, '-n'. No network provided. Exiting.")
  146. os.Exit(1)
  147. }
  148. fmt.Println("Beginning node cleanup.")
  149. err := functions.Remove(*tnetwork)
  150. if err != nil {
  151. /*
  152. fmt.Println("Error uninstalling: ", err)
  153. fmt.Println("Wiping local.")
  154. err = functions.WipeLocal()
  155. if err != nil {
  156. fmt.Println("Error removing artifacts: ", err)
  157. }
  158. err = functions.RemoveSystemDServices()
  159. if err != nil {
  160. fmt.Println("Error removing services: ", err)
  161. }
  162. */
  163. fmt.Println("Error deleting node: ", err)
  164. os.Exit(1)
  165. }
  166. default:
  167. fmt.Println("You must select from the following commands: install|remove|checkin", err)
  168. os.Exit(1)
  169. }
  170. fmt.Println("Command " + *command + " Executed Successfully")
  171. }