-
Notifications
You must be signed in to change notification settings - Fork 9
/
routes.go
794 lines (684 loc) · 20.8 KB
/
routes.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
package main
import (
"bytes"
"fmt"
"net/http"
"os"
"sort"
"strconv"
"time"
"github.com/BurntSushi/toml"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pkg/errors"
"gorm.io/gorm/clause"
)
var (
cachedStatus []TeamRecord
cachedRound int
// In-memory cache of persists for this round. map[team.ID][box.Name][]offender.ID
persistHits map[uint]map[string][]uint
// Uptime agent hits
agentHits map[uint]map[string]time.Time
uptimeSLA time.Duration
// Adjustments to process
manualAdjustments map[uint]int
)
func viewStatus(c *gin.Context) {
if roundNumber != cachedRound {
var statusRecords []TeamRecord
res := db.Limit(len(dwConf.Team)).Preload(clause.Associations).Order("time desc").Find(&statusRecords)
if res.Error != nil {
errorOutGraceful(c, res.Error)
}
// Build results map
for i, rec := range statusRecords {
for j, res := range statusRecords[i].Results {
statusRecords[i].Results[j].Uptime = int((float64(res.Points) / float64(res.RoundCount)) * 100)
}
statusRecords[i].Total = calculateScoreTotal(rec)
statusRecords[i].ResultsMap = makeResultsMap(rec.Results)
}
// Sort by team ID.
sort.SliceStable(statusRecords, func(i, j int) bool {
return statusRecords[i].TeamID < statusRecords[j].TeamID
})
cachedStatus = statusRecords
cachedRound = roundNumber
}
// TODO fix this, horrendous
teamMutex.Lock()
sortedRecords := make([]TeamRecord, len(dwConf.Team))
copy(sortedRecords, cachedStatus)
teamMutex.Unlock()
// Sort by total score.
sort.SliceStable(sortedRecords, func(i, j int) bool {
return sortedRecords[i].Total > sortedRecords[j].Total
})
// Get graphs for both color schemes
graphScores(sortedRecords, true)
graphScores(sortedRecords, false)
c.HTML(http.StatusOK, "index.html", pageData(c, "Scoreboard", gin.H{"statusRecords": cachedStatus, "records": sortedRecords, "persists": persistHits, "round": roundNumber, "pauseTime": pauseTime}))
}
func viewScoreboard(c *gin.Context) {
if roundNumber != cachedRound {
var statusRecords []TeamRecord
res := db.Limit(len(dwConf.Team)).Preload(clause.Associations).Order("time desc").Find(&statusRecords)
if res.Error != nil {
errorOutGraceful(c, res.Error)
}
// Build results map
for i, rec := range statusRecords {
for j, res := range statusRecords[i].Results {
statusRecords[i].Results[j].Uptime = int((float64(res.Points) / float64(res.RoundCount)) * 100)
}
statusRecords[i].Total = calculateScoreTotal(rec)
statusRecords[i].ResultsMap = makeResultsMap(rec.Results)
}
// Sort by team ID.
sort.SliceStable(statusRecords, func(i, j int) bool {
return statusRecords[i].TeamID < statusRecords[j].TeamID
})
cachedStatus = statusRecords
cachedRound = roundNumber
}
// TODO fix this, horrendous
teamMutex.Lock()
sortedRecords := make([]TeamRecord, len(dwConf.Team))
copy(sortedRecords, cachedStatus)
teamMutex.Unlock()
// Sort by total score.
sort.SliceStable(sortedRecords, func(i, j int) bool {
return sortedRecords[i].Total > sortedRecords[j].Total
})
// Get graphs for both color schemes
graphScores(sortedRecords, true)
graphScores(sortedRecords, false)
team := getUserOptional(c)
ip := c.ClientIP()
c.HTML(http.StatusOK, "scoreboard.html", pageData(c, "Scoreboard", gin.H{"statusRecords": cachedStatus, "records": sortedRecords, "persists": persistHits, "team": team, "ip": ip, "round": roundNumber, "pauseTime": pauseTime, "configErrors": configErrors}))
}
func viewTeam(c *gin.Context) {
id, err := strconv.Atoi(c.Param("team"))
if err != nil || id < 1 {
errorOutAnnoying(c, errors.New("invalid team id: "+c.Param("team")))
return
}
team := validateTeam(c, uint(id))
// failed
if team.Name == "" {
return
}
var records []TeamRecord
res := db.Limit(10).Preload("Results").Order("time desc").Find(&records, "team_id = ?", team.ID)
if res.Error != nil {
errorOutGraceful(c, res.Error)
return
}
// Sort all the Results...
if len(records) > 0 {
for j, res := range records[0].Results {
records[0].Results[j].Uptime = int((float64(res.Points) / float64(res.RoundCount)) * 100)
}
for i := range records {
records[i].ResultsMap = makeResultsMap(records[i].Results)
}
}
var slaViolations []SLA
res = db.Order("time desc").Find(&slaViolations, "team_id = ?", team.ID)
if res.Error != nil {
errorOutGraceful(c, res.Error)
return
}
c.HTML(http.StatusOK, "team.html", pageData(c, "Scoreboard", gin.H{"team": team, "sla": slaViolations, "records": records}))
}
func viewCheck(c *gin.Context) {
id, err := strconv.Atoi(c.Param("team"))
if err != nil {
errorOutAnnoying(c, errors.New("invalid team id: "+c.Param("team")))
return
}
team := validateTeam(c, uint(id))
check, err := dwConf.getCheck(c.Param("check"))
if err != nil {
errorOutAnnoying(c, err)
}
var sla SLA
res := db.Limit(1).Find(&sla, "team_id = ? and reason = ?", team.ID, check.FetchName())
if res.Error != nil {
errorOutGraceful(c, res.Error)
return
}
var results []ResultEntry
res = db.Order("time desc").Find(&results, "team_id = ? and name = ?", team.ID, check.FetchName())
if res.Error != nil {
errorOutGraceful(c, res.Error)
return
}
c.HTML(http.StatusOK, "check.html", pageData(c, team.Name+": "+check.FetchName(), gin.H{"team": team, "check": check, "sla": sla, "results": results}))
}
func viewPCR(c *gin.Context) {
team := getUser(c)
var submissions []InjectSubmission
if team.IsAdmin() {
// Get all PCR entries
res := db.Order("time desc").Preload("Team").Where("inject_id = 1 and graded = true and feedback = ''").Find(&submissions)
if res.Error != nil {
errorPrint(res.Error)
return
}
} else {
// Get only team's PCRs
res := db.Order("time desc").Preload("Team").Where("inject_id = 1 and feedback = '' and graded = true and team_id = ?", team.ID).Find(&submissions)
if res.Error != nil {
errorPrint(res.Error)
return
}
}
// Load all content from on disk
// TODO: skip the middleman and just do this when received?
for i, submission := range submissions {
submissions[i].Content = readInject(submission)
}
c.HTML(http.StatusOK, "pcr.html", pageData(c, "PCRs", gin.H{"team": team, "creds": ct.Creds, "submissions": submissions}))
}
func submitPCR(c *gin.Context) {
team := getUser(c)
c.Request.ParseForm()
if team.IsAdmin() {
id, err := strconv.Atoi(c.Request.Form.Get("team"))
if err != nil {
c.HTML(http.StatusBadRequest, "pcr.html", pageData(c, "PCRs", gin.H{"error": err}))
return
}
if team.Name == "" {
c.HTML(http.StatusBadRequest, "pcr.html", pageData(c, "PCRs", gin.H{"error": "Invalid team"}))
return
}
team = getTeam(uint(id))
}
newSubmission := InjectSubmission{
Time: time.Now(),
Updated: time.Now(),
TeamID: team.ID,
InjectID: 1,
FileName: fmt.Sprintf("%s_pcr_%s_pcr_PWD_pcr_1.txt", team.Name, c.Request.Form.Get("check")),
DiskFile: uuid.New().String(),
}
if res := db.Save(&newSubmission); res.Error != nil {
c.HTML(http.StatusBadRequest, "pcr.html", pageData(c, "PCRs", gin.H{"error": res.Error}))
return
}
file, err := os.Create("submissions/" + newSubmission.DiskFile)
if err != nil {
c.HTML(http.StatusBadRequest, "pcr.html", pageData(c, "PCRs", gin.H{"error": "unable to save file"}))
}
defer file.Close()
file.WriteString(c.Request.Form.Get("pcr"))
c.Redirect(http.StatusSeeOther, "/pcr")
}
func setManualAdjustment(c *gin.Context) {
selectedTeam := c.PostForm("team")
adjustment := c.PostForm("adjustment")
id, err := strconv.Atoi(selectedTeam)
if err != nil {
errorOutAnnoying(c, errors.New("invalid team id: "+selectedTeam))
return
}
validateTeam(c, uint(id))
teamID := uint(id)
adjustmentVal, err := strconv.Atoi(adjustment)
if err != nil {
errorOutAnnoying(c, errors.New("invalid adjustment: "+adjustment))
return
}
adjustmentMutex.Lock()
defer adjustmentMutex.Unlock()
// Add to manual adjustments to process
if val, ok := manualAdjustments[teamID]; ok {
manualAdjustments[teamID] = val + adjustmentVal
} else {
manualAdjustments[teamID] = adjustmentVal
}
c.Redirect(http.StatusSeeOther, "/settings")
}
func viewPersist(c *gin.Context) {
// previous rounds from db
var previous []Persist
db.Preload(clause.Associations).Order("round desc").Find(&previous)
// Sort by round descending
c.HTML(http.StatusOK, "persists.html", pageData(c, "Persists", gin.H{"current": persistHits, "previous": previous}))
}
func scorePersist(c *gin.Context) {
teamMutex.Lock()
defer teamMutex.Unlock()
// Identify box (team and check)
remoteIP := c.RemoteIP()
team, boxName, err := boxFromIP(remoteIP)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "your IP is not a valid box"})
return
}
offender, err := tokenToTeam(c.Param("token"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if team.ID == offender.ID {
c.JSON(http.StatusBadRequest, gin.H{"error": "you can't hack yourself..."})
return
}
// Initialize map if not already created
if _, ok := persistHits[team.ID]; !ok {
persistHits[team.ID] = make(map[string][]uint)
}
// Ensure not a duplicate
for _, persist := range persistHits[team.ID][boxName] {
if persist == offender.ID {
c.JSON(http.StatusOK, "OK")
return
}
}
// Append offender ID
persistHits[team.ID][boxName] = append(persistHits[team.ID][boxName], offender.ID)
c.JSON(http.StatusOK, "OK")
}
func scoreAgent(c *gin.Context) {
teamMutex.Lock()
defer teamMutex.Unlock()
// Identify box (team and check)
remoteIP := c.RemoteIP()
team, boxName, err := boxFromIP(remoteIP)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "your IP is not a valid box"})
return
}
// Insert last seen time
agentHits[team.ID][boxName] = time.Now()
c.JSON(http.StatusOK, "OK")
}
/*
func viewRed(c *gin.Context) {
// yeet
}
func submitRed(c *gin.Context) {
// yeet
}
*/
func viewResets(c *gin.Context) {
// yeet
}
func submitReset(c *gin.Context) {
// yeet
}
func viewInjects(c *gin.Context) {
team := getUser(c)
// view all injects
var injects []Inject
// populate status for each inject
if !team.IsAdmin() {
res := db.Find(&injects)
if res.Error != nil {
errorOutGraceful(c, res.Error)
return
}
for i, inj := range injects {
var submissions []InjectSubmission
res := db.Order("time desc").Where("team_id = ? and inject_id = ?", team.ID, inj.ID).Find(&submissions)
if res.Error != nil {
errorOutGraceful(c, res.Error)
return
}
if len(submissions) != 0 {
for _, sub := range submissions {
if sub.Graded {
injects[i].Status = GRADED
break
}
}
if injects[i].Status != GRADED {
injects[i].Status = SUBMITTED
}
}
}
} else {
res := db.Preload("Submissions").Find(&injects)
if res.Error != nil {
errorOutGraceful(c, res.Error)
return
}
}
c.HTML(http.StatusOK, "injects.html", pageData(c, "injects", gin.H{"injects": injects}))
}
func injectFeed(c *gin.Context) {
var submissions []InjectSubmission
team := getUser(c)
if !team.IsAdmin() {
errorOutAnnoying(c, errors.New("non-admin feed access"))
return
}
res := db.Find(&submissions, "invalid = false and feedback = '' and score = 0")
if res.Error != nil {
errorOutGraceful(c, res.Error)
return
}
c.HTML(http.StatusOK, "feed.html", pageData(c, "Inject Feed", gin.H{"submissions": submissions}))
}
func createInject(c *gin.Context) {
apiKey := c.GetHeader("X-Api-Key")
if apiKey != dwConf.InjectAPIKey {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid key"})
return
}
var newInject Inject
err := c.BindJSON(&newInject)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
newInject.Time = ZeroTime.Add(time.Now().Sub(startTime))
res := db.Create(&newInject)
if res.Error != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": res.Error})
return
}
c.JSON(http.StatusOK, gin.H{"status": "OK"})
}
func viewInject(c *gin.Context) {
// view individual inject
injectID, err := strconv.Atoi(c.Param("inject"))
if err != nil {
errorOutAnnoying(c, errors.New("invalid inject id (not a number)"))
return
}
var inject Inject
res := db.First(&inject, "id = ?", injectID)
if res.Error != nil {
errorOutAnnoying(c, errors.New("invalid inject id"))
return
}
team := getUser(c)
var submissions []InjectSubmission
if team.IsAdmin() {
res := db.Preload("Team").Find(&submissions, "inject_id = ?", inject.ID)
if res.Error != nil {
errorOutGraceful(c, err)
return
}
} else {
res := db.Order("time desc").Find(&submissions, "team_id = ? and inject_id = ?", team.ID, inject.ID)
if res.Error != nil {
errorOutGraceful(c, err)
return
}
if time.Now().Before(inject.OpenTime()) {
errorOutAnnoying(c, errors.New("non-admin attempted inject before being available"))
return
}
}
c.HTML(http.StatusOK, "inject.html", pageData(c, "injects", gin.H{"inject": inject, "submissions": submissions}))
}
func deleteInject(c *gin.Context) {
team := getUser(c)
if !team.IsAdmin() {
errorOutAnnoying(c, errors.New("non-admin tried to delete inject: "+c.Param("team")))
return
}
// delete inject
injectID, err := strconv.Atoi(c.Param("inject"))
if err != nil {
errorOutAnnoying(c, errors.New("invalid inject id (not a number)"))
return
}
var inject Inject
res := db.First(&inject, "id = ?", injectID)
if res.Error != nil {
errorOutAnnoying(c, errors.New("invalid inject id"))
return
}
// lol no sql injection tho
res = db.Exec(fmt.Sprintf("DELETE FROM injects where id = %d", inject.ID))
if res.Error != nil {
errorOutAnnoying(c, errors.New("invalid inject id"))
return
}
c.Redirect(http.StatusSeeOther, "/injects")
}
func submitInject(c *gin.Context) {
team := getUser(c)
c.Request.ParseForm()
injectID, err := strconv.Atoi(c.Param("inject"))
if err != nil {
errorOutAnnoying(c, errors.New("invalid inject id (not a number)"))
return
}
var inject Inject
res := db.Find(&inject, "id = ?", injectID)
if res.Error != nil {
errorOutAnnoying(c, errors.New("invalid inject id"))
return
}
if !team.IsAdmin() {
file, err := c.FormFile("submission")
if err != nil {
c.Redirect(http.StatusSeeOther, "/injects/view/"+strconv.Itoa(int(inject.ID)))
return
}
if dwConf.NoPasswords || injectID != 1 {
if len(file.Filename) < 4 || file.Filename[len(file.Filename)-4:] != ".pdf" {
c.HTML(http.StatusOK, "inject.html", pageData(c, "Injects", gin.H{"error": "Your inject upload must have a .PDF extension.", "inject": inject}))
return
}
if len(file.Header["Content-Type"]) != 1 || file.Header["Content-Type"][0] != "application/pdf" {
c.HTML(http.StatusOK, "inject.html", pageData(c, "Injects", gin.H{"error": "Your inject upload must be a PDF.", "inject": inject}))
return
}
}
newSubmission := InjectSubmission{
Time: time.Now(),
Updated: time.Now(),
TeamID: team.ID,
InjectID: uint(inject.ID),
FileName: file.Filename,
DiskFile: uuid.New().String(),
}
if newSubmission.Time.After(inject.Closes) {
c.HTML(http.StatusOK, "inject.html", pageData(c, "Injects", gin.H{"error": "inject is no longer accepting submissions", "inject": inject}))
return
}
if err := c.SaveUploadedFile(file, "submissions/"+newSubmission.DiskFile); err != nil {
c.HTML(http.StatusOK, "inject.html", pageData(c, "Injects", gin.H{"error": "unable to save file", "inject": inject}))
return
}
if res := db.Save(&newSubmission); res.Error != nil {
c.HTML(http.StatusOK, "inject.html", pageData(c, "Injects", gin.H{"error": res.Error, "inject": inject}))
return
}
} else {
c.HTML(http.StatusOK, "inject.html", pageData(c, "Injects", gin.H{"error": "Sorry boss, admins can't submit injects.", "inject": inject}))
}
c.Redirect(http.StatusSeeOther, "/injects/view/"+strconv.Itoa(int(inject.ID)))
}
func invalidateInject(c *gin.Context) {
team := getUser(c)
injectID, err := strconv.Atoi(c.Param("inject"))
if err != nil {
errorOutAnnoying(c, errors.New("invalid InjectID"))
return
}
submissionId, err := strconv.Atoi(c.Param("submission"))
if err != nil {
errorOutAnnoying(c, errors.New("submissionId is not a number"))
return
}
var submission InjectSubmission
res := db.Find(&submission, "id = ? and inject_id = ?", submissionId, injectID)
if res.Error != nil {
errorOutGraceful(c, err)
return
}
fmt.Println(submission, err, submissionId)
if err != nil || submission.Updated.IsZero() {
errorOutAnnoying(c, errors.New("invalid team or inject id"))
return
}
if !team.IsAdmin() && team.ID != submission.TeamID {
errorOutAnnoying(c, errors.New("non-admin and non-team invalidation access"))
return
}
submission.Invalid = true
submission.Updated = time.Now()
res = db.Save(submission)
if res.Error != nil {
errorPrint(res.Error)
}
c.Redirect(http.StatusSeeOther, "/injects/view/"+strconv.Itoa(int(submission.InjectID)))
}
func gradeInject(c *gin.Context) {
var injects []Inject
res := db.Find(&injects)
if res.Error != nil {
errorPrint(res.Error)
return
}
injectId, err := strconv.Atoi(c.Param("inject"))
if err != nil {
errorOutAnnoying(c, errors.New("invalid inject id"))
return
}
team := getUser(c)
var submission InjectSubmission
if team.IsAdmin() {
submissionId, err := strconv.Atoi(c.Param("submission"))
if err != nil {
errorOutAnnoying(c, errors.New("submissionId is not a number"))
return
}
res := db.First(&submission, "id = ? and inject_id = ?", submissionId, injectId)
if res.Error != nil {
errorOutGraceful(c, err)
return
}
} else {
errorOutAnnoying(c, errors.New("non-admin attempted grade access"))
return
}
c.HTML(http.StatusOK, "grade.html", pageData(c, "grading", gin.H{"submission": submission}))
}
func submitInjectGrade(c *gin.Context) {
submissionId, err := strconv.Atoi(c.Param("submission"))
if err != nil {
errorOutAnnoying(c, errors.New("submissionId is not a number"))
return
}
var submission InjectSubmission
res := db.Find(&submission, "id = ?", submissionId)
if res.Error != nil {
errorOutGraceful(c, err)
return
}
submission.Score, err = strconv.Atoi(c.PostForm("score"))
if err != nil {
errorOutGraceful(c, err)
return
}
submission.Graded = true
submission.Feedback = c.PostForm("feedback")
res = db.Save(&submission)
if res.Error != nil {
errorPrint(res.Error)
}
fmt.Println("Score: ", submission.Score, "\nFeedback: ", submission.Feedback)
c.Redirect(http.StatusSeeOther, "/injects/view/"+strconv.Itoa(int(submission.InjectID)))
}
func exportTeamData(c *gin.Context) {
id, err := strconv.Atoi(c.Param("team"))
if err != nil {
errorOutAnnoying(c, errors.New("invalid team id: "+c.Param("team")))
return
}
team := validateTeam(c, uint(id))
csvString := "time,round,service,inject,sla,"
for _, b := range dwConf.Box {
for _, c := range b.CheckList {
csvString += c.FetchName() + ","
}
}
csvString += "total\n"
var records []TeamRecord
res := db.Find(&records, "team = ?", team.Name)
if res.Error != nil {
errorOutGraceful(c, res.Error)
return
}
for _, r := range records {
csvString += r.Time.In(loc).Format("03:04:05 PM") + ","
csvString += strconv.Itoa(r.Round) + ","
csvString += strconv.Itoa(r.ServicePoints) + ","
csvString += strconv.Itoa(r.InjectPoints) + ","
statusString := ""
for _, c := range r.Results {
if c.Status {
statusString += "up,"
} else {
statusString += "down,"
}
}
csvString += "-" + strconv.Itoa(r.SlaViolations*dwConf.SlaPoints) + ","
csvString += statusString
csvString += strconv.Itoa(r.Total) + "\n"
}
c.Data(200, "text/csv", []byte(csvString))
}
func viewSettings(c *gin.Context) {
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(dwConf); err != nil {
c.HTML(http.StatusInternalServerError, "settings.html", pageData(c, "Settings", gin.H{"error": err}))
return
}
adjustmentMutex.Lock()
defer adjustmentMutex.Unlock()
c.HTML(http.StatusOK, "settings.html", pageData(c, "Settings", gin.H{"config": buf.String(), "adjustments": manualAdjustments}))
}
func pageData(c *gin.Context, title string, ginMap gin.H) gin.H {
newGinMap := gin.H{}
newGinMap["title"] = title
newGinMap["user"] = getUserOptional(c)
newGinMap["m"] = dwConf
newGinMap["event"] = dwConf.Event
newGinMap["loc"] = loc
newGinMap["start"] = startTime
newGinMap["time"] = time.Now()
runtime := time.Since(startTime).Round(time.Second)
if runtime < 0 {
runtime = 0
}
newGinMap["runtime"] = runtime
for key, value := range ginMap {
newGinMap[key] = value
}
return newGinMap
}
// validateTeam tests to see if the team currently logged in
// has authorization to access the team id requested. It always
// allows if admin, and errors out if invalid user.
func validateTeam(c *gin.Context, id uint) TeamData {
team := getUser(c)
if team.ID == id {
return team
} else if team.IsAdmin() {
if realTeam, err := dwConf.GetTeam(id); err == nil {
return realTeam
} else {
errorPrint(err)
}
}
errorOutAnnoying(c, errors.New("team could not be validated"))
return TeamData{}
}
func (m *config) IsValid(team TeamData, id string) bool {
if team.Name == id || team.IsAdmin() {
return true
}
return false
}