improve relay

This commit is contained in:
Zack Scholl 2018-07-01 09:23:15 -07:00
parent e055918656
commit dd8d8ee4ce
3 changed files with 19 additions and 12 deletions

View File

@ -117,6 +117,5 @@ func relay(c *cli.Context) error {
cr.TcpPorts = strings.Split(c.GlobalString("tcp"), ",") cr.TcpPorts = strings.Split(c.GlobalString("tcp"), ",")
cr.ServerPort = c.GlobalString("port") cr.ServerPort = c.GlobalString("port")
cr.CurveType = c.GlobalString("curve") cr.CurveType = c.GlobalString("curve")
fmt.Println("relay") return cr.Relay()
return nil
} }

View File

@ -67,7 +67,7 @@ func (c *Croc) Send(fname string, codePhrase string) (err error) {
} }
// start relay for listening // start relay for listening
runClientError := make(chan error) runClientError := make(chan error, 2)
go func() { go func() {
d := Init() d := Init()
d.ServerPort = "8140" d.ServerPort = "8140"
@ -94,7 +94,12 @@ func (c *Croc) Send(fname string, codePhrase string) (err error) {
go func() { go func() {
runClientError <- c.client(0, channel) runClientError <- c.client(0, channel)
}() }()
return <-runClientError err = <-runClientError
if err != nil {
return
}
err = <-runClientError
return
} }
// Receive will receive something through the croc relay // Receive will receive something through the croc relay

View File

@ -22,7 +22,7 @@ import (
var isPrinted bool var isPrinted bool
func (c *Croc) client(role int, channel string, address ...string) (err error) { func (c *Croc) client(role int, channel string) (err error) {
defer log.Flush() defer log.Flush()
defer c.cleanup() defer c.cleanup()
// initialize the channel data for this client // initialize the channel data for this client
@ -31,16 +31,19 @@ func (c *Croc) client(role int, channel string, address ...string) (err error) {
signal.Notify(interrupt, os.Interrupt) signal.Notify(interrupt, os.Interrupt)
// connect to the websocket // connect to the websocket
var u url.URL u := url.URL{Scheme: strings.Split(c.WebsocketAddress, "://")[0], Host: strings.Split(c.WebsocketAddress, "://")[1], Path: "/"}
if len(address) != 0 {
u = url.URL{Scheme: strings.Split(address[0], "://")[0], Host: strings.Split(address[0], "://")[1], Path: "/"}
} else {
u = url.URL{Scheme: strings.Split(c.WebsocketAddress, "://")[0], Host: strings.Split(c.WebsocketAddress, "://")[1], Path: "/"}
}
log.Debugf("connecting to %s", u.String()) log.Debugf("connecting to %s", u.String())
ws, _, err := websocket.DefaultDialer.Dial(u.String(), nil) ws, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil { if err != nil {
// don't return error if sender can't connect, so
// that croc can be used locally without
// an internet connection
if role == 0 {
log.Debugf("dial %s error: %s", c.WebsocketAddress, err.Error())
err = nil
} else {
log.Error("dial:", err) log.Error("dial:", err)
}
return return
} }
defer ws.Close() defer ws.Close()