fix: Fixes API routes.

This commit is contained in:
Tera << 8 2024-12-23 20:47:01 -05:00
parent fe8980b265
commit bcf97fde6d
Signed by: imterah
GPG key ID: 8FA7DD57BA6CEA37
15 changed files with 284 additions and 132 deletions

View file

@ -6,6 +6,7 @@ import (
"net/http"
"strings"
"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"
@ -23,12 +24,13 @@ type BackendLookupRequest struct {
}
type SanitizedBackend struct {
Name string `json:"name"`
BackendID uint `json:"id"`
OwnerID uint `json:"ownerID"`
Description *string `json:"description"`
Backend string `json:"backend"`
BackendParameters *string `json:"connectionDetails"`
Name string `json:"name"`
BackendID uint `json:"id"`
OwnerID uint `json:"ownerID"`
Description *string `json:"description"`
Backend string `json:"backend"`
BackendParameters *string `json:"connectionDetails"`
Logs []string `json:"logs"`
}
type LookupResponse struct {
@ -121,12 +123,25 @@ func LookupBackend(c *gin.Context) {
hasSecretVisibility := permissions.UserHasPermission(user, "backends.secretVis")
for backendIndex, backend := range backends {
foundBackend, ok := backendruntime.RunningBackends[backend.ID]
if !ok {
log.Warnf("Failed to get backend #%d controller", backend.ID)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to get backends",
})
return
}
sanitizedBackends[backendIndex] = &SanitizedBackend{
BackendID: backend.ID,
OwnerID: backend.UserID,
Name: backend.Name,
Description: backend.Description,
Backend: backend.Backend,
Logs: foundBackend.Logs,
}
if backend.UserID == user.ID || hasSecretVisibility {

View file

@ -106,33 +106,10 @@ func GetConnections(c *gin.Context) {
return
}
var backend dbcore.Backend
backendRequest := dbcore.DB.Where("id = ?", proxy.BackendID).First(&backend)
if backendRequest.Error != nil {
log.Warnf("failed to find backend: %s", backendRequest.Error)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to find backend entry",
})
return
}
backendExists := backendRequest.RowsAffected > 0
if !backendExists {
c.JSON(http.StatusBadRequest, gin.H{
"error": "No forward entry found",
})
return
}
backendRuntime, ok := backendruntime.RunningBackends[backend.ID]
backendRuntime, ok := backendruntime.RunningBackends[proxy.BackendID]
if !ok {
log.Warnf("Couldn't fetch backend runtime from backend ID #%d", backend.ID)
log.Warnf("Couldn't fetch backend runtime from backend ID #%d", proxy.BackendID)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Couldn't fetch backend runtime",

View file

@ -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,
})
}

View file

@ -19,23 +19,23 @@ type ProxyLookupRequest struct {
Name *string `json:"name"`
Description *string `json:"description"`
Protocol *string `json:"protocol"`
SourceIP *string `json:"source_ip"`
SourcePort *uint16 `json:"source_port"`
DestinationPort *uint16 `json:"destination_port"`
ProviderID *uint `json:"provider_id"`
AutoStart *bool `json:"auto_start"`
SourceIP *string `json:"sourceIP"`
SourcePort *uint16 `json:"sourcePort"`
DestinationPort *uint16 `json:"destPort"`
ProviderID *uint `json:"providerID"`
AutoStart *bool `json:"autoStart"`
}
type SanitizedProxy struct {
Id uint `json:"id"`
Name string `json:"name"`
Description *string `json:"description"`
Protcol string `json:"protcol"`
SourceIP string `json:"source_ip"`
SourcePort uint16 `json:"source_port"`
DestinationPort uint16 `json:"destination_port"`
ProviderID uint `json:"provider_id"`
AutoStart bool `json:"auto_start"`
Protcol string `json:"protocol"`
SourceIP string `json:"sourceIP"`
SourcePort uint16 `json:"sourcePort"`
DestinationPort uint16 `json:"destPort"`
ProviderID uint `json:"providerID"`
AutoStart bool `json:"autoStart"`
}
type ProxyLookupResponse struct {
@ -63,6 +63,7 @@ func LookupProxy(c *gin.Context) {
}
user, err := jwtcore.GetUserFromJWT(req.Token)
if err != nil {
if err.Error() == "token is expired" || err.Error() == "user does not exist" {
c.JSON(http.StatusForbidden, gin.H{
@ -89,13 +90,16 @@ func LookupProxy(c *gin.Context) {
return
}
if *req.Protcol != "tcp" && *req.Protcol != "udp" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Protocol specified in body must either be 'tcp' or 'udp'",
})
if req.Protocol != nil {
if *req.Protocol != "tcp" && *req.Protocol != "udp" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Protocol specified in body must either be 'tcp' or 'udp'",
})
}
}
proxies := []dbcore.Proxy{}
queryString := []string{}
queryParameters := []interface{}{}
@ -103,34 +107,42 @@ func LookupProxy(c *gin.Context) {
queryString = append(queryString, "id = ?")
queryParameters = append(queryParameters, req.Id)
}
if req.Name != nil {
queryString = append(queryString, "name = ?")
queryParameters = append(queryParameters, req.Name)
}
if req.Description != nil {
queryString = append(queryString, "description = ?")
queryParameters = append(queryParameters, req.Description)
}
if req.SourceIP != nil {
queryString = append(queryString, "name = ?")
queryParameters = append(queryParameters, req.Name)
}
if req.SourcePort != nil {
queryString = append(queryString, "sourceport = ?")
queryParameters = append(queryParameters, req.SourcePort)
}
if req.DestinationPort != nil {
queryString = append(queryString, "destinationport = ?")
queryParameters = append(queryParameters, req.DestinationPort)
}
if req.ProviderID != nil {
queryString = append(queryString, "backendid = ?")
queryParameters = append(queryParameters, req.ProviderID)
}
if req.AutoStart != nil {
queryString = append(queryString, "autostart = ?")
queryParameters = append(queryParameters, req.AutoStart)
}
if req.Protocol != nil {
queryString = append(queryString, "protocol = ?")
queryParameters = append(queryParameters, req.Protocol)
@ -140,7 +152,7 @@ func LookupProxy(c *gin.Context) {
log.Warnf("failed to get proxies: %s", err.Error())
c.JSON(http.StatusInternalServerError, gin.H{
"error": "failed to get forward rules",
"error": "Failed to get proxies",
})
return
@ -149,15 +161,10 @@ func LookupProxy(c *gin.Context) {
sanitizedProxies := make([]*SanitizedProxy, len(proxies))
for proxyIndex, proxy := range proxies {
description := ""
if proxy.Description != nil {
description = *proxy.Description
}
sanitizedProxies[proxyIndex] = &SanitizedProxy{
Id: proxy.ID,
Name: proxy.Name,
Description: &description,
Description: proxy.Description,
Protcol: proxy.Protocol,
SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort,

View file

@ -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"
@ -17,10 +19,6 @@ type ProxyRemovalRequest struct {
ID uint `validate:"required" json:"id"`
}
type ProxyRemovalResponse struct {
Success bool `json:"success"`
}
func RemoveProxy(c *gin.Context) {
var req ProxyRemovalRequest
@ -100,7 +98,56 @@ func RemoveProxy(c *gin.Context) {
return
}
c.JSON(http.StatusOK, &ProxyRemovalResponse{
Success: true,
backend, ok := backendruntime.RunningBackends[proxy.BackendID]
if !ok {
log.Warnf("Couldn't fetch backend runtime from backend ID #%d", proxy.BackendID)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Couldn't fetch backend runtime",
})
return
}
backend.RuntimeCommands <- &commonbackend.RemoveProxy{
Type: "removeProxy",
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. Proxy was still successfully deleted",
})
return
case *commonbackend.ProxyStatusResponse:
if responseMessage.IsActive {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to stop proxy. Proxy was still successfully deleted",
})
return
}
default:
log.Errorf("Got illegal response type for backend #%d: %T", proxy.BackendID, responseMessage)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Got invalid response from backend. Proxy was still successfully deleted",
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
})
}

