ethstats: use github.com/gorilla/websocket

This commit is contained in:
Felix Lange 2019-07-19 16:54:59 +02:00
parent 80f3a0647f
commit 0ef114123c

View file

@ -23,7 +23,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"net" "net/http"
"regexp" "regexp"
"runtime" "runtime"
"strconv" "strconv"
@ -41,7 +41,7 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"golang.org/x/net/websocket" "github.com/gorilla/websocket"
) )
const ( const (
@ -200,21 +200,21 @@ func (s *Service) loop() {
path := fmt.Sprintf("%s/api", s.host) path := fmt.Sprintf("%s/api", s.host)
urls := []string{path} urls := []string{path}
if !strings.Contains(path, "://") { // url.Parse and url.IsAbs is unsuitable (https://github.com/golang/go/issues/19779) // url.Parse and url.IsAbs is unsuitable (https://github.com/golang/go/issues/19779)
if !strings.Contains(path, "://") {
urls = []string{"wss://" + path, "ws://" + path} urls = []string{"wss://" + path, "ws://" + path}
} }
// Establish a websocket connection to the server on any supported URL // Establish a websocket connection to the server on any supported URL
var ( var (
conf *websocket.Config
conn *websocket.Conn conn *websocket.Conn
err error err error
) )
dialer := websocket.Dialer{HandshakeTimeout: 5 * time.Second}
header := make(http.Header)
header.Set("origin", "http://localhost")
for _, url := range urls { for _, url := range urls {
if conf, err = websocket.NewConfig(url, "http://localhost/"); err != nil { conn, _, err = dialer.Dial(url, header)
continue if err == nil {
}
conf.Dialer = &net.Dialer{Timeout: 5 * time.Second}
if conn, err = websocket.DialConfig(conf); err == nil {
break break
} }
} }
@ -284,7 +284,7 @@ func (s *Service) readLoop(conn *websocket.Conn) {
for { for {
// Retrieve the next generic network packet and bail out on error // Retrieve the next generic network packet and bail out on error
var msg map[string][]interface{} var msg map[string][]interface{}
if err := websocket.JSON.Receive(conn, &msg); err != nil { if err := conn.ReadJSON(&msg); err != nil {
log.Warn("Failed to decode stats server message", "err", err) log.Warn("Failed to decode stats server message", "err", err)
return return
} }
@ -399,12 +399,12 @@ func (s *Service) login(conn *websocket.Conn) error {
login := map[string][]interface{}{ login := map[string][]interface{}{
"emit": {"hello", auth}, "emit": {"hello", auth},
} }
if err := websocket.JSON.Send(conn, login); err != nil { if err := conn.WriteJSON(login); err != nil {
return err return err
} }
// Retrieve the remote ack or connection termination // Retrieve the remote ack or connection termination
var ack map[string][]string var ack map[string][]string
if err := websocket.JSON.Receive(conn, &ack); err != nil || len(ack["emit"]) != 1 || ack["emit"][0] != "ready" { if err := conn.ReadJSON(&ack); err != nil || len(ack["emit"]) != 1 || ack["emit"][0] != "ready" {
return errors.New("unauthorized") return errors.New("unauthorized")
} }
return nil return nil
@ -441,7 +441,7 @@ func (s *Service) reportLatency(conn *websocket.Conn) error {
"clientTime": start.String(), "clientTime": start.String(),
}}, }},
} }
if err := websocket.JSON.Send(conn, ping); err != nil { if err := conn.WriteJSON(ping); err != nil {
return err return err
} }
// Wait for the pong request to arrive back // Wait for the pong request to arrive back
@ -463,7 +463,7 @@ func (s *Service) reportLatency(conn *websocket.Conn) error {
"latency": latency, "latency": latency,
}}, }},
} }
return websocket.JSON.Send(conn, stats) return conn.WriteJSON(stats)
} }
// blockStats is the information to report about individual blocks. // blockStats is the information to report about individual blocks.
@ -514,7 +514,7 @@ func (s *Service) reportBlock(conn *websocket.Conn, block *types.Block) error {
report := map[string][]interface{}{ report := map[string][]interface{}{
"emit": {"block", stats}, "emit": {"block", stats},
} }
return websocket.JSON.Send(conn, report) return conn.WriteJSON(report)
} }
// assembleBlockStats retrieves any required metadata to report a single block // assembleBlockStats retrieves any required metadata to report a single block
@ -628,7 +628,7 @@ func (s *Service) reportHistory(conn *websocket.Conn, list []uint64) error {
report := map[string][]interface{}{ report := map[string][]interface{}{
"emit": {"history", stats}, "emit": {"history", stats},
} }
return websocket.JSON.Send(conn, report) return conn.WriteJSON(report)
} }
// pendStats is the information to report about pending transactions. // pendStats is the information to report about pending transactions.
@ -658,7 +658,7 @@ func (s *Service) reportPending(conn *websocket.Conn) error {
report := map[string][]interface{}{ report := map[string][]interface{}{
"emit": {"pending", stats}, "emit": {"pending", stats},
} }
return websocket.JSON.Send(conn, report) return conn.WriteJSON(report)
} }
// nodeStats is the information to report about the local node. // nodeStats is the information to report about the local node.
@ -713,5 +713,5 @@ func (s *Service) reportStats(conn *websocket.Conn) error {
report := map[string][]interface{}{ report := map[string][]interface{}{
"emit": {"stats", stats}, "emit": {"stats", stats},
} }
return websocket.JSON.Send(conn, report) return conn.WriteJSON(report)
} }