aboutsummaryrefslogtreecommitdiffstats
path: root/ethstats
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2017-03-20 18:02:39 +0800
committerPéter Szilágyi <peterke@gmail.com>2017-03-20 18:45:43 +0800
commit2ba9374789c622a2d6ff799219a5e21dc5a0507c (patch)
tree8cd89d9ed5ce5e5a759eb2f03ec7c71a822408cd /ethstats
parentdc4c59d42b793414833c10b91c2902e971de8477 (diff)
downloaddexon-2ba9374789c622a2d6ff799219a5e21dc5a0507c.tar
dexon-2ba9374789c622a2d6ff799219a5e21dc5a0507c.tar.gz
dexon-2ba9374789c622a2d6ff799219a5e21dc5a0507c.tar.bz2
dexon-2ba9374789c622a2d6ff799219a5e21dc5a0507c.tar.lz
dexon-2ba9374789c622a2d6ff799219a5e21dc5a0507c.tar.xz
dexon-2ba9374789c622a2d6ff799219a5e21dc5a0507c.tar.zst
dexon-2ba9374789c622a2d6ff799219a5e21dc5a0507c.zip
ethstats: try both ws:// and wss:// if none specified
Diffstat (limited to 'ethstats')
-rw-r--r--ethstats/ethstats.go38
1 files changed, 28 insertions, 10 deletions
diff --git a/ethstats/ethstats.go b/ethstats/ethstats.go
index 7b3a3439e..4d38d3a50 100644
--- a/ethstats/ethstats.go
+++ b/ethstats/ethstats.go
@@ -22,10 +22,11 @@ import (
"errors"
"fmt"
"math/big"
+ "net"
+ "net/url"
"regexp"
"runtime"
"strconv"
- "strings"
"time"
"github.com/ethereum/go-ethereum/common"
@@ -123,17 +124,34 @@ func (s *Service) loop() {
// Loop reporting until termination
for {
- // Establish a websocket connection to the server and authenticate the node
- url := fmt.Sprintf("%s/api", s.host)
- if !strings.Contains(url, "://") {
- url = "wss://" + url
+ // Resolve the URL, defaulting to TLS, but falling back to none too
+ path := fmt.Sprintf("%s/api", s.host)
+ urls := []string{path}
+
+ if parsed, err := url.Parse(path); err == nil && !parsed.IsAbs() {
+ urls = []string{"wss://" + path, "ws://" + path}
+ }
+ // Establish a websocket connection to the server on any supported URL
+ var (
+ conf *websocket.Config
+ conn *websocket.Conn
+ err error
+ )
+ for _, url := range urls {
+ if conf, err = websocket.NewConfig(url, "http://localhost/"); err != nil {
+ continue
+ }
+ conf.Dialer = &net.Dialer{Timeout: 3 * time.Second}
+ if conn, err = websocket.DialConfig(conf); err == nil {
+ break
+ }
}
- conn, err := websocket.Dial(url, "", "http://localhost/")
if err != nil {
log.Warn("Stats server unreachable", "err", err)
time.Sleep(10 * time.Second)
continue
}
+ // Authenticate the client with the server
in := json.NewDecoder(conn)
out := json.NewEncoder(conn)
@@ -244,12 +262,12 @@ func (s *Service) readLoop(conn *websocket.Conn, in *json.Decoder) {
// Make sure the request is valid and doesn't crash us
request, ok := msg["emit"][1].(map[string]interface{})
if !ok {
- log.Warn("Invalid history request", "msg", msg["emit"][1])
- return
+ log.Warn("Invalid stats history request", "msg", msg["emit"][1])
+ continue // Ethstats sometime sends invalid history requests, ignore those
}
list, ok := request["list"].([]interface{})
if !ok {
- log.Warn("Invalid history block list", "list", request["list"])
+ log.Warn("Invalid stats history block list", "list", request["list"])
return
}
// Convert the block number list to an integer list
@@ -257,7 +275,7 @@ func (s *Service) readLoop(conn *websocket.Conn, in *json.Decoder) {
for i, num := range list {
n, ok := num.(float64)
if !ok {
- log.Warn("Invalid history block number", "number", num)
+ log.Warn("Invalid stats history block number", "number", num)
return
}
numbers[i] = uint64(n)