0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

update read

This commit is contained in:
Zack Scholl 2018-09-23 14:12:45 -07:00
parent 47fe8a2116
commit 2ebe3c1328
2 changed files with 19 additions and 7 deletions

View file

@ -34,22 +34,32 @@ func (c Comm) Write(b []byte) (int, error) {
if n != len(b) {
err = fmt.Errorf("wanted to write %d but wrote %d", n, len(b))
}
// log.Printf("wanted to write %d but wrote %d", n, len(b))
return n, err
}
func (c Comm) Read() (buf []byte, err error) {
bs := make([]byte, 2)
func (c Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
bs = make([]byte, 2)
_, err = c.connection.Read(bs)
if err != nil {
return
}
numBytes := int(binary.LittleEndian.Uint16(bs[:2]))
for {
bs = bytes.Trim(bytes.Trim(bs, "\x00"), "\x05")
if len(bs) == 2 {
break
}
tmp := make([]byte, 1)
c.connection.Read(tmp)
bs = append(bs, tmp...)
}
numBytes = int(binary.LittleEndian.Uint16(bs[:]))
buf = []byte{}
tmp := make([]byte, numBytes)
for {
_, err = c.connection.Read(tmp)
if err != nil {
return nil, err
return nil, numBytes, bs, err
}
tmp = bytes.Trim(tmp, "\x00")
tmp = bytes.Trim(tmp, "\x05")
@ -72,7 +82,7 @@ func (c Comm) Send(message string) (err error) {
// Receive a message
func (c Comm) Receive() (s string, err error) {
b, err := c.Read()
b, _, _, err := c.Read()
s = string(b)
return
}

View file

@ -185,6 +185,8 @@ func receive(serverAddress, serverTCP string, isLocal bool, c *websocket.Conn, c
)
c.WriteMessage(websocket.BinaryMessage, []byte("ready"))
startTime := time.Now()
var numBytes int
var bs []byte
for {
if isLocal {
var messageType int
@ -195,7 +197,7 @@ func receive(serverAddress, serverTCP string, isLocal bool, c *websocket.Conn, c
}
} else {
// read from TCP connection
message, err = tcpConnection.Read()
message, numBytes, bs, err = tcpConnection.Read()
// log.Debugf("message: %s", message)
}
if err != nil {
@ -207,7 +209,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)
log.Errorf("%s: %s (%d/%d) %+v", err.Error(), message, len(message), numBytes, bs)
return err
}
decrypted, err := enc.Decrypt(sessionKey, !fstats.IsEncrypted)