about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/challenge.go14
-rw-r--r--src/http.go12
-rw-r--r--src/seed.go19
3 files changed, 45 insertions, 0 deletions
diff --git a/src/challenge.go b/src/challenge.go
index 0203378..1d0a210 100644
--- a/src/challenge.go
+++ b/src/challenge.go
@@ -23,6 +23,12 @@ type StrippedChallenge struct {
 	IPAddress string `json:"IPAddress"`
 }
 
+type StatsStrippedChallenge struct {
+	Name string `json:"name"`
+	FoundFlag int64 `json:"foundFlag"`
+	FlagTries uint `json:"flagTries"`
+}
+
 func stripChallenge(c Challenge) (StrippedChallenge) {
 	return StrippedChallenge{
 		Name: c.Name,
@@ -34,3 +40,11 @@ func stripChallenge(c Challenge) (StrippedChallenge) {
 		IPAddress: getAddressForChallengeContainer(c.Container),
 	}
 }
+
+func stripChallengeForStatistics(c Challenge) (StatsStrippedChallenge) {
+	return StatsStrippedChallenge{
+		Name: c.Name,
+		FoundFlag: c.FoundFlag.Unix(),
+		FlagTries: c.FlagTries,
+	}
+}
diff --git a/src/http.go b/src/http.go
index 6feeb33..480e356 100644
--- a/src/http.go
+++ b/src/http.go
@@ -37,6 +37,7 @@ func setupHTTPServer() (http.Server) {
 	r.HandleFunc("/api/stopContainer", stopContainerHandler).Methods("POST")
 	r.HandleFunc("/api/getAccess", getAccessHandler).Methods("GET")
 	r.HandleFunc("/api/getTimeLimit", getTimeLimitHandler).Methods("GET")
+	r.HandleFunc("/api/getStats", getStatsHandler).Methods("GET")
 
 	return http.Server{
 		Addr: fmt.Sprintf("0.0.0.0:%d", *port),
@@ -203,6 +204,17 @@ func getChallengesHandler(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
+func getStatsHandler(w http.ResponseWriter, r *http.Request) {
+	json, jsonErr := generateJSONFromChallengesForStats()
+
+	if jsonErr == nil {
+		w.Write([]byte(json))
+	} else {
+		log.Println(jsonErr)
+		w.WriteHeader(500)
+	}
+}
+
 func submitFlagHandler(w http.ResponseWriter, r *http.Request) {
 	r.ParseForm()
 	challengeName := r.Form.Get("challengeName")
diff --git a/src/seed.go b/src/seed.go
index af14d38..dd836ff 100644
--- a/src/seed.go
+++ b/src/seed.go
@@ -109,3 +109,22 @@ func generateJSONFromChallenges() (string, error) {
 	})
 	return string(marshalled), marshalError
 }
+
+// Generate a JSON string from the stored challenges, just containing enough to call it statistics
+func generateJSONFromChallengesForStats() (string, error) {
+	// To include only required information for statistics, we need to strip the challenge
+	var strippedChallenges []StatsStrippedChallenge
+	categories := map[string]int{}
+
+	for _, challenge := range challenges {
+		// Append challenge to list
+		strippedChallenges = append(strippedChallenges, stripChallengeForStatistics(challenge))
+
+		categories[challenge.Category]++
+	}
+
+	marshalled, marshalError := json.Marshal(map[string]interface{}{
+		"challenges": strippedChallenges,
+	})
+	return string(marshalled), marshalError
+}