From 0d2fe61af698a0e1d1f650d80f296bd88fc3cb6e Mon Sep 17 00:00:00 2001 From: Emile Date: Wed, 9 Oct 2019 18:34:23 +0200 Subject: gofmt and added user to stats struct --- src/http.go | 36 ++++++++++++++++++++---------------- src/seed.go | 23 +++++++++++++---------- 2 files changed, 33 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/http.go b/src/http.go index 480e356..839f6ea 100644 --- a/src/http.go +++ b/src/http.go @@ -1,16 +1,17 @@ package main import ( + "encoding/json" "flag" - "github.com/gorilla/mux" - "net/http" "fmt" - "strings" "io/ioutil" - "time" "log" - "encoding/json" + "net/http" "strconv" + "strings" + "time" + + "github.com/gorilla/mux" ) var ( @@ -21,7 +22,7 @@ func registerHTTPFlags() { port = flag.Int("port", 8080, "The port for HTTP") } -func setupHTTPServer() (http.Server) { +func setupHTTPServer() http.Server { r := mux.NewRouter() r.HandleFunc("/", indexHandler) @@ -40,7 +41,7 @@ func setupHTTPServer() (http.Server) { r.HandleFunc("/api/getStats", getStatsHandler).Methods("GET") return http.Server{ - Addr: fmt.Sprintf("0.0.0.0:%d", *port), + Addr: fmt.Sprintf("0.0.0.0:%d", *port), Handler: r, } } @@ -96,6 +97,9 @@ func loginPostHandler(w http.ResponseWriter, r *http.Request) { accessCode := r.Form.Get("accesscode") session, cookieNotFoundError := r.Cookie("session") + // store the name of the user for the scoreboard scraper + user = username + validRedirect := false // we need to verify that the client doesn't already have a valid session @@ -109,9 +113,9 @@ func loginPostHandler(w http.ResponseWriter, r *http.Request) { // credentials valid, create session newSessionToken := createSession() http.SetCookie(w, &http.Cookie{ - Name: "session", - Value: newSessionToken, - Path: "/", + Name: "session", + Value: newSessionToken, + Path: "/", Expires: time.Now().Add(time.Hour * 24), }) validRedirect = true @@ -149,9 +153,9 @@ func logoutHandler(w http.ResponseWriter, r *http.Request) { // and delete the cookie http.SetCookie(w, &http.Cookie{ - Name: "session", - Value: "", - Path: "/", + Name: "session", + Value: "", + Path: "/", Expires: time.Unix(0, 0), }) } @@ -263,7 +267,7 @@ func submitFlagHandler(w http.ResponseWriter, r *http.Request) { // inform our client jsonAnswer, _ := json.Marshal(map[string]string{ "correctFlag": strconv.FormatBool(correctFlag), - "error": errorString, + "error": errorString, }) w.Write([]byte(jsonAnswer)) } @@ -308,7 +312,7 @@ func startContainerHandler(w http.ResponseWriter, r *http.Request) { // inform our client jsonAnswer, _ := json.Marshal(map[string]string{ - "error": errorString, + "error": errorString, "address": addressString, }) w.Write([]byte(jsonAnswer)) @@ -369,7 +373,7 @@ func getTimeLimitHandler(w http.ResponseWriter, r *http.Request) { jsonAnswer, _ := json.Marshal(map[string]string{ "endTimestamp": fmt.Sprintf("%d", *endTimestamp), - "endAfter": fmt.Sprintf("%d", *endAfter), + "endAfter": fmt.Sprintf("%d", *endAfter), }) w.Write([]byte(jsonAnswer)) } diff --git a/src/seed.go b/src/seed.go index dd836ff..805ab20 100644 --- a/src/seed.go +++ b/src/seed.go @@ -4,15 +4,17 @@ import ( "encoding/json" "flag" "fmt" - "github.com/pkg/errors" "io/ioutil" "log" "time" + + "github.com/pkg/errors" ) var ( - challenges = []Challenge{} - seedFilePath* string + challenges = []Challenge{} + seedFilePath *string + user string ) func registerSeedFlags() { @@ -52,13 +54,13 @@ func readSeedFile(path string) ([]Challenge, error) { if nameOK && descOK && flagOK && contOK && categoryOK { tmpChallenges = append(tmpChallenges, Challenge{ - Name: name, + Name: name, Description: desc, - Flag: flag, - FoundFlag: time.Unix(0, 0), - FlagTries: 0, - Container: cont, - Category: category, + Flag: flag, + FoundFlag: time.Unix(0, 0), + FlagTries: 0, + Container: cont, + Category: category, }) } else { log.Printf("Ignoring challenge at position %d: Not all values are parseable (name: %b, desc: %b, flag: %b, container: %b, category: %b", index, nameOK, descOK, flagOK, contOK, categoryOK) @@ -69,7 +71,7 @@ func readSeedFile(path string) ([]Challenge, error) { } // Read the file we set up using flags and store the returned challenge array -func getChallengesFromSeedFile() (error) { +func getChallengesFromSeedFile() error { tmpChallenges, error := readSeedFile(*seedFilePath) if error != nil { @@ -124,6 +126,7 @@ func generateJSONFromChallengesForStats() (string, error) { } marshalled, marshalError := json.Marshal(map[string]interface{}{ + "user": user, "challenges": strippedChallenges, }) return string(marshalled), marshalError -- cgit 1.4.1