From 8499bcb07183d238905a5232dc600bb12c26504c Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Wed, 12 Apr 2017 21:11:13 +0200 Subject: [PATCH] node: make RPCCors config option an array instead of comma sep string --- cmd/utils/flags.go | 20 ++++++++++++++------ node/api.go | 11 ++++++++--- node/config.go | 2 +- node/node.go | 2 +- rpc/http.go | 11 +++-------- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index e14c3b3961..03563998da 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -523,9 +523,17 @@ func setNAT(ctx *cli.Context, cfg *p2p.Config) { } } -// makeRPCModules splits input separated by a comma and trims excessive white -// space from the substrings. -func makeRPCModules(input string) []string { +// splitAndTrim splits input separated by a comma +// and trims excessive white space from the substrings. +func splitAndTrim(input string) []string { + result := strings.Split(input, ",") + for i, r := range result { + result[i] = strings.TrimSpace(r) + } + return result +} + +func makeRPCCORS(input string) []string { result := strings.Split(input, ",") for i, r := range result { result[i] = strings.TrimSpace(r) @@ -547,10 +555,10 @@ func setHTTP(ctx *cli.Context, cfg *node.Config) { cfg.HTTPPort = ctx.GlobalInt(RPCPortFlag.Name) } if ctx.GlobalIsSet(RPCCORSDomainFlag.Name) { - cfg.HTTPCors = ctx.GlobalString(RPCCORSDomainFlag.Name) + cfg.HTTPCors = splitAndTrim(ctx.GlobalString(RPCCORSDomainFlag.Name)) } if ctx.GlobalIsSet(RPCApiFlag.Name) { - cfg.HTTPModules = makeRPCModules(ctx.GlobalString(RPCApiFlag.Name)) + cfg.HTTPModules = splitAndTrim(ctx.GlobalString(RPCApiFlag.Name)) } } @@ -571,7 +579,7 @@ func setWS(ctx *cli.Context, cfg *node.Config) { cfg.WSOrigins = ctx.GlobalString(WSAllowedOriginsFlag.Name) } if ctx.GlobalIsSet(WSApiFlag.Name) { - cfg.WSModules = makeRPCModules(ctx.GlobalString(WSApiFlag.Name)) + cfg.WSModules = splitAndTrim(ctx.GlobalString(WSApiFlag.Name)) } } diff --git a/node/api.go b/node/api.go index 3c451fc8ac..a2910e7fa5 100644 --- a/node/api.go +++ b/node/api.go @@ -92,8 +92,13 @@ func (api *PrivateAdminAPI) StartRPC(host *string, port *int, cors *string, apis if port == nil { port = &api.node.config.HTTPPort } - if cors == nil { - cors = &api.node.config.HTTPCors + + allowedOrigins := api.node.config.HTTPCors + if cors != nil { + allowedOrigins = nil + for _, origin := range strings.Split(*cors, ",") { + allowedOrigins = append(allowedOrigins, strings.TrimSpace(origin)) + } } modules := api.node.httpWhitelist @@ -104,7 +109,7 @@ func (api *PrivateAdminAPI) StartRPC(host *string, port *int, cors *string, apis } } - if err := api.node.startHTTP(fmt.Sprintf("%s:%d", *host, *port), api.node.rpcAPIs, modules, *cors); err != nil { + if err := api.node.startHTTP(fmt.Sprintf("%s:%d", *host, *port), api.node.rpcAPIs, modules, allowedOrigins); err != nil { return false, err } return true, nil diff --git a/node/config.go b/node/config.go index 7c17e707dd..70a88076dd 100644 --- a/node/config.go +++ b/node/config.go @@ -100,7 +100,7 @@ type Config struct { // HTTPCors is the Cross-Origin Resource Sharing header to send to requesting // clients. Please be aware that CORS is a browser enforced security, it's fully // useless for custom HTTP clients. - HTTPCors string `toml:",omitempty"` + HTTPCors []string `toml:",omitempty"` // HTTPModules is a list of API modules to expose via the HTTP 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 2ecff23088..19181d0fa9 100644 --- a/node/node.go +++ b/node/node.go @@ -372,7 +372,7 @@ func (n *Node) stopIPC() { } // startHTTP initializes and starts the HTTP RPC endpoint. -func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors string) error { +func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors []string) error { // Short circuit if the HTTP endpoint isn't being exposed if endpoint == "" { return nil diff --git a/rpc/http.go b/rpc/http.go index 89175b1496..022f9ce8f3 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -25,7 +25,6 @@ import ( "io/ioutil" "net" "net/http" - "strings" "sync" "time" @@ -140,8 +139,8 @@ func (t *httpReadWriteNopCloser) Close() error { // NewHTTPServer creates a new HTTP RPC server around an API provider. // // Deprecated: Server implements http.Handler -func NewHTTPServer(corsString string, srv *Server) *http.Server { - return &http.Server{Handler: newCorsHandler(srv, corsString)} +func NewHTTPServer(cors []string, srv *Server) *http.Server { + return &http.Server{Handler: newCorsHandler(srv, cors)} } // ServeHTTP serves JSON-RPC requests over HTTP. @@ -162,11 +161,7 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { srv.ServeSingleRequest(codec, OptionMethodInvocation) } -func newCorsHandler(srv *Server, corsString string) http.Handler { - var allowedOrigins []string - for _, domain := range strings.Split(corsString, ",") { - allowedOrigins = append(allowedOrigins, strings.TrimSpace(domain)) - } +func newCorsHandler(srv *Server, allowedOrigins []string) http.Handler { c := cors.New(cors.Options{ AllowedOrigins: allowedOrigins, AllowedMethods: []string{"POST", "GET"},