diff --git a/.gitignore b/.gitignore index b9ffcc5..1af6afa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ bin/ testFiles/ -out/ \ No newline at end of file +out/ +sha512sums.txt \ No newline at end of file diff --git a/internal/client/client.go b/internal/client/client.go index 99baf72..0feccd2 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -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) diff --git a/internal/common/go.mod b/internal/common/go.mod index 6d65dc5..5e08a72 100644 --- a/internal/common/go.mod +++ b/internal/common/go.mod @@ -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 +) diff --git a/internal/common/go.sum b/internal/common/go.sum index 4888ad1..ac74b56 100644 --- a/internal/common/go.sum +++ b/internal/common/go.sum @@ -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= diff --git a/internal/common/packets_test.go b/internal/common/packets_test.go new file mode 100644 index 0000000..82a5776 --- /dev/null +++ b/internal/common/packets_test.go @@ -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() + } +} diff --git a/internal/server/server.go b/internal/server/server.go index 51fcc29..fdbd1e4 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -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, diff --git a/protocol.md b/protocol.md index 2061924..089a15c 100644 --- a/protocol.md +++ b/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