0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 21:30:16 +02:00

show the transfer rate

This commit is contained in:
Zack Scholl 2018-07-03 11:54:14 -07:00
parent 967d3d7c56
commit 846e3d5d00
2 changed files with 26 additions and 4 deletions

View file

@ -131,8 +131,16 @@ func (c *Croc) client(role int, channel string) (err error) {
c.cs.Lock() c.cs.Lock()
if c.cs.channel.finishedHappy { if c.cs.channel.finishedHappy {
log.Info("file recieved!") log.Info("file recieved!")
log.Debug(float64(c.cs.channel.fileMetaData.Size))
log.Debug(c.cs.channel.transferTime.Seconds())
transferRate := float64(c.cs.channel.fileMetaData.Size) / 1000000.0 / c.cs.channel.transferTime.Seconds()
transferType := "MB/s"
if transferRate < 1 {
transferRate = float64(c.cs.channel.fileMetaData.Size) / 1000.0 / c.cs.channel.transferTime.Seconds()
transferType = "kB/s"
}
if c.cs.channel.Role == 0 { if c.cs.channel.Role == 0 {
fmt.Fprintf(os.Stderr, "\nTransfer complete.\n") fmt.Fprintf(os.Stderr, "\nTransfer complete (%2.1f %s)\n", transferRate, transferType)
} else { } else {
folderOrFile := "file" folderOrFile := "file"
if c.cs.channel.fileMetaData.IsDir { if c.cs.channel.fileMetaData.IsDir {
@ -140,7 +148,7 @@ func (c *Croc) client(role int, channel string) (err error) {
} }
// push to stdout if required // push to stdout if required
if c.Stdout && !c.cs.channel.fileMetaData.IsDir { if c.Stdout && !c.cs.channel.fileMetaData.IsDir {
fmt.Fprintf(os.Stderr, "\nReceived %s written to %s", folderOrFile, "stdout") fmt.Fprintf(os.Stderr, "\nReceived %s written to %s. (%2.1f %s)\n", folderOrFile, "stdout", transferRate, transferType)
var bFile []byte var bFile []byte
bFile, err = ioutil.ReadFile(c.cs.channel.fileMetaData.Name) bFile, err = ioutil.ReadFile(c.cs.channel.fileMetaData.Name)
if err != nil { if err != nil {
@ -149,8 +157,7 @@ func (c *Croc) client(role int, channel string) (err error) {
os.Stdout.Write(bFile) os.Stdout.Write(bFile)
os.Remove(c.cs.channel.fileMetaData.Name) os.Remove(c.cs.channel.fileMetaData.Name)
} else { } else {
fmt.Fprintf(os.Stderr, "\nReceived %s written to %s. (%2.1f %s)\n", folderOrFile, c.cs.channel.fileMetaData.Name, transferRate, transferType)
fmt.Fprintf(os.Stderr, "\nReceived %s written to %s", folderOrFile, c.cs.channel.fileMetaData.Name)
} }
} }
} else { } else {
@ -394,6 +401,11 @@ func (c *Croc) dialUp() (err error) {
} }
log.Debug("sending file") log.Debug("sending file")
filename := c.crocFileEncrypted + "." + strconv.Itoa(i) filename := c.crocFileEncrypted + "." + strconv.Itoa(i)
if i == 0 {
c.cs.Lock()
c.cs.channel.startTransfer = time.Now()
c.cs.Unlock()
}
err = c.sendFile(filename, i, connection) err = c.sendFile(filename, i, connection)
} else { } else {
go func() { go func() {
@ -410,6 +422,11 @@ func (c *Croc) dialUp() (err error) {
c.cs.Unlock() c.cs.Unlock()
log.Debug("receive file") log.Debug("receive file")
}() }()
if i == 0 {
c.cs.Lock()
c.cs.channel.startTransfer = time.Now()
c.cs.Unlock()
}
receiveFileName := c.crocFileEncrypted + "." + strconv.Itoa(i) receiveFileName := c.crocFileEncrypted + "." + strconv.Itoa(i)
log.Debugf("receiving file into %s", receiveFileName) log.Debugf("receiving file into %s", receiveFileName)
err = c.receiveFile(receiveFileName, i, connection) err = c.receiveFile(receiveFileName, i, connection)
@ -429,6 +446,9 @@ func (c *Croc) dialUp() (err error) {
c.cs.channel.ws.WriteJSON(c.cs.channel) c.cs.channel.ws.WriteJSON(c.cs.channel)
} }
} }
c.cs.Lock()
c.cs.channel.transferTime = time.Since(c.cs.channel.startTransfer)
c.cs.Unlock()
log.Debug("leaving dialup") log.Debug("leaving dialup")
return return
} }

View file

@ -164,6 +164,8 @@ type channelData struct {
notSentMetaData bool notSentMetaData bool
finishedHappy bool finishedHappy bool
filesReady bool filesReady bool
startTransfer time.Time
transferTime time.Duration
// ws is the connection that the client has to the relay // ws is the connection that the client has to the relay
ws *websocket.Conn ws *websocket.Conn