Added test to Packets and more
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
bin/
|
||||
testFiles/
|
||||
out/
|
||||
sha512sums.txt
|
||||
@@ -120,6 +120,21 @@ func GetFile(path string, address string) {
|
||||
|
||||
var recvPackets bitmap.Bitmap
|
||||
|
||||
dataReceived := 0
|
||||
|
||||
go func() {
|
||||
last := 0
|
||||
|
||||
fmt.Print("\033[s")
|
||||
|
||||
for {
|
||||
fmt.Print("\033[u\033[K")
|
||||
fmt.Printf("Throughput: %v Mbit/s\n", ((dataReceived-last)*8)/1024/1024)
|
||||
last = dataReceived
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
pck := ReceivePacket(key, conn)
|
||||
if pck.Flag == common.End {
|
||||
@@ -132,13 +147,13 @@ func GetFile(path string, address string) {
|
||||
}
|
||||
|
||||
recvPackets.Set(pck.Sync)
|
||||
|
||||
offset := (int64(pck.Sync) - int64(ackPck.Sync+1)) * int64(common.MaxDataSize)
|
||||
|
||||
_, err = file.WriteAt(pck.Data, offset)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dataReceived += int(pck.DataLength)
|
||||
}
|
||||
|
||||
lostPackets := make([]uint32, 0)
|
||||
|
||||
@@ -4,4 +4,7 @@ go 1.21.1
|
||||
|
||||
require golang.org/x/crypto v0.15.0
|
||||
|
||||
require golang.org/x/sys v0.14.0 // indirect
|
||||
require (
|
||||
github.com/google/go-cmp v0.6.0
|
||||
golang.org/x/sys v0.14.0 // indirect
|
||||
)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
|
||||
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
|
||||
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
|
||||
|
||||
29
internal/common/packets_test.go
Normal file
29
internal/common/packets_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestPacketFromBytes(t *testing.T) {
|
||||
sid := [8]byte{255, 255, 255, 255, 255, 255, 255, 255}
|
||||
data := []byte{1, 0, 1}
|
||||
dataLength := len(data)
|
||||
|
||||
want := Packet{
|
||||
Flag: Request,
|
||||
Sync: 0,
|
||||
Data: data,
|
||||
Sid: sid,
|
||||
DataLength: uint32(dataLength),
|
||||
}
|
||||
|
||||
bytes := []byte{0, 0, 0, 0, 0, 1, 0, 1}
|
||||
|
||||
pck := PacketFromBytes(bytes, uint32(dataLength), sid)
|
||||
|
||||
if !cmp.Equal(pck, want) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,10 @@ func New() (*Server, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.SetFormatter(&log.TextFormatter{
|
||||
ForceColors: true,
|
||||
})
|
||||
|
||||
return &Server{
|
||||
sessions: make(map[common.SessionID]*info),
|
||||
rsa: key,
|
||||
|
||||
13
protocol.md
13
protocol.md
@@ -2,7 +2,7 @@
|
||||
|
||||
// Handshake or encryption setup here
|
||||
1. Client sends request for file
|
||||
2. Server answers with error or FileSize (possibly also Packetsize)
|
||||
2. Server answers with error or FileSize
|
||||
3. Client ack
|
||||
4. Server sends File Packets with Sync to keep track
|
||||
5. Server sends Sync End Packet
|
||||
@@ -13,8 +13,15 @@
|
||||
8. If Client has all packets, or retries has exceeded limit. Client sends Ack Packet for last Sync
|
||||
9. Client and Server "close" / forget the connection
|
||||
|
||||
## Header
|
||||
4 Byte Header Length | 4 Byte Request Flag (Request, Pte, Ack, File, End, Resend) | 4 Byte Sync | 4 Byte Data Length | x Byte Data
|
||||
### Secure Header | Unencrypted
|
||||
1 Byte RSA Flag | 24 Byte Nonce | 8 Byte Session ID | 4 Byte Encrypted Data Length
|
||||
|
||||
### Packet Header | Encrypted
|
||||
1 Byte Header Type Flag | 4 Byte Sync
|
||||
|
||||
### Packet Structure
|
||||
37 Byte Secure Header | 5 Byte Packet Header | <= 446 Byte Data | 16 Byte chacha20poly1305 overhead
|
||||
|
||||
|
||||
### Data
|
||||
|
||||
|
||||
Reference in New Issue
Block a user