From bf7a1bbdff3948380cdb0751fbc048e8e9369388 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sat, 30 Jun 2018 11:59:22 -0700 Subject: [PATCH] discovering --- src/api.go | 58 +++++++++++++++++++++++++++------------------------ src/client.go | 9 ++++++-- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/api.go b/src/api.go index a780ffd..07db150 100644 --- a/src/api.go +++ b/src/api.go @@ -28,16 +28,15 @@ func (c *Croc) Send(fname string, codePhrase string) (err error) { c.cs.Lock() c.cs.channel.codePhrase = codePhrase - if len(codePhrase) > 0 { - if len(codePhrase) < 4 { - err = errors.New("code phrase must be more than 4 characters") - c.cs.Unlock() - return - } - } else { + if len(codePhrase) == 0 { // generate code phrase codePhrase = getRandomName() } + if len(codePhrase) < 4 { + err = errors.New("code phrase must be more than 4 characters") + c.cs.Unlock() + return + } c.cs.channel.codePhrase = codePhrase c.cs.channel.Channel = codePhrase[:3] c.cs.channel.passPhrase = codePhrase[3:] @@ -77,6 +76,11 @@ func (c *Croc) Send(fname string, codePhrase string) (err error) { go d.startServer() e := Init() e.WebsocketAddress = "ws://127.0.0.1:8140" + e.cs.Lock() + c.cs.channel.codePhrase = codePhrase + c.cs.channel.Channel = codePhrase[:3] + c.cs.channel.passPhrase = codePhrase[3:] + e.cs.Unlock() runClientError <- e.client(0, channel) }() @@ -89,26 +93,6 @@ func (c *Croc) Send(fname string, codePhrase string) (err error) { // Receive will receive something through the croc relay func (c *Croc) Receive(codePhrase string) (err error) { - // prepare codephrase - c.cs.Lock() - if len(codePhrase) == 0 { - // prompt codephrase - codePhrase = promptCodePhrase() - } - if len(codePhrase) < 4 { - err = errors.New("code phrase must be more than 4 characters") - c.cs.Unlock() - return - } - c.cs.channel.codePhrase = codePhrase - c.cs.channel.Channel = codePhrase[:3] - c.cs.channel.passPhrase = codePhrase[3:] - log.Debugf("codephrase: '%s'", codePhrase) - log.Debugf("channel: '%s'", c.cs.channel.Channel) - log.Debugf("passPhrase: '%s'", c.cs.channel.passPhrase) - channel := c.cs.channel.Channel - c.cs.Unlock() - // try to discovery codephrase and server through peer network discovered, errDiscover := peerdiscovery.Discover(peerdiscovery.Settings{ Limit: 1, @@ -134,5 +118,25 @@ func (c *Croc) Receive(codePhrase string) (err error) { log.Debug("discovered no peers") } + // prepare codephrase + c.cs.Lock() + if len(codePhrase) == 0 { + // prompt codephrase + codePhrase = promptCodePhrase() + } + if len(codePhrase) < 4 { + err = errors.New("code phrase must be more than 4 characters") + c.cs.Unlock() + return + } + c.cs.channel.codePhrase = codePhrase + c.cs.channel.Channel = codePhrase[:3] + c.cs.channel.passPhrase = codePhrase[3:] + log.Debugf("codephrase: '%s'", codePhrase) + log.Debugf("channel: '%s'", c.cs.channel.Channel) + log.Debugf("passPhrase: '%s'", c.cs.channel.passPhrase) + channel := c.cs.channel.Channel + c.cs.Unlock() + return c.client(1, channel) } diff --git a/src/client.go b/src/client.go index ac36ea7..5d3a071 100644 --- a/src/client.go +++ b/src/client.go @@ -22,7 +22,7 @@ import ( var isPrinted bool -func (c *Croc) client(role int, channel string) (err error) { +func (c *Croc) client(role int, channel string, address ...string) (err error) { defer log.Flush() defer c.cleanup() // initialize the channel data for this client @@ -31,7 +31,12 @@ func (c *Croc) client(role int, channel string) (err error) { signal.Notify(interrupt, os.Interrupt) // connect to the websocket - u := url.URL{Scheme: strings.Split(c.WebsocketAddress, "://")[0], Host: strings.Split(c.WebsocketAddress, "://")[1], Path: "/"} + var u url.URL + 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()) ws, _, err := websocket.DefaultDialer.Dial(u.String(), nil) if err != nil {