routes.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. Render the wireguard configuration seed to be used when templating into the config file
  39. :param req: the struct containing the target client/server pair for the configuration
  40. */
  41. func (c *Context) configSeed(req ConfigRenderRequest) (wg.WireguardTemplateSeed, error) {
  42. var seed wg.WireguardTemplateSeed
  43. serverKeypair, err := c.keyring.GetKey(req.Server + "_" + c.Keytags.WgKeypairKeyname())
  44. if err != nil {
  45. return seed, err
  46. }
  47. clientKeypair, err := c.keyring.GetKey(req.Client + "_" + c.Keytags.WgKeypairKeyname())
  48. if err != nil {
  49. return seed, err
  50. }
  51. server, err := c.Config.GetServer(req.Server)
  52. if err != nil {
  53. return seed, err
  54. }
  55. client, err := c.Config.GetClient(req.Client)
  56. if err != nil {
  57. return seed, err
  58. }
  59. seed = wg.WireguardTemplateSeed{
  60. VpnClientPrivateKey: clientKeypair.GetSecret(),
  61. VpnClientAddress: client.VpnIpv4.String() + "/32",
  62. Peers: []wg.WireguardTemplatePeer{
  63. {
  64. Pubkey: serverKeypair.GetPublic(),
  65. Address: server.WanIpv4,
  66. Port: c.Config.VpnServerPort(),
  67. },
  68. }}
  69. return seed, nil
  70. }
  71. /*
  72. wrapping the VPN show configuration function in a route friendly interface
  73. :param msg: a message to parse from the daemon socket
  74. */
  75. func (c *Context) VpnShowHandler(msg SockMessage) SockMessage {
  76. var req ConfigRenderRequest
  77. err := json.Unmarshal(msg.Body, &req)
  78. if err != nil {
  79. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  80. }
  81. seed, err := c.configSeed(req)
  82. if err != nil {
  83. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  84. }
  85. cfg, err := wg.RenderClientConfiguration(seed)
  86. if err != nil {
  87. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  88. }
  89. return *NewSockMessage(MsgResponse, REQUEST_OK, cfg)
  90. }
  91. /*
  92. wrapping the VPN save configuration function in a route friendly interface
  93. :param msg: a message to parse from the daemon socket
  94. */
  95. func (c *Context) VpnSaveHandler(msg SockMessage) SockMessage {
  96. var req ConfigRenderRequest
  97. err := json.Unmarshal(msg.Body, &req)
  98. if err != nil {
  99. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  100. }
  101. seed, err := c.configSeed(req)
  102. if err != nil {
  103. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  104. }
  105. cfg, err := wg.RenderClientConfiguration(seed)
  106. if err != nil {
  107. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  108. }
  109. fpath := path.Join(c.Config.HostInfo.WireguardSavePath, req.Server+".conf")
  110. err = os.WriteFile(fpath, cfg, 0666)
  111. if err != nil {
  112. return *NewSockMessage(MsgResponse, REQUEST_FAILED, []byte(err.Error()))
  113. }
  114. return *NewSockMessage(MsgResponse, REQUEST_OK, []byte("Configuration saved to: "+fpath))
  115. }
  116. type VpnRouter struct {
  117. routes map[Method]func(SockMessage) SockMessage
  118. }
  119. func (v *VpnRouter) Register(method Method, callable func(SockMessage) SockMessage) {
  120. v.routes[method] = callable
  121. }
  122. func (v *VpnRouter) Routes() map[Method]func(SockMessage) SockMessage {
  123. return v.routes
  124. }
  125. func NewVpnRouter() *VpnRouter {
  126. return &VpnRouter{routes: map[Method]func(SockMessage) SockMessage{}}
  127. }