Ajout récupération de fichier (fingerprint à check)

This commit is contained in:
Jérémi N ‘EndMove’ 2022-03-15 11:58:06 +01:00
parent 7db026f710
commit 8a61165428
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
8 changed files with 183 additions and 12 deletions

View File

@ -55,6 +55,14 @@ func main() {
eraseErrorRule := writers.CreateEraseErrorRule("^ERASE_ERROR\r\n$")
protocolRepository.AddWriter(&eraseErrorRule)
// Creation of the EraseOkRule
retrieveOkRule := writers.CreateRetrieveOkRule("^RETRIEVE_OK ([A-Za-z0-9.]{50,200} [0-9]{1,10} [A-Za-z0-9.]{50,200})\r\n$", appConfig.StoragePath)
protocolRepository.AddWriter(&retrieveOkRule)
// Creation of the EraseErrorRule
retrieveErrorRule := writers.CreateRetrieveErrorRule("^RETRIEVE_ERROR\r\n$")
protocolRepository.AddWriter(&retrieveErrorRule)
/**
===== Init all Reader here =====
*/
@ -62,10 +70,14 @@ func main() {
eraseFileRule := readers.CreateEraseFileRule("^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository, appConfig.StoragePath)
protocolRepository.AddReader(&eraseFileRule)
// Creation of the SendFileRule // TODO reset to 50,200
// Creation of the SendFileRule
sendFileRule := readers.CreateSendFileRule("^SENDFILE ([A-Za-z0-9.]{50,200}) ([0-9]{1,10}) ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository, appConfig.StoragePath)
protocolRepository.AddReader(&sendFileRule)
// Create of the RetrieveFileRule
retrieveFileRule := readers.CreateRetrieveFileRule("^RETRIEVEFILE ([A-Za-z0-9.]{50,200})\r\n$", protocolRepository, appConfig.StoragePath)
protocolRepository.AddReader(&retrieveFileRule)
// Create a Multicast Client & run it
multicast := network.CreateClientMulticast(
appConfig.MulticastNetworkInterface, appConfig.MulticastAddress, appConfig.Domain,

View File

@ -85,10 +85,10 @@ func (client ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) {
func ResolveInterfaceAddr(inter string) (*net.UDPAddr, bool) {
ip, err := utils.RetrieveIPv4FromInterface(inter)
if err != nil {
println("Error : " + err.Error())
println("Error while loading interface : " + err.Error())
return nil, true
}
println("Selected IP : " + ip.String())
println("Selected IP for multicast : " + ip.String())
return &net.UDPAddr{IP: ip}, false
}

View File

@ -0,0 +1,60 @@
package readers
import (
"StoreBackEnd/pkg/protocol"
"StoreBackEnd/pkg/protocol/repository"
"StoreBackEnd/pkg/protocol/rules/writers"
"StoreBackEnd/pkg/utils"
"bufio"
"fmt"
"os"
)
// RetrieveFileRulePrefix Identifiant de cette règle
const RetrieveFileRulePrefix = "RETRIEVEFILE"
// RetrieveFileRule Demande de suppression d'un fichier
type RetrieveFileRule struct {
// Cmd Nom de la règle
Cmd string
// matcher Permet de vérifier le matching
matcher *protocol.RegexMatcher
// protocolRepo Instance de ProtocolRepository
protocolRepo *repository.ProtocolRepository
// storagePath Chemin de stockage du fichier
storagePath string
}
// CreateRetrieveFileRule Création d'une instance de RetrieveFileRule
func CreateRetrieveFileRule(pattern string, protocolRepo *repository.ProtocolRepository, storagePath string) protocol.IProtocolReader {
return &RetrieveFileRule{
Cmd: RetrieveFileRulePrefix,
matcher: protocol.CreateRegexMatcher(pattern),
protocolRepo: protocolRepo,
storagePath: storagePath,
}
}
func (rule RetrieveFileRule) GetCmd() string {
return rule.Cmd
}
func (rule RetrieveFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) {
if rule.Match(data) {
values := rule.matcher.Parse(data)
hashFileName := values[1]
path := fmt.Sprintf("%s/%s", rule.storagePath, hashFileName)
if fileinfo, err := os.Stat(path); err == nil {
println("OKOKOKOEKOEKE : ", hashFileName, fmt.Sprintf("%d", fileinfo.Size()), fmt.Sprintf("%x", utils.GenFingerPrint(path)))
return rule.protocolRepo.ExecuteWriter(writers.RetrieveOkRulePrefix, hashFileName, fmt.Sprintf("%d", fileinfo.Size()), fmt.Sprintf("%x", utils.GenFingerPrint(path))), nil
}
}
return rule.protocolRepo.ExecuteWriter(writers.RetrieveErrorRulePrefix), nil
}
func (rule RetrieveFileRule) Match(data string) bool {
return rule.matcher.Match(data)
}

View File

@ -0,0 +1,38 @@
package writers
import (
"StoreBackEnd/pkg/protocol"
)
// RetrieveErrorRulePrefix Rule command prefix
const RetrieveErrorRulePrefix = "RETRIEVE_ERROR"
// RetrieveErrorRule Storage structure for the RetrieveErrorRule
type RetrieveErrorRule struct {
// cmd Rule name
cmd string
// matcher Allow to make a regex match
matcher *protocol.RegexMatcher
}
// CreateRetrieveErrorRule Creating an instance of RetrieveErrorRule
func CreateRetrieveErrorRule(pattern string) protocol.IProtocolWriter {
return &RetrieveErrorRule{
cmd: RetrieveErrorRulePrefix,
matcher: protocol.CreateRegexMatcher(pattern),
}
}
// GetCmd retrieve the command name.
func (rule RetrieveErrorRule) GetCmd() string {
return rule.cmd
}
// Execute the Rule with a string command.
func (rule RetrieveErrorRule) Execute(argsData ...string) *protocol.ProtocolWriterResult {
buildedCmd, _ := rule.matcher.Build(RetrieveErrorRulePrefix, argsData...)
return &protocol.ProtocolWriterResult{
Cmd: buildedCmd,
}
}

View File

@ -0,0 +1,58 @@
package writers
import (
"StoreBackEnd/pkg/protocol"
"StoreBackEnd/pkg/utils"
"bufio"
"fmt"
"strconv"
)
// RetrieveOkRulePrefix Rule command prefix
const RetrieveOkRulePrefix = "RETRIEVE_OK"
// RetrieveOkRule Storage structure for the RetrieveOkRule
type RetrieveOkRule struct {
// cmd Rule name
cmd string
// matcher Allow to make a regex match
matcher *protocol.RegexMatcher
// storagePath Chemin de stockage du fichier
storagePath string
}
// CreateRetrieveOkRule Creating an instance of RetrieveOkRule
func CreateRetrieveOkRule(pattern string, storagePath string) protocol.IProtocolWriter {
return &RetrieveOkRule{
cmd: RetrieveOkRulePrefix,
matcher: protocol.CreateRegexMatcher(pattern),
storagePath: storagePath,
}
}
// GetCmd retrieve the command name.
func (rule RetrieveOkRule) GetCmd() string {
return rule.cmd
}
// Execute the Rule with a string command.
func (rule RetrieveOkRule) Execute(argsData ...string) *protocol.ProtocolWriterResult {
buildedCmd, _ := rule.matcher.Build(RetrieveOkRulePrefix, argsData...)
return &protocol.ProtocolWriterResult{
Cmd: buildedCmd,
Write: func(writer *bufio.Writer) {
fileSize, _ := strconv.Atoi(argsData[1])
rule.onWrite(writer, argsData[0], fileSize)
},
}
}
func (rule RetrieveOkRule) onWrite(writer *bufio.Writer, hashFileName string, fileSize int) {
path := fmt.Sprintf("%s/%s", rule.storagePath, hashFileName)
if utils.SendFile(path, fileSize, writer) {
println("Fichier envoyé au client")
} else {
println("Fichier non-envoyé au client")
}
}

View File

@ -8,9 +8,9 @@ import (
)
// SendFile Envoie d'un fichier local sur le réseau
func SendFile(fileName string, fileSize int, writer *bufio.Writer) bool {
func SendFile(filePath string, fileSize int, writer *bufio.Writer) bool {
// Ouverture du fichier
file, fileErr := os.Open(fmt.Sprintf("/home/benjamin/sbe/%s", fileName))
file, fileErr := os.Open(fmt.Sprintf("%s", filePath))
if fileErr != nil {
return false
}
@ -25,5 +25,5 @@ func SendFile(fileName string, fileSize int, writer *bufio.Writer) bool {
return false
}
return false
return true
}

View File

@ -10,6 +10,12 @@ import (
)
func HashFileCompare(path string, fingerPrint string) bool {
sum := GenFingerPrint(path)
return fingerPrint == fmt.Sprintf("%x", sum)
}
func GenFingerPrint(path string) []byte {
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
@ -24,8 +30,5 @@ func HashFileCompare(path string, fingerPrint string) bool {
}
sum := hash.Sum(nil)
fmt.Printf("%x\n", sum)
println(fingerPrint, fmt.Sprintf("%x", sum))
return fingerPrint == fmt.Sprintf("%x", sum)
return sum
}

View File

@ -1,8 +1,8 @@
{
"multicastNetworkInterface" : "Wi-Fi",
"multicastNetworkInterface" : "Loopback Pseudo-Interface 1",
"multicastAddress" : "224.66.66.1:15502",
"multicastSecond" : 30,
"domain" : "lightcontainerSB01",
"unicastPort" : 58000,
"storagePath" : "/home/benjamin/sbe"
"storagePath" : "D:\\sbe"
}