about summary refs log tree commit diff
path: root/src/docker.go
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-10-09 18:32:29 +0200
committerEmile <hanemile@protonmail.com>2019-10-09 18:32:29 +0200
commitde78273f080634b57cc0e0e7c3e05e3ff03d0478 (patch)
tree3597a0160b615499797a0d0e725b2cfe35f58315 /src/docker.go
parentb88e0e41d59c26dd281b41bdd0c754f54e90e3e2 (diff)
metrics exposed on /
Diffstat (limited to 'src/docker.go')
-rw-r--r--src/docker.go60
1 files changed, 52 insertions, 8 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]++
+				}
+			}
 		}
 	}
 }