routes.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package daemon
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path"
  6. wg "git.aetherial.dev/aeth/yosai/pkg/wireguard/centos"
  7. )
  8. type AddServerRequest struct {
  9. Name string `json:"name"`
  10. Image string `json:"image"`
  11. Region string `json:"region"`
  12. Type string `json:"type"`
  13. }
  14. type AddServerResponse struct {
  15. Id int `json:"id"`
  16. Ipv4 []string `json:"ipv4"`
  17. Label string `json:"label"`
  18. Created string `json:"created"`
  19. Region string `json:"region"`
  20. Status string `json:"status"`
  21. }
  22. type StartWireguardRequest struct {
  23. InterfaceName string `json:"interface_name"`
  24. }
  25. type AddHostRequest struct {
  26. Target string `json:"target"`
  27. }
  28. type ConfigRenderRequest struct {
  29. Client string `json:"client"`
  30. Server string `json:"server"`
  31. OutputToFile bool `json:"output_to_file"`
  32. }
  33. // Client for building internal Daemon route requests
  34. func (c *Context) CreateServer(msg SockMessage) SockMessage {
  35. return *NewSockMessage(MsgResponse, REQUEST_OK, []byte("Server provisioned and indexed into Semaphore."))
  36. }
  37. /*
  38. Route handler for all of the exposed functions that the daemon allows for
  39. :param msg: a SockMessage containing all of the request information
  40. */
  41. func (c *Context) DaemonRouter(msg SockMessage) SockMessage {
  42. switch msg.Method {
  43. case "wg-up":
  44. var req StartWireguardRequest
  45. err := json.Unmarshal(msg.Body, &req)
  46. if err != nil {
  47. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  48. }
  49. out, err := wg.ChangeWgInterfaceState(path.Join(c.Config.HostInfo.WireguardSavePath, req.InterfaceName+".conf"), "up")
  50. if err != nil {
  51. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  52. }
  53. return *NewSockMessage(MsgResponse, REQUEST_OK, out)
  54. case "wg-down":
  55. var req StartWireguardRequest
  56. err := json.Unmarshal(msg.Body, &req)
  57. if err != nil {
  58. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  59. }
  60. out, err := wg.ChangeWgInterfaceState(path.Join(c.Config.HostInfo.WireguardSavePath, req.InterfaceName+".conf"), "down")
  61. if err != nil {
  62. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  63. }
  64. return *NewSockMessage(MsgResponse, REQUEST_OK, out)
  65. case "render-config":
  66. var req ConfigRenderRequest
  67. err := json.Unmarshal(msg.Body, &req)
  68. if err != nil {
  69. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  70. }
  71. serverKeypair, err := c.keyring.GetKey(req.Server + "_" + c.Keytags.WgKeypairKeyname())
  72. if err != nil {
  73. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  74. }
  75. clientKeypair, err := c.keyring.GetKey(req.Client + "_" + c.Keytags.WgKeypairKeyname())
  76. if err != nil {
  77. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  78. }
  79. server, err := c.Config.GetServer(req.Server)
  80. if err != nil {
  81. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  82. }
  83. client, err := c.Config.GetClient(req.Client)
  84. if err != nil {
  85. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  86. }
  87. seed := wg.WireguardTemplateSeed{
  88. VpnClientPrivateKey: clientKeypair.GetSecret(),
  89. VpnClientAddress: client.VpnIpv4.String() + "/32",
  90. Peers: []wg.WireguardTemplatePeer{
  91. wg.WireguardTemplatePeer{
  92. Pubkey: serverKeypair.GetPublic(),
  93. Address: server.WanIpv4,
  94. Port: c.Config.VpnServerPort(),
  95. },
  96. }}
  97. cfg, err := wg.RenderClientConfiguration(seed)
  98. if err != nil {
  99. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  100. }
  101. if req.OutputToFile == true {
  102. fpath := path.Join(c.Config.HostInfo.WireguardSavePath, server.Name+".conf")
  103. err = os.WriteFile(fpath, cfg, 0666)
  104. if err != nil {
  105. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  106. }
  107. return *NewSockMessage(MsgResponse, REQUEST_OK, []byte("Configuration saved to: "+fpath))
  108. }
  109. return *NewSockMessage(MsgResponse, REQUEST_OK, cfg)
  110. default:
  111. return *NewSockMessage(MsgResponse, REQUEST_UNRESOLVED, []byte("Unresolved Method"))
  112. }
  113. }