mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
discovering
This commit is contained in:
parent
1cdbdcb08e
commit
bf7a1bbdff
2 changed files with 38 additions and 29 deletions
58
src/api.go
58
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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue