cmd/geth, node: allow configuring JSON-RPC on custom path prefix (#22184)
This change allows users to set a custom path prefix on which to mount the http-rpc or ws-rpc handlers via the new flags --http.rpcprefix and --ws.rpcprefix. Fixes #21826 Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
		
							
								
								
									
										20
									
								
								node/node.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								node/node.go
									
									
									
									
									
								
							| @@ -135,6 +135,14 @@ func New(conf *Config) (*Node, error) { | ||||
| 		node.server.Config.NodeDatabase = node.config.NodeDB() | ||||
| 	} | ||||
|  | ||||
| 	// Check HTTP/WS prefixes are valid. | ||||
| 	if err := validatePrefix("HTTP", conf.HTTPPathPrefix); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	if err := validatePrefix("WebSocket", conf.WSPathPrefix); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	// Configure RPC servers. | ||||
| 	node.http = newHTTPServer(node.log, conf.HTTPTimeouts) | ||||
| 	node.ws = newHTTPServer(node.log, rpc.DefaultHTTPTimeouts) | ||||
| @@ -346,6 +354,7 @@ func (n *Node) startRPC() error { | ||||
| 			CorsAllowedOrigins: n.config.HTTPCors, | ||||
| 			Vhosts:             n.config.HTTPVirtualHosts, | ||||
| 			Modules:            n.config.HTTPModules, | ||||
| 			prefix:             n.config.HTTPPathPrefix, | ||||
| 		} | ||||
| 		if err := n.http.setListenAddr(n.config.HTTPHost, n.config.HTTPPort); err != nil { | ||||
| 			return err | ||||
| @@ -361,6 +370,7 @@ func (n *Node) startRPC() error { | ||||
| 		config := wsConfig{ | ||||
| 			Modules: n.config.WSModules, | ||||
| 			Origins: n.config.WSOrigins, | ||||
| 			prefix:  n.config.WSPathPrefix, | ||||
| 		} | ||||
| 		if err := server.setListenAddr(n.config.WSHost, n.config.WSPort); err != nil { | ||||
| 			return err | ||||
| @@ -457,6 +467,7 @@ func (n *Node) RegisterHandler(name, path string, handler http.Handler) { | ||||
| 	if n.state != initializingState { | ||||
| 		panic("can't register HTTP handler on running/stopped node") | ||||
| 	} | ||||
|  | ||||
| 	n.http.mux.Handle(path, handler) | ||||
| 	n.http.handlerNames[path] = name | ||||
| } | ||||
| @@ -513,17 +524,18 @@ func (n *Node) IPCEndpoint() string { | ||||
| 	return n.ipc.endpoint | ||||
| } | ||||
|  | ||||
| // HTTPEndpoint returns the URL of the HTTP server. | ||||
| // HTTPEndpoint returns the URL of the HTTP server. Note that this URL does not | ||||
| // contain the JSON-RPC path prefix set by HTTPPathPrefix. | ||||
| func (n *Node) HTTPEndpoint() string { | ||||
| 	return "http://" + n.http.listenAddr() | ||||
| } | ||||
|  | ||||
| // WSEndpoint retrieves the current WS endpoint used by the protocol stack. | ||||
| // WSEndpoint returns the current JSON-RPC over WebSocket endpoint. | ||||
| func (n *Node) WSEndpoint() string { | ||||
| 	if n.http.wsAllowed() { | ||||
| 		return "ws://" + n.http.listenAddr() | ||||
| 		return "ws://" + n.http.listenAddr() + n.http.wsConfig.prefix | ||||
| 	} | ||||
| 	return "ws://" + n.ws.listenAddr() | ||||
| 	return "ws://" + n.ws.listenAddr() + n.ws.wsConfig.prefix | ||||
| } | ||||
|  | ||||
| // EventMux retrieves the event multiplexer used by all the network services in | ||||
|   | ||||
		Reference in New Issue
	
	Block a user