Début vérification de l'empreinte lors de l'envoi d'un fichier

This commit is contained in:
Benjamin 2022-03-13 12:29:05 +01:00
parent d7f1407c0c
commit 259bb3c730
6 changed files with 57 additions and 15 deletions

View File

@ -55,7 +55,7 @@ func main() {
// protocolRepository.AddReader(&eraseFileRule) // protocolRepository.AddReader(&eraseFileRule)
// Creation of the SendFileRule // TODO reset to 50,200 // 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) protocolRepository.AddReader(&sendFileRule)
// Create a Multicast Client & run it // Create a Multicast Client & run it

View File

@ -16,4 +16,7 @@ type AppConfig struct {
// UnicastPort Contient le port unicast auquel le FileFrontEnd se connecte // UnicastPort Contient le port unicast auquel le FileFrontEnd se connecte
UnicastPort int `json:"unicastPort"` UnicastPort int `json:"unicastPort"`
// StoragePath Chemin vers le lieu de stockage de l'application
StoragePath string `json:"storagePath"`
} }

View File

@ -6,6 +6,8 @@ import (
"StoreBackEnd/pkg/protocol/rules/writers" "StoreBackEnd/pkg/protocol/rules/writers"
"StoreBackEnd/pkg/utils" "StoreBackEnd/pkg/utils"
"bufio" "bufio"
"fmt"
"os"
"strconv" "strconv"
) )
@ -20,14 +22,17 @@ type SendFileRule struct {
matcher *protocol.RegexMatcher matcher *protocol.RegexMatcher
// protocolRepo Instance de ProtocolRepository // protocolRepo Instance de ProtocolRepository
protocolRepo *repository.ProtocolRepository protocolRepo *repository.ProtocolRepository
// storagePath Chemin de stockage du fichier
storagePath string
} }
// CreateSendFileRule Creating an instance of SendFileRule // 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{ return &SendFileRule{
cmd: SendFileRulePrefix, cmd: SendFileRulePrefix,
matcher: protocol.CreateRegexMatcher(pattern), matcher: protocol.CreateRegexMatcher(pattern),
protocolRepo: protocolRepo, 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) { func (rule SendFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) {
println(0) println(0)
if rule.Match(data) { // TODO : cloture this command. if rule.Match(data) { // TODO : cloture this command.
println("OK")
values := rule.matcher.Parse(data) values := rule.matcher.Parse(data)
// Values // Values
fileName := values[1] fileName := values[1]
fileSize, _ := strconv.Atoi(values[2]) fileSize, _ := strconv.Atoi(values[2])
// fileContentHash := values[3] fileContentHash := values[3]
println(1)
// function callback // function callback
callback := rule.onRead(fileName, fileSize) callback := rule.onRead(fileName, fileSize, fileContentHash)
println(2)
return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix), callback return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix), callback
} else { } else {
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix), nil 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 { return func(reader *bufio.Reader) *protocol.ProtocolWriterResult {
hasReceive := utils.ReceiveFile(fileName, fileSize, reader) path := fmt.Sprintf("%s/%s", rule.storagePath, fileName)
println(3) hasReceive := utils.ReceiveFile(path, fileSize, reader)
if !hasReceive {
println("OK ", utils.HashFileCompare(path, fingerPrint))
if !hasReceive || !utils.HashFileCompare(path, fingerPrint) {
os.Remove(path) // Suppression du fichier
return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix) return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix)
} else { } else {
return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix) return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix)

View File

@ -2,13 +2,12 @@ package utils
import ( import (
"bufio" "bufio"
"fmt"
"os" "os"
) )
// ReceiveFile Permet de récupérer un fichier sur un reader // ReceiveFile Permet de récupérer un fichier sur un reader
func ReceiveFile(fileName string, fileSize int, reader *bufio.Reader) bool { func ReceiveFile(path string, fileSize int, reader *bufio.Reader) bool {
file, fileErr := os.Create(fmt.Sprintf("/home/benjamin/sbe/%s", fileName)) file, fileErr := os.Create(path)
if fileErr != nil { if fileErr != nil {
return false return false
} }

31
pkg/utils/Hasher.go Normal file
View File

@ -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)
}

View File

@ -3,5 +3,6 @@
"multicastAddress" : "226.66.66.1:15502", "multicastAddress" : "226.66.66.1:15502",
"multicastSecond" : 10, "multicastSecond" : 10,
"domain" : "lightcontainerSB01", "domain" : "lightcontainerSB01",
"unicastPort" : 58000 "unicastPort" : 58000,
"storagePath" : "/home/benjamin/sbe"
} }