Fix issues caused by accessing clients via array identifier

This commit is contained in:
raul 2024-06-11 13:41:49 +02:00
parent 85d5bfd1ca
commit d07e6435fb
2 changed files with 26 additions and 12 deletions

View File

@ -76,6 +76,13 @@ func removeClient(c *gin.Context) {
c.String(http.StatusInternalServerError, "Error happened fetching client: %v", err) c.String(http.StatusInternalServerError, "Error happened fetching client: %v", err)
return return
} }
client, err := returnClient(intClientID)
if err != nil {
return
}
client.Instruct(Instructions{
IsKillswitch: true,
})
log.Printf("Removing client %v\n", intClientID) log.Printf("Removing client %v\n", intClientID)
if len(clientList) != 1 { if len(clientList) != 1 {
clientList = append(clientList[:intClientID], clientList[intClientID+1:]...) clientList = append(clientList[:intClientID], clientList[intClientID+1:]...)
@ -143,14 +150,19 @@ func execCMD(c *gin.Context) {
return return
} }
if clientList[idInt].IsOnline == false { client, err := returnClient(idInt)
if err != nil {
return
}
if client.IsOnline == false {
c.String(http.StatusOK, "Client is currently offline!") c.String(http.StatusOK, "Client is currently offline!")
return return
} }
command, _ := c.GetPostForm("cmd") command, _ := c.GetPostForm("cmd")
out, err := sendCommand(idInt, command) out, err := sendCommand(client, command)
if err != nil { if err != nil {
e := fmt.Sprintf("Error happened executing command: %v\n", err) e := fmt.Sprintf("Error happened executing command: %v\n", err)
c.String(http.StatusOK, e) c.String(http.StatusOK, e)

View File

@ -119,13 +119,13 @@ func handleConn(conn net.Conn) {
go Heartbeat(ID) go Heartbeat(ID)
} }
func sendCommand(ID int, command string) (Output string, err error) { func sendCommand(client Client, command string) (Output string, err error) {
inst := Instructions{ inst := Instructions{
IsCommand: true, IsCommand: true,
Message: command, Message: command,
} }
clientList[ID].Instruct(inst) client.Instruct(inst)
resp, err := ServerMessageReceiver(clientList[ID].Conn) resp, err := ServerMessageReceiver(client.Conn)
if err != nil || resp.Successful != true { if err != nil || resp.Successful != true {
e := fmt.Errorf("%v\n", resp.Message) e := fmt.Errorf("%v\n", resp.Message)
return "", e return "", e
@ -135,22 +135,24 @@ func sendCommand(ID int, command string) (Output string, err error) {
func Heartbeat(ID int) { func Heartbeat(ID int) {
for { for {
if len(clientList) == 0 { client, err := returnClient(ID)
if err != nil {
return return
} }
inst := Instructions{ inst := Instructions{
IsHeartbeat: true, IsHeartbeat: true,
Message: "PING", Message: "PING",
} }
clientList[ID].Instruct(inst) client.Instruct(inst)
resp, err := ServerMessageReceiver(clientList[ID].Conn) resp, err := ServerMessageReceiver(client.Conn)
if err == nil && resp.Message == "PONG" { if err == nil && resp.Message == "PONG" {
if clientList[ID].IsOnline != true { if client.IsOnline != true {
clientList[ID].IsOnline = true client.IsOnline = true
} }
} else { } else {
log.Printf("Client %v is offline :(\n", clientList[ID].ClientBasicInfo.Hostname) log.Printf("Client %v is offline :(\n", client.ClientBasicInfo.Hostname)
clientList[ID].IsOnline = false client.IsOnline = false
return return
} }