From 1a302cb9fbf8abb2f96f76a35bc4d0644c018563 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Sun, 14 Nov 2021 20:55:17 +0300 Subject: [PATCH] [node] bugfixes? [make] cross compilation automatisation --- Makefile | 5 +++++ src/node/node.go | 43 ++++++++++++++++++++++++----------------- src/protocol/headers.go | 10 +++++++++- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index d62f79b..04d70d3 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,11 @@ INSTALLATION_DIR := /usr/local/bin/ all: cd $(SRC_DIR) && go build && mv $(EXE_NAME) .. +cross: + cd $(SRC_DIR) && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ftu_linux_amd64 && mv ftu_linux_amd64 .. + cd $(SRC_DIR) && CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o ftu_darwin_amd64 && mv ftu_darwin_amd64 .. + cd $(SRC_DIR) && CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o ftu_windows_amd64.exe && mv ftu_windows_amd64.exe .. + race: cd $(SRC_DIR) && go build -race && mv $(EXE_NAME) .. diff --git a/src/node/node.go b/src/node/node.go index 075f0f0..2eeede6 100644 --- a/src/node/node.go +++ b/src/node/node.go @@ -354,8 +354,12 @@ func (node *Node) Start() { if err != nil { panic(err) } + } + filesInfoDonePacket := protocol.Packet{ + Header: protocol.HeaderFilesInfoDone, } + protocol.SendPacket(node.netInfo.Conn, filesInfoDonePacket) case false: // send a filepacket of a single file @@ -586,15 +590,6 @@ func (node *Node) Start() { panic(err) } - // notify the node that we`re ready to transportation. No need - // for encryption because the body is nil - err = protocol.SendPacket(node.netInfo.Conn, protocol.Packet{ - Header: protocol.HeaderReady, - }) - if err != nil { - panic(err) - } - } else { // no @@ -621,7 +616,7 @@ func (node *Node) Start() { panic(err) } - if file.RelativeParentPath == "" { + if strings.TrimSpace(file.RelativeParentPath) == "" { // does not have a parent dir file.Path = filepath.Join(node.transferInfo.Receiving.DownloadsPath, file.Name) } else { @@ -641,16 +636,19 @@ func (node *Node) Start() { // check if it is the exact file existingFileHandler, err := os.Open(file.Path) if err != nil { - os.Remove(file.Path) + panic(err) } - existingFileChecksum, _ := checksum.GetPartialCheckSum(existingFileHandler) + existingFileChecksum, err := checksum.GetPartialCheckSum(existingFileHandler) + if err != nil { + panic(err) + } if existingFileChecksum == file.Checksum { // it`s the exact same file. No need to receive it again // notify the other node - fmt.Printf("Already have \"%s\". Skipping...\n\n", file.Name) + fmt.Printf("| Already have \"%s\". Skipping...\n\n", file.Name) alreadyHavePacketBodyBuffer := new(bytes.Buffer) binary.Write(alreadyHavePacketBodyBuffer, binary.BigEndian, file.ID) @@ -660,13 +658,14 @@ func (node *Node) Start() { Body: alreadyHavePacketBodyBuffer.Bytes(), } - encryptedBody, err := encryption.Encrypt(node.netInfo.EncryptionKey, alreadyHavePacket.Body) - if err != nil { - panic(err) + if node.netInfo.EncryptionKey != nil { + encryptedBody, err := encryption.Encrypt(node.netInfo.EncryptionKey, alreadyHavePacket.Body) + if err != nil { + panic(err) + } + alreadyHavePacket.Body = encryptedBody } - alreadyHavePacket.Body = encryptedBody - protocol.SendPacket(node.netInfo.Conn, alreadyHavePacket) } else { @@ -720,6 +719,14 @@ func (node *Node) Start() { } } + readyPacket := protocol.Packet{ + Header: protocol.HeaderReady, + } + protocol.SendPacket(node.netInfo.Conn, readyPacket) + + case protocol.HeaderFilesInfoDone: + // have all information about the files + // notify the other node that this one is ready err = protocol.SendPacket(node.netInfo.Conn, protocol.Packet{ Header: protocol.HeaderReady, diff --git a/src/protocol/headers.go b/src/protocol/headers.go index 14dc7fc..8f98483 100644 --- a/src/protocol/headers.go +++ b/src/protocol/headers.go @@ -55,11 +55,19 @@ const HeaderDone Header = "DONE" // READY. // Sent by receiver when it has read and processed the last -// FILEBYTES packet. The sender is not allowed to "spam" FILEBYTES +// FILEBYTES packet or when it has information about all the files and it`s ready to +// receive bytes (FILESINFODONE). The sender is not allowed to "spam" FILEBYTES // packets without the permission of receiver. // ie: READY!~ const HeaderReady Header = "READY" +// FILESINFODONE. +// Sent by sender after it`s announced about all the files that are +// going to be sent. It is not allowed to send any file bytes before +// packet with this header was sent. +// ie: FILESINFODONE~ +const HeaderFilesInfoDone Header = "FILESINFODONE" + // BYE!. // Packet with this header can be sent both by receiver and sender. // It`s used when the sender or the receiver are going to disconnect