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

make sure recipient connects to TCP first

This commit is contained in:
Zack Scholl 2018-10-18 09:35:08 -07:00
parent 365eca9653
commit ec148d98c1
2 changed files with 25 additions and 23 deletions

View file

@ -202,6 +202,9 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string,
blocksBytes, _ := json.Marshal(blocks)
// encrypt the block data and send
encblockBytes := crypt.Encrypt(blocksBytes, sessionKey)
// wait for TCP connections if using them
_ = <-isConnectedIfUsingTCP
c.WriteMessage(websocket.BinaryMessage, encblockBytes.Bytes())
// prompt user about the file
@ -378,7 +381,7 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string,
}(finished, dataChan)
log.Debug("telling sender i'm ready")
c.WriteMessage(websocket.BinaryMessage, append([]byte("ready"), blocksBytes...))
c.WriteMessage(websocket.BinaryMessage, []byte("ready"))
startTime := time.Now()
if useWebsockets {
@ -408,7 +411,6 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string,
// }
}
} else {
_ = <-isConnectedIfUsingTCP
log.Debugf("starting listening with tcp with %d connections", len(tcpConnections))
// using TCP
var wg sync.WaitGroup

View file

@ -205,26 +205,6 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL
return errors.New("recipient refused file")
}
// connect to TCP in background
tcpConnections = make([]comm.Comm, len(tcpPorts))
go func() {
if !useWebsockets {
log.Debugf("connecting to server")
for i, tcpPort := range tcpPorts {
log.Debugf("connecting to %s on connection %d", tcpPort, i)
var message string
tcpConnections[i], message, err = connectToTCPServer(utils.SHA256(fmt.Sprintf("%d%x", i, sessionKey)), serverAddress+":"+tcpPort)
if err != nil {
log.Error(err)
}
if message != "sender" {
log.Errorf("got wrong message: %s", message)
}
}
}
isConnectedIfUsingTCP <- true
}()
err = <-fileReady // block until file is ready
if err != nil {
return err
@ -239,7 +219,7 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL
// send the file meta data
c.WriteMessage(websocket.BinaryMessage, enc.Bytes())
case 4:
log.Debugf("[%d] recipient declares gives blocks", step)
log.Debugf("[%d] recipient gives blocks", step)
// recipient sends blocks, and sender does not send anything back
// determine if any blocks were sent to skip
enc, err := crypt.FromBytes(message)
@ -266,6 +246,26 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL
}
log.Debugf("found blocks: %+v", blocksToSkip)
// connect to TCP in background
tcpConnections = make([]comm.Comm, len(tcpPorts))
go func() {
if !useWebsockets {
log.Debugf("connecting to server")
for i, tcpPort := range tcpPorts {
log.Debugf("connecting to %s on connection %d", tcpPort, i)
var message string
tcpConnections[i], message, err = connectToTCPServer(utils.SHA256(fmt.Sprintf("%d%x", i, sessionKey)), serverAddress+":"+tcpPort)
if err != nil {
log.Error(err)
}
if message != "sender" {
log.Errorf("got wrong message: %s", message)
}
}
}
isConnectedIfUsingTCP <- true
}()
// start loading the file into memory
// start streaming encryption/compression
if cr.FileInfo.IsDir {