|
17 | 17 | package rpc |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "fmt" |
20 | 21 | "net" |
21 | 22 |
|
22 | 23 | "github.com/ethereum/go-ethereum/log" |
23 | 24 | ) |
24 | 25 |
|
25 | | -// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules |
| 26 | +// checkModuleAvailability check that all names given in modules are actually |
| 27 | +// available API services. |
| 28 | +func checkModuleAvailability(modules []string, apis []API) error { |
| 29 | + available := make(map[string]struct{}) |
| 30 | + var availableNames string |
| 31 | + for i, api := range apis { |
| 32 | + if _, ok := available[api.Namespace]; !ok { |
| 33 | + available[api.Namespace] = struct{}{} |
| 34 | + if i > 0 { |
| 35 | + availableNames += ", " |
| 36 | + } |
| 37 | + availableNames += api.Namespace |
| 38 | + } |
| 39 | + } |
| 40 | + for _, name := range modules { |
| 41 | + if _, ok := available[name]; !ok { |
| 42 | + return fmt.Errorf("invalid API %q in whitelist (available: %s)", name, availableNames) |
| 43 | + } |
| 44 | + } |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules. |
26 | 49 | func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) { |
| 50 | + if err := checkModuleAvailability(modules, apis); err != nil { |
| 51 | + return nil, nil, err |
| 52 | + } |
27 | 53 | // Generate the whitelist based on the allowed modules |
28 | 54 | whitelist := make(map[string]bool) |
29 | 55 | for _, module := range modules { |
@@ -51,9 +77,11 @@ func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []str |
51 | 77 | return listener, handler, err |
52 | 78 | } |
53 | 79 |
|
54 | | -// StartWSEndpoint starts a websocket endpoint |
| 80 | +// StartWSEndpoint starts a websocket endpoint. |
55 | 81 | func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) { |
56 | | - |
| 82 | + if err := checkModuleAvailability(modules, apis); err != nil { |
| 83 | + return nil, nil, err |
| 84 | + } |
57 | 85 | // Generate the whitelist based on the allowed modules |
58 | 86 | whitelist := make(map[string]bool) |
59 | 87 | for _, module := range modules { |
|
0 commit comments