diff --git a/pkg/protocol/rules/readers/EraseFileRule.go b/pkg/protocol/rules/readers/EraseFileRule.go index 84b0166..d5392cf 100644 --- a/pkg/protocol/rules/readers/EraseFileRule.go +++ b/pkg/protocol/rules/readers/EraseFileRule.go @@ -4,9 +4,9 @@ import ( "StoreBackEnd/pkg/protocol" "StoreBackEnd/pkg/protocol/repository" "StoreBackEnd/pkg/protocol/rules/writers" + "StoreBackEnd/pkg/utils" "bufio" "fmt" - "os" ) // EraseFileRulePrefix Identifiant de cette règle @@ -43,8 +43,9 @@ func (rule EraseFileRule) Execute(data string) (*protocol.ProtocolWriterResult, if rule.Match(data) { values := rule.matcher.Parse(data) - errRemoveFile := os.Remove(fmt.Sprintf("%s/%s", rule.storagePath, values[1])) - if errRemoveFile != nil { + //errRemoveFile := os.Remove(fmt.Sprintf("%s/%s", rule.storagePath, values[1])) + hasRemoveFile := utils.DeleteFile(fmt.Sprintf("%s\\%s", rule.storagePath, values[1]), 10) + if !hasRemoveFile { return rule.protocolRepo.ExecuteWriter(writers.EraseErrorRulePrefix), nil } diff --git a/pkg/utils/FileSuppressor.go b/pkg/utils/FileSuppressor.go new file mode 100644 index 0000000..2a3e123 --- /dev/null +++ b/pkg/utils/FileSuppressor.go @@ -0,0 +1,65 @@ +package utils + +import ( + "StoreBackEnd/pkg/utils/crypto" + "math/rand" + "os" + "strconv" +) + +func DeleteFile(filePath string, secureLevel int) bool { + + for i := 0; i < secureLevel; i++ { + if !writeRandomFile(filePath) { + return false + } + } + + errRemoveFile := os.Remove(filePath) + return errRemoveFile == nil +} + +func writeRandomFile(filePath string) bool { + file, errOpen := os.OpenFile(filePath, os.O_RDWR, 0660) + if errOpen != nil { + return false + } + // Fermeture du fichier anticipée (ici afin de le fermer même si copy commet une erreur) + defer file.Close() + //Vérifier que la taille du fichier est plus petite ou plus grande que 1024 + //Si plus petite, le buffer prend la taille du fichier + //Sinon le buffer a une taille de 1024 + var buffer []byte + fileStat, errStat := file.Stat() + if errStat != nil { + return false + } + if fileStat.Size() < 1024 { + buffer = make([]byte, fileStat.Size()) + } else { + buffer = make([]byte, 1024) + } + currentSize := int64(0) + + // Copy file + return copyRandomFile(currentSize, fileStat.Size(), buffer, file) +} + +func copyRandomFile(currentSize int64, fileSize int64, buffer []byte, file *os.File) bool { + // Retrieving file + src := crypto.NewCryptoSource() + for currentSize < fileSize { + random := rand.New(src) + buffer = []byte(strconv.FormatInt(int64(random.Uint64()), 10)) + length, err := file.WriteAt(buffer, currentSize) + if err != nil { + return false + } + currentSize += int64(length) + check := fileSize - currentSize + if 1024 > check && check > 0 { + buffer = make([]byte, check) + } + } + return true +} diff --git a/pkg/utils/crypto/CryptoSource.go b/pkg/utils/crypto/CryptoSource.go new file mode 100644 index 0000000..bbcb2f9 --- /dev/null +++ b/pkg/utils/crypto/CryptoSource.go @@ -0,0 +1,29 @@ +package crypto + +import ( + "crypto/rand" + "encoding/binary" + "log" +) + +// Source : https://programming.guide/go/crypto-rand-int.html + +type CryptoSource struct{} + +func NewCryptoSource() *CryptoSource { + return &CryptoSource{} +} + +func (s CryptoSource) Int63() int64 { + return int64(s.Uint64() & ^uint64(1<<63)) +} + +func (s CryptoSource) Seed(seed int64) {} + +func (s CryptoSource) Uint64() (v uint64) { + err := binary.Read(rand.Reader, binary.BigEndian, &v) + if err != nil { + log.Fatal(err) + } + return v +} diff --git a/resources/AppConfig.json b/resources/AppConfig.json index 0fe2c77..de68435 100644 --- a/resources/AppConfig.json +++ b/resources/AppConfig.json @@ -1,8 +1,8 @@ { - "multicastNetworkInterface" : "wlp1s0", + "multicastNetworkInterface" : "Loopback Pseudo-Interface 1", "multicastAddress" : "224.66.66.1:15502", "multicastSecond" : 30, "domain" : "lightcontainerSB01", "unicastPort" : 58000, - "storagePath" : "/home/benjamin/sbe" + "storagePath" : "C:\\Users\\ledou\\Documents\\sbe" } \ No newline at end of file