mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
rpc: improve module availability check
This commit is contained in:
parent
7296f49b3f
commit
311ac0d0f7
1 changed files with 25 additions and 35 deletions
|
|
@ -23,43 +23,36 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// checkModuleAvailable check that requested api modules (eg. via --rpcapi) are actually
|
// checkModuleAvailability check that all names given in modules are actually
|
||||||
// available API services. If an invalid module is given (ie API "foo" wanted which does not exist),
|
// available API services.
|
||||||
// then an error is returned including the invalid module and a list of available
|
func checkModuleAvailability(modules []string, apis []API) error {
|
||||||
// API service names.
|
available := make(map[string]struct{})
|
||||||
func checkModuleAvailable(module string, apis []API) (err error) {
|
var availableNames string
|
||||||
for _, api := range apis {
|
for i, api := range apis {
|
||||||
if module == api.Namespace {
|
if _, ok := available[api.Namespace]; !ok {
|
||||||
return nil
|
available[api.Namespace] = struct{}{}
|
||||||
}
|
if i > 0 {
|
||||||
}
|
availableNames += ", "
|
||||||
// Module did not find a matching api namespace: this is an invalid module.
|
|
||||||
// Collect list of available modules for user debugging.
|
|
||||||
available := []string{}
|
|
||||||
outer:
|
|
||||||
for _, api := range apis {
|
|
||||||
|
|
||||||
// Only include unique api names
|
|
||||||
for _, av := range available {
|
|
||||||
if av == api.Namespace {
|
|
||||||
continue outer
|
|
||||||
}
|
}
|
||||||
|
availableNames += api.Namespace
|
||||||
}
|
}
|
||||||
available = append(available, api.Namespace)
|
|
||||||
}
|
}
|
||||||
return fmt.Errorf("invalid api module: module=%s available=%v", module, available)
|
for _, name := range modules {
|
||||||
|
if _, ok := available[name]; !ok {
|
||||||
|
return fmt.Errorf("invalid API %q in whitelist (available: %s)", name, availableNames)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules
|
// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules.
|
||||||
func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) {
|
func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) {
|
||||||
|
if err := checkModuleAvailability(modules, apis); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
// 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 {
|
||||||
|
|
||||||
// Ensure the requested module is actually available.
|
|
||||||
if err := checkModuleAvailable(module, apis); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
whitelist[module] = true
|
whitelist[module] = true
|
||||||
}
|
}
|
||||||
// Register all the APIs exposed by the services
|
// Register all the APIs exposed by the services
|
||||||
|
|
@ -84,17 +77,14 @@ func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []str
|
||||||
return listener, handler, err
|
return listener, handler, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartWSEndpoint starts a websocket endpoint
|
// StartWSEndpoint starts a websocket endpoint.
|
||||||
func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) {
|
func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) {
|
||||||
|
if err := checkModuleAvailability(modules, apis); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
// 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 {
|
||||||
|
|
||||||
// Ensure the requested module is actually available.
|
|
||||||
if err := checkModuleAvailable(module, apis); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
whitelist[module] = true
|
whitelist[module] = true
|
||||||
}
|
}
|
||||||
// Register all the APIs exposed by the services
|
// Register all the APIs exposed by the services
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue