mirror of
https://github.com/schollz/croc.git
synced 2025-01-05 21:03:40 +03:00
allow stdout
This commit is contained in:
parent
1acbe9c475
commit
e13f8e7067
@ -128,7 +128,7 @@ func (c *Croc) sendReceive(websocketAddress, fname, codephrase string, isSender
|
|||||||
// start peerdiscovery relay server
|
// start peerdiscovery relay server
|
||||||
go sender.Send(done, sock, fname, codephrase)
|
go sender.Send(done, sock, fname, codephrase)
|
||||||
} else {
|
} else {
|
||||||
go recipient.Receive(done, sock, codephrase, c.NoRecipientPrompt)
|
go recipient.Receive(done, sock, codephrase, c.NoRecipientPrompt, c.Stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -28,9 +29,9 @@ import (
|
|||||||
var DebugLevel string
|
var DebugLevel string
|
||||||
|
|
||||||
// Receive is the async operation to receive a file
|
// Receive is the async operation to receive a file
|
||||||
func Receive(done chan struct{}, c *websocket.Conn, codephrase string, noPrompt bool) {
|
func Receive(done chan struct{}, c *websocket.Conn, codephrase string, noPrompt bool, useStdout bool) {
|
||||||
logger.SetLogLevel(DebugLevel)
|
logger.SetLogLevel(DebugLevel)
|
||||||
err := receive(c, codephrase, noPrompt)
|
err := receive(c, codephrase, noPrompt, useStdout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if strings.HasPrefix(err.Error(), "websocket: close 100") {
|
if strings.HasPrefix(err.Error(), "websocket: close 100") {
|
||||||
return
|
return
|
||||||
@ -40,7 +41,7 @@ func Receive(done chan struct{}, c *websocket.Conn, codephrase string, noPrompt
|
|||||||
done <- struct{}{}
|
done <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func receive(c *websocket.Conn, codephrase string, noPrompt bool) (err error) {
|
func receive(c *websocket.Conn, codephrase string, noPrompt bool, useStdout bool) (err error) {
|
||||||
var fstats models.FileStats
|
var fstats models.FileStats
|
||||||
var sessionKey []byte
|
var sessionKey []byte
|
||||||
var transferTime time.Duration
|
var transferTime time.Duration
|
||||||
@ -209,7 +210,6 @@ func receive(c *websocket.Conn, codephrase string, noPrompt bool) (err error) {
|
|||||||
// receive the hash from the sender so we can check it and quit
|
// receive the hash from the sender so we can check it and quit
|
||||||
log.Debugf("got hash: %x", message)
|
log.Debugf("got hash: %x", message)
|
||||||
if bytes.Equal(hash256, message) {
|
if bytes.Equal(hash256, message) {
|
||||||
c.WriteMessage(websocket.BinaryMessage, []byte("ok"))
|
|
||||||
// open directory
|
// open directory
|
||||||
if fstats.IsDir {
|
if fstats.IsDir {
|
||||||
err = zipper.UnzipFile(fstats.SentName, ".")
|
err = zipper.UnzipFile(fstats.SentName, ".")
|
||||||
@ -220,6 +220,15 @@ func receive(c *websocket.Conn, codephrase string, noPrompt bool) (err error) {
|
|||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
if useStdout && !fstats.IsDir {
|
||||||
|
var bFile []byte
|
||||||
|
bFile, err = ioutil.ReadFile(fstats.SentName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
os.Stdout.Write(bFile)
|
||||||
|
os.Remove(fstats.SentName)
|
||||||
|
}
|
||||||
transferRate := float64(fstats.Size) / 1000000.0 / transferTime.Seconds()
|
transferRate := float64(fstats.Size) / 1000000.0 / transferTime.Seconds()
|
||||||
transferType := "MB/s"
|
transferType := "MB/s"
|
||||||
if transferRate < 1 {
|
if transferRate < 1 {
|
||||||
@ -230,11 +239,17 @@ func receive(c *websocket.Conn, codephrase string, noPrompt bool) (err error) {
|
|||||||
if fstats.IsDir {
|
if fstats.IsDir {
|
||||||
folderOrFile = "folder"
|
folderOrFile = "folder"
|
||||||
}
|
}
|
||||||
|
if useStdout {
|
||||||
|
fstats.Name = "stdout"
|
||||||
|
}
|
||||||
fmt.Fprintf(os.Stderr, "\nReceived %s written to %s (%2.1f %s)\n", folderOrFile, fstats.Name, transferRate, transferType)
|
fmt.Fprintf(os.Stderr, "\nReceived %s written to %s (%2.1f %s)\n", folderOrFile, fstats.Name, transferRate, transferType)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
c.WriteMessage(websocket.BinaryMessage, []byte("not"))
|
if DebugLevel != "debug" {
|
||||||
|
log.Debug("removing corrupted file")
|
||||||
|
os.Remove(fstats.SentName)
|
||||||
|
}
|
||||||
return errors.New("file corrupted")
|
return errors.New("file corrupted")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user