main.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 = "wcdaemon"
  17. description = "Wirecat 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. tserver := flag.String("s", "localhost:50051", "The location (including port) of the remote gRPC server.")
  33. tgroup := flag.String("g", "badgroup", "The node group you are attempting to join.")
  34. 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.")
  35. tnoforward := flag.Bool("nf", false, "No Forward mode. If true, netclient will not check for IP forwarding. This may break functionality")
  36. command := flag.String("c", "required", "The command to run")
  37. flag.Parse()
  38. getID := exec.Command("id", "-u")
  39. out, err := getID.Output()
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. id, err := strconv.Atoi(string(out[:len(out)-1]))
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. if id != 0 {
  48. 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.")
  49. }
  50. _, err = exec.LookPath("wg")
  51. if err != nil {
  52. log.Println(err)
  53. log.Fatal("WireGuard not installed. Please install WireGuard (wireguard-tools) and try again.")
  54. }
  55. switch *command {
  56. case "required":
  57. fmt.Println("command flag 'c' is required. Pick one of |install|checkin|update|remove|")
  58. os.Exit(1)
  59. log.Fatal("Exiting")
  60. case "install":
  61. if !*tnoforward {
  62. forward := exec.Command("sysctl", "net.ipv4.ip_forward")
  63. out, err := forward.Output()
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. //s := strings.Split(string(out), " ", "\n")
  68. s := strings.Fields(string(out))
  69. if err != nil {
  70. log.Fatal(err)
  71. }
  72. if s[2] != "1" {
  73. 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'")
  74. }
  75. }
  76. fmt.Println("Beginning agent installation.")
  77. err := functions.Install(*taccesskey, *tpassword, *tserver, *tgroup, *tnoauto)
  78. if err != nil {
  79. fmt.Println("Error installing: ", err)
  80. fmt.Println("Cleaning up (uninstall)")
  81. err = functions.Remove()
  82. if err != nil {
  83. fmt.Println("Error uninstalling: ", err)
  84. fmt.Println("Wiping local.")
  85. err = functions.WipeLocal()
  86. if err != nil {
  87. fmt.Println("Error removing artifacts: ", err)
  88. }
  89. err = functions.RemoveSystemDServices()
  90. if err != nil {
  91. fmt.Println("Error removing services: ", err)
  92. }
  93. }
  94. os.Exit(1)
  95. }
  96. case "service-install":
  97. fmt.Println("Beginning service installation.")
  98. err := functions.ConfigureSystemD()
  99. if err != nil {
  100. fmt.Println("Error installing service: ", err)
  101. os.Exit(1)
  102. }
  103. case "service-uninstall":
  104. fmt.Println("Beginning service uninstall.")
  105. err := functions.RemoveSystemDServices()
  106. if err != nil {
  107. fmt.Println("Error installing service: ", err)
  108. os.Exit(1)
  109. }
  110. case "checkin":
  111. fmt.Println("Beginning node check in.")
  112. err := functions.CheckIn()
  113. if err != nil {
  114. fmt.Println("Error checking in: ", err)
  115. os.Exit(1)
  116. }
  117. case "remove":
  118. fmt.Println("Beginning node cleanup.")
  119. err := functions.Remove()
  120. if err != nil {
  121. /*
  122. fmt.Println("Error uninstalling: ", err)
  123. fmt.Println("Wiping local.")
  124. err = functions.WipeLocal()
  125. if err != nil {
  126. fmt.Println("Error removing artifacts: ", err)
  127. }
  128. err = functions.RemoveSystemDServices()
  129. if err != nil {
  130. fmt.Println("Error removing services: ", err)
  131. }
  132. */
  133. fmt.Println("Error deleting node: ", err)
  134. os.Exit(1)
  135. }
  136. }
  137. fmt.Println("Command " + *command + " Executed Successfully")
  138. }