This commit is contained in:
Zack Scholl 2018-06-30 11:45:30 -07:00
parent bda038b8fc
commit 2b61224707
2 changed files with 82 additions and 69 deletions

View File

@ -5,6 +5,7 @@ import (
"time"
log "github.com/cihub/seelog"
"github.com/pkg/errors"
"github.com/schollz/peerdiscovery"
)
@ -22,7 +23,50 @@ func (c *Croc) Relay() error {
}
// Send will take an existing file or folder and send it through the croc relay
func (c *Croc) Send(fname string, codephrase string) (err error) {
func (c *Croc) Send(fname string, codePhrase string) (err error) {
// prepare code phrase
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 {
// generate code phrase
codePhrase = getRandomName()
}
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()
// start peer discovery
go func() {
log.Debug("listening for local croc relay...")
go peerdiscovery.Discover(peerdiscovery.Settings{
Limit: 1,
TimeLimit: 600 * time.Second,
Delay: 50 * time.Millisecond,
Payload: []byte(codePhrase),
})
}()
if len(fname) == 0 {
err = errors.New("must include filename")
return
}
err = c.processFile(fname)
if err != nil {
return
}
// start relay for listening
runClientError := make(chan error)
go func() {
@ -33,24 +77,52 @@ func (c *Croc) Send(fname string, codephrase string) (err error) {
go d.startServer()
e := Init()
e.WebsocketAddress = "ws://127.0.0.1:8140"
runClientError <- e.client(0, codephrase, fname)
runClientError <- e.client(0, channel)
}()
// start main client
go func() {
runClientError <- c.client(0, codephrase, fname)
runClientError <- c.client(0, channel)
}()
return <-runClientError
}
// Receive will receive something through the croc relay
func (c *Croc) Receive(codephrase string) (err error) {
func (c *Croc) Receive(codePhrase string) (err error) {
// prepare codephrase
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
}
c.cs.channel.Channel = codePhrase[:3]
c.cs.channel.passPhrase = codePhrase[3:]
} else {
// 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.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,
TimeLimit: 1 * time.Second,
Delay: 50 * time.Millisecond,
Payload: []byte(codephrase),
Payload: []byte(codePhrase),
})
if errDiscover != nil {
log.Debug(errDiscover)
@ -62,7 +134,7 @@ func (c *Croc) Receive(codephrase string) (err error) {
log.Debug("connected")
c.WebsocketAddress = "ws://" + discovered[0].Address + ":8140"
log.Debug(discovered[0].Address)
codephrase = string(discovered[0].Payload)
codePhrase = string(discovered[0].Payload)
} else {
log.Debug("but could not connect to ports")
}
@ -70,5 +142,5 @@ func (c *Croc) Receive(codephrase string) (err error) {
log.Debug("discovered no peers")
}
return c.client(1, codephrase)
return c.client(1, channel)
}

View File

@ -17,79 +17,20 @@ import (
"github.com/gorilla/websocket"
"github.com/pkg/errors"
"github.com/schollz/croc/src/pake"
"github.com/schollz/peerdiscovery"
"github.com/schollz/progressbar"
)
func (c *Croc) client(role int, codePhrase string, fname ...string) (err error) {
var isPrinted bool
func (c *Croc) client(role int, channel string) (err error) {
defer log.Flush()
defer c.cleanup()
// initialize the channel data for this client
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
}
c.cs.channel.Channel = codePhrase[:3]
c.cs.channel.passPhrase = codePhrase[3:]
} else {
// TODO
if role == 0 {
// generate code phrase
codePhrase = getRandomName()
dash := strings.Index(codePhrase, "-")
c.cs.channel.Channel = codePhrase[:dash]
c.cs.channel.passPhrase = codePhrase[dash:]
} else {
codePhrase = promptCodePhrase()
if len(codePhrase) < 4 {
err = errors.New("code phrase must be more than 4 characters")
c.cs.Unlock()
return
}
c.cs.channel.Channel = codePhrase[:3]
c.cs.channel.passPhrase = codePhrase[3:]
}
c.cs.channel.codePhrase = codePhrase
}
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()
if role == 0 {
// start peer discovery
go func() {
log.Debug("listening for local croc relay...")
go peerdiscovery.Discover(peerdiscovery.Settings{
Limit: 1,
TimeLimit: 600 * time.Second,
Delay: 50 * time.Millisecond,
Payload: []byte(codePhrase),
})
}()
if len(fname) == 0 {
err = errors.New("must include filename")
return
}
err = c.processFile(fname[0])
if err != nil {
return
}
}
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
// connect to the websocket
// TODO:
// use predefined host and HTTPS, if exists
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)