From 82771dd1e492a203f4c075d99306541fc13e8309 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 14 Nov 2019 09:05:17 +0100 Subject: [PATCH] cmd/faucet: use github.com/gorilla/websocket (#20283) golang.org/x/net/websocket is unmaintained, and we have already switched to using github.com/gorilla/websocket for package rpc. --- cmd/faucet/faucet.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/cmd/faucet/faucet.go b/cmd/faucet/faucet.go index 905eeb24a74c..3700ff9c59be 100644 --- a/cmd/faucet/faucet.go +++ b/cmd/faucet/faucet.go @@ -58,7 +58,7 @@ import ( "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/nat" "github.com/ethereum/go-ethereum/params" - "golang.org/x/net/websocket" + "github.com/gorilla/websocket" ) var ( @@ -296,8 +296,7 @@ func (f *faucet) listenAndServe(port int) error { go f.loop() http.HandleFunc("/", f.webHandler) - http.Handle("/api", websocket.Handler(f.apiHandler)) - + http.HandleFunc("/api", f.apiHandler) return http.ListenAndServe(fmt.Sprintf(":%d", port), nil) } @@ -308,7 +307,13 @@ func (f *faucet) webHandler(w http.ResponseWriter, r *http.Request) { } // apiHandler handles requests for Ether grants and transaction statuses. -func (f *faucet) apiHandler(conn *websocket.Conn) { +func (f *faucet) apiHandler(w http.ResponseWriter, r *http.Request) { + upgrader := websocket.Upgrader{} + conn, err := upgrader.Upgrade(w, r, nil) + if err != nil { + return + } + // Start tracking the connection and drop at the end defer conn.Close() @@ -331,7 +336,6 @@ func (f *faucet) apiHandler(conn *websocket.Conn) { head *types.Header balance *big.Int nonce uint64 - err error ) for head == nil || balance == nil { // Retrieve the current stats cached by the faucet @@ -376,7 +380,7 @@ func (f *faucet) apiHandler(conn *websocket.Conn) { Tier uint `json:"tier"` Captcha string `json:"captcha"` } - if err = websocket.JSON.Receive(conn, &msg); err != nil { + if err = conn.ReadJSON(&msg); err != nil { return } if !*noauthFlag && !strings.HasPrefix(msg.URL, "https://gist.github.com/") && !strings.HasPrefix(msg.URL, "https://twitter.com/") && @@ -657,7 +661,7 @@ func send(conn *websocket.Conn, value interface{}, timeout time.Duration) error timeout = 60 * time.Second } conn.SetWriteDeadline(time.Now().Add(timeout)) - return websocket.JSON.Send(conn, value) + return conn.WriteJSON(value) } // sendError transmits an error to the remote end of the websocket, also setting