yosaictl.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "git.aetherial.dev/aeth/yosai/pkg/daemon"
  9. dclient "git.aetherial.dev/aeth/yosai/pkg/daemonclient"
  10. "git.aetherial.dev/aeth/yosai/pkg/semaphore"
  11. )
  12. const PRIMARY_SERVER = "primary-vpn"
  13. const SECONDARY_SERVER = "secondary-vpn"
  14. func main() {
  15. var args []string
  16. args = os.Args[1:]
  17. dClient := dclient.DaemonClient{SockPath: dclient.UNIX_DOMAIN_SOCK_PATH}
  18. var rb = bytes.NewBuffer([]byte{})
  19. if strings.Contains(args[0], "ansible-") {
  20. req := semaphore.SemaphoreRequest{Target: args[2]}
  21. b, _ := json.Marshal(req)
  22. resp := dClient.Call(b, args[0], args[1])
  23. rb.Write(resp.Body)
  24. }
  25. switch args[0] {
  26. case "ansible":
  27. switch args[1] {
  28. case "bootstrap":
  29. err := dClient.BootstrapAll()
  30. if err != nil {
  31. rb.Write([]byte(err.Error()))
  32. }
  33. rb.Write([]byte("Ansible bootstrapped successfully."))
  34. }
  35. case "cloud":
  36. switch args[1] {
  37. case "delete":
  38. err := dClient.DestroyServer(args[2])
  39. if err != nil {
  40. rb.Write([]byte("Error deleting the server: " + args[2] + " Error: " + err.Error()))
  41. } else {
  42. rb.Write([]byte("Server: " + args[2] + " successfully removed."))
  43. }
  44. case "add":
  45. err := dClient.NewServer(args[2])
  46. if err != nil {
  47. rb.Write([]byte(err.Error()))
  48. }
  49. case "poll":
  50. resp, err := dClient.PollServer(args[2])
  51. if err != nil {
  52. rb.Write([]byte(err.Error()))
  53. }
  54. rb.Write(resp.Body)
  55. case "show":
  56. resp := dClient.Call([]byte(dclient.BLANK_JSON), "cloud", "show")
  57. rb.Write(resp.Body)
  58. }
  59. case "keyring":
  60. switch args[1] {
  61. case "show":
  62. if len(args) > 2 {
  63. b, _ := json.Marshal(daemon.KeyringRequest{Name: args[2]})
  64. resp := dClient.Call(b, "keyring", "show")
  65. rb.Write(resp.Body)
  66. } else {
  67. resp := dClient.Call([]byte(dclient.BLANK_JSON), "show", "all")
  68. rb.Write(resp.Body)
  69. }
  70. case "reload":
  71. resp := dClient.Call([]byte(dclient.BLANK_JSON), "keyring", "reload")
  72. rb.Write(resp.Body)
  73. }
  74. case "routes":
  75. switch args[1] {
  76. case "show":
  77. resp := dClient.ShowAllRoutes()
  78. rb.Write(resp.Body)
  79. }
  80. case "vpn-config":
  81. switch args[1] {
  82. case "save":
  83. resp := dClient.SaveWgConfig(args[2])
  84. rb.Write(resp.Body)
  85. case "show":
  86. resp := dClient.RenderWgConfig(args[2])
  87. rb.Write(resp.Body)
  88. }
  89. case "daemon":
  90. switch args[1] {
  91. case "show":
  92. resp := dClient.ShowAllRoutes()
  93. rb.Write(resp.Body)
  94. }
  95. case "config":
  96. switch args[1] {
  97. case "show":
  98. conf := dClient.GetConfig()
  99. b, _ := json.MarshalIndent(conf, " ", " ")
  100. rb.Write(b)
  101. case "save":
  102. err := dClient.ForceSave()
  103. if err != nil {
  104. rb.Write([]byte(err.Error()))
  105. }
  106. rb.Write([]byte("Daemon configuration saved."))
  107. case "server":
  108. switch args[2] {
  109. case "add":
  110. err := dClient.AddServeToConfig(args[3])
  111. if err != nil {
  112. rb.Write([]byte(err.Error()))
  113. }
  114. rb.Write([]byte("Server added."))
  115. case "delete":
  116. b, _ := json.Marshal(daemon.VpnServer{Name: args[3]})
  117. resp := dClient.Call(b, "config-server", "delete")
  118. rb.Write(resp.Body)
  119. }
  120. case "client":
  121. switch args[2] {
  122. case "add":
  123. b, _ := json.Marshal(daemon.VpnClient{Name: args[3]})
  124. resp := dClient.Call(b, "config-peer", "delete")
  125. rb.Write(resp.Body)
  126. case "delete":
  127. b, _ := json.Marshal(daemon.VpnClient{Name: args[3]})
  128. resp := dClient.Call(b, "config-peer", "add")
  129. rb.Write(resp.Body)
  130. }
  131. case "reload":
  132. err := dClient.ForceReload()
  133. if err != nil {
  134. rb.Write([]byte(err.Error()))
  135. } else {
  136. rb.Write([]byte("configuration reloaded."))
  137. }
  138. }
  139. }
  140. out := bytes.NewBuffer([]byte{})
  141. err := json.Indent(out, rb.Bytes(), "", " ")
  142. if err != nil {
  143. fmt.Println(string(rb.Bytes()))
  144. os.Exit(0)
  145. }
  146. fmt.Println(string(out.Bytes()))
  147. }