From ec148d98c1c2c9696856d1cebe6c67b342682e7b Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Thu, 18 Oct 2018 09:35:08 -0700 Subject: [PATCH] make sure recipient connects to TCP first --- src/croc/recipient.go | 6 ++++-- src/croc/sender.go | 42 +++++++++++++++++++++--------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/croc/recipient.go b/src/croc/recipient.go index 873a0a3c..fdde6dbe 100644 --- a/src/croc/recipient.go +++ b/src/croc/recipient.go @@ -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 diff --git a/src/croc/sender.go b/src/croc/sender.go index 997270da..b954ec75 100644 --- a/src/croc/sender.go +++ b/src/croc/sender.go @@ -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 {