This repository was archived by the owner on Aug 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.go
More file actions
155 lines (118 loc) 路 3.14 KB
/
Copy pathadmin.go
File metadata and controls
155 lines (118 loc) 路 3.14 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/gin-gonic/gin"
)
func adminRouter(admin *gin.RouterGroup) {
admin.GET("/", func(c *gin.Context) {
chall, _ := GetAllChall()
RenderTemplates(c, gin.H{
"challenges": chall,
}, "admin")
})
admin.GET("/add-image", func(c *gin.Context) {
RenderTemplates(c, gin.H{})
})
admin.POST("/image/add", createContainerHandler)
admin.POST("/image/del", removeContainerHandler)
admin.POST("/image/pull", pullImageHandler)
admin.GET("/online", func(c *gin.Context) {
c.HTML(http.StatusOK, "online.tmpl", gin.H{
"online": GetOnlineSandbox(),
})
})
admin.POST("/instance/reset", func(c *gin.Context) {
ResetSandbox()
})
admin.GET("/online/sse", func(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
c.Status(http.StatusOK)
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
data := GetOnlineSandbox()
var buf bytes.Buffer
t, _ := template.ParseFiles(filepath.Join("templates", "components", "online.tmpl"))
if err := t.Execute(&buf, data); err != nil {
fmt.Println("Error executing template:", err)
return
}
_, _ = fmt.Fprintf(c.Writer, "data: %s\n\n", strings.ReplaceAll(buf.String(), "\n", ""))
c.Writer.Flush()
case <-c.Writer.CloseNotify():
return
}
}
})
}
func pullImageHandler(c *gin.Context) {
dockerImage := c.PostForm("dockerImage")
// Validate input (you can add more validation logic)
if dockerImage == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input"})
return
}
PullImage(dockerImage)
chall, _ := GetAllChall()
RenderTemplates(c, gin.H{
"challenges": chall,
"Message": "Image Pulled!",
}, "admin")
}
func createContainerHandler(c *gin.Context) {
containerName := c.PostForm("containerName")
dockerImage := c.PostForm("dockerImage")
imageType := c.PostForm("type")
env := make([]string, 0)
for i := 1; i < 10; i++ {
envKey := c.PostForm(fmt.Sprintf("envKey%d", i))
envValue := c.PostForm(fmt.Sprintf("envValue%d", i))
if envKey == "" || envValue == "" {
break
}
env = append(env, envKey+"="+envValue)
}
// Validate input (you can add more validation logic)
if containerName == "" || dockerImage == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input"})
return
}
newChall := Challenge{
Image: dockerImage,
Name: containerName,
Type: imageType,
Env: env,
}
AddChall(newChall)
chall, _ := GetAllChall()
RenderTemplates(c, gin.H{
"challenges": chall,
"Message": "Container Created!",
}, "admin")
}
func removeContainerHandler(c *gin.Context) {
containerName := c.PostForm("containerName")
log.Println(containerName)
// Validate input (you can add more validation logic)
if containerName == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input"})
return
}
RemoveChall(containerName)
chall, _ := GetAllChall()
RenderTemplates(c, gin.H{
"challenges": chall,
"Message": "Container Deleted!",
}, "admin")
}