diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/docker.go | 60 | ||||
-rw-r--r-- | src/http.go | 8 | ||||
-rw-r--r-- | src/main.go | 7 |
3 files changed, 63 insertions, 12 deletions
diff --git a/src/docker.go b/src/docker.go index 6bf39ee..236580f 100644 --- a/src/docker.go +++ b/src/docker.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/json" "fmt" "io/ioutil" "log" @@ -20,8 +21,22 @@ const ( var ( dockerCtx context.Context dockerCLI *client.Client + + // store the amount of flags each user has found + userFlags map[string]int + users []string // list of users ) +// Statistics stores companion statistics +type Statistics struct { + Challenges []struct { + Name string `json:"name"` + FoundFlag int64 `json:"foundFlag"` + FlagTries uint `json:"flagTries"` + } `json:"challenges"` + User string `json:"user"` +} + func setupContext() { if dockerCtx == nil { dockerCtx = context.Background() @@ -73,17 +88,17 @@ func listDockerContainers() { var companionContainerIPs []string for _, network := range networks { - for _, container := range network.Containers { - for _, name := range companionContainerNames { - if container.Name == name { - companionContainerIPs = append(companionContainerIPs, container.IPv4Address) + if network.Name == "circus" { + for _, container := range network.Containers { + for _, name := range companionContainerNames { + if container.Name == name { + companionContainerIPs = append(companionContainerIPs, container.IPv4Address) + } } } } } - fmt.Printf("%#v\n\n", companionContainerIPs) - // print all ips found for _, rawip := range companionContainerIPs { @@ -97,11 +112,40 @@ func listDockerContainers() { fmt.Printf("ip: %s\n\n", ip) // get the stats from the container - stats, err := getStats(ip, 8080) + statsJSON, err := getStats(ip, 8080) if err != nil { fmt.Println(err) } - fmt.Printf("%s\n", stats) + + // unmarshal the json statistics into a golang struct + var statsGoStruct Statistics + err = json.Unmarshal([]byte(statsJSON), &statsGoStruct) + if err != nil { + fmt.Println("error: ", err) + } + + // if the user is not in the users list, add the user to the users list + present := false + for _, user := range users { + if user == statsGoStruct.User { + present = true + } + } + if present == false { + users = append(users, statsGoStruct.User) + } + + if userFlags == nil { + userFlags = make(map[string]int) + } + + // count the amount of flags each user has found + userFlags[statsGoStruct.User] = 0 + for _, chall := range statsGoStruct.Challenges { + if chall.FoundFlag > 1 { + userFlags[statsGoStruct.User]++ + } + } } } } diff --git a/src/http.go b/src/http.go index 0095663..0add89e 100644 --- a/src/http.go +++ b/src/http.go @@ -21,7 +21,11 @@ func setupHTTPServer() http.Server { // indexHandler handles the "/" endpoint func indexHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintln(w, "This is the index Handler speaking! Prepare to HACK THE PLANET!") + if userFlags == nil { + userFlags = make(map[string]int) + } - // insert scoreboard here + for _, user := range users { + fmt.Fprintf(w, "challengesSolves{name=%s} %d\n", user, userFlags[user]) + } } diff --git a/src/main.go b/src/main.go index a52c043..b974704 100644 --- a/src/main.go +++ b/src/main.go @@ -8,6 +8,11 @@ import ( var ( port *int + + // map the username to the amount of challenges solved + // TODO: challenges should get a score and the score should be exposed + // instead of "solved" or "not solved". + userScore map[string]int ) func main() { @@ -19,9 +24,7 @@ func main() { go func() { for { // get stats - log.Printf("Fetching the stats") listDockerContainers() - log.Printf("Done fetching the stats") time.Sleep(1 * time.Second) } }() |