-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
347 lines (311 loc) · 9.04 KB
/
main.go
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"errors"
"flag"
"fmt"
"html/template"
"net/http"
"strconv"
"sync"
"time"
"github.com/gin-gonic/gin"
)
var (
imageStatus = struct {
sync.RWMutex
m map[string]map[string]gin.H
}{m: make(map[string]map[string]gin.H)}
sarpConfig = config{}
debugEnabled = false
acceptingScores = true
alternateCompletion = false
)
func init() {
flag.BoolVar(&debugEnabled, "d", false, "Print verbose debug information")
flag.Parse()
}
func main() {
readConfig(&sarpConfig)
checkConfig()
// Initialize Gin router
// gin.SetMode(gin.ReleaseMode)
r := gin.Default()
// Add Increment Function to Router
r.SetFuncMap(template.FuncMap{
"increment": func(num int) int {
return num + 1
},
})
r.LoadHTMLGlob("templates/*")
r.Static("/assets", "./assets")
initCookies(r)
// Routes
routes := r.Group("/")
{
routes.GET("/login", func(c *gin.Context) {
c.HTML(http.StatusOK, "login.html", pageData(c, "login", nil))
})
routes.GET("/", viewScoreboard)
routes.GET("/announcements", viewAnnounce)
routes.GET("/status/:id/:image", getStatus)
routes.POST("/login", login)
routes.POST("/update", scoreUpdate)
routes.GET("/team/:team", viewTeam)
routes.GET("/image/:image", viewImage)
routes.GET("/team/:team/image/:image", viewTeamImage)
}
authRoutes := routes.Group("/")
authRoutes.Use(authRequired)
{
authRoutes.GET("/logout", logout)
authRoutes.GET("/settings", viewSettings)
authRoutes.POST("/settings", changeSettings)
authRoutes.GET("/export", exportCsv)
}
fmt.Println("Initializing scoreboard data...")
initScoreboard()
s := &http.Server{
Addr: ":4013", // Listening port
Handler: r,
ReadTimeout: time.Duration(sarpConfig.Timeout) * time.Second,
WriteTimeout: time.Duration(sarpConfig.Timeout) * time.Second,
}
s.ListenAndServe()
}
func viewScoreboard(c *gin.Context) {
teamScores, err := getTop()
if err != nil {
panic(err)
}
teamData, err := parseScoresIntoTeams(teamScores)
if err != nil {
panic(err)
}
c.HTML(http.StatusOK, "index.html", pageData(c, "Scoreboard", gin.H{"scores": teamData}))
}
func viewImage(c *gin.Context) {
imageName := c.Param("image")
if !validateString(imageName) {
errorOut(c, errors.New("Invalid image name: "+imageName))
}
teamScores, err := getTop()
if err != nil {
panic(err)
}
filteredScores := []scoreEntry{}
for _, score := range teamScores {
if score.Image.Name == imageName {
filteredScores = append(filteredScores, score)
}
}
teamData, err := parseScoresIntoTeams(filteredScores)
if err != nil {
panic(err)
}
c.HTML(http.StatusOK, "index.html", pageData(c, "Scoreboard for "+imageName, gin.H{"scores": teamData, "imageFilter": getImage(imageName), "event": sarpConfig.Event}))
}
func viewTeam(c *gin.Context) {
teamName := c.Param("team")
if !validateString(teamName) || !validateTeam(teamName) {
errorOutGraceful(c, errors.New("Invalid team name: "+teamName))
return
}
teamScore := getScore(teamName, "")
if len(teamScore) <= 0 {
errorOutGraceful(c, errors.New("Team doesn't have any image data"))
return
}
for index, score := range teamScore {
for _, vuln := range score.Vulns.VulnItems {
if vuln.VulnPoints < 0 {
teamScore[index].Penalties++
}
}
}
teamData, err := parseScoresIntoTeam(teamScore)
if err != nil {
errorOutGraceful(c, errors.New("Parsing team scores failed"))
return
}
allRecords := getAll(teamName, "")
imageCopies := []imageData{}
for _, image := range sarpConfig.Image {
imageCopies = append(imageCopies, image)
}
images, labels := consolidateRecords(allRecords, imageCopies)
for index := range images {
recordIndex := c.Request.URL.Query().Get("record" + strconv.Itoa(index))
if recordIndex != "" {
images[index].Index, err = strconv.Atoi(recordIndex)
if err != nil {
errorOutGraceful(c, errors.New("Invalid record number given"))
return
}
} else {
images[index].Index = len(images[index].Records) - 1
}
}
loc, _ := time.LoadLocation(sarpConfig.Timezone)
for index, score := range teamScore {
teamScore[index].Time = score.Time.In(loc)
if teamScore[index].CompletionTime != (time.Time{}) {
teamScore[index].CompletionTime = score.CompletionTime.In(loc)
}
}
for index := range images {
for index2, record := range images[index].Records {
images[index].Records[index2].Time = record.Time.In(loc)
}
}
c.HTML(http.StatusOK, "detail.html", pageData(c, "Scoreboard for "+teamName, gin.H{"data": teamScore, "team": teamData, "labels": labels, "images": images}))
}
func exportCsv(c *gin.Context) {
c.Data(200, "text/csv", []byte(getCsv()))
}
func viewTeamImage(c *gin.Context) {
teamName := c.Param("team")
if !validateString(teamName) || !validateTeam(teamName) {
errorOutGraceful(c, errors.New("Invalid team name"))
return
}
imageName := c.Param("image")
if !validateString(imageName) || !validateImage(imageName) {
errorOutGraceful(c, errors.New("Invalid image name"))
return
}
teamScore := getScore(teamName, imageName)
if len(teamScore) <= 0 {
errorOutGraceful(c, errors.New("Team doesn't have any image data"))
return
}
for index, score := range teamScore {
for _, vuln := range score.Vulns.VulnItems {
if vuln.VulnPoints < 0 {
teamScore[index].Penalties++
}
}
}
teamData, err := parseScoresIntoTeam(teamScore)
if err != nil {
errorOutGraceful(c, errors.New("Parsing team scores failed"))
return
}
allRecords := getAll(teamName, "")
images, labels := consolidateRecords(allRecords, []imageData{getImage(imageName)})
for index := range images {
recordIndex := c.Request.URL.Query().Get("record" + strconv.Itoa(index))
if recordIndex != "" {
images[index].Index, err = strconv.Atoi(recordIndex)
if err != nil {
errorOutGraceful(c, errors.New("Invalid record number given"))
return
}
} else {
images[index].Index = len(images[index].Records) - 1
}
}
c.HTML(http.StatusOK, "detail.html", pageData(c, "Scoreboard for "+teamName, gin.H{"data": teamScore, "team": teamData, "labels": labels, "images": images, "imageFilter": getImage(imageName)}))
}
func getStatus(c *gin.Context) {
id, image, err := validateReq(c)
if err != nil {
errorOut(c, err)
return
}
if sarpConfig.PlayTime != "" {
playTimeLimit, _ := time.ParseDuration(sarpConfig.PlayTime)
recentRecord, err := getLastScore(&scoreEntry{
Team: getTeam(id),
Image: getImage(image),
})
// Send kill signal if they're over play time limit
if err == nil && recentRecord.PlayTime > playTimeLimit && sarpConfig.Enforce {
c.JSON(200, gin.H{"status": "DIE"})
return
}
}
if !acceptingScores {
c.JSON(400, gin.H{"status": "DISABLED"})
return
}
imageStatus.Lock()
defer imageStatus.Unlock()
if v, ok := imageStatus.m[id][image]; ok {
// Delete existing key
delete(imageStatus.m[id], image)
c.JSON(200, v)
return
}
c.JSON(200, gin.H{"status": "OK"})
}
func viewSettings(c *gin.Context) {
c.HTML(http.StatusOK, "settings.html", pageData(c, "settings", gin.H{"scoring": acceptingScores}))
}
func viewAnnounce(c *gin.Context) {
allAnnouncements, err := getAnnouncements()
if err != nil {
allAnnouncements = []announcement{}
fmt.Println("Error retrieving announcements", err)
}
c.HTML(http.StatusOK, "announce.html", pageData(c, "announcements", gin.H{"announcements": allAnnouncements}))
}
func scoreUpdate(c *gin.Context) {
if !acceptingScores {
c.JSON(400, gin.H{"status": "DISABLED"})
return
}
c.Request.ParseForm()
cryptUpdate := c.Request.Form.Get("update")
newScore, err := parseUpdate(cryptUpdate)
if err != nil {
errorOut(c, err)
fmt.Println("Error decrypting update-- maybe your password is wrong?")
return
}
err = insertScore(newScore)
if err != nil {
errorOut(c, err)
return
}
c.JSON(200, gin.H{"status": "OK"})
}
func changeSettings(c *gin.Context) {
c.Request.ParseForm()
settingType := c.Request.Form.Get("settingType")
var err error
var msg string
if settingType == "announcement" {
announceTitle := c.Request.Form.Get("title")
announceBody := c.Request.Form.Get("body")
loc, _ := time.LoadLocation(sarpConfig.Timezone)
postToDiscord("**" + announceTitle + "**\n" + announceBody)
insertAnnouncement(&announcement{time.Now().In(loc), announceTitle, announceBody})
msg = "Successfully announced!"
} else if settingType == "toggleScoring" {
acceptingScores = !acceptingScores
} else if settingType == "wipeDatabase" {
err = wipeDatabase()
if err != nil {
fmt.Println("Error wiping database", err)
}
msg = "Successfully wiped database!"
} else if settingType == "disableTestingID" {
err = clearTeamScore("testing_id")
if err != nil {
fmt.Println("Error clearing testing_id results", err)
}
msg = "Cleared data for testing_id."
}
c.HTML(http.StatusOK, "settings.html", pageData(c, "settings", gin.H{"scoring": acceptingScores, "msg": msg, "err": err}))
}
func pageData(c *gin.Context, title string, ginMap gin.H) gin.H {
newGinMap := gin.H{}
newGinMap["title"] = title
newGinMap["user"] = getUser(c)
newGinMap["event"] = sarpConfig.Event
newGinMap["config"] = sarpConfig
for key, value := range ginMap {
newGinMap[key] = value
}
return newGinMap
}