restructuring
This commit is contained in:
parent
0fec126ac4
commit
199aa57c96
49
functions.go
49
functions.go
@ -1,15 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.nils.zone/clientserverapi/server/storage"
|
"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) {
|
func check(err error) {
|
||||||
@ -22,47 +17,3 @@ func check(err error) {
|
|||||||
func homePage(w http.ResponseWriter, r *http.Request) {
|
func homePage(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintf(w, "Nothing here...")
|
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)
|
|
||||||
}
|
|
||||||
|
55
requests.go
Normal file
55
requests.go
Normal file
@ -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)
|
||||||
|
}
|
@ -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 {
|
type Instruction struct {
|
||||||
Command string `json:"command"`
|
Command string `json:"command"`
|
||||||
Args []string `json:"args"`
|
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 {
|
type CmdResponse struct {
|
||||||
Stdout string `json:"stdout"`
|
Stdout string `json:"stdout"`
|
||||||
Stderr string `json:"stderr"`
|
Stderr string `json:"stderr"`
|
||||||
ExitCode int `json:"exitcode"`
|
ExitCode int `json:"exitcode"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// osInfo needs a comment
|
// OSInfo needs a comment
|
||||||
type OSInfo struct {
|
type OSInfo struct {
|
||||||
OSType string `json:"os_type"`
|
OSType string `json:"os_type"`
|
||||||
OSArch string `json:"os_arch"`
|
OSArch string `json:"os_arch"`
|
Loading…
Reference in New Issue
Block a user