mirror of https://github.com/schollz/croc.git
not working dunno why
This commit is contained in:
parent
55b1fa35e4
commit
02f4b08929
|
@ -3,6 +3,7 @@ package comm
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -39,20 +40,33 @@ func (c Comm) Write(b []byte) (int, error) {
|
||||||
|
|
||||||
func (c Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
|
func (c Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
|
||||||
// read until we get 5 bytes
|
// read until we get 5 bytes
|
||||||
bs = make([]byte, 5)
|
tmp := make([]byte, 5)
|
||||||
_, err = c.connection.Read(bs)
|
n, err := c.connection.Read(tmp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tmp := make([]byte, 1)
|
tmpCopy := make([]byte, n)
|
||||||
|
// Copy the buffer so it doesn't get changed while read by the recipient.
|
||||||
|
copy(tmpCopy, tmp[:n])
|
||||||
|
bs = tmpCopy
|
||||||
|
|
||||||
|
tmp = make([]byte, 1)
|
||||||
for {
|
for {
|
||||||
|
// see if we have enough bytes
|
||||||
bs = bytes.Trim(bs, "\x00")
|
bs = bytes.Trim(bs, "\x00")
|
||||||
if len(bs) == 5 {
|
if len(bs) == 5 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
c.connection.Read(tmp)
|
n, err := c.connection.Read(tmp)
|
||||||
bs = append(bs, tmp...)
|
if err != nil {
|
||||||
|
return nil, 0, nil, err
|
||||||
|
}
|
||||||
|
tmpCopy = make([]byte, n)
|
||||||
|
// Copy the buffer so it doesn't get changed while read by the recipient.
|
||||||
|
copy(tmpCopy, tmp[:n])
|
||||||
|
bs = append(bs, tmpCopy...)
|
||||||
}
|
}
|
||||||
|
|
||||||
numBytes, err = strconv.Atoi(strings.TrimLeft(string(bs), "0"))
|
numBytes, err = strconv.Atoi(strings.TrimLeft(string(bs), "0"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, nil, err
|
return nil, 0, nil, err
|
||||||
|
@ -61,20 +75,27 @@ func (c Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
|
||||||
tmp = make([]byte, numBytes)
|
tmp = make([]byte, numBytes)
|
||||||
bufStart := 0
|
bufStart := 0
|
||||||
for {
|
for {
|
||||||
_, err = c.connection.Read(tmp)
|
n, err := c.connection.Read(tmp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, numBytes, bs, err
|
return nil, 0, nil, err
|
||||||
}
|
}
|
||||||
tmp = bytes.TrimRight(tmp, "\x00")
|
tmpCopy := make([]byte, n)
|
||||||
copy(buf[bufStart:bufStart+len(tmp)], tmp[:])
|
// Copy the buffer so it doesn't get changed while read by the recipient.
|
||||||
|
copy(tmpCopy, tmp[:n])
|
||||||
|
|
||||||
|
tmpCopy = bytes.TrimSpace(tmpCopy)
|
||||||
|
tmpCopy = bytes.Replace(tmpCopy, []byte(" "), []byte{}, -1)
|
||||||
|
tmpCopy = bytes.Trim(tmpCopy, "\x00")
|
||||||
|
copy(buf[bufStart:bufStart+len(tmpCopy)], tmpCopy[:])
|
||||||
bufStart = len(buf)
|
bufStart = len(buf)
|
||||||
if bufStart < numBytes {
|
if bufStart < numBytes {
|
||||||
|
// shrink the amount we need to read
|
||||||
tmp = tmp[:numBytes-bufStart]
|
tmp = tmp[:numBytes-bufStart]
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// log.Printf("wanted %d and got %d", numBytes, len(buf))
|
log.Printf("wanted %d and got %d", numBytes, len(buf))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ func TestSendReceiveWebsockets(t *testing.T) {
|
||||||
sendAndReceive(t, 1)
|
sendAndReceive(t, 1)
|
||||||
}
|
}
|
||||||
func TestSendReceiveTCP(t *testing.T) {
|
func TestSendReceiveTCP(t *testing.T) {
|
||||||
sendAndReceive(t, 1)
|
sendAndReceive(t, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateRandomFile(megabytes int) (fname string) {
|
func generateRandomFile(megabytes int) (fname string) {
|
||||||
|
|
|
@ -207,6 +207,7 @@ func send(forceSend int, serverAddress, serverTCP string, isLocal bool, c *webso
|
||||||
return errors.New("recipient refused file")
|
return errors.New("recipient refused file")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buffer := make([]byte, models.WEBSOCKET_BUFFER_SIZE/8)
|
||||||
if !useWebsockets {
|
if !useWebsockets {
|
||||||
// connection to TCP
|
// connection to TCP
|
||||||
tcpConnection, err = connectToTCPServer(utils.SHA256(fmt.Sprintf("%x", sessionKey)), serverAddress+":"+serverTCP)
|
tcpConnection, err = connectToTCPServer(utils.SHA256(fmt.Sprintf("%x", sessionKey)), serverAddress+":"+serverTCP)
|
||||||
|
@ -214,15 +215,13 @@ func send(forceSend int, serverAddress, serverTCP string, isLocal bool, c *webso
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
buffer = make([]byte, models.TCP_BUFFER_SIZE/2)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "\rSending (->%s)...\n", otherIP)
|
fmt.Fprintf(os.Stderr, "\rSending (->%s)...\n", otherIP)
|
||||||
// send file, compure hash simultaneously
|
// send file, compure hash simultaneously
|
||||||
startTransfer = time.Now()
|
startTransfer = time.Now()
|
||||||
buffer := make([]byte, models.WEBSOCKET_BUFFER_SIZE/8)
|
|
||||||
if !useWebsockets {
|
|
||||||
buffer = make([]byte, models.TCP_BUFFER_SIZE/2)
|
|
||||||
}
|
|
||||||
bar := progressbar.NewOptions(
|
bar := progressbar.NewOptions(
|
||||||
int(fstats.Size),
|
int(fstats.Size),
|
||||||
progressbar.OptionSetRenderBlankState(true),
|
progressbar.OptionSetRenderBlankState(true),
|
||||||
|
|
|
@ -126,7 +126,7 @@ func chanFromConn(conn net.Conn) chan []byte {
|
||||||
c := make(chan []byte)
|
c := make(chan []byte)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
b := make([]byte, models.WEBSOCKET_BUFFER_SIZE)
|
b := make([]byte, models.TCP_BUFFER_SIZE)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, err := conn.Read(b)
|
n, err := conn.Read(b)
|
||||||
|
|
Loading…
Reference in New Issue