From 1f64d1c93a7c7d3c4ccadad9af59873854d6d283 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sat, 22 Sep 2018 10:43:52 -0700 Subject: [PATCH] check if discovered is valid --- src/croc/sending.go | 17 ++++++++++++++++- src/relay/relay.go | 4 ++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/croc/sending.go b/src/croc/sending.go index afdaaa4..be298e9 100644 --- a/src/croc/sending.go +++ b/src/croc/sending.go @@ -3,6 +3,7 @@ package croc import ( "errors" "fmt" + "net/http" "os" "os/signal" "time" @@ -85,7 +86,20 @@ func (c *Croc) Receive(codephrase string) (err error) { } if len(discovered) > 0 { log.Debugf("discovered %s:%s", discovered[0].Address, discovered[0].Payload) - return c.sendReceive(fmt.Sprintf("ws://%s:%s", discovered[0].Address, discovered[0].Payload), "", codephrase, false) + // see if we can actually connect to it + timeout := time.Duration(200 * time.Millisecond) + client := http.Client{ + Timeout: timeout, + } + resp, err := client.Get(fmt.Sprintf("http://%s:%s/", discovered[0].Address, discovered[0].Payload)) + if err == nil { + if resp.StatusCode == http.StatusOK { + // we connected, so use this + return c.sendReceive(fmt.Sprintf("ws://%s:%s", discovered[0].Address, discovered[0].Payload), "", codephrase, false) + } + } else { + log.Debugf("could not connect: %s", err.Error()) + } } else { log.Debug("discovered no peers") } @@ -93,6 +107,7 @@ func (c *Croc) Receive(codephrase string) (err error) { // use public relay if !c.LocalOnly { + log.Debug("using public relay") return c.sendReceive(c.WebsocketAddress, "", codephrase, false) } diff --git a/src/relay/relay.go b/src/relay/relay.go index c8ad2f9..19830c0 100644 --- a/src/relay/relay.go +++ b/src/relay/relay.go @@ -1,6 +1,7 @@ package relay import ( + "fmt" "net/http" log "github.com/cihub/seelog" @@ -18,6 +19,9 @@ func Run(port string) (err error) { http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { serveWs(w, r) }) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "ok") + }) err = http.ListenAndServe(":"+port, nil) return }