mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-28 15:46:43 +00:00
node: make WSOrigins config option an array instead of comma sep string
This commit is contained in:
parent
8499bcb071
commit
2ca78dc225
6 changed files with 16 additions and 19 deletions
|
|
@ -533,14 +533,6 @@ func splitAndTrim(input string) []string {
|
|||
return result
|
||||
}
|
||||
|
||||
func makeRPCCORS(input string) []string {
|
||||
result := strings.Split(input, ",")
|
||||
for i, r := range result {
|
||||
result[i] = strings.TrimSpace(r)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// setHTTP creates the HTTP RPC listener interface string from the set
|
||||
// command line flags, returning empty if the HTTP endpoint is disabled.
|
||||
func setHTTP(ctx *cli.Context, cfg *node.Config) {
|
||||
|
|
@ -576,7 +568,7 @@ func setWS(ctx *cli.Context, cfg *node.Config) {
|
|||
cfg.WSPort = ctx.GlobalInt(WSPortFlag.Name)
|
||||
}
|
||||
if ctx.GlobalIsSet(WSAllowedOriginsFlag.Name) {
|
||||
cfg.WSOrigins = ctx.GlobalString(WSAllowedOriginsFlag.Name)
|
||||
cfg.WSOrigins = splitAndTrim(ctx.GlobalString(WSAllowedOriginsFlag.Name))
|
||||
}
|
||||
if ctx.GlobalIsSet(WSApiFlag.Name) {
|
||||
cfg.WSModules = splitAndTrim(ctx.GlobalString(WSApiFlag.Name))
|
||||
|
|
|
|||
11
node/api.go
11
node/api.go
|
|
@ -146,8 +146,13 @@ func (api *PrivateAdminAPI) StartWS(host *string, port *int, allowedOrigins *str
|
|||
if port == nil {
|
||||
port = &api.node.config.WSPort
|
||||
}
|
||||
if allowedOrigins == nil {
|
||||
allowedOrigins = &api.node.config.WSOrigins
|
||||
|
||||
origins := api.node.config.WSOrigins
|
||||
if allowedOrigins != nil {
|
||||
origins = nil
|
||||
for _, origin := range strings.Split(*allowedOrigins, ",") {
|
||||
origins = append(origins, strings.TrimSpace(origin))
|
||||
}
|
||||
}
|
||||
|
||||
modules := api.node.config.WSModules
|
||||
|
|
@ -158,7 +163,7 @@ func (api *PrivateAdminAPI) StartWS(host *string, port *int, allowedOrigins *str
|
|||
}
|
||||
}
|
||||
|
||||
if err := api.node.startWS(fmt.Sprintf("%s:%d", *host, *port), api.node.rpcAPIs, modules, *allowedOrigins); err != nil {
|
||||
if err := api.node.startWS(fmt.Sprintf("%s:%d", *host, *port), api.node.rpcAPIs, modules, origins); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ type Config struct {
|
|||
// WSOrigins is the list of domain to accept websocket requests from. Please be
|
||||
// aware that the server can only act upon the HTTP request the client sends and
|
||||
// cannot verify the validity of the request header.
|
||||
WSOrigins string `toml:",omitempty"`
|
||||
WSOrigins []string `toml:",omitempty"`
|
||||
|
||||
// WSModules is a list of API modules to expose via the websocket RPC interface.
|
||||
// If the module list is empty, all RPC API endpoints designated public will be
|
||||
|
|
|
|||
|
|
@ -426,7 +426,7 @@ func (n *Node) stopHTTP() {
|
|||
}
|
||||
|
||||
// startWS initializes and starts the websocket RPC endpoint.
|
||||
func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrigins string) error {
|
||||
func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrigins []string) error {
|
||||
// Short circuit if the WS endpoint isn't being exposed
|
||||
if endpoint == "" {
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -394,7 +394,7 @@ func TestClientReconnect(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
go http.Serve(l, srv.WebsocketHandler("*"))
|
||||
go http.Serve(l, srv.WebsocketHandler([]string{"*"}))
|
||||
return srv, l
|
||||
}
|
||||
|
||||
|
|
@ -466,7 +466,7 @@ func httpTestClient(srv *Server, transport string, fl *flakeyListener) (*Client,
|
|||
var hs *httptest.Server
|
||||
switch transport {
|
||||
case "ws":
|
||||
hs = httptest.NewUnstartedServer(srv.WebsocketHandler("*"))
|
||||
hs = httptest.NewUnstartedServer(srv.WebsocketHandler([]string{"*"}))
|
||||
case "http":
|
||||
hs = httptest.NewUnstartedServer(srv)
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -36,9 +36,9 @@ import (
|
|||
//
|
||||
// allowedOrigins should be a comma-separated list of allowed origin URLs.
|
||||
// To allow connections with any origin, pass "*".
|
||||
func (srv *Server) WebsocketHandler(allowedOrigins string) http.Handler {
|
||||
func (srv *Server) WebsocketHandler(allowedOrigins []string) http.Handler {
|
||||
return websocket.Server{
|
||||
Handshake: wsHandshakeValidator(strings.Split(allowedOrigins, ",")),
|
||||
Handshake: wsHandshakeValidator(allowedOrigins),
|
||||
Handler: func(conn *websocket.Conn) {
|
||||
srv.ServeCodec(NewJSONCodec(conn), OptionMethodInvocation|OptionSubscriptions)
|
||||
},
|
||||
|
|
@ -48,7 +48,7 @@ func (srv *Server) WebsocketHandler(allowedOrigins string) http.Handler {
|
|||
// NewWSServer creates a new websocket RPC server around an API provider.
|
||||
//
|
||||
// Deprecated: use Server.WebsocketHandler
|
||||
func NewWSServer(allowedOrigins string, srv *Server) *http.Server {
|
||||
func NewWSServer(allowedOrigins []string, srv *Server) *http.Server {
|
||||
return &http.Server{Handler: srv.WebsocketHandler(allowedOrigins)}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue