From 259bb3c73078ede3df21a35456e7f22432cd85bd Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sun, 13 Mar 2022 12:29:05 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20v=C3=A9rification=20de=20l'emprein?= =?UTF-8?q?te=20lors=20de=20l'envoi=20d'un=20fichier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 2 +- pkg/config/AppConfig.go | 3 +++ pkg/protocol/rules/readers/SendFileRule.go | 28 ++++++++++++------- pkg/utils/FileReceiver.go | 5 ++-- pkg/utils/Hasher.go | 31 ++++++++++++++++++++++ resources/AppConfig.json | 3 ++- 6 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 pkg/utils/Hasher.go diff --git a/cmd/main.go b/cmd/main.go index b23fe07..dd1f695 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -55,7 +55,7 @@ func main() { // protocolRepository.AddReader(&eraseFileRule) // Creation of the SendFileRule // TODO reset to 50,200 - sendFileRule := readers.CreateSendFileRule("^SENDFILE ([A-Za-z0-9.]{1,200}) ([0-9]{1,10}) ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository) + sendFileRule := readers.CreateSendFileRule("^SENDFILE ([A-Za-z0-9.]{1,200}) ([0-9]{1,10}) ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository, appConfig.StoragePath) protocolRepository.AddReader(&sendFileRule) // Create a Multicast Client & run it diff --git a/pkg/config/AppConfig.go b/pkg/config/AppConfig.go index 3777a54..d2e1628 100644 --- a/pkg/config/AppConfig.go +++ b/pkg/config/AppConfig.go @@ -16,4 +16,7 @@ type AppConfig struct { // UnicastPort Contient le port unicast auquel le FileFrontEnd se connecte UnicastPort int `json:"unicastPort"` + + // StoragePath Chemin vers le lieu de stockage de l'application + StoragePath string `json:"storagePath"` } diff --git a/pkg/protocol/rules/readers/SendFileRule.go b/pkg/protocol/rules/readers/SendFileRule.go index 56f2dd2..051c8d1 100644 --- a/pkg/protocol/rules/readers/SendFileRule.go +++ b/pkg/protocol/rules/readers/SendFileRule.go @@ -6,6 +6,8 @@ import ( "StoreBackEnd/pkg/protocol/rules/writers" "StoreBackEnd/pkg/utils" "bufio" + "fmt" + "os" "strconv" ) @@ -20,14 +22,17 @@ type SendFileRule struct { matcher *protocol.RegexMatcher // protocolRepo Instance de ProtocolRepository protocolRepo *repository.ProtocolRepository + // storagePath Chemin de stockage du fichier + storagePath string } // CreateSendFileRule Creating an instance of SendFileRule -func CreateSendFileRule(pattern string, protocolRepo *repository.ProtocolRepository) protocol.IProtocolReader { +func CreateSendFileRule(pattern string, protocolRepo *repository.ProtocolRepository, storagePath string) protocol.IProtocolReader { return &SendFileRule{ cmd: SendFileRulePrefix, matcher: protocol.CreateRegexMatcher(pattern), protocolRepo: protocolRepo, + storagePath: storagePath, } } @@ -40,29 +45,32 @@ func (rule SendFileRule) GetCmd() string { func (rule SendFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) { println(0) if rule.Match(data) { // TODO : cloture this command. - println("OK") values := rule.matcher.Parse(data) // Values fileName := values[1] fileSize, _ := strconv.Atoi(values[2]) - // fileContentHash := values[3] - println(1) + fileContentHash := values[3] + // function callback - callback := rule.onRead(fileName, fileSize) - println(2) + callback := rule.onRead(fileName, fileSize, fileContentHash) + return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix), callback } else { return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix), nil } } -func (rule SendFileRule) onRead(fileName string, fileSize int) func(reader *bufio.Reader) *protocol.ProtocolWriterResult { +func (rule SendFileRule) onRead(fileName string, fileSize int, fingerPrint string) func(reader *bufio.Reader) *protocol.ProtocolWriterResult { return func(reader *bufio.Reader) *protocol.ProtocolWriterResult { - hasReceive := utils.ReceiveFile(fileName, fileSize, reader) - println(3) - if !hasReceive { + path := fmt.Sprintf("%s/%s", rule.storagePath, fileName) + hasReceive := utils.ReceiveFile(path, fileSize, reader) + + println("OK ", utils.HashFileCompare(path, fingerPrint)) + + if !hasReceive || !utils.HashFileCompare(path, fingerPrint) { + os.Remove(path) // Suppression du fichier return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix) } else { return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix) diff --git a/pkg/utils/FileReceiver.go b/pkg/utils/FileReceiver.go index 9a22bfa..a9b77c6 100644 --- a/pkg/utils/FileReceiver.go +++ b/pkg/utils/FileReceiver.go @@ -2,13 +2,12 @@ package utils import ( "bufio" - "fmt" "os" ) // ReceiveFile Permet de récupérer un fichier sur un reader -func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool { - file, fileErr := os.Create(fmt.Sprintf("/home/benjamin/sbe/%s", fileName)) +func ReceiveFile(path string, fileSize int, reader *bufio.Reader) bool { + file, fileErr := os.Create(path) if fileErr != nil { return false } diff --git a/pkg/utils/Hasher.go b/pkg/utils/Hasher.go new file mode 100644 index 0000000..3178285 --- /dev/null +++ b/pkg/utils/Hasher.go @@ -0,0 +1,31 @@ +package utils + +import ( + "bufio" + "crypto/sha256" + "fmt" + "io" + "log" + "os" +) + +func HashFileCompare(path string, fingerPrint string) bool { + f, err := os.Open(path) + if err != nil { + log.Fatal(err) + } + defer f.Close() + + input := bufio.NewReader(f) + + hash := sha256.New() + if _, err := io.Copy(hash, input); err != nil { + log.Fatal(err) + } + sum := hash.Sum(nil) + + fmt.Printf("%x\n", sum) + + println(fingerPrint, fmt.Sprintf("%x", sum)) + return fingerPrint == fmt.Sprintf("%x", sum) +} diff --git a/resources/AppConfig.json b/resources/AppConfig.json index 16552c9..c548bda 100644 --- a/resources/AppConfig.json +++ b/resources/AppConfig.json @@ -3,5 +3,6 @@ "multicastAddress" : "226.66.66.1:15502", "multicastSecond" : 10, "domain" : "lightcontainerSB01", - "unicastPort" : 58000 + "unicastPort" : 58000, + "storagePath" : "/home/benjamin/sbe" } \ No newline at end of file