server/functions.go

69 lines
1.5 KiB
Go
Raw Normal View History

2020-12-11 20:23:16 +00:00
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)
}