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:
parent
365eca9653
commit
ec148d98c1
2 changed files with 25 additions and 23 deletions
|
@ -202,6 +202,9 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string,
|
||||||
blocksBytes, _ := json.Marshal(blocks)
|
blocksBytes, _ := json.Marshal(blocks)
|
||||||
// encrypt the block data and send
|
// encrypt the block data and send
|
||||||
encblockBytes := crypt.Encrypt(blocksBytes, sessionKey)
|
encblockBytes := crypt.Encrypt(blocksBytes, sessionKey)
|
||||||
|
|
||||||
|
// wait for TCP connections if using them
|
||||||
|
_ = <-isConnectedIfUsingTCP
|
||||||
c.WriteMessage(websocket.BinaryMessage, encblockBytes.Bytes())
|
c.WriteMessage(websocket.BinaryMessage, encblockBytes.Bytes())
|
||||||
|
|
||||||
// prompt user about the file
|
// prompt user about the file
|
||||||
|
@ -378,7 +381,7 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string,
|
||||||
}(finished, dataChan)
|
}(finished, dataChan)
|
||||||
|
|
||||||
log.Debug("telling sender i'm ready")
|
log.Debug("telling sender i'm ready")
|
||||||
c.WriteMessage(websocket.BinaryMessage, append([]byte("ready"), blocksBytes...))
|
c.WriteMessage(websocket.BinaryMessage, []byte("ready"))
|
||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
if useWebsockets {
|
if useWebsockets {
|
||||||
|
@ -408,7 +411,6 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string,
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_ = <-isConnectedIfUsingTCP
|
|
||||||
log.Debugf("starting listening with tcp with %d connections", len(tcpConnections))
|
log.Debugf("starting listening with tcp with %d connections", len(tcpConnections))
|
||||||
// using TCP
|
// using TCP
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
|
@ -205,26 +205,6 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL
|
||||||
return errors.New("recipient refused file")
|
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
|
err = <-fileReady // block until file is ready
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -239,7 +219,7 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL
|
||||||
// send the file meta data
|
// send the file meta data
|
||||||
c.WriteMessage(websocket.BinaryMessage, enc.Bytes())
|
c.WriteMessage(websocket.BinaryMessage, enc.Bytes())
|
||||||
case 4:
|
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
|
// recipient sends blocks, and sender does not send anything back
|
||||||
// determine if any blocks were sent to skip
|
// determine if any blocks were sent to skip
|
||||||
enc, err := crypt.FromBytes(message)
|
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)
|
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 loading the file into memory
|
||||||
// start streaming encryption/compression
|
// start streaming encryption/compression
|
||||||
if cr.FileInfo.IsDir {
|
if cr.FileInfo.IsDir {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue