diff --git a/functions.go b/functions.go index d3c0dea..fea10d1 100644 --- a/functions.go +++ b/functions.go @@ -1,15 +1,10 @@ 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) { @@ -22,47 +17,3 @@ func check(err error) { 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/requests.go b/requests.go new file mode 100644 index 0000000..017bee6 --- /dev/null +++ b/requests.go @@ -0,0 +1,55 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" + + s "git.nils.zone/clientserverapi/server/shared" + "git.nils.zone/nils/prettify" + "github.com/elastic/go-sysinfo/types" +) + +func commands(w http.ResponseWriter, r *http.Request) { + // Client requests instructions + log.Println("Client requesting instructions, sending the following:") + var o s.OSInfo + err := json.NewDecoder(r.Body).Decode(&o) + check(err) + log.Println("Received the following OS info:") + prettify.Print(o) + + var i s.Instruction + + // check the OS and send the appropriate command + switch o.OSType { + case "linux": + // i = instruction{Command: "uname", Args: []string{"-a"}} + i = s.Instruction{Command: "poweroff"} + case "windows": + i = s.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 s.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/types/types.go b/shared/types.go similarity index 66% rename from types/types.go rename to shared/types.go index 42390a6..54a44d8 100644 --- a/types/types.go +++ b/shared/types.go @@ -1,19 +1,19 @@ -package types +package shared -// instruction is a structured way of sending commands +// 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 +// 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 +// OSInfo needs a comment type OSInfo struct { OSType string `json:"os_type"` OSArch string `json:"os_arch"`