rpc: refactor to install module validity checker for WS server too

Signed-off-by: meows <b5c6@protonmail.com>
This commit is contained in:
meows 2019-12-18 08:59:07 -06:00
parent 0572ca3c61
commit ddaee90378
No known key found for this signature in database
GPG key ID: 5786AEA5C8D5A520

View file

@ -23,11 +23,11 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )
// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules // mustAvailableModule enforces that requested api modules (eg. via --rpcapi) are actually
func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) { // available API services. If an invalid module is given (ie API "foo" wanted which does not exist),
// Generate the whitelist based on the allowed modules // then log.Crit is used to cause program to exit, logging the invalid module and a list of available
whitelist := make(map[string]bool) // API service names.
for _, module := range modules { func mustAvailableModule(module string, apis []API) {
apiExists := false apiExists := false
for _, api := range apis { for _, api := range apis {
if module == api.Namespace { if module == api.Namespace {
@ -51,6 +51,14 @@ func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []str
return strings.Join(available, ",") return strings.Join(available, ",")
}()) }())
} }
}
// 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) {
// Generate the whitelist based on the allowed modules
whitelist := make(map[string]bool)
for _, module := range modules {
mustAvailableModule(module, apis)
whitelist[module] = true whitelist[module] = true
} }
// Register all the APIs exposed by the services // Register all the APIs exposed by the services
@ -81,6 +89,7 @@ func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []
// 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 {
mustAvailableModule(module, apis)
whitelist[module] = true whitelist[module] = true
} }
// Register all the APIs exposed by the services // Register all the APIs exposed by the services