-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
117 lines (112 loc) · 4.39 KB
/
plugin.go
File metadata and controls
117 lines (112 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"SpeedCPanelManager/schema"
"context"
"fmt"
"net/http"
"regexp"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/golang-jwt/jwt"
"github.com/labstack/echo/v4"
"go.mongodb.org/mongo-driver/bson"
)
type AddPluginParams struct {
Server string `path:"service"`
PluginId string `head:"X-Plugin-ID"`
}
func addPlugin(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
if err := user.Claims.Valid(); err == nil {
var params AddPluginParams
var cont schema.Container
hasSpigetVariable := false
claims := user.Claims.(*jwtCustomClaims)
c.Bind(¶ms)
timeout, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
mongo := db.Database(config.DB.Database).Collection("Containers").FindOne(timeout, bson.M{"_id": params.Server})
if err = mongo.Decode(cont); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
if claims.UserID != cont.Owner || claims.TeamID != cont.Owner {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Errorf("attempted unauthorised update of container"))
}
servicedata, err := client.ServiceList(timeout, types.ServiceListOptions{
Filters: filters.NewArgs(filters.KeyValuePair{"id", cont.DockerID}),
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
re := regexp.MustCompile(`^(SPIGET_RESOURCES=)(([0-9]+)\,\s*)*(?P<pluginId>[0-9]+)$`)
for index, env := range servicedata[0].Spec.TaskTemplate.ContainerSpec.Env {
if re.MatchString(env) {
hasSpigetVariable = true
if contains(strings.Split(strings.Trim(env, "SPIGET_ROUC="), ","), compareString(params.PluginId)) {
return echo.NewHTTPError(http.StatusAlreadyReported, fmt.Errorf("plugin already present"))
} else {
servicedata[0].Spec.TaskTemplate.ContainerSpec.Env[index] += ("," + params.PluginId)
}
}
}
if !hasSpigetVariable {
servicedata[0].Spec.TaskTemplate.ContainerSpec.Env = append(servicedata[0].Spec.TaskTemplate.ContainerSpec.Env, fmt.Sprintf("SPIGET_RESOURCES=%s", params.PluginId))
}
response, err := client.ServiceUpdate(timeout, params.Server, servicedata[0].Version, servicedata[0].Spec, types.ServiceUpdateOptions{})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
return c.JSON(http.StatusOK, response)
} else {
return err
}
}
func deletePlugin(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
if err := user.Claims.Valid(); err == nil {
var params AddPluginParams
var cont schema.Container
hasSpigetVariable := false
claims := user.Claims.(*jwtCustomClaims)
c.Bind(¶ms)
timeout, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
mongo := db.Database(config.DB.Database).Collection("Containers").FindOne(timeout, bson.M{"_id": params.Server})
if err = mongo.Decode(cont); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
if claims.UserID != cont.Owner || claims.TeamID != cont.Owner {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Errorf("attempted unauthorised update of container"))
}
servicedata, err := client.ServiceList(timeout, types.ServiceListOptions{
Filters: filters.NewArgs(filters.KeyValuePair{"id", cont.DockerID}),
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
re := regexp.MustCompile(`^(SPIGET_RESOURCES=)(([0-9]+)\,\s*)*(?P<pluginId>[0-9]+)$`)
for index, env := range servicedata[0].Spec.TaskTemplate.ContainerSpec.Env {
if re.MatchString(env) {
hasSpigetVariable = true
plugins := strings.Split(strings.Trim(env, "SPIGET_ROUC="), ",")
if contains(plugins, compareString(params.PluginId)) {
servicedata[0].Spec.TaskTemplate.ContainerSpec.Env[index] = fmt.Sprintf("SPIGET_RESOURCES=%s", stringArrayToString(deleteFromAray(plugins, compareString(params.PluginId)), ","))
} else {
return echo.NewHTTPError(http.StatusAlreadyReported, fmt.Errorf("plugin not present"))
}
}
}
if !hasSpigetVariable {
return echo.NewHTTPError(http.StatusNotFound, fmt.Errorf("no plugins to be deleted"))
}
response, err := client.ServiceUpdate(timeout, params.Server, servicedata[0].Version, servicedata[0].Spec, types.ServiceUpdateOptions{})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
return c.JSON(http.StatusOK, response)
} else {
return err
}
}