View file

@ -19,10 +19,6 @@ type ProxyStartRequest struct {
ID uint `validate:"required" json:"id"`
}
type ProxyStartResponse struct {
Success bool `json:"success"`
}
func StartProxy(c *gin.Context) {
var req ProxyStartRequest
@ -92,9 +88,19 @@ func StartProxy(c *gin.Context) {
return
}
backend := backendruntime.RunningBackends[proxy.BackendID]
backend, ok := backendruntime.RunningBackends[proxy.BackendID]
backend.RuntimeCommands <- commonbackend.AddProxy{
if !ok {
log.Warnf("Couldn't fetch backend runtime from backend ID #%d", proxy.BackendID)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Couldn't fetch backend runtime",
})
return
}
backend.RuntimeCommands <- &commonbackend.AddProxy{
Type: "addProxy",
SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort,
@ -121,13 +127,19 @@ func StartProxy(c *gin.Context) {
return
}
break
default:
log.Errorf("Got illegal response type for backend #%d: %T", proxy.BackendID, responseMessage)
break
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Got invalid response from backend. Proxy was still successfully deleted",
})
return
}
c.JSON(http.StatusOK, &ProxyStartResponse{
Success: true,
c.JSON(http.StatusOK, gin.H{
"success": true,
})
}

View file

@ -19,10 +19,6 @@ type ProxyStopRequest struct {
ID uint `validate:"required" json:"id"`
}
type ProxyStopResponse struct {
Success bool `json:"success"`
}
func StopProxy(c *gin.Context) {
var req ProxyStopRequest
@ -92,9 +88,19 @@ func StopProxy(c *gin.Context) {
return
}
backend := backendruntime.RunningBackends[proxy.BackendID]
backend, ok := backendruntime.RunningBackends[proxy.BackendID]
backend.RuntimeCommands <- commonbackend.RemoveProxy{
if !ok {
log.Warnf("Couldn't fetch backend runtime from backend ID #%d", proxy.BackendID)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Couldn't fetch backend runtime",
})
return
}
backend.RuntimeCommands <- &commonbackend.RemoveProxy{
Type: "removeProxy",
SourceIP: proxy.SourceIP,
SourcePort: proxy.SourcePort,
@ -102,7 +108,36 @@ func StopProxy(c *gin.Context) {
Protocol: proxy.Protocol,
}
c.JSON(http.StatusOK, &ProxyStopResponse{
Success: true,
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 {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "failed to stop proxy",
})
return
}
default:
log.Errorf("Got illegal response type for backend #%d: %T", proxy.BackendID, responseMessage)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Got invalid response from backend. Proxy was still successfully deleted",
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
})
}

View file

@ -11,7 +11,6 @@ import (
"git.terah.dev/imterah/hermes/api/backendruntime"
"git.terah.dev/imterah/hermes/api/controllers/v1/backends"
"git.terah.dev/imterah/hermes/api/controllers/v1/forward"
"git.terah.dev/imterah/hermes/api/controllers/v1/proxies"
"git.terah.dev/imterah/hermes/api/controllers/v1/users"
"git.terah.dev/imterah/hermes/api/dbcore"
@ -202,7 +201,7 @@ func entrypoint(cCtx *cli.Context) error {
engine.POST("/api/v1/forward/remove", proxies.RemoveProxy)
engine.POST("/api/v1/forward/start", proxies.StartProxy)
engine.POST("/api/v1/forward/stop", proxies.StopProxy)
engine.POST("/api/v1/forward/connections", forward.GetConnections)
engine.POST("/api/v1/forward/connections", proxies.GetConnections)
log.Infof("Listening on '%s'", listeningAddress)
err = engine.Run(listeningAddress)