fixed api stuff
This commit is contained in:
parent
a6a63805e4
commit
106b7326f2
@ -81,7 +81,7 @@ func AuthorizedTo(requiredPermissions models.Permission, overwriters ...func(*gi
|
||||
}
|
||||
if claims, ok := token.Claims.(*AuthClaims); ok && token.Valid {
|
||||
ctx.Set("claims", claims)
|
||||
if requiredPermissions&claims.Permissions != requiredPermissions && models.Admin&claims.Permissions != models.Admin {
|
||||
if (requiredPermissions&claims.Permissions != requiredPermissions) && (models.Admin&claims.Permissions != models.Admin) {
|
||||
for _, overwrite := range overwriters {
|
||||
if overwrite(ctx) {
|
||||
return
|
||||
|
1
main.go
1
main.go
@ -51,6 +51,7 @@ func main() {
|
||||
auth.LoadGroup(router.Group("/auth"), client, config)
|
||||
servers.LoadGroup(router.Group("/servers"), client, config)
|
||||
servers.LoadBrowsersGroup(router.Group("/browsers"), client, config)
|
||||
servers.LoadeImagesGroup(router.Group("/images"), client, config)
|
||||
|
||||
router.Run("127.0.0.1:8080")
|
||||
}
|
||||
|
@ -46,7 +46,42 @@ func (con Connection) getBrowserInfo(volume volume.Volume) (*models.FileBrowserI
|
||||
Id: container.ID,
|
||||
OwnerId: browserInfo.OwnerId,
|
||||
ConnectedTo: *serverInfo,
|
||||
Url: container.ID[:12] + "." + DOMAIN,
|
||||
Url: serverInfo.Id[:12] + "." + DOMAIN,
|
||||
}, nil
|
||||
|
||||
}
|
||||
func (con Connection) getBrowserInfoFromServerId(serverId string) (*models.FileBrowserInfo, error) {
|
||||
serverInfo, err := con.getServerInfoFromId(serverId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
containers, err := con.dockerClient.ContainerList(context.TODO(), container.ListOptions{
|
||||
All: true,
|
||||
Filters: filters.NewArgs(filters.Arg("label", "type=FILE_BROWSER"), filters.Arg("label", fmt.Sprintf("volume_id=%s", serverInfo.Id))),
|
||||
})
|
||||
if err != nil || len(containers) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
container := containers[0]
|
||||
jsonData, err := json.Marshal(container.Labels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var browserInfo ContainerLabels
|
||||
err = json.Unmarshal(jsonData, &browserInfo)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &models.FileBrowserInfo{
|
||||
Id: container.ID,
|
||||
OwnerId: browserInfo.OwnerId,
|
||||
ConnectedTo: *serverInfo,
|
||||
Url: serverInfo.Id[:12] + "." + DOMAIN,
|
||||
}, nil
|
||||
|
||||
}
|
||||
@ -62,7 +97,7 @@ func (con Connection) GetBrowsers(ctx *gin.Context) {
|
||||
ctx.AbortWithError(500, err)
|
||||
}
|
||||
|
||||
var servers []models.FileBrowserInfo
|
||||
var servers []models.FileBrowserInfo = make([]models.FileBrowserInfo, 0, len(volumes.Volumes))
|
||||
|
||||
for _, volume := range volumes.Volumes {
|
||||
browserInfo, err := con.getBrowserInfo(*volume)
|
||||
|
@ -2,10 +2,12 @@ package servers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"acooldomain.co/backend/auth"
|
||||
"acooldomain.co/backend/models"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/image"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -13,9 +15,10 @@ import (
|
||||
)
|
||||
|
||||
type ImageData struct {
|
||||
Id string
|
||||
Name string
|
||||
Version string
|
||||
Id string
|
||||
Name string
|
||||
Version string
|
||||
DisplayName string
|
||||
}
|
||||
|
||||
func convertImageToImageData(imageSummary image.Summary) ImageData {
|
||||
@ -23,13 +26,14 @@ func convertImageToImageData(imageSummary image.Summary) ImageData {
|
||||
splitImageId := strings.Split(imageId, ":")
|
||||
imageName, imageVersion := splitImageId[0], splitImageId[1]
|
||||
return ImageData{
|
||||
Id: imageId,
|
||||
Name: imageName,
|
||||
Version: imageVersion,
|
||||
Id: imageId,
|
||||
Name: imageName,
|
||||
Version: imageVersion,
|
||||
DisplayName: fmt.Sprintf("%s %s", imageName, imageVersion),
|
||||
}
|
||||
}
|
||||
func (con Connection) GetImages(c *gin.Context) {
|
||||
images, err := con.dockerClient.ImageList(context.TODO(), image.ListOptions{Filters: filters.NewArgs(filters.NewArg("label", "type=GAME"))})
|
||||
images, err := con.dockerClient.ImageList(context.TODO(), image.ListOptions{Filters: filters.NewArgs(filters.Arg("label", "type=GAME"))})
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
@ -50,5 +54,5 @@ func LoadeImagesGroup(group *gin.RouterGroup, mongo_client *mongo.Client, config
|
||||
defer apiClient.Close()
|
||||
|
||||
connection := Connection{databaseConnection: mongo_client, dockerClient: apiClient}
|
||||
group.GET("/", auth.AuthorizedTo(0), connection.GetBrowsers)
|
||||
group.GET("/", auth.AuthorizedTo(0), connection.GetImages)
|
||||
}
|
||||
|
@ -220,6 +220,7 @@ func (con Connection) CreateServer(ctx *gin.Context) {
|
||||
VolumeId: volumeResponse.Name,
|
||||
DefaultPorts: request.DefaultPorts,
|
||||
DefaultCommand: request.DefaultCommand,
|
||||
Nickname: request.Nickname,
|
||||
})
|
||||
|
||||
ctx.JSON(200, volumeResponse.Name)
|
||||
@ -297,8 +298,12 @@ func (con Connection) StartServer(ctx *gin.Context) {
|
||||
portMapping[nat.Port(fmt.Sprintf("%d/%s", portCouple.Source.Number, portCouple.Source.Protocol))] = []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: fmt.Sprint(portCouple.Destination.Number)}}
|
||||
}
|
||||
}
|
||||
command := request.Command
|
||||
if command == "" {
|
||||
command = serverInfo.DefaultCommand
|
||||
}
|
||||
|
||||
words, err := shellwords.Split(request.Command)
|
||||
words, err := shellwords.Split(command)
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
@ -391,7 +396,7 @@ func (con Connection) GetServers(ctx *gin.Context) {
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
}
|
||||
var servers []models.ServerInfo
|
||||
var servers []models.ServerInfo = make([]models.ServerInfo, 0, len(volumes.Volumes))
|
||||
for _, volume := range volumes.Volumes {
|
||||
serverInfo, err := con.getServerInfo(*volume)
|
||||
if err != nil {
|
||||
@ -435,8 +440,7 @@ func (con Connection) DeleteServer(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
for _, containerInstance := range containers {
|
||||
con.dockerClient.ContainerStop(context.TODO(), containerInstance.ID, container.StopOptions{})
|
||||
err := con.dockerClient.ContainerRemove(context.TODO(), containerInstance.ID, container.RemoveOptions{Force: true, RemoveLinks: true})
|
||||
err := con.dockerClient.ContainerRemove(context.TODO(), containerInstance.ID, container.RemoveOptions{Force: true})
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
@ -462,7 +466,7 @@ func (con Connection) RunCommand(ctx *gin.Context) {
|
||||
|
||||
serverId := ctx.Param("server_id")
|
||||
log.Print("Writing command \"", request.Command, "\"")
|
||||
containers, err := con.dockerClient.ContainerList(context.TODO(), container.ListOptions{Filters: filters.NewArgs(filters.Arg("label", "volume_id="+serverId))})
|
||||
containers, err := con.dockerClient.ContainerList(context.TODO(), container.ListOptions{Filters: filters.NewArgs(filters.Arg("label", "volume_id="+serverId), filters.Arg("label", "type=GAME"))})
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
@ -470,11 +474,11 @@ func (con Connection) RunCommand(ctx *gin.Context) {
|
||||
|
||||
for _, containerData := range containers {
|
||||
hijacked, err := con.dockerClient.ContainerAttach(context.TODO(), containerData.ID, container.AttachOptions{Stream: true, Stdin: true})
|
||||
defer func() { hijacked.Close(); hijacked.CloseWrite() }()
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
defer func() { hijacked.Close(); hijacked.CloseWrite() }()
|
||||
number, err := hijacked.Conn.Write([]byte(request.Command + "\n"))
|
||||
log.Print("Wrote ", number, " bytes")
|
||||
if err != nil {
|
||||
@ -684,14 +688,14 @@ func (con Connection) BrowseServer(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
containers, err := con.dockerClient.ContainerList(context.TODO(), container.ListOptions{Filters: filters.NewArgs(filters.Arg("label", "type=FILE_BROWSER"), filters.Arg("label", "volume_id="+serverInfo.Id))})
|
||||
browserInfo, err := con.getBrowserInfoFromServerId(serverInfo.Id)
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(containers) > 0 {
|
||||
ctx.JSON(200, "OK")
|
||||
if browserInfo != nil {
|
||||
ctx.JSON(200, browserInfo.Url)
|
||||
return
|
||||
}
|
||||
|
||||
@ -725,7 +729,17 @@ func (con Connection) BrowseServer(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(200, "OK")
|
||||
browserInfo, err = con.getBrowserInfoFromServerId(serverInfo.Id)
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
if browserInfo == nil {
|
||||
ctx.AbortWithError(500, fmt.Errorf("failed to open browser for server %s", serverInfo.Id))
|
||||
return
|
||||
}
|
||||
ctx.JSON(200, browserInfo.Url)
|
||||
}
|
||||
|
||||
func (con Connection) serverAuthorized(permissions models.Permission) func(*gin.Context) bool {
|
||||
|
@ -18,12 +18,18 @@ type Connection struct {
|
||||
connection *mongo.Client
|
||||
}
|
||||
|
||||
type UserResponse struct {
|
||||
Username string
|
||||
Permissions models.Permission
|
||||
Email string
|
||||
}
|
||||
|
||||
func (con Connection) GetUsers(c *gin.Context) {
|
||||
users, err := con.connection.Database("Backend").Collection("Users").Find(context.TODO(), bson.D{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var response []models.User
|
||||
var response []UserResponse
|
||||
|
||||
err = users.All(context.TODO(), &response)
|
||||
|
||||
@ -34,12 +40,6 @@ func (con Connection) GetUsers(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
type UserResponse struct {
|
||||
Username string
|
||||
Permissions models.Permission
|
||||
Email string
|
||||
}
|
||||
|
||||
func (con Connection) GetUser(c *gin.Context) {
|
||||
claims, exists := c.Get("claims")
|
||||
if !exists {
|
||||
|
Loading…
x
Reference in New Issue
Block a user