Début vérification de l'empreinte lors de l'envoi d'un fichier
This commit is contained in:
parent
d7f1407c0c
commit
259bb3c730
@ -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
|
||||
|
@ -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"`
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
31
pkg/utils/Hasher.go
Normal file
31
pkg/utils/Hasher.go
Normal 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)
|
||||
}
|
@ -3,5 +3,6 @@
|
||||
"multicastAddress" : "226.66.66.1:15502",
|
||||
"multicastSecond" : 10,
|
||||
"domain" : "lightcontainerSB01",
|
||||
"unicastPort" : 58000
|
||||
"unicastPort" : 58000,
|
||||
"storagePath" : "/home/benjamin/sbe"
|
||||
}
|
Loading…
Reference in New Issue
Block a user