From 72b245af4add1918c44035cddc407097501f43b4 Mon Sep 17 00:00:00 2001 From: raul Date: Thu, 13 Jun 2024 09:24:20 +0200 Subject: [PATCH] Create generic kill/remove funcs and setup bulk actions backend --- cmd/httpServer.go | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/cmd/httpServer.go b/cmd/httpServer.go index 431ed0c..04e4c32 100644 --- a/cmd/httpServer.go +++ b/cmd/httpServer.go @@ -41,9 +41,35 @@ func WebServer() { r.GET("/dump", dumpClients) r.POST("/save", saveClients) r.POST("/remove/:clientid", removeClient) + r.POST("/bulk-kill", bulkKill) + r.POST("/bulk-remove/", bulkRemove) + + // Start r.Run(":" + WebPort) } +func bulkKill(c *gin.Context) { + list := Bulk{} + c.ShouldBind(&list) + if len(list.ClientIDs) == 0 { + return + } + for _, v := range list.ClientIDs { + genericKillswitch(v) + } +} + +func bulkRemove(c *gin.Context) { + list := Bulk{} + c.ShouldBind(&list) + if len(list.ClientIDs) == 0 { + return + } + for _, v := range list.ClientIDs { + genericRemoveClient(v) + } +} + func dumpClients(c *gin.Context) { jsonClients := marshalClients() c.IndentedJSON(http.StatusOK, jsonClients) @@ -76,7 +102,11 @@ func removeClient(c *gin.Context) { c.String(http.StatusInternalServerError, "Error happened fetching client: %v", err) return } - client, id, err := returnClient(intClientID) + genericRemoveClient(intClientID) +} + +func genericRemoveClient(id int) { + client, id, err := returnClient(id) if err != nil { return } @@ -87,7 +117,7 @@ func removeClient(c *gin.Context) { }) } - log.Printf("Removing client %v\n", intClientID) + log.Printf("Removing client %v\n", id) if len(clientList) != 1 { clientList = append(clientList[:id], clientList[id+1:]...) } else { @@ -134,7 +164,11 @@ func sendKillswitch(c *gin.Context) { c.String(http.StatusInternalServerError, "Error happened fetching client: %v", err) return } - _, id, err := returnClient(intClientID) + genericKillswitch(intClientID) +} + +func genericKillswitch(id int) { + _, id, err := returnClient(id) if err != nil { return }