Clean up code and prepare for download implementation

This commit is contained in:
raul 2024-06-18 10:08:38 +02:00
parent 475deffb4c
commit 287d7f82c8
1 changed files with 24 additions and 19 deletions

View File

@ -39,6 +39,7 @@ func WebServer() {
r.GET("/fs/:clientid", getFilesystem) r.GET("/fs/:clientid", getFilesystem)
r.POST("/ls/:clientid", listFiles) r.POST("/ls/:clientid", listFiles)
r.POST("/upload/:clientid", uploadFile) r.POST("/upload/:clientid", uploadFile)
r.GET("/download/:clientid", downloadFile)
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)
@ -51,21 +52,36 @@ func WebServer() {
r.Run(":" + WebPort) r.Run(":" + WebPort)
} }
func uploadFile(c *gin.Context) { func downloadFile(c *gin.Context) {
// path, _ := c.GetPostForm("fileToDownload")
//
// c.FileAttachment("", "")
}
func clientCheck(c *gin.Context) (Client, error) {
id := c.Param("clientid") id := c.Param("clientid")
idInt, err := strconv.Atoi(id) idInt, err := strconv.Atoi(id)
if err != nil { if err != nil {
c.String(http.StatusInternalServerError, "Error happened, please make this a proper error later") c.String(http.StatusInternalServerError, "Error happened, please make this a proper error later")
return return Client{}, err
} }
client, _, err := returnClient(idInt) client, _, err := returnClient(idInt)
if err != nil { if err != nil {
return return Client{}, err
} }
if client.IsOnline == false { if client.IsOnline == false {
c.String(http.StatusOK, "Client is currently offline!") c.String(http.StatusOK, "Client is currently offline!")
return Client{}, err
}
return *client, nil
}
func uploadFile(c *gin.Context) {
client, err := clientCheck(c)
if err != nil {
return return
} }
@ -75,7 +91,7 @@ func uploadFile(c *gin.Context) {
log.Println(err) log.Println(err)
} }
resp, err := uploadFileC2(*client, *file, path) resp, err := uploadFileC2(client, *file, path)
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)
@ -90,20 +106,8 @@ func uploadFile(c *gin.Context) {
} }
func listFiles(c *gin.Context) { func listFiles(c *gin.Context) {
id := c.Param("clientid") client, err := clientCheck(c)
idInt, err := strconv.Atoi(id)
if err != nil { if err != nil {
c.String(http.StatusInternalServerError, "Error happened, please make this a proper error later")
return
}
client, _, err := returnClient(idInt)
if err != nil {
return
}
if client.IsOnline == false {
c.String(http.StatusOK, "Client is currently offline!")
return return
} }
@ -137,7 +141,7 @@ func listFiles(c *gin.Context) {
client.ClientID, parentFolder) client.ClientID, parentFolder)
list += parentFolderLink list += parentFolderLink
resp, err := requestFiles(*client, path) resp, err := requestFiles(client, path)
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)
list += e list += e
@ -157,7 +161,8 @@ func listFiles(c *gin.Context) {
client.ClientID, v.FullPath, v.Name) client.ClientID, v.FullPath, v.Name)
list += entry list += entry
} else { } else {
entry := fmt.Sprintf("<a id=\"pointer\">%v</a><br>", v.Name) entry := fmt.Sprintf("<a hx-get=\"/download/%v\" hx-vals='{\"fileToDownload\": \"%v\"}' id=\"pointer\">%v</a><br>",
client.ClientID, v.FullPath+v.Name, v.Name)
list += entry list += entry
} }
} }