mirror of https://github.com/schollz/croc.git
pass errors all the way back
This commit is contained in:
parent
8a882cc174
commit
068fdb77fc
|
@ -1,7 +1,6 @@
|
||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -13,6 +12,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
humanize "github.com/dustin/go-humanize"
|
humanize "github.com/dustin/go-humanize"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/schollz/croc/src/croc"
|
"github.com/schollz/croc/src/croc"
|
||||||
"github.com/schollz/croc/src/utils"
|
"github.com/schollz/croc/src/utils"
|
||||||
"github.com/skratchdot/open-golang/open"
|
"github.com/skratchdot/open-golang/open"
|
||||||
|
@ -131,7 +131,7 @@ func Run() {
|
||||||
|
|
||||||
err := app.Run(os.Args)
|
err := app.Run(os.Args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "\nerror: %s", err.Error())
|
fmt.Fprintf(os.Stderr, "\r\n%s", err.Error())
|
||||||
}
|
}
|
||||||
fmt.Fprintf(os.Stderr, "\r\n")
|
fmt.Fprintf(os.Stderr, "\r\n")
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,19 +31,13 @@ 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 (cr *Croc) startRecipient(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, done chan struct{}, c *websocket.Conn, codephrase string, noPrompt bool, useStdout bool) {
|
func (cr *Croc) startRecipient(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, done chan error, c *websocket.Conn, codephrase string, noPrompt bool, useStdout bool) {
|
||||||
logger.SetLogLevel(DebugLevel)
|
logger.SetLogLevel(DebugLevel)
|
||||||
err := cr.receive(forceSend, serverAddress, tcpPorts, isLocal, c, codephrase, noPrompt, useStdout)
|
err := cr.receive(forceSend, serverAddress, tcpPorts, isLocal, c, codephrase, noPrompt, useStdout)
|
||||||
if err != nil {
|
if err != nil && strings.HasPrefix(err.Error(), "websocket: close 100") {
|
||||||
if !strings.HasPrefix(err.Error(), "websocket: close 100") {
|
err = nil
|
||||||
fmt.Fprintf(os.Stderr, "\n"+err.Error())
|
|
||||||
cr.StateString = err.Error()
|
|
||||||
err = errors.Wrap(err, "error in recipient:")
|
|
||||||
c.WriteMessage(websocket.TextMessage, []byte(err.Error()))
|
|
||||||
time.Sleep(50 * time.Millisecond)
|
|
||||||
}
|
}
|
||||||
}
|
done <- err
|
||||||
done <- struct{}{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, c *websocket.Conn, codephrase string, noPrompt bool, useStdout bool) (err error) {
|
func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, c *websocket.Conn, codephrase string, noPrompt bool, useStdout bool) (err error) {
|
||||||
|
|
|
@ -29,22 +29,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Send is the async call to send data
|
// Send is the async call to send data
|
||||||
func (cr *Croc) startSender(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, done chan struct{}, c *websocket.Conn, fname string, codephrase string, useCompression bool, useEncryption bool) {
|
func (cr *Croc) startSender(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, done chan error, c *websocket.Conn, fname string, codephrase string, useCompression bool, useEncryption bool) {
|
||||||
logger.SetLogLevel(DebugLevel)
|
logger.SetLogLevel(DebugLevel)
|
||||||
log.Debugf("sending %s", fname)
|
log.Debugf("sending %s", fname)
|
||||||
err := cr.send(forceSend, serverAddress, tcpPorts, isLocal, c, fname, codephrase, useCompression, useEncryption)
|
err := cr.send(forceSend, serverAddress, tcpPorts, isLocal, c, fname, codephrase, useCompression, useEncryption)
|
||||||
if err != nil {
|
if err != nil && strings.HasPrefix(err.Error(), "websocket: close 100") {
|
||||||
log.Debug(err)
|
err = nil
|
||||||
if !strings.HasPrefix(err.Error(), "websocket: close 100") {
|
|
||||||
fmt.Fprintf(os.Stderr, "\n"+err.Error())
|
|
||||||
err = errors.Wrap(err, "error in sender:")
|
|
||||||
c.WriteMessage(websocket.TextMessage, []byte(err.Error()))
|
|
||||||
time.Sleep(50 * time.Millisecond)
|
|
||||||
cr.StateString = err.Error()
|
|
||||||
}
|
}
|
||||||
}
|
done <- err
|
||||||
|
|
||||||
done <- struct{}{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, c *websocket.Conn, fname string, codephrase string, useCompression bool, useEncryption bool) (err error) {
|
func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, c *websocket.Conn, fname string, codephrase string, useCompression bool, useEncryption bool) (err error) {
|
||||||
|
@ -128,7 +120,7 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL
|
||||||
if messageType == websocket.PongMessage || messageType == websocket.PingMessage {
|
if messageType == websocket.PongMessage || messageType == websocket.PingMessage {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if messageType == websocket.TextMessage && bytes.Equal(message, []byte("interrupt")) {
|
if messageType == websocket.TextMessage && bytes.HasPrefix(message, []byte("interrupt")) {
|
||||||
return errors.New("\rinterrupted by other party")
|
return errors.New("\rinterrupted by other party")
|
||||||
}
|
}
|
||||||
if messageType == websocket.TextMessage && bytes.HasPrefix(message, []byte("err")) {
|
if messageType == websocket.TextMessage && bytes.HasPrefix(message, []byte("err")) {
|
||||||
|
|
|
@ -142,7 +142,7 @@ func (c *Croc) sendReceive(address, websocketPort string, tcpPorts []string, fna
|
||||||
interrupt := make(chan os.Signal, 1)
|
interrupt := make(chan os.Signal, 1)
|
||||||
signal.Notify(interrupt, os.Interrupt)
|
signal.Notify(interrupt, os.Interrupt)
|
||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan error)
|
||||||
// connect to server
|
// connect to server
|
||||||
websocketAddress := ""
|
websocketAddress := ""
|
||||||
if len(websocketPort) > 0 {
|
if len(websocketPort) > 0 {
|
||||||
|
@ -173,9 +173,14 @@ func (c *Croc) sendReceive(address, websocketPort string, tcpPorts []string, fna
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-done:
|
case doneError := <-done:
|
||||||
log.Debug("received done signal")
|
log.Debug("received done signal")
|
||||||
return nil
|
if doneError != nil {
|
||||||
|
c.StateString = doneError.Error()
|
||||||
|
sock.WriteMessage(websocket.TextMessage, []byte("error: "+doneError.Error()))
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
return doneError
|
||||||
case <-interrupt:
|
case <-interrupt:
|
||||||
if !c.Debug {
|
if !c.Debug {
|
||||||
SetDebugLevel("critical")
|
SetDebugLevel("critical")
|
||||||
|
|
Loading…
Reference in New Issue