add no-local

This commit is contained in:
Zack Scholl 2018-07-01 13:43:15 -07:00
parent 51f3de95a7
commit 2e545e6594
4 changed files with 40 additions and 23 deletions

View File

@ -69,6 +69,7 @@ func main() {
app.Flags = []cli.Flag{
cli.StringFlag{Name: "relay", Value: "ws://croc3.schollz.com"},
cli.StringFlag{Name: "code, c", Usage: "codephrase used to connect to relay"},
cli.BoolFlag{Name: "no-local", Usage: "disable local mode"},
cli.BoolFlag{Name: "local", Usage: "use only local mode"},
cli.BoolFlag{Name: "debug", Usage: "increase verbosity (a lot)"},
cli.BoolFlag{Name: "yes", Usage: "automatically agree to all prompts"},
@ -91,6 +92,7 @@ func main() {
cr.Yes = c.GlobalBool("yes")
cr.Stdout = c.GlobalBool("stdout")
cr.LocalOnly = c.GlobalBool("local")
cr.NoLocal = c.GlobalBool("no-local")
return nil
}

View File

@ -48,6 +48,9 @@ func (c *Croc) Send(fname string, codePhrase string) (err error) {
// start peer discovery
go func() {
if c.NoLocal {
return
}
log.Debug("listening for local croc relay...")
go peerdiscovery.Discover(peerdiscovery.Settings{
Limit: 1,
@ -73,6 +76,9 @@ func (c *Croc) Send(fname string, codePhrase string) (err error) {
}
runClientError := make(chan runInfo, 2)
go func() {
if c.NoLocal {
return
}
d := Init()
d.ServerPort = "8140"
d.TcpPorts = []string{"27140", "27141"}
@ -99,14 +105,18 @@ func (c *Croc) Send(fname string, codePhrase string) (err error) {
// start main client
go func() {
if c.LocalOnly {
return
}
var ri runInfo
ri.err = c.client(0, channel)
ri.bothConnected = c.bothConnected
runClientError <- ri
}()
var ri runInfo
ri = <-runClientError
if ri.bothConnected {
if ri.bothConnected || c.LocalOnly || c.NoLocal {
return ri.err
}
ri = <-runClientError
@ -115,29 +125,31 @@ 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) {
// 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+":27140", 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("but could not connect to ports")
if !c.NoLocal {
// 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+":27140", 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("but could not connect to ports")
}
} else {
log.Debug("discovered no peers")
}
} else {
log.Debug("discovered no peers")
}
// prepare codephrase

View File

@ -127,6 +127,7 @@ func (c *Croc) client(role int, channel string) (err error) {
}(&wg)
wg.Wait()
log.Debug("waiting for unlock")
c.cs.Lock()
if c.cs.channel.finishedHappy {
log.Info("file recieved!")
@ -143,6 +144,7 @@ func (c *Croc) client(role int, channel string) (err error) {
}
}
c.cs.Unlock()
log.Debug("returning")
return
}

View File

@ -29,6 +29,7 @@ type Croc struct {
WebsocketAddress string
Timeout time.Duration
LocalOnly bool
NoLocal bool
// Options for file transfering
UseEncryption bool