wg-portal/internal/app/api/v0/handlers/endpoint_interfaces.go

289 lines
8.0 KiB
Go
Raw Normal View History

2023-02-12 23:13:04 +01:00
package handlers
import (
"github.com/gin-gonic/gin"
"github.com/h44z/wg-portal/internal/app"
2023-06-14 23:03:24 +02:00
"github.com/h44z/wg-portal/internal/app/api/v0/model"
"github.com/h44z/wg-portal/internal/domain"
"net/http"
2023-02-12 23:13:04 +01:00
)
type interfaceEndpoint struct {
app *app.App
authenticator *authenticationHandler
}
func (e interfaceEndpoint) GetName() string {
return "InterfaceEndpoint"
}
func (e interfaceEndpoint) RegisterRoutes(g *gin.RouterGroup, authenticator *authenticationHandler) {
apiGroup := g.Group("/interface", e.authenticator.LoggedIn())
apiGroup.GET("/prepare", e.handlePrepareGet())
apiGroup.GET("/all", e.handleAllGet())
apiGroup.GET("/get/:id", e.handleSingleGet())
apiGroup.PUT("/:id", e.handleUpdatePut())
apiGroup.DELETE("/:id", e.handleDelete())
2023-02-12 23:13:04 +01:00
apiGroup.POST("/new", e.handleCreatePost())
2023-06-14 23:03:24 +02:00
apiGroup.GET("/config/:id", e.handleConfigGet())
2023-02-12 23:13:04 +01:00
apiGroup.GET("/peers/:id", e.handlePeersGet())
}
// handlePrepareGet returns a gorm handler function.
//
// @ID interfaces_handlePrepareGet
// @Tags Interface
// @Summary Prepare a new interface.
// @Produce json
// @Success 200 {object} model.Interface
// @Failure 500 {object} model.Error
// @Router /interface/prepare [get]
func (e interfaceEndpoint) handlePrepareGet() gin.HandlerFunc {
return func(c *gin.Context) {
2023-06-14 23:03:24 +02:00
in, err := e.app.PrepareInterface(c.Request.Context())
2023-02-12 23:13:04 +01:00
if err != nil {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusInternalServerError, model.Error{
2023-02-12 23:13:04 +01:00
Code: http.StatusInternalServerError, Message: err.Error(),
})
return
}
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusOK, model.NewInterface(in))
2023-02-12 23:13:04 +01:00
}
}
// handleAllGet returns a gorm handler function.
//
// @ID interfaces_handleAllGet
// @Tags Interface
// @Summary Get all available interfaces.
// @Produce json
// @Success 200 {object} []model.Interface
// @Failure 500 {object} model.Error
// @Router /interface/all [get]
func (e interfaceEndpoint) handleAllGet() gin.HandlerFunc {
return func(c *gin.Context) {
2023-06-14 23:03:24 +02:00
interfaces, err := e.app.GetAllInterfaces(c.Request.Context())
2023-02-12 23:13:04 +01:00
if err != nil {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusInternalServerError, model.Error{
2023-02-12 23:13:04 +01:00
Code: http.StatusInternalServerError, Message: err.Error(),
})
return
}
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusOK, model.NewInterfaces(interfaces))
2023-02-12 23:13:04 +01:00
}
}
// handleSingleGet returns a gorm handler function.
//
// @ID interfaces_handleSingleGet
// @Tags Interface
// @Summary Get single interface.
// @Produce json
// @Success 200 {object} model.Interface
// @Failure 400 {object} model.Error
// @Failure 500 {object} model.Error
// @Router /interface/get/{id} [get]
func (e interfaceEndpoint) handleSingleGet() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
if id == "" {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusBadRequest, model.Error{
2023-02-12 23:13:04 +01:00
Code: http.StatusInternalServerError, Message: "missing id parameter",
})
return
}
2023-06-14 23:03:24 +02:00
iface, _, err := e.app.GetInterfaceAndPeers(c.Request.Context(), domain.InterfaceIdentifier(id))
2023-02-12 23:13:04 +01:00
if err != nil {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusInternalServerError, model.Error{
2023-02-12 23:13:04 +01:00
Code: http.StatusInternalServerError, Message: err.Error(),
})
return
}
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusOK, model.NewInterface(iface))
}
}
// handleConfigGet returns a gorm handler function.
//
// @ID interfaces_handleConfigGet
// @Tags Interface
// @Summary Get interface configuration as string.
// @Produce json
// @Success 200 {object} string
// @Failure 400 {object} model.Error
// @Failure 500 {object} model.Error
// @Router /interface/config/{id} [get]
func (e interfaceEndpoint) handleConfigGet() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, model.Error{
Code: http.StatusInternalServerError, Message: "missing id parameter",
})
return
}
c.JSON(http.StatusOK, `[Interface]
Address = 10.0.0.1/32, fd12:3456:789a::1/128
ListenPort = 51820
PrivateKey = <Private Key>
SaveConfig = true
[Peer]
PublicKey = <Client public key>
PresharedKey = <Pre-Shared Key>
AllowedIPs = 10.0.0.2/32,fd12:3456:789a::2/128
PersistentKeepalive = 25`)
2023-02-12 23:13:04 +01:00
}
}
// handleUpdatePut returns a gorm handler function.
//
// @ID interfaces_handleUpdatePut
// @Tags Interface
// @Summary Update the interface record.
// @Produce json
// @Param id path string true "The interface identifier"
// @Param request body model.Interface true "The interface data"
// @Success 200 {object} model.Interface
// @Failure 400 {object} model.Error
// @Failure 500 {object} model.Error
// @Router /interface/{id} [put]
func (e interfaceEndpoint) handleUpdatePut() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := domain.SetUserInfoFromGin(c)
id := c.Param("id")
if id == "" {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: "missing interface id"})
2023-02-12 23:13:04 +01:00
return
}
2023-06-14 23:03:24 +02:00
var in model.Interface
2023-02-12 23:13:04 +01:00
err := c.BindJSON(&in)
if err != nil {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: err.Error()})
2023-02-12 23:13:04 +01:00
return
}
if id != in.Identifier {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: "interface id mismatch"})
2023-02-12 23:13:04 +01:00
return
}
2023-06-14 23:03:24 +02:00
updatedInterface, err := e.app.UpdateInterface(ctx, model.NewDomainInterface(&in))
2023-02-12 23:13:04 +01:00
if err != nil {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusInternalServerError, model.Error{
2023-02-12 23:13:04 +01:00
Code: http.StatusInternalServerError, Message: err.Error(),
})
return
}
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusOK, model.NewInterface(updatedInterface))
2023-02-12 23:13:04 +01:00
}
}
// handleCreatePost returns a gorm handler function.
//
// @ID interfaces_handleCreatePost
// @Tags Interface
// @Summary Create the new interface record.
// @Produce json
// @Param request body model.Interface true "The interface data"
// @Success 200 {object} model.Interface
// @Failure 400 {object} model.Error
// @Failure 500 {object} model.Error
// @Router /interface/new [post]
func (e interfaceEndpoint) handleCreatePost() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := domain.SetUserInfoFromGin(c)
2023-06-14 23:03:24 +02:00
var in model.Interface
2023-02-12 23:13:04 +01:00
err := c.BindJSON(&in)
if err != nil {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: err.Error()})
2023-02-12 23:13:04 +01:00
return
}
2023-06-14 23:03:24 +02:00
newInterface, err := e.app.CreateInterface(ctx, model.NewDomainInterface(&in))
2023-02-12 23:13:04 +01:00
if err != nil {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusInternalServerError, model.Error{
2023-02-12 23:13:04 +01:00
Code: http.StatusInternalServerError, Message: err.Error(),
})
return
}
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusOK, model.NewInterface(newInterface))
2023-02-12 23:13:04 +01:00
}
}
// handlePeersGet returns a gorm handler function.
//
// @ID interfaces_handlePeersGet
// @Tags Interface
// @Summary Get peers for the given interface.
// @Produce json
// @Success 200 {object} []model.Peer
// @Failure 500 {object} model.Error
// @Router /interface/peers/{id} [get]
func (e interfaceEndpoint) handlePeersGet() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
if id == "" {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusBadRequest, model.Error{
2023-02-12 23:13:04 +01:00
Code: http.StatusInternalServerError, Message: "missing id parameter",
})
return
}
2023-06-14 23:03:24 +02:00
_, peers, err := e.app.GetInterfaceAndPeers(c.Request.Context(), domain.InterfaceIdentifier(id))
2023-02-12 23:13:04 +01:00
if err != nil {
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusInternalServerError, model.Error{
2023-02-12 23:13:04 +01:00
Code: http.StatusInternalServerError, Message: err.Error(),
})
return
}
2023-06-14 23:03:24 +02:00
c.JSON(http.StatusOK, model.NewPeers(peers))
2023-02-12 23:13:04 +01:00
}
}
// handleDelete returns a gorm handler function.
//
// @ID interfaces_handleDelete
// @Tags Interface
// @Summary Delete the interface record.
// @Produce json
// @Param id path string true "The interface identifier"
// @Success 204 "No content if deletion was successful"
// @Failure 400 {object} model.Error
// @Failure 500 {object} model.Error
// @Router /interface/{id} [delete]
func (e interfaceEndpoint) handleDelete() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := domain.SetUserInfoFromGin(c)
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: "missing interface id"})
return
}
err := e.app.DeleteInterface(ctx, domain.InterfaceIdentifier(id))
if err != nil {
c.JSON(http.StatusInternalServerError, model.Error{
Code: http.StatusInternalServerError, Message: err.Error(),
})
return
}
c.Status(http.StatusNoContent)
}
}