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"
"fmt"
"math/big"
"net"
"net/http"
"regexp"
"runtime"
"strconv"
@ -41,7 +41,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
"golang.org/x/net/websocket"
"github.com/gorilla/websocket"
)
const (
@ -200,21 +200,21 @@ func (s *Service) loop() {
path := fmt.Sprintf("%s/api", s.host)
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}
}
// Establish a websocket connection to the server on any supported URL
var (
conf *websocket.Config
conn *websocket.Conn
err error
)
dialer := websocket.Dialer{HandshakeTimeout: 5 * time.Second}
header := make(http.Header)
header.Set("origin", "http://localhost")
for _, url := range urls {
if conf, err = websocket.NewConfig(url, "http://localhost/"); err != nil {
continue
}
conf.Dialer = &net.Dialer{Timeout: 5 * time.Second}
if conn, err = websocket.DialConfig(conf); err == nil {
conn, _, err = dialer.Dial(url, header)
if err == nil {
break
}
}
@ -284,7 +284,7 @@ func (s *Service) readLoop(conn *websocket.Conn) {
for {
// Retrieve the next generic network packet and bail out on error
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)
return
}
@ -399,12 +399,12 @@ func (s *Service) login(conn *websocket.Conn) error {
login := map[string][]interface{}{
"emit": {"hello", auth},
}
if err := websocket.JSON.Send(conn, login); err != nil {
if err := conn.WriteJSON(login); err != nil {
return err
}
// Retrieve the remote ack or connection termination
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 nil
@ -441,7 +441,7 @@ func (s *Service) reportLatency(conn *websocket.Conn) error {
"clientTime": start.String(),
}},
}
if err := websocket.JSON.Send(conn, ping); err != nil {
if err := conn.WriteJSON(ping); err != nil {
return err
}
// Wait for the pong request to arrive back
@ -463,7 +463,7 @@ func (s *Service) reportLatency(conn *websocket.Conn) error {
"latency": latency,
}},
}
return websocket.JSON.Send(conn, stats)
return conn.WriteJSON(stats)
}
// 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{}{
"emit": {"block", stats},
}
return websocket.JSON.Send(conn, report)
return conn.WriteJSON(report)
}
// 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{}{
"emit": {"history", stats},
}
return websocket.JSON.Send(conn, report)
return conn.WriteJSON(report)
}
// 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{}{
"emit": {"pending", stats},
}
return websocket.JSON.Send(conn, report)
return conn.WriteJSON(report)
}
// 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{}{
"emit": {"stats", stats},
}
return websocket.JSON.Send(conn, report)
return conn.WriteJSON(report)
}