about summary refs log tree commit diff
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
parentb88e0e41d59c26dd281b41bdd0c754f54e90e3e2 (diff)
metrics exposed on /
-rw-r--r--.idea/circus-scoreboard.iml9
-rw-r--r--.idea/misc.xml6
-rw-r--r--.idea/modules.xml8
-rw-r--r--.idea/vcs.xml6
-rw-r--r--.idea/workspace.xml48
-rw-r--r--src/docker.go60
-rw-r--r--src/http.go8
-rw-r--r--src/main.go7
8 files changed, 140 insertions, 12 deletions
diff --git a/.idea/circus-scoreboard.iml b/.idea/circus-scoreboard.iml
new file mode 100644
index 0000000..5e764c4
--- /dev/null
+++ b/.idea/circus-scoreboard.iml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..28a804d
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="JavaScriptSettings">
+    <option name="languageLevel" value="ES6" />
+  </component>
+</project>
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..f68fe11
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/circus-scoreboard.iml" filepath="$PROJECT_DIR$/.idea/circus-scoreboard.iml" />
+    </modules>
+  </component>
+</project>
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000..ae3c018
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ChangeListManager">
+    <list default="true" id="1b279729-7d7b-40e3-8739-117eb4139c83" name="Default Changelist" comment="" />
+    <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
+    <option name="SHOW_DIALOG" value="false" />
+    <option name="HIGHLIGHT_CONFLICTS" value="true" />
+    <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
+    <option name="LAST_RESOLUTION" value="IGNORE" />
+  </component>
+  <component name="GOROOT" path="/usr/lib/go" />
+  <component name="Git.Settings">
+    <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
+  </component>
+  <component name="ProjectId" id="1RsOvkQ2Ee34JbzNz2yak68AYrU" />
+  <component name="PropertiesComponent">
+    <property name="WebServerToolWindowFactoryState" value="false" />
+    <property name="go.import.settings.migrated" value="true" />
+    <property name="go.sdk.automatically.set" value="true" />
+    <property name="last_opened_file_path" value="$PROJECT_DIR$/.." />
+  </component>
+  <component name="RunDashboard">
+    <option name="ruleStates">
+      <list>
+        <RuleState>
+          <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
+        </RuleState>
+        <RuleState>
+          <option name="name" value="StatusDashboardGroupingRule" />
+        </RuleState>
+      </list>
+    </option>
+  </component>
+  <component name="TypeScriptGeneratedFilesManager">
+    <option name="version" value="1" />
+  </component>
+  <component name="Vcs.Log.Tabs.Properties">
+    <option name="TAB_STATES">
+      <map>
+        <entry key="MAIN">
+          <value>
+            <State />
+          </value>
+        </entry>
+      </map>
+    </option>
+  </component>
+</project>
\ No newline at end of file
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)
 		}
 	}()