1
0
mirror of https://github.com/schollz/croc.git synced 2024-11-24 08:02:33 +03:00

add peer discovery

This commit is contained in:
Zack Scholl 2018-06-30 10:56:42 -07:00
parent aa0c5acc60
commit f7c22067d4
2 changed files with 50 additions and 0 deletions

View File

@ -1,5 +1,13 @@
package croc
import (
"net"
"time"
log "github.com/cihub/seelog"
"github.com/schollz/peerdiscovery"
)
func init() {
SetLogLevel("debug")
}
@ -15,10 +23,40 @@ 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) {
// start relay for listening
c.TcpPorts = []string{"27140,27141"}
c.ServerPort = "8140"
go c.Relay()
// start client
return c.client(0, codephrase, fname)
}
// Receive will receive something through the croc relay
func (c *Croc) Receive(codephrase string) (err error) {
// 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),
})
if errDiscover != nil {
log.Debug(errDiscover)
}
if len(discovered) > 0 {
log.Debugf("discovered %s on %s", discovered[0].Payload, discovered[0].Address)
_, connectTimeout := net.DialTimeout("tcp", discovered[0].Address+":27001", 1*time.Second)
if connectTimeout == nil {
log.Debug("connected")
c.WebsocketAddress = "ws://" + discovered[0].Address + ":8140"
log.Debug(discovered[0].Address)
codephrase = string(discovered[0].Payload)
}
} else {
log.Debug("discovered no peers")
}
return c.client(1, codephrase)
}

View File

@ -17,6 +17,7 @@ import (
"github.com/gorilla/websocket"
"github.com/pkg/errors"
"github.com/schollz/croc/src/pake"
"github.com/schollz/peerdiscovery"
"github.com/schollz/progressbar"
)
@ -61,6 +62,17 @@ func (c *Croc) client(role int, codePhrase string, fname ...string) (err error)
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