diff --git a/src/comm/comm.go b/src/comm/comm.go index 9bae4ae..008c47c 100644 --- a/src/comm/comm.go +++ b/src/comm/comm.go @@ -3,6 +3,7 @@ package comm import ( "bytes" "encoding/binary" + "fmt" "net" "time" ) @@ -30,6 +31,9 @@ func (c Comm) Write(b []byte) (int, error) { binary.LittleEndian.PutUint16(bs, uint16(len(b))) c.connection.Write(bs) n, err := c.connection.Write(b) + if n != len(b) { + err = fmt.Errorf("wanted to write %d but wrote %d", n, len(b)) + } return n, err } @@ -43,22 +47,20 @@ func (c Comm) Read() (buf []byte, err error) { buf = []byte{} tmp := make([]byte, numBytes) for { - n, err := c.connection.Read(tmp) + _, err = c.connection.Read(tmp) if err != nil { return nil, err } - tmp = bytes.TrimRight(tmp, "\x00") - tmp = bytes.TrimLeft(tmp, "\x00") - tmp = bytes.TrimRight(tmp, "\x05") - tmp = bytes.TrimLeft(tmp, "\x05") + tmp = bytes.Trim(tmp, "\x00") + tmp = bytes.Trim(tmp, "\x05") buf = append(buf, tmp...) - if n < numBytes { - numBytes -= n - tmp = make([]byte, numBytes) + if len(buf) < numBytes { + tmp = make([]byte, numBytes-len(buf)) } else { break } } + // log.Printf("wanted %d and got %d", numBytes, len(buf)) return } diff --git a/src/crypt/crypt.go b/src/crypt/crypt.go index 6a6b0a8..0f1915b 100644 --- a/src/crypt/crypt.go +++ b/src/crypt/crypt.go @@ -11,7 +11,9 @@ import ( // Encryption stores the data type Encryption struct { - Encrypted, Salt, IV []byte + Encrypted []byte `json:"e"` + Salt []byte `json:"s"` + IV []byte `json:"i"` } // Encrypt will generate an encryption diff --git a/src/recipient/recipient.go b/src/recipient/recipient.go index cfe77a6..07a6314 100644 --- a/src/recipient/recipient.go +++ b/src/recipient/recipient.go @@ -196,6 +196,7 @@ func receive(serverAddress, serverTCP string, isLocal bool, c *websocket.Conn, c } else { // read from TCP connection message, err = tcpConnection.Read() + // log.Debugf("message: %s", message) } if err != nil { log.Error(err) @@ -206,6 +207,7 @@ func receive(serverAddress, serverTCP string, isLocal bool, c *websocket.Conn, c var enc crypt.Encryption err = json.Unmarshal(message, &enc) if err != nil { + log.Errorf("%s: %s", err.Error(), message) return err } decrypted, err := enc.Decrypt(sessionKey, !fstats.IsEncrypted)