From de78273f080634b57cc0e0e7c3e05e3ff03d0478 Mon Sep 17 00:00:00 2001 From: Emile Date: Wed, 9 Oct 2019 18:32:29 +0200 Subject: metrics exposed on / --- src/docker.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 8 deletions(-) (limited to 'src/docker.go') 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]++ + } + } } } } -- cgit 1.4.1