1
0
mirror of https://github.com/schollz/croc.git synced 2024-12-11 06:35:01 +03:00

check if discovered is valid

This commit is contained in:
Zack Scholl 2018-09-22 10:43:52 -07:00
parent edfb018ae2
commit 1f64d1c93a
2 changed files with 20 additions and 1 deletions

View File

@ -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)
}

View File

@ -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
}