2023-02-12 23:13:04 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2023-06-13 23:50:32 +02:00
|
|
|
model2 "github.com/h44z/wg-portal/internal/app/api/v0/model"
|
2023-02-12 23:13:04 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/h44z/wg-portal/internal/domain"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/h44z/wg-portal/internal/app"
|
|
|
|
)
|
|
|
|
|
|
|
|
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.POST("/new", e.handleCreatePost())
|
|
|
|
|
|
|
|
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) {
|
|
|
|
in, err := e.app.WireGuard.PrepareInterface(c.Request.Context())
|
|
|
|
if err != nil {
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusInternalServerError, model2.Error{
|
2023-02-12 23:13:04 +01:00
|
|
|
Code: http.StatusInternalServerError, Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusOK, model2.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) {
|
|
|
|
interfaces, err := e.app.WireGuard.GetAllInterfaces(c.Request.Context())
|
|
|
|
if err != nil {
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusInternalServerError, model2.Error{
|
2023-02-12 23:13:04 +01:00
|
|
|
Code: http.StatusInternalServerError, Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusOK, model2.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-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusBadRequest, model2.Error{
|
2023-02-12 23:13:04 +01:00
|
|
|
Code: http.StatusInternalServerError, Message: "missing id parameter",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
iface, _, err := e.app.WireGuard.GetInterfaceAndPeers(c.Request.Context(), domain.InterfaceIdentifier(id))
|
|
|
|
if err != nil {
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusInternalServerError, model2.Error{
|
2023-02-12 23:13:04 +01:00
|
|
|
Code: http.StatusInternalServerError, Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusOK, model2.NewInterface(iface))
|
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-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusBadRequest, model2.Error{Code: http.StatusBadRequest, Message: "missing interface id"})
|
2023-02-12 23:13:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-13 23:50:32 +02:00
|
|
|
var in model2.Interface
|
2023-02-12 23:13:04 +01:00
|
|
|
err := c.BindJSON(&in)
|
|
|
|
if err != nil {
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusBadRequest, model2.Error{Code: http.StatusBadRequest, Message: err.Error()})
|
2023-02-12 23:13:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if id != in.Identifier {
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusBadRequest, model2.Error{Code: http.StatusBadRequest, Message: "interface id mismatch"})
|
2023-02-12 23:13:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-13 23:50:32 +02:00
|
|
|
updatedInterface, err := e.app.WireGuard.UpdateInterface(ctx, model2.NewDomainInterface(&in))
|
2023-02-12 23:13:04 +01:00
|
|
|
if err != nil {
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusInternalServerError, model2.Error{
|
2023-02-12 23:13:04 +01:00
|
|
|
Code: http.StatusInternalServerError, Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusOK, model2.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-13 23:50:32 +02:00
|
|
|
var in model2.Interface
|
2023-02-12 23:13:04 +01:00
|
|
|
err := c.BindJSON(&in)
|
|
|
|
if err != nil {
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusBadRequest, model2.Error{Code: http.StatusBadRequest, Message: err.Error()})
|
2023-02-12 23:13:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-13 23:50:32 +02:00
|
|
|
newInterface, err := e.app.WireGuard.CreateInterface(ctx, model2.NewDomainInterface(&in))
|
2023-02-12 23:13:04 +01:00
|
|
|
if err != nil {
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusInternalServerError, model2.Error{
|
2023-02-12 23:13:04 +01:00
|
|
|
Code: http.StatusInternalServerError, Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusOK, model2.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-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusBadRequest, model2.Error{
|
2023-02-12 23:13:04 +01:00
|
|
|
Code: http.StatusInternalServerError, Message: "missing id parameter",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, peers, err := e.app.WireGuard.GetInterfaceAndPeers(c.Request.Context(), domain.InterfaceIdentifier(id))
|
|
|
|
if err != nil {
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusInternalServerError, model2.Error{
|
2023-02-12 23:13:04 +01:00
|
|
|
Code: http.StatusInternalServerError, Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-13 23:50:32 +02:00
|
|
|
c.JSON(http.StatusOK, model2.NewPeers(peers))
|
2023-02-12 23:13:04 +01:00
|
|
|
}
|
|
|
|
}
|