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.
This commit is contained in:
parent
9504c5c360
commit
987648b0ad
@ -58,7 +58,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/p2p/nat"
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"golang.org/x/net/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -296,8 +296,7 @@ func (f *faucet) listenAndServe(port int) error {
|
|||||||
go f.loop()
|
go f.loop()
|
||||||
|
|
||||||
http.HandleFunc("/", f.webHandler)
|
http.HandleFunc("/", f.webHandler)
|
||||||
http.Handle("/api", websocket.Handler(f.apiHandler))
|
http.HandleFunc("/api", f.apiHandler)
|
||||||
|
|
||||||
return http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
|
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.
|
// 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
|
// Start tracking the connection and drop at the end
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
@ -331,7 +336,6 @@ func (f *faucet) apiHandler(conn *websocket.Conn) {
|
|||||||
head *types.Header
|
head *types.Header
|
||||||
balance *big.Int
|
balance *big.Int
|
||||||
nonce uint64
|
nonce uint64
|
||||||
err error
|
|
||||||
)
|
)
|
||||||
for head == nil || balance == nil {
|
for head == nil || balance == nil {
|
||||||
// Retrieve the current stats cached by the faucet
|
// Retrieve the current stats cached by the faucet
|
||||||
@ -376,7 +380,7 @@ func (f *faucet) apiHandler(conn *websocket.Conn) {
|
|||||||
Tier uint `json:"tier"`
|
Tier uint `json:"tier"`
|
||||||
Captcha string `json:"captcha"`
|
Captcha string `json:"captcha"`
|
||||||
}
|
}
|
||||||
if err = websocket.JSON.Receive(conn, &msg); err != nil {
|
if err = conn.ReadJSON(&msg); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !*noauthFlag && !strings.HasPrefix(msg.URL, "https://gist.github.com/") && !strings.HasPrefix(msg.URL, "https://twitter.com/") &&
|
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
|
timeout = 60 * time.Second
|
||||||
}
|
}
|
||||||
conn.SetWriteDeadline(time.Now().Add(timeout))
|
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
|
// sendError transmits an error to the remote end of the websocket, also setting
|
||||||
|
Loading…
Reference in New Issue
Block a user