mirror of https://github.com/schollz/croc.git
return errors through websockets
This commit is contained in:
parent
304355af48
commit
d05e196139
|
@ -87,6 +87,7 @@ func Run() {
|
||||||
}
|
}
|
||||||
app.Before = func(c *cli.Context) error {
|
app.Before = func(c *cli.Context) error {
|
||||||
cr = croc.Init(c.GlobalBool("debug"))
|
cr = croc.Init(c.GlobalBool("debug"))
|
||||||
|
cr.Version = Version
|
||||||
cr.AllowLocalDiscovery = true
|
cr.AllowLocalDiscovery = true
|
||||||
cr.Address = c.GlobalString("addr")
|
cr.Address = c.GlobalString("addr")
|
||||||
cr.AddressTCPPorts = strings.Split(c.GlobalString("addr-tcp"), ",")
|
cr.AddressTCPPorts = strings.Split(c.GlobalString("addr-tcp"), ",")
|
||||||
|
|
|
@ -17,6 +17,8 @@ func init() {
|
||||||
|
|
||||||
// Croc options
|
// Croc options
|
||||||
type Croc struct {
|
type Croc struct {
|
||||||
|
// Version is the version of croc
|
||||||
|
Version string
|
||||||
// Options for all
|
// Options for all
|
||||||
Debug bool
|
Debug bool
|
||||||
// ShowText will display text on the stderr
|
// ShowText will display text on the stderr
|
||||||
|
|
|
@ -26,7 +26,6 @@ import (
|
||||||
"github.com/schollz/pake"
|
"github.com/schollz/pake"
|
||||||
progressbar "github.com/schollz/progressbar/v2"
|
progressbar "github.com/schollz/progressbar/v2"
|
||||||
"github.com/schollz/spinner"
|
"github.com/schollz/spinner"
|
||||||
"github.com/tscholl2/siec"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Send is the async call to send data
|
// Send is the async call to send data
|
||||||
|
@ -37,8 +36,11 @@ func (cr *Croc) startSender(forceSend int, serverAddress string, tcpPorts []stri
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !strings.HasPrefix(err.Error(), "websocket: close 100") {
|
if !strings.HasPrefix(err.Error(), "websocket: close 100") {
|
||||||
fmt.Fprintf(os.Stderr, "\n"+err.Error())
|
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()
|
||||||
}
|
}
|
||||||
cr.StateString = err.Error()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
done <- struct{}{}
|
done <- struct{}{}
|
||||||
|
@ -91,12 +93,10 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL
|
||||||
spin.Writer = os.Stderr
|
spin.Writer = os.Stderr
|
||||||
defer spin.Stop()
|
defer spin.Stop()
|
||||||
|
|
||||||
// pick an elliptic curve
|
|
||||||
curve := siec.SIEC255()
|
|
||||||
// both parties should have a weak key
|
// both parties should have a weak key
|
||||||
pw := []byte(codephrase)
|
pw := []byte(codephrase)
|
||||||
// initialize sender P ("0" indicates sender)
|
// initialize sender P ("0" indicates sender)
|
||||||
P, err := pake.Init(pw, 0, curve, 1*time.Millisecond)
|
P, err := pake.InitCurve(pw, 0, cr.CurveType, 1*time.Millisecond)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -113,6 +113,9 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL
|
||||||
if messageType == websocket.TextMessage && bytes.Equal(message, []byte("interrupt")) {
|
if messageType == websocket.TextMessage && bytes.Equal(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")) {
|
||||||
|
return errors.New("\r" + string(message))
|
||||||
|
}
|
||||||
log.Debugf("got %d: %s", messageType, message)
|
log.Debugf("got %d: %s", messageType, message)
|
||||||
switch step {
|
switch step {
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -123,11 +126,24 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL
|
||||||
} else {
|
} else {
|
||||||
ip, _ = utils.PublicIP()
|
ip, _ = utils.PublicIP()
|
||||||
}
|
}
|
||||||
// send my IP address
|
|
||||||
c.WriteMessage(websocket.BinaryMessage, []byte(ip))
|
initialData := models.Initial{
|
||||||
|
CurveType: cr.CurveType,
|
||||||
|
IPAddress: ip,
|
||||||
|
VersionString: cr.Version, // version should match
|
||||||
|
}
|
||||||
|
bInitialData, _ := json.Marshal(initialData)
|
||||||
|
// send the initial data
|
||||||
|
c.WriteMessage(websocket.BinaryMessage, bInitialData)
|
||||||
case 1:
|
case 1:
|
||||||
// first receive the IP address from the sender
|
// first receive the initial data from the recipient
|
||||||
cr.OtherIP = string(message)
|
var initialData models.Initial
|
||||||
|
err = json.Unmarshal(message, &initialData)
|
||||||
|
if err != nil {
|
||||||
|
err = errors.Wrap(err, "incompatible versions of croc")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cr.OtherIP = initialData.IPAddress
|
||||||
log.Debugf("recipient IP: %s", cr.OtherIP)
|
log.Debugf("recipient IP: %s", cr.OtherIP)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|
Loading…
Reference in New Issue