From 2ca78dc2252dc58dbb22344750c765cc74d87d2b Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 12 Apr 2017 21:30:54 +0200 Subject: [PATCH] node: make WSOrigins config option an array instead of comma sep string --- cmd/utils/flags.go | 10 +--------- node/api.go | 11 ++++++++--- node/config.go | 2 +- node/node.go | 2 +- rpc/client_test.go | 4 ++-- rpc/websocket.go | 6 +++--- 6 files changed, 16 insertions(+), 19 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 03563998da..b35574c86a 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -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)) diff --git a/node/api.go b/node/api.go index a2910e7fa5..570cb9d98e 100644 --- a/node/api.go +++ b/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 diff --git a/node/config.go b/node/config.go index 70a88076dd..1bab4c5745 100644 --- a/node/config.go +++ b/node/config.go @@ -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 diff --git a/node/node.go b/node/node.go index 19181d0fa9..dc2ff0701d 100644 --- a/node/node.go +++ b/node/node.go @@ -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 diff --git a/rpc/client_test.go b/rpc/client_test.go index 41471dceaf..10d74670b7 100644 --- a/rpc/client_test.go +++ b/rpc/client_test.go @@ -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: diff --git a/rpc/websocket.go b/rpc/websocket.go index 5870108206..5f9593a43f 100644 --- a/rpc/websocket.go +++ b/rpc/websocket.go @@ -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)} }