Added test to Packets and more

This commit is contained in:
Pablu23
2023-12-06 17:13:37 +01:00
parent 38876f6577
commit 5ccd83c9da
7 changed files with 68 additions and 7 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
bin/ bin/
testFiles/ testFiles/
out/ out/
sha512sums.txt

View File

@@ -120,6 +120,21 @@ func GetFile(path string, address string) {
var recvPackets bitmap.Bitmap 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 { for {
pck := ReceivePacket(key, conn) pck := ReceivePacket(key, conn)
if pck.Flag == common.End { if pck.Flag == common.End {
@@ -132,13 +147,13 @@ func GetFile(path string, address string) {
} }
recvPackets.Set(pck.Sync) recvPackets.Set(pck.Sync)
offset := (int64(pck.Sync) - int64(ackPck.Sync+1)) * int64(common.MaxDataSize) offset := (int64(pck.Sync) - int64(ackPck.Sync+1)) * int64(common.MaxDataSize)
_, err = file.WriteAt(pck.Data, offset) _, err = file.WriteAt(pck.Data, offset)
if err != nil { if err != nil {
panic(err) panic(err)
} }
dataReceived += int(pck.DataLength)
} }
lostPackets := make([]uint32, 0) lostPackets := make([]uint32, 0)

View File

@@ -4,4 +4,7 @@ go 1.21.1
require golang.org/x/crypto v0.15.0 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
)

View File

@@ -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 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=

View 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()
}
}

View File

@@ -37,6 +37,10 @@ func New() (*Server, error) {
return nil, err return nil, err
} }
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
})
return &Server{ return &Server{
sessions: make(map[common.SessionID]*info), sessions: make(map[common.SessionID]*info),
rsa: key, rsa: key,

View File

@@ -2,7 +2,7 @@
// Handshake or encryption setup here // Handshake or encryption setup here
1. Client sends request for file 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 3. Client ack
4. Server sends File Packets with Sync to keep track 4. Server sends File Packets with Sync to keep track
5. Server sends Sync End Packet 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 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 9. Client and Server "close" / forget the connection
## Header ### Secure Header | Unencrypted
4 Byte Header Length | 4 Byte Request Flag (Request, Pte, Ack, File, End, Resend) | 4 Byte Sync | 4 Byte Data Length | x Byte Data 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 ### Data