mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
move handler creation to pkg node, remove deprecated function
This commit is contained in:
parent
631488c081
commit
339ba89ed2
5 changed files with 202 additions and 177 deletions
|
|
@ -891,8 +891,8 @@ func retesteth(ctx *cli.Context) error {
|
||||||
|
|
||||||
srv := rpc.NewServer()
|
srv := rpc.NewServer()
|
||||||
|
|
||||||
handler := rpc.NewHTTPHandlerStack(srv, cors, vhosts)
|
handler := node.NewHTTPHandlerStack(srv, cors, vhosts)
|
||||||
handler = rpc.NewWebsocketUpgradeHandler(handler, srv.WebsocketHandler(wsOrigins))
|
handler = node.NewWebsocketUpgradeHandler(handler, srv.WebsocketHandler(wsOrigins))
|
||||||
|
|
||||||
// start http server
|
// start http server
|
||||||
var RetestethHTTPTimeouts = rpc.HTTPTimeouts{
|
var RetestethHTTPTimeouts = rpc.HTTPTimeouts{
|
||||||
|
|
|
||||||
|
|
@ -372,7 +372,7 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
|
||||||
|
|
||||||
srv := rpc.NewServer()
|
srv := rpc.NewServer()
|
||||||
|
|
||||||
err := RegisterApisFromWhitelist(apis, modules, srv)
|
err := registerApisFromWhitelist(apis, modules, srv)
|
||||||
|
|
||||||
var ws http.Handler
|
var ws http.Handler
|
||||||
if n.httpEndpoint == n.wsEndpoint {
|
if n.httpEndpoint == n.wsEndpoint {
|
||||||
|
|
@ -380,7 +380,7 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
|
||||||
}
|
}
|
||||||
|
|
||||||
// wrap handler in websocket handler only if websocket port is the same as http rpc
|
// wrap handler in websocket handler only if websocket port is the same as http rpc
|
||||||
handler := n.AddWebsocketHandler(rpc.NewHTTPHandlerStack(srv, cors, vhosts), ws)
|
handler := n.AddWebsocketHandler(NewHTTPHandlerStack(srv, cors, vhosts), ws)
|
||||||
|
|
||||||
listener, err := rpc.StartHTTPEndpoint(endpoint, apis, modules, timeouts, handler)
|
listener, err := rpc.StartHTTPEndpoint(endpoint, apis, modules, timeouts, handler)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -405,7 +405,7 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
|
||||||
// AddWebsocketHandler creates the handler stack necessary to handle both http rpc requests and websocket requests
|
// AddWebsocketHandler creates the handler stack necessary to handle both http rpc requests and websocket requests
|
||||||
func (n *Node) AddWebsocketHandler(handler http.Handler, websocket http.Handler) http.Handler {
|
func (n *Node) AddWebsocketHandler(handler http.Handler, websocket http.Handler) http.Handler {
|
||||||
if websocket != nil {
|
if websocket != nil {
|
||||||
return rpc.NewWebsocketUpgradeHandler(handler, websocket)
|
return NewWebsocketUpgradeHandler(handler, websocket)
|
||||||
}
|
}
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
|
@ -697,7 +697,7 @@ func (n *Node) apis() []rpc.API {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterApisFromWhitelist(apis []rpc.API, modules []string, srv *rpc.Server) error {
|
func registerApisFromWhitelist(apis []rpc.API, modules []string, srv *rpc.Server) error {
|
||||||
// Generate the whitelist based on the allowed modules
|
// Generate the whitelist based on the allowed modules
|
||||||
whitelist := make(map[string]bool)
|
whitelist := make(map[string]bool)
|
||||||
for _, module := range modules {
|
for _, module := range modules {
|
||||||
|
|
|
||||||
157
node/rpcstack.go
Normal file
157
node/rpcstack.go
Normal file
|
|
@ -0,0 +1,157 @@
|
||||||
|
// Copyright 2015 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package node
|
||||||
|
|
||||||
|
import (
|
||||||
|
"compress/gzip"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/log"
|
||||||
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
"github.com/rs/cors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewHTTPHandlerStack returns wrapped http-related handlers
|
||||||
|
func NewHTTPHandlerStack(srv *rpc.Server, cors []string, vhosts []string) http.Handler {
|
||||||
|
// Wrap the CORS-handler within a host-handler
|
||||||
|
handler := newCorsHandler(srv, cors)
|
||||||
|
handler = newVHostHandler(vhosts, handler)
|
||||||
|
return newGzipHandler(handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newCorsHandler(srv http.Handler, allowedOrigins []string) http.Handler {
|
||||||
|
// disable CORS support if user has not specified a custom CORS configuration
|
||||||
|
if len(allowedOrigins) == 0 {
|
||||||
|
return srv
|
||||||
|
}
|
||||||
|
c := cors.New(cors.Options{
|
||||||
|
AllowedOrigins: allowedOrigins,
|
||||||
|
AllowedMethods: []string{http.MethodPost, http.MethodGet},
|
||||||
|
MaxAge: 600,
|
||||||
|
AllowedHeaders: []string{"*"},
|
||||||
|
})
|
||||||
|
return c.Handler(srv)
|
||||||
|
}
|
||||||
|
|
||||||
|
// virtualHostHandler is a handler which validates the Host-header of incoming requests.
|
||||||
|
// The virtualHostHandler can prevent DNS rebinding attacks, which do not utilize CORS-headers,
|
||||||
|
// since they do in-domain requests against the RPC api. Instead, we can see on the Host-header
|
||||||
|
// which domain was used, and validate that against a whitelist.
|
||||||
|
type virtualHostHandler struct {
|
||||||
|
vhosts map[string]struct{}
|
||||||
|
next http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func newVHostHandler(vhosts []string, next http.Handler) http.Handler {
|
||||||
|
vhostMap := make(map[string]struct{})
|
||||||
|
for _, allowedHost := range vhosts {
|
||||||
|
vhostMap[strings.ToLower(allowedHost)] = struct{}{}
|
||||||
|
}
|
||||||
|
return &virtualHostHandler{vhostMap, next}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP serves JSON-RPC requests over HTTP, implements http.Handler
|
||||||
|
func (h *virtualHostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// if r.Host is not set, we can continue serving since a browser would set the Host header
|
||||||
|
if r.Host == "" {
|
||||||
|
h.next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
host, _, err := net.SplitHostPort(r.Host)
|
||||||
|
if err != nil {
|
||||||
|
// Either invalid (too many colons) or no port specified
|
||||||
|
host = r.Host
|
||||||
|
}
|
||||||
|
if ipAddr := net.ParseIP(host); ipAddr != nil {
|
||||||
|
// It's an IP address, we can serve that
|
||||||
|
h.next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
|
||||||
|
}
|
||||||
|
// Not an IP address, but a hostname. Need to validate
|
||||||
|
if _, exist := h.vhosts["*"]; exist {
|
||||||
|
h.next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, exist := h.vhosts[host]; exist {
|
||||||
|
h.next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
http.Error(w, "invalid host specified", http.StatusForbidden)
|
||||||
|
}
|
||||||
|
|
||||||
|
var gzPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
w := gzip.NewWriter(ioutil.Discard)
|
||||||
|
return w
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
type gzipResponseWriter struct {
|
||||||
|
io.Writer
|
||||||
|
http.ResponseWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *gzipResponseWriter) WriteHeader(status int) {
|
||||||
|
w.Header().Del("Content-Length")
|
||||||
|
w.ResponseWriter.WriteHeader(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
|
||||||
|
return w.Writer.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newGzipHandler(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Encoding", "gzip")
|
||||||
|
|
||||||
|
gz := gzPool.Get().(*gzip.Writer)
|
||||||
|
defer gzPool.Put(gz)
|
||||||
|
|
||||||
|
gz.Reset(w)
|
||||||
|
defer gz.Close()
|
||||||
|
|
||||||
|
next.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWebsocketUpgradeHandler(h http.Handler, ws http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if isWebsocket(r) {
|
||||||
|
ws.ServeHTTP(w, r)
|
||||||
|
log.Debug("serving websocket request")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func isWebsocket(r *http.Request) bool {
|
||||||
|
return strings.ToLower(r.Header.Get("Upgrade")) == "websocket" &&
|
||||||
|
strings.ToLower(r.Header.Get("Connection")) == "upgrade"
|
||||||
|
}
|
||||||
87
rpc/gzip.go
87
rpc/gzip.go
|
|
@ -16,51 +16,42 @@
|
||||||
|
|
||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
//var gzPool = sync.Pool{
|
||||||
"compress/gzip"
|
// New: func() interface{} {
|
||||||
"io"
|
// w := gzip.NewWriter(ioutil.Discard)
|
||||||
"io/ioutil"
|
// return w
|
||||||
"net/http"
|
// },
|
||||||
"strings"
|
//}
|
||||||
"sync"
|
//
|
||||||
)
|
//type gzipResponseWriter struct {
|
||||||
|
// io.Writer
|
||||||
var gzPool = sync.Pool{
|
// http.ResponseWriter
|
||||||
New: func() interface{} {
|
//}
|
||||||
w := gzip.NewWriter(ioutil.Discard)
|
//
|
||||||
return w
|
//func (w *gzipResponseWriter) WriteHeader(status int) {
|
||||||
},
|
// w.Header().Del("Content-Length")
|
||||||
}
|
// w.ResponseWriter.WriteHeader(status)
|
||||||
|
//}
|
||||||
type gzipResponseWriter struct {
|
//
|
||||||
io.Writer
|
//func (w *gzipResponseWriter) Write(b []byte) (int, error) {
|
||||||
http.ResponseWriter
|
// return w.Writer.Write(b)
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
func (w *gzipResponseWriter) WriteHeader(status int) {
|
//func newGzipHandler(next http.Handler) http.Handler {
|
||||||
w.Header().Del("Content-Length")
|
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.ResponseWriter.WriteHeader(status)
|
// if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||||
}
|
// next.ServeHTTP(w, r)
|
||||||
|
// return
|
||||||
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
|
// }
|
||||||
return w.Writer.Write(b)
|
//
|
||||||
}
|
// w.Header().Set("Content-Encoding", "gzip")
|
||||||
|
//
|
||||||
func newGzipHandler(next http.Handler) http.Handler {
|
// gz := gzPool.Get().(*gzip.Writer)
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
// defer gzPool.Put(gz)
|
||||||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
//
|
||||||
next.ServeHTTP(w, r)
|
// gz.Reset(w)
|
||||||
return
|
// defer gz.Close()
|
||||||
}
|
//
|
||||||
|
// next.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r)
|
||||||
w.Header().Set("Content-Encoding", "gzip")
|
// })
|
||||||
|
//}
|
||||||
gz := gzPool.Get().(*gzip.Writer)
|
|
||||||
defer gzPool.Put(gz)
|
|
||||||
|
|
||||||
gz.Reset(w)
|
|
||||||
defer gz.Close()
|
|
||||||
|
|
||||||
next.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
||||||
123
rpc/http.go
123
rpc/http.go
|
|
@ -25,14 +25,9 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"mime"
|
"mime"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
"github.com/rs/cors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -209,38 +204,6 @@ func (t *httpServerConn) RemoteAddr() string {
|
||||||
// SetWriteDeadline does nothing and always returns nil.
|
// SetWriteDeadline does nothing and always returns nil.
|
||||||
func (t *httpServerConn) SetWriteDeadline(time.Time) error { return nil }
|
func (t *httpServerConn) SetWriteDeadline(time.Time) error { return nil }
|
||||||
|
|
||||||
// NewHTTPServer creates a new HTTP RPC server around an API provider.
|
|
||||||
//
|
|
||||||
// Deprecated: Server implements http.Handler
|
|
||||||
func NewHTTPServer(cors []string, vhosts []string, timeouts HTTPTimeouts, srv http.Handler, ws http.Handler) *http.Server {
|
|
||||||
// Wrap the CORS-handler within a host-handler
|
|
||||||
handler := newCorsHandler(srv, cors)
|
|
||||||
handler = newVHostHandler(vhosts, handler)
|
|
||||||
handler = newGzipHandler(handler)
|
|
||||||
handler = NewWebsocketUpgradeHandler(handler, ws)
|
|
||||||
|
|
||||||
// Make sure timeout values are meaningful
|
|
||||||
if timeouts.ReadTimeout < time.Second {
|
|
||||||
log.Warn("Sanitizing invalid HTTP read timeout", "provided", timeouts.ReadTimeout, "updated", DefaultHTTPTimeouts.ReadTimeout)
|
|
||||||
timeouts.ReadTimeout = DefaultHTTPTimeouts.ReadTimeout
|
|
||||||
}
|
|
||||||
if timeouts.WriteTimeout < time.Second {
|
|
||||||
log.Warn("Sanitizing invalid HTTP write timeout", "provided", timeouts.WriteTimeout, "updated", DefaultHTTPTimeouts.WriteTimeout)
|
|
||||||
timeouts.WriteTimeout = DefaultHTTPTimeouts.WriteTimeout
|
|
||||||
}
|
|
||||||
if timeouts.IdleTimeout < time.Second {
|
|
||||||
log.Warn("Sanitizing invalid HTTP idle timeout", "provided", timeouts.IdleTimeout, "updated", DefaultHTTPTimeouts.IdleTimeout)
|
|
||||||
timeouts.IdleTimeout = DefaultHTTPTimeouts.IdleTimeout
|
|
||||||
}
|
|
||||||
// Bundle and start the HTTP server
|
|
||||||
return &http.Server{
|
|
||||||
Handler: handler,
|
|
||||||
ReadTimeout: timeouts.ReadTimeout,
|
|
||||||
WriteTimeout: timeouts.WriteTimeout,
|
|
||||||
IdleTimeout: timeouts.IdleTimeout,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServeHTTP serves JSON-RPC requests over HTTP.
|
// ServeHTTP serves JSON-RPC requests over HTTP.
|
||||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// Permit dumb empty requests for remote health-checks (AWS)
|
// Permit dumb empty requests for remote health-checks (AWS)
|
||||||
|
|
@ -297,89 +260,3 @@ func validateRequest(r *http.Request) (int, error) {
|
||||||
err := fmt.Errorf("invalid content type, only %s is supported", contentType)
|
err := fmt.Errorf("invalid content type, only %s is supported", contentType)
|
||||||
return http.StatusUnsupportedMediaType, err
|
return http.StatusUnsupportedMediaType, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHTTPHandlerStack returns wrapped http-related handlers
|
|
||||||
func NewHTTPHandlerStack(srv *Server, cors []string, vhosts []string) http.Handler {
|
|
||||||
// Wrap the CORS-handler within a host-handler
|
|
||||||
handler := newCorsHandler(srv, cors)
|
|
||||||
handler = newVHostHandler(vhosts, handler)
|
|
||||||
return newGzipHandler(handler)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newCorsHandler(srv http.Handler, allowedOrigins []string) http.Handler {
|
|
||||||
// disable CORS support if user has not specified a custom CORS configuration
|
|
||||||
if len(allowedOrigins) == 0 {
|
|
||||||
return srv
|
|
||||||
}
|
|
||||||
c := cors.New(cors.Options{
|
|
||||||
AllowedOrigins: allowedOrigins,
|
|
||||||
AllowedMethods: []string{http.MethodPost, http.MethodGet},
|
|
||||||
MaxAge: 600,
|
|
||||||
AllowedHeaders: []string{"*"},
|
|
||||||
})
|
|
||||||
return c.Handler(srv)
|
|
||||||
}
|
|
||||||
|
|
||||||
// virtualHostHandler is a handler which validates the Host-header of incoming requests.
|
|
||||||
// The virtualHostHandler can prevent DNS rebinding attacks, which do not utilize CORS-headers,
|
|
||||||
// since they do in-domain requests against the RPC api. Instead, we can see on the Host-header
|
|
||||||
// which domain was used, and validate that against a whitelist.
|
|
||||||
type virtualHostHandler struct {
|
|
||||||
vhosts map[string]struct{}
|
|
||||||
next http.Handler
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServeHTTP serves JSON-RPC requests over HTTP, implements http.Handler
|
|
||||||
func (h *virtualHostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// if r.Host is not set, we can continue serving since a browser would set the Host header
|
|
||||||
if r.Host == "" {
|
|
||||||
h.next.ServeHTTP(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
host, _, err := net.SplitHostPort(r.Host)
|
|
||||||
if err != nil {
|
|
||||||
// Either invalid (too many colons) or no port specified
|
|
||||||
host = r.Host
|
|
||||||
}
|
|
||||||
if ipAddr := net.ParseIP(host); ipAddr != nil {
|
|
||||||
// It's an IP address, we can serve that
|
|
||||||
h.next.ServeHTTP(w, r)
|
|
||||||
return
|
|
||||||
|
|
||||||
}
|
|
||||||
// Not an IP address, but a hostname. Need to validate
|
|
||||||
if _, exist := h.vhosts["*"]; exist {
|
|
||||||
h.next.ServeHTTP(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if _, exist := h.vhosts[host]; exist {
|
|
||||||
h.next.ServeHTTP(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
http.Error(w, "invalid host specified", http.StatusForbidden)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newVHostHandler(vhosts []string, next http.Handler) http.Handler {
|
|
||||||
vhostMap := make(map[string]struct{})
|
|
||||||
for _, allowedHost := range vhosts {
|
|
||||||
vhostMap[strings.ToLower(allowedHost)] = struct{}{}
|
|
||||||
}
|
|
||||||
return &virtualHostHandler{vhostMap, next}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewWebsocketUpgradeHandler(h http.Handler, ws http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if isWebsocket(r) {
|
|
||||||
ws.ServeHTTP(w, r)
|
|
||||||
log.Debug("serving websocket request")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
h.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func isWebsocket(r *http.Request) bool {
|
|
||||||
return strings.ToLower(r.Header.Get("Upgrade")) == "websocket" &&
|
|
||||||
strings.ToLower(r.Header.Get("Connection")) == "upgrade"
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue