fix: Fixes API routes.
This commit is contained in:
parent
fe8980b265
commit
bcf97fde6d
15 changed files with 284 additions and 132 deletions
|
@ -4,9 +4,11 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.terah.dev/imterah/hermes/api/backendruntime"
|
||||
"git.terah.dev/imterah/hermes/api/dbcore"
|
||||
"git.terah.dev/imterah/hermes/api/jwtcore"
|
||||
"git.terah.dev/imterah/hermes/api/permissions"
|
||||
"git.terah.dev/imterah/hermes/commonbackend"
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
|
@ -16,17 +18,12 @@ type ProxyCreationRequest struct {
|
|||
Token string `validate:"required" json:"token"`
|
||||
Name string `validate:"required" json:"name"`
|
||||
Description *string `json:"description"`
|
||||
Protcol string `validate:"required" json:"protcol"`
|
||||
SourceIP string `validate:"required" json:"source_ip"`
|
||||
SourcePort uint16 `validate:"required" json:"source_port"`
|
||||
DestinationPort uint16 `validate:"required" json:"destination_port"`
|
||||
ProviderID uint `validate:"required" json:"provider_id"`
|
||||
AutoStart bool `json:"auto_start"`
|
||||
}
|
||||
|
||||
type ProxyCreationResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Id uint `json:"id"`
|
||||
Protocol string `validate:"required" json:"protocol"`
|
||||
SourceIP string `validate:"required" json:"sourceIP"`
|
||||
SourcePort uint16 `validate:"required" json:"sourcePort"`
|
||||
DestinationPort uint16 `validate:"required" json:"destinationPort"`
|
||||
ProviderID uint `validate:"required" json:"providerID"`
|
||||
AutoStart *bool `json:"autoStart"`
|
||||
}
|
||||
|
||||
func CreateProxy(c *gin.Context) {
|
||||
|
@ -75,9 +72,9 @@ func CreateProxy(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
if req.Protcol != "tcp" && req.Protcol != "udp" {
|
||||
if req.Protocol != "tcp" && req.Protocol != "udp" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Body protocol must be either 'tcp' or 'udp'",
|
||||
"error": "Protocol must be either 'tcp' or 'udp'",
|
||||
})
|
||||
|
||||
return
|
||||
|
@ -85,31 +82,39 @@ func CreateProxy(c *gin.Context) {
|
|||
|
||||
var backend dbcore.Backend
|
||||
backendRequest := dbcore.DB.Where("id = ?", req.ProviderID).First(&backend)
|
||||
|
||||
if backendRequest.Error != nil {
|
||||
log.Warnf("failed to find if backend exists or not: %s", backendRequest.Error)
|
||||
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "Failed to find if provider exists",
|
||||
"error": "Failed to find if backend exists",
|
||||
})
|
||||
}
|
||||
|
||||
backendExists := backendRequest.RowsAffected > 0
|
||||
|
||||
if !backendExists {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Could not find provider",
|
||||
"error": "Could not find backend",
|
||||
})
|
||||
}
|
||||
|
||||
autoStart := false
|
||||
|
||||
if req.AutoStart != nil {
|
||||
autoStart = *req.AutoStart
|
||||
}
|
||||
|
||||
proxy := &dbcore.Proxy{
|
||||
UserID: user.ID,
|
||||
BackendID: req.ProviderID,
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
Protocol: req.Protcol,
|
||||
Protocol: req.Protocol,
|
||||
SourceIP: req.SourceIP,
|
||||
SourcePort: req.SourcePort,
|
||||
DestinationPort: req.DestinationPort,
|
||||
AutoStart: req.AutoStart,
|
||||
AutoStart: autoStart,
|
||||
}
|
||||
|
||||
if result := dbcore.DB.Create(proxy); result.Error != nil {
|
||||
|
@ -120,8 +125,50 @@ func CreateProxy(c *gin.Context) {
|
|||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, &ProxyCreationResponse{
|
||||
Success: true,
|
||||
Id: proxy.ID,
|
||||
if autoStart {
|
||||
backend, ok := backendruntime.RunningBackends[proxy.BackendID]
|
||||
|
||||
if !ok {
|
||||
log.Warnf("Couldn't fetch backend runtime from backend ID #%d", proxy.BackendID)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"id": proxy.ID,
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
backend.RuntimeCommands <- &commonbackend.AddProxy{
|
||||
Type: "addProxy",
|
||||
SourceIP: proxy.SourceIP,
|
||||
SourcePort: proxy.SourcePort,
|
||||
DestPort: proxy.DestinationPort,
|
||||
Protocol: proxy.Protocol,
|
||||
}
|
||||
|
||||
backendResponse := <-backend.RuntimeCommands
|
||||
|
||||
switch responseMessage := backendResponse.(type) {
|
||||
case error:
|
||||
log.Warnf("Failed to get response for backend #%d: %s", proxy.BackendID, responseMessage.Error())
|
||||
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"error": "failed to get response from backend",
|
||||
})
|
||||
|
||||
return
|
||||
case *commonbackend.ProxyStatusResponse:
|
||||
if !responseMessage.IsActive {
|
||||
log.Warnf("Failed to start proxy for backend #%d", proxy.BackendID)
|
||||
}
|
||||
default:
|
||||
log.Errorf("Got illegal response type for backend #%d: %T", proxy.BackendID, responseMessage)
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"id": proxy.ID,
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue