From 0fec126ac4e751e8c4d71513ba3590ec434c9587 Mon Sep 17 00:00:00 2001
From: Nils Jakobi <jakobi.nils@gmail.com>
Date: Fri, 11 Dec 2020 21:23:16 +0100
Subject: [PATCH] first draft

---
 functions.go                | 68 +++++++++++++++++++++++++++++++++++++
 go.mod                      |  9 +++++
 go.sum                      | 39 +++++++++++++++++++++
 main.go                     | 42 +++++++++++++++++++++++
 storage/storageFunctions.go |  7 ++++
 types/types.go              | 20 +++++++++++
 6 files changed, 185 insertions(+)
 create mode 100644 functions.go
 create mode 100644 go.mod
 create mode 100644 go.sum
 create mode 100644 main.go
 create mode 100644 storage/storageFunctions.go
 create mode 100644 types/types.go

diff --git a/functions.go b/functions.go
new file mode 100644
index 0000000..d3c0dea
--- /dev/null
+++ b/functions.go
@@ -0,0 +1,68 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"log"
+	"net/http"
+
+	"git.nils.zone/clientserverapi/server/storage"
+	t "git.nils.zone/clientserverapi/server/types"
+	"git.nils.zone/nils/prettify"
+	"github.com/elastic/go-sysinfo/types"
+)
+
+func check(err error) {
+	if err != nil {
+		panic(err)
+	}
+	storage.GetComputers()
+}
+
+func homePage(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintf(w, "Nothing here...")
+}
+
+func commands(w http.ResponseWriter, r *http.Request) {
+	// Client requests instructions
+	log.Println("Client requesting instructions, sending the following:")
+	var o t.OSInfo
+	err := json.NewDecoder(r.Body).Decode(&o)
+	check(err)
+	log.Println("Received the following OS info:")
+	prettify.Print(o)
+
+	var i t.Instruction
+
+	// check the OS and send the appropriate command
+	switch o.OSType {
+	case "linux":
+		// i = instruction{Command: "uname", Args: []string{"-a"}}
+		i = t.Instruction{Command: "poweroff"}
+	case "windows":
+		i = t.Instruction{Command: "winver"}
+	default:
+		return
+	}
+
+	log.Println("Sending the following instructions:")
+	prettify.Print(i)
+	w.Header().Set("Content-Type", "application/json")
+	json.NewEncoder(w).Encode(i)
+}
+
+func response(w http.ResponseWriter, r *http.Request) {
+	log.Println("Got the following output from client:")
+	var p t.CmdResponse
+	err := json.NewDecoder(r.Body).Decode(&p)
+	check(err)
+	prettify.Print(p)
+}
+
+func register(w http.ResponseWriter, r *http.Request) {
+	log.Println("Got the following output from client:")
+	var h types.HostInfo
+	err := json.NewDecoder(r.Body).Decode(&h)
+	check(err)
+	prettify.Print(h)
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..927ba10
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,9 @@
+module git.nils.zone/clientserverapi/server
+
+go 1.15
+
+require (
+	git.nils.zone/nils/prettify v0.0.4
+	github.com/elastic/go-sysinfo v1.4.0
+	github.com/gorilla/mux v1.8.0
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..d4a9305
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,39 @@
+git.nils.zone/nils/prettify v0.0.4 h1:y014Ejp0yhFAc9vu5U0nJRFxtXN6k6eWW6LiUkwn4NQ=
+git.nils.zone/nils/prettify v0.0.4/go.mod h1:q4ydEhSvXuywHe5U6uSEoEeS9f2Upz/n8z0MJylLz5c=
+github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
+github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/elastic/go-sysinfo v1.4.0 h1:LUnK6TNOuy8JEByuDzTAQH3iQ6bIywy55+Z+QlKNSWk=
+github.com/elastic/go-sysinfo v1.4.0/go.mod h1:i1ZYdU10oLNfRzq4vq62BEwD2fH8KaWh6eh0ikPT9F0=
+github.com/elastic/go-windows v1.0.0/go.mod h1:TsU0Nrp7/y3+VwE82FoZF8gC/XFg/Elz6CcloAxnPgU=
+github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
+github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
+github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
+github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
+github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e h1:0aewS5NTyxftZHSnFaJmWE5oCCrj4DyEXkAiMa1iZJM=
+github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
+github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
+github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901/go.mod h1:Z86h9688Y0wesXCyonoVr47MasHilkuLMqGhRZ4Hpak=
+github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
+github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
+github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
+github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
+github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
+github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
+github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
+github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
+github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
+github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
+golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20191025021431-6c3a3bfe00ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
+golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..110cca3
--- /dev/null
+++ b/main.go
@@ -0,0 +1,42 @@
+package main
+
+import (
+	"flag"
+	"log"
+	"net/http"
+
+	"github.com/gorilla/mux"
+)
+
+func handleRequests(runmode string) {
+	// creates a new instance of a mux router
+	router := mux.NewRouter().StrictSlash(true)
+	// replace http.HandleFunc with myRouter.HandleFunc
+	router.HandleFunc("/", homePage)
+	router.HandleFunc("/commands", commands)
+	router.HandleFunc("/response", response)
+	if runmode == "reg" {
+		router.HandleFunc("/register", register)
+	}
+
+	// serve the router
+	log.Fatal(http.ListenAndServe(":10000", router))
+}
+
+func main() {
+	// Flags
+	registerPtr := flag.Bool("register", false, "allow new clients to register with the server")
+
+	// parse all flags
+	flag.Parse()
+
+	if *registerPtr == true {
+		log.Println("Starting the server in registration mode...")
+		handleRequests("reg")
+	} else {
+		log.Println("Starting the server...")
+		handleRequests("")
+	}
+
+	// start server
+}
diff --git a/storage/storageFunctions.go b/storage/storageFunctions.go
new file mode 100644
index 0000000..abd1f81
--- /dev/null
+++ b/storage/storageFunctions.go
@@ -0,0 +1,7 @@
+package storage
+
+import "log"
+
+func GetComputers() {
+	log.Println("No computers here!")
+}
diff --git a/types/types.go b/types/types.go
new file mode 100644
index 0000000..42390a6
--- /dev/null
+++ b/types/types.go
@@ -0,0 +1,20 @@
+package types
+
+// instruction is a structured way of sending commands
+type Instruction struct {
+	Command string   `json:"command"`
+	Args    []string `json:"args"`
+}
+
+// cmdResponse is a response from the client to the server
+type CmdResponse struct {
+	Stdout   string `json:"stdout"`
+	Stderr   string `json:"stderr"`
+	ExitCode int    `json:"exitcode"`
+}
+
+// osInfo needs a comment
+type OSInfo struct {
+	OSType string `json:"os_type"`
+	OSArch string `json:"os_arch"`
+}