diff --git a/connect.go b/connect.go index bb161bde..36cc9c1f 100644 --- a/connect.go +++ b/connect.go @@ -27,6 +27,7 @@ type Connection struct { IsSender bool Debug bool DontEncrypt bool + Wait bool bars []*uiprogress.Bar rate int } @@ -41,6 +42,7 @@ func NewConnection(flags *Flags) *Connection { c := new(Connection) c.Debug = flags.Debug c.DontEncrypt = flags.DontEncrypt + c.Wait = flags.Wait c.Server = flags.Server c.Code = flags.Code c.NumberOfConnections = flags.NumberOfConnections @@ -79,16 +81,21 @@ func (c *Connection) Run() error { // check code goodCode := true m := strings.Split(c.Code, "-") + log.Debug(m) numThreads, errParse := strconv.Atoi(m[0]) if len(m) < 2 { goodCode = false + log.Debug("code too short") } else if numThreads > MAX_NUMBER_THREADS || numThreads < 1 || (forceSingleThreaded && numThreads != 1) { c.NumberOfConnections = MAX_NUMBER_THREADS goodCode = false + log.Debug("incorrect number of threads") } else if errParse != nil { goodCode = false + log.Debug("problem parsing threads") } log.Debug(m) + log.Debug(goodCode) if !goodCode { if c.IsSender { if forceSingleThreaded { @@ -130,6 +137,8 @@ func (c *Connection) Run() error { if err != nil { return err } + fmt.Printf("Sending %d byte file named '%s'\n", c.File.Size, c.File.Name) + fmt.Printf("Code is: %s\n", c.Code) } return c.runClient() @@ -154,6 +163,7 @@ func (c *Connection) runClient() error { gotOK := false gotResponse := false gotConnectionInUse := false + notPresent := false for id := 0; id < c.NumberOfConnections; id++ { go func(id int) { defer wg.Done() @@ -176,7 +186,11 @@ func (c *Connection) runClient() error { sendMessage("s."+c.HashedCode+"."+hex.EncodeToString(encryptedMetaData)+"-"+salt+"-"+iv, connection) } else { logger.Debugf("telling relay: %s", "r."+c.Code) - sendMessage("r."+c.HashedCode+".0.0.0", connection) + if c.Wait { + sendMessage("r."+c.HashedCode+".0.0.0", connection) + } else { + sendMessage("c."+c.HashedCode+".0.0.0", connection) + } } if c.IsSender { // this is a sender logger.Debug("waiting for ok from relay") @@ -205,6 +219,11 @@ func (c *Connection) runClient() error { } else { m := strings.Split(message, "-") encryptedData, salt, iv, sendersAddress := m[0], m[1], m[2], m[3] + if sendersAddress == "0.0.0.0" { + notPresent = true + time.Sleep(1 * time.Second) + return + } encryptedBytes, err := hex.DecodeString(encryptedData) if err != nil { log.Error(err) @@ -245,7 +264,9 @@ func (c *Connection) runClient() error { } else { sendMessage("ok", connection) logger.Debug("receive file") - fmt.Printf("\n\nReceiving (<-%s)..\n", sendersAddress) + if id == 0 { + fmt.Printf("\n\nReceiving (<-%s)..\n", sendersAddress) + } c.receiveFile(id, connection) } } @@ -261,6 +282,10 @@ func (c *Connection) runClient() error { if c.IsSender { // TODO: Add confirmation } else { // Is a Receiver + if notPresent { + fmt.Println("Sender/Code not present") + return nil + } if !gotOK { return errors.New("Transfer interrupted") } @@ -291,6 +316,10 @@ func (c *Connection) runClient() error { } else { fmt.Printf("\nReceived file written to %s", c.File.Name) } + + } else { + fmt.Println("File sent.") + // TODO: Add confirmation } return nil } @@ -354,6 +383,7 @@ func (c *Connection) receiveFile(id int, connection net.Conn) error { logger.Debug("waiting for file") var receivedBytes int64 + receivedFirstBytes := false for { if !c.Debug { c.bars[id].Incr() @@ -370,6 +400,10 @@ func (c *Connection) receiveFile(id int, connection net.Conn) error { } io.CopyN(newFile, connection, BUFFERSIZE) receivedBytes += BUFFERSIZE + if !receivedFirstBytes { + receivedFirstBytes = true + logger.Debug("Receieved first bytes!") + } } logger.Debug("received file") return nil diff --git a/main.go b/main.go index 2a319b03..7945b54a 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ var oneGigabytePerSecond = 1000000 // expressed as kbps type Flags struct { Relay bool Debug bool + Wait bool DontEncrypt bool Server string File string @@ -37,6 +38,7 @@ croc version ` + version + ` flags := new(Flags) flag.BoolVar(&flags.Relay, "relay", false, "run as relay") flag.BoolVar(&flags.Debug, "debug", false, "debug mode") + flag.BoolVar(&flags.Wait, "wait", false, "wait for code to be sent") flag.StringVar(&flags.Server, "server", "cowyo.com", "address of relay server") flag.StringVar(&flags.File, "send", "", "file to send") flag.StringVar(&flags.Code, "code", "", "use your own code phrase") diff --git a/relay.go b/relay.go index 9c95c133..f28c35fd 100644 --- a/relay.go +++ b/relay.go @@ -128,7 +128,6 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) { sendMessage("no", connection) return } - logger.Debug("got sender") r.connections.Lock() r.connections.metadata[key] = metaData @@ -174,7 +173,6 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) { r.connections.Lock() r.connections.potentialReceivers[key] = struct{}{} r.connections.Unlock() - // wait for sender's metadata sendersAddress := "" for { @@ -188,6 +186,10 @@ func (r *Relay) clientCommuncation(id int, connection net.Conn) { } } r.connections.RUnlock() + if connectionType == "c" { + sendMessage("0-0-0-0.0.0.0", connection) + return + } time.Sleep(100 * time.Millisecond) } // send meta data