mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
moved whitelist generation and api registration into separate method
This commit is contained in:
parent
fdff70c14c
commit
5eb0146774
1 changed files with 34 additions and 20 deletions
54
node/node.go
54
node/node.go
|
|
@ -372,24 +372,15 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
|
||||||
|
|
||||||
srv := rpc.NewServer()
|
srv := rpc.NewServer()
|
||||||
|
|
||||||
// Generate the whitelist based on the allowed modules
|
err := RegisterApisFromWhitelist(apis, modules, srv)
|
||||||
whitelist := make(map[string]bool)
|
|
||||||
for _, module := range modules {
|
var ws http.Handler
|
||||||
whitelist[module] = true
|
if n.httpEndpoint == n.wsEndpoint {
|
||||||
|
ws = srv.WebsocketHandler(wsOrigins)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register all the APIs exposed by the services
|
// wrap handler in websocket handler only if websocket port is the same as http rpc
|
||||||
for _, api := range apis {
|
handler := n.AddWebsocketHandler(rpc.NewHTTPHandlerStack(srv, cors, vhosts), ws)
|
||||||
if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
|
|
||||||
if err := srv.RegisterName(api.Namespace, api.Service); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.Debug("HTTP registered", "namespace", api.Namespace)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// create handler stack
|
|
||||||
handler := n.CreateHandler(srv, cors, vhosts, wsOrigins)
|
|
||||||
|
|
||||||
listener, err := rpc.StartHTTPEndpoint(endpoint, apis, modules, timeouts, handler)
|
listener, err := rpc.StartHTTPEndpoint(endpoint, apis, modules, timeouts, handler)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -411,10 +402,13 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateHandler 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) CreateHandler(srv *rpc.Server, cors []string, vhosts []string, wsOrigins []string) http.Handler {
|
func (n *Node) AddWebsocketHandler(handler http.Handler, websocket http.Handler) http.Handler {
|
||||||
handler := rpc.NewHTTPHandlerStack(srv, cors, vhosts)
|
if websocket != nil {
|
||||||
return rpc.NewWebsocketUpgradeHandler(handler, srv.WebsocketHandler(wsOrigins))
|
return rpc.NewWebsocketUpgradeHandler(handler, websocket)
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// stopHTTP terminates the HTTP RPC endpoint.
|
// stopHTTP terminates the HTTP RPC endpoint.
|
||||||
|
|
@ -702,3 +696,23 @@ func (n *Node) apis() []rpc.API {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RegisterApisFromWhitelist(apis []rpc.API, modules []string, srv *rpc.Server) error {
|
||||||
|
// Generate the whitelist based on the allowed modules
|
||||||
|
whitelist := make(map[string]bool)
|
||||||
|
for _, module := range modules {
|
||||||
|
whitelist[module] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register all the APIs exposed by the services
|
||||||
|
for _, api := range apis {
|
||||||
|
if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
|
||||||
|
if err := srv.RegisterName(api.Namespace, api.Service); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Debug("HTTP registered", "namespace", api.Namespace)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue