1
0
mirror of https://github.com/schollz/croc.git synced 2024-11-28 09:35:14 +03:00

don't count empty bytes

This commit is contained in:
Zack Scholl 2018-09-23 14:00:31 -07:00
parent a8c0c02755
commit 47fe8a2116
3 changed files with 15 additions and 9 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)