mirror of https://github.com/schollz/croc.git
fix bugs in relay
This commit is contained in:
parent
cc79aaeecb
commit
992e109279
16
connect.go
16
connect.go
|
@ -228,6 +228,7 @@ func (c *Connection) Run() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.Server != "localhost" {
|
||||||
// broadcast local connection from sender
|
// broadcast local connection from sender
|
||||||
log.Debug("settings payload to ", c.Code)
|
log.Debug("settings payload to ", c.Code)
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -241,6 +242,7 @@ func (c *Connection) Run() error {
|
||||||
runClientError <- c.runClient("localhost")
|
runClientError <- c.runClient("localhost")
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Debug("checking code validity")
|
log.Debug("checking code validity")
|
||||||
if len(c.Code) == 0 && !c.IsSender {
|
if len(c.Code) == 0 && !c.IsSender {
|
||||||
|
@ -464,6 +466,7 @@ func (c *Connection) runClient(serverName string) error {
|
||||||
} else { // this is a receiver
|
} else { // this is a receiver
|
||||||
log.Debug("waiting for meta data from sender")
|
log.Debug("waiting for meta data from sender")
|
||||||
message = receiveMessage(connection)
|
message = receiveMessage(connection)
|
||||||
|
log.Debugf("message from server: %s", message)
|
||||||
if message == "no" {
|
if message == "no" {
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
fmt.Println("The specifed code is already in use by a sender.")
|
fmt.Println("The specifed code is already in use by a sender.")
|
||||||
|
@ -586,7 +589,7 @@ func (c *Connection) runClient(serverName string) error {
|
||||||
responses.startTime = time.Now()
|
responses.startTime = time.Now()
|
||||||
responses.Unlock()
|
responses.Unlock()
|
||||||
if !c.Debug && id == 0 {
|
if !c.Debug && id == 0 {
|
||||||
// c.bar.SetMax(c.File.Size)
|
c.bar.Finish()
|
||||||
c.bar.Reset()
|
c.bar.Reset()
|
||||||
} else {
|
} else {
|
||||||
// try to let the first thread start first
|
// try to let the first thread start first
|
||||||
|
@ -613,13 +616,13 @@ func (c *Connection) runClient(serverName string) error {
|
||||||
|
|
||||||
timeSinceStart := time.Since(responses.startTime).Nanoseconds()
|
timeSinceStart := time.Since(responses.startTime).Nanoseconds()
|
||||||
|
|
||||||
if !fileTransfered {
|
if c.IsSender {
|
||||||
fmt.Fprintf(os.Stderr, "\nNo mutual consent")
|
|
||||||
return nil
|
|
||||||
} else if c.IsSender {
|
|
||||||
if responses.gotTimeout {
|
if responses.gotTimeout {
|
||||||
fmt.Println("Timeout waiting for receiver")
|
fmt.Println("Timeout waiting for receiver")
|
||||||
return nil
|
return nil
|
||||||
|
} else if !fileTransfered {
|
||||||
|
fmt.Fprintf(os.Stderr, "\nNo mutual consent")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
fileOrFolder := "File"
|
fileOrFolder := "File"
|
||||||
if c.File.IsDir {
|
if c.File.IsDir {
|
||||||
|
@ -630,6 +633,9 @@ func (c *Connection) runClient(serverName string) error {
|
||||||
if responses.notPresent {
|
if responses.notPresent {
|
||||||
fmt.Println("Either code is incorrect or sender is not ready. Use -wait to wait until sender connects.")
|
fmt.Println("Either code is incorrect or sender is not ready. Use -wait to wait until sender connects.")
|
||||||
return nil
|
return nil
|
||||||
|
} else if !fileTransfered {
|
||||||
|
fmt.Fprintf(os.Stderr, "\nNo mutual consent")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
if !responses.gotOK {
|
if !responses.gotOK {
|
||||||
return errors.New("Transfer interrupted")
|
return errors.New("Transfer interrupted")
|
||||||
|
|
82
relay.go
82
relay.go
|
@ -34,13 +34,6 @@ func (c *connectionMap) IsSenderConnected(key string) (found bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connectionMap) IsPotentialReceiverConnected(key string) (found bool) {
|
|
||||||
c.RLock()
|
|
||||||
defer c.RUnlock()
|
|
||||||
_, found = c.potentialReceivers[key]
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type Relay struct {
|
type Relay struct {
|
||||||
connections connectionMap
|
connections connectionMap
|
||||||
Debug bool
|
Debug bool
|
||||||
|
@ -141,11 +134,16 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
|
|
||||||
switch connectionType {
|
switch connectionType {
|
||||||
case "s": // sender connection
|
case "s": // sender connection
|
||||||
defer func() {
|
startTime := time.Now()
|
||||||
|
deleteAll := func() {
|
||||||
r.connections.Lock()
|
r.connections.Lock()
|
||||||
// close connections
|
// close connections
|
||||||
|
if _, ok := r.connections.sender[key]; ok {
|
||||||
r.connections.sender[key].Close()
|
r.connections.sender[key].Close()
|
||||||
|
}
|
||||||
|
if _, ok := r.connections.receiver[key]; ok {
|
||||||
r.connections.receiver[key].Close()
|
r.connections.receiver[key].Close()
|
||||||
|
}
|
||||||
// delete connctions
|
// delete connctions
|
||||||
delete(r.connections.sender, key)
|
delete(r.connections.sender, key)
|
||||||
delete(r.connections.receiver, key)
|
delete(r.connections.receiver, key)
|
||||||
|
@ -157,7 +155,9 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
delete(r.connections.passphrase, key)
|
delete(r.connections.passphrase, key)
|
||||||
r.connections.Unlock()
|
r.connections.Unlock()
|
||||||
logger.Debug("deleted sender and receiver")
|
logger.Debug("deleted sender and receiver")
|
||||||
}()
|
}
|
||||||
|
defer deleteAll()
|
||||||
|
|
||||||
if r.connections.IsSenderConnected(key) {
|
if r.connections.IsSenderConnected(key) {
|
||||||
sendMessage("no", connection)
|
sendMessage("no", connection)
|
||||||
return
|
return
|
||||||
|
@ -172,10 +172,11 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
receiversAddress := ""
|
receiversAddress := ""
|
||||||
receiversPublicKey := ""
|
receiversPublicKey := ""
|
||||||
isTimeout := time.Duration(0)
|
isTimeout := time.Duration(0)
|
||||||
|
log.Debug("waiting for reciever for sender")
|
||||||
for {
|
for {
|
||||||
if CONNECTION_TIMEOUT <= isTimeout {
|
if CONNECTION_TIMEOUT <= isTimeout {
|
||||||
sendMessage("timeout", connection)
|
sendMessage("timeout", connection)
|
||||||
break
|
return
|
||||||
}
|
}
|
||||||
r.connections.RLock()
|
r.connections.RLock()
|
||||||
if _, ok := r.connections.receiver[key]; ok {
|
if _, ok := r.connections.receiver[key]; ok {
|
||||||
|
@ -212,6 +213,7 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
r.connections.Unlock()
|
r.connections.Unlock()
|
||||||
|
|
||||||
// wait for receiver ready
|
// wait for receiver ready
|
||||||
|
startTime = time.Now()
|
||||||
for {
|
for {
|
||||||
r.connections.RLock()
|
r.connections.RLock()
|
||||||
if _, ok := r.connections.receiverReady[key]; ok {
|
if _, ok := r.connections.receiverReady[key]; ok {
|
||||||
|
@ -219,6 +221,9 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
r.connections.RUnlock()
|
r.connections.RUnlock()
|
||||||
|
if time.Since(startTime) > 5*time.Minute {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// go reciever ready tell sender to go
|
// go reciever ready tell sender to go
|
||||||
sendMessage("go", connection)
|
sendMessage("go", connection)
|
||||||
|
@ -231,12 +236,20 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
Pipe(con1, con2)
|
Pipe(con1, con2)
|
||||||
logger.Debug("done piping")
|
logger.Debug("done piping")
|
||||||
case "r", "c": // receiver
|
case "r", "c": // receiver
|
||||||
if r.connections.IsPotentialReceiverConnected(key) {
|
startTime := time.Now()
|
||||||
|
log.Debug("is receiver")
|
||||||
|
|
||||||
|
r.connections.RLock()
|
||||||
|
_, foundReceiver := r.connections.potentialReceivers[key]
|
||||||
|
r.connections.RUnlock()
|
||||||
|
if foundReceiver {
|
||||||
|
log.Debug("already have receiver")
|
||||||
sendMessage("no", connection)
|
sendMessage("no", connection)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// add as a potential receiver
|
// add as a potential receiver
|
||||||
|
logger.Debug("adding as potential reciever")
|
||||||
r.connections.Lock()
|
r.connections.Lock()
|
||||||
r.connections.potentialReceivers[key] = struct{}{}
|
r.connections.potentialReceivers[key] = struct{}{}
|
||||||
r.connections.rpublicKey[key] = publicKey
|
r.connections.rpublicKey[key] = publicKey
|
||||||
|
@ -245,8 +258,15 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
// wait for sender's metadata
|
// wait for sender's metadata
|
||||||
sendersAddress := ""
|
sendersAddress := ""
|
||||||
sendersPublicKey := ""
|
sendersPublicKey := ""
|
||||||
|
startTime = time.Now()
|
||||||
for {
|
for {
|
||||||
r.connections.RLock()
|
r.connections.RLock()
|
||||||
|
// check if been deleted
|
||||||
|
if _, ok := r.connections.potentialReceivers[key]; !ok {
|
||||||
|
log.Debug("deleting and finishing")
|
||||||
|
r.connections.RUnlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
if _, ok := r.connections.metadata[key]; ok {
|
if _, ok := r.connections.metadata[key]; ok {
|
||||||
if _, ok2 := r.connections.sender[key]; ok2 {
|
if _, ok2 := r.connections.sender[key]; ok2 {
|
||||||
sendersAddress = r.connections.sender[key].RemoteAddr().String()
|
sendersAddress = r.connections.sender[key].RemoteAddr().String()
|
||||||
|
@ -270,6 +290,9 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
if time.Since(startTime) > 5*time.Minute {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// send meta data
|
// send meta data
|
||||||
r.connections.RLock()
|
r.connections.RLock()
|
||||||
|
@ -279,16 +302,26 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
|
|
||||||
// check for senders consent
|
// check for senders consent
|
||||||
sendersConsent := ""
|
sendersConsent := ""
|
||||||
|
startTime = time.Now()
|
||||||
for {
|
for {
|
||||||
r.connections.RLock()
|
r.connections.RLock()
|
||||||
|
// check if been deleted
|
||||||
|
if _, ok := r.connections.potentialReceivers[key]; !ok {
|
||||||
|
log.Debug("deleting and finishing")
|
||||||
|
r.connections.RUnlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
if _, ok := r.connections.sconsent[key]; ok {
|
if _, ok := r.connections.sconsent[key]; ok {
|
||||||
sendersConsent = r.connections.sconsent[key]
|
sendersConsent = r.connections.sconsent[key]
|
||||||
logger.Debugf("got sender passphrase: %s", sendersConsent)
|
|
||||||
}
|
}
|
||||||
r.connections.RUnlock()
|
r.connections.RUnlock()
|
||||||
if sendersConsent != "" {
|
if sendersConsent != "" {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
if time.Since(startTime) > 5*time.Minute {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if sendersConsent != "ok" {
|
if sendersConsent != "ok" {
|
||||||
// TODO: delete everything
|
// TODO: delete everything
|
||||||
|
@ -297,8 +330,16 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
|
|
||||||
// now get passphrase
|
// now get passphrase
|
||||||
sendersPassphrase := ""
|
sendersPassphrase := ""
|
||||||
|
startTime = time.Now()
|
||||||
for {
|
for {
|
||||||
r.connections.RLock()
|
r.connections.RLock()
|
||||||
|
// check if been deleted
|
||||||
|
if _, ok := r.connections.potentialReceivers[key]; !ok {
|
||||||
|
log.Debug("deleting and finishing")
|
||||||
|
r.connections.RUnlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if _, ok := r.connections.passphrase[key]; ok {
|
if _, ok := r.connections.passphrase[key]; ok {
|
||||||
sendersPassphrase = r.connections.passphrase[key]
|
sendersPassphrase = r.connections.passphrase[key]
|
||||||
logger.Debugf("got sender passphrase: %s", sendersPassphrase)
|
logger.Debugf("got sender passphrase: %s", sendersPassphrase)
|
||||||
|
@ -307,6 +348,10 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
if sendersPassphrase != "" {
|
if sendersPassphrase != "" {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
if time.Since(startTime) > 5*time.Minute {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for receiver's consent
|
// check for receiver's consent
|
||||||
|
@ -316,8 +361,16 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
logger.Debug("got consent")
|
logger.Debug("got consent")
|
||||||
// wait for encrypted passphrase
|
// wait for encrypted passphrase
|
||||||
encryptedPassphrase := ""
|
encryptedPassphrase := ""
|
||||||
|
startTime = time.Now()
|
||||||
for {
|
for {
|
||||||
r.connections.RLock()
|
r.connections.RLock()
|
||||||
|
// check if been deleted
|
||||||
|
if _, ok := r.connections.potentialReceivers[key]; !ok {
|
||||||
|
log.Debug("deleting and finishing")
|
||||||
|
r.connections.RUnlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if _, ok := r.connections.passphrase[key]; ok {
|
if _, ok := r.connections.passphrase[key]; ok {
|
||||||
encryptedPassphrase = r.connections.passphrase[key]
|
encryptedPassphrase = r.connections.passphrase[key]
|
||||||
logger.Debugf("got passphrase: %s", r.connections.passphrase[key])
|
logger.Debugf("got passphrase: %s", r.connections.passphrase[key])
|
||||||
|
@ -326,7 +379,10 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) {
|
||||||
if encryptedPassphrase != "" {
|
if encryptedPassphrase != "" {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
time.Sleep(10 * time.Millisecond)
|
if time.Since(startTime) > 5*time.Minute {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
sendMessage(encryptedPassphrase, connection)
|
sendMessage(encryptedPassphrase, connection)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue