mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 05:11:06 +02:00
Fix data races #44
This commit is contained in:
parent
0056cddc1d
commit
3fd9a345c4
1 changed files with 52 additions and 27 deletions
79
connect.go
79
connect.go
|
@ -221,11 +221,16 @@ func (c *Connection) runClient() error {
|
||||||
if !c.Debug {
|
if !c.Debug {
|
||||||
c.bars = make([]*uiprogress.Bar, c.NumberOfConnections)
|
c.bars = make([]*uiprogress.Bar, c.NumberOfConnections)
|
||||||
}
|
}
|
||||||
gotTimeout := false
|
type responsesStruct struct {
|
||||||
gotOK := false
|
gotTimeout bool
|
||||||
gotResponse := false
|
gotOK bool
|
||||||
gotConnectionInUse := false
|
gotResponse bool
|
||||||
notPresent := false
|
gotConnectionInUse bool
|
||||||
|
notPresent bool
|
||||||
|
sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
responses := new(responsesStruct)
|
||||||
for id := 0; id < c.NumberOfConnections; id++ {
|
for id := 0; id < c.NumberOfConnections; id++ {
|
||||||
go func(id int) {
|
go func(id int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
@ -265,7 +270,9 @@ func (c *Connection) runClient() error {
|
||||||
logger.Debug("waiting for ok from relay")
|
logger.Debug("waiting for ok from relay")
|
||||||
message = receiveMessage(connection)
|
message = receiveMessage(connection)
|
||||||
if message == "timeout" {
|
if message == "timeout" {
|
||||||
gotTimeout = true
|
responses.Lock()
|
||||||
|
responses.gotTimeout = true
|
||||||
|
responses.Unlock()
|
||||||
fmt.Println("You've just exceeded limit waiting time.")
|
fmt.Println("You've just exceeded limit waiting time.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -273,7 +280,9 @@ func (c *Connection) runClient() error {
|
||||||
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.")
|
||||||
}
|
}
|
||||||
gotConnectionInUse = true
|
responses.Lock()
|
||||||
|
responses.gotConnectionInUse = true
|
||||||
|
responses.Unlock()
|
||||||
} else {
|
} else {
|
||||||
logger.Debug("got ok from relay")
|
logger.Debug("got ok from relay")
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
|
@ -294,29 +303,33 @@ func (c *Connection) runClient() error {
|
||||||
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.")
|
||||||
}
|
}
|
||||||
gotConnectionInUse = true
|
responses.Lock()
|
||||||
|
responses.gotConnectionInUse = true
|
||||||
|
responses.Unlock()
|
||||||
} else {
|
} else {
|
||||||
m := strings.Split(message, "-")
|
m := strings.Split(message, "-")
|
||||||
encryptedData, salt, iv, sendersAddress := m[0], m[1], m[2], m[3]
|
encryptedData, salt, iv, sendersAddress := m[0], m[1], m[2], m[3]
|
||||||
if sendersAddress == "0.0.0.0" {
|
if sendersAddress == "0.0.0.0" {
|
||||||
notPresent = true
|
responses.Lock()
|
||||||
|
responses.notPresent = true
|
||||||
|
responses.Unlock()
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
encryptedBytes, err := hex.DecodeString(encryptedData)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
decryptedBytes, _ := Decrypt(encryptedBytes, c.Code, salt, iv, c.DontEncrypt)
|
|
||||||
err = json.Unmarshal(decryptedBytes, &c.File)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Debugf("meta data received: %v", c.File)
|
|
||||||
// have the main thread ask for the okay
|
// have the main thread ask for the okay
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
|
encryptedBytes, err := hex.DecodeString(encryptedData)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
decryptedBytes, _ := Decrypt(encryptedBytes, c.Code, salt, iv, c.DontEncrypt)
|
||||||
|
err = json.Unmarshal(decryptedBytes, &c.File)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Debugf("meta data received: %v", c.File)
|
||||||
fType := "file"
|
fType := "file"
|
||||||
fName := path.Join(c.Path, c.File.Name)
|
fName := path.Join(c.Path, c.File.Name)
|
||||||
if c.File.IsDir {
|
if c.File.IsDir {
|
||||||
|
@ -342,18 +355,28 @@ func (c *Connection) runClient() error {
|
||||||
}
|
}
|
||||||
getOK := getInput("ok? (y/n): ")
|
getOK := getInput("ok? (y/n): ")
|
||||||
if getOK == "y" {
|
if getOK == "y" {
|
||||||
gotOK = true
|
responses.Lock()
|
||||||
|
responses.gotOK = true
|
||||||
|
responses.Unlock()
|
||||||
sentFileNames = append(sentFileNames, c.File.Name)
|
sentFileNames = append(sentFileNames, c.File.Name)
|
||||||
}
|
}
|
||||||
gotResponse = true
|
responses.Lock()
|
||||||
|
responses.gotResponse = true
|
||||||
|
responses.Unlock()
|
||||||
}
|
}
|
||||||
// wait for the main thread to get the okay
|
// wait for the main thread to get the okay
|
||||||
for limit := 0; limit < 1000; limit++ {
|
for limit := 0; limit < 1000; limit++ {
|
||||||
|
responses.Lock()
|
||||||
|
gotResponse := responses.gotResponse
|
||||||
|
responses.Unlock()
|
||||||
if gotResponse {
|
if gotResponse {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
responses.RLock()
|
||||||
|
gotOK := responses.gotOK
|
||||||
|
responses.RUnlock()
|
||||||
if !gotOK {
|
if !gotOK {
|
||||||
sendMessage("not ok", connection)
|
sendMessage("not ok", connection)
|
||||||
} else {
|
} else {
|
||||||
|
@ -372,22 +395,24 @@ func (c *Connection) runClient() error {
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
if gotConnectionInUse {
|
responses.Lock()
|
||||||
|
defer responses.Unlock()
|
||||||
|
if responses.gotConnectionInUse {
|
||||||
return nil // connection was in use, just quit cleanly
|
return nil // connection was in use, just quit cleanly
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.IsSender {
|
if c.IsSender {
|
||||||
if gotTimeout {
|
if responses.gotTimeout {
|
||||||
fmt.Println("Timeout waiting for receiver")
|
fmt.Println("Timeout waiting for receiver")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
fmt.Println("\nFile sent.")
|
fmt.Println("\nFile sent.")
|
||||||
} else { // Is a Receiver
|
} else { // Is a Receiver
|
||||||
if notPresent {
|
if responses.notPresent {
|
||||||
fmt.Println("Sender is not ready. Use -wait to wait until sender connects.")
|
fmt.Println("Sender is not ready. Use -wait to wait until sender connects.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if !gotOK {
|
if !responses.gotOK {
|
||||||
return errors.New("Transfer interrupted")
|
return errors.New("Transfer interrupted")
|
||||||
}
|
}
|
||||||
if err := c.catFile(); err != nil {
|
if err := c.catFile(); err != nil {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue