mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 15:16:43 +00:00
node: make RPCCors config option an array instead of comma sep string
This commit is contained in:
parent
aff3c05d3b
commit
8499bcb071
5 changed files with 27 additions and 19 deletions
|
|
@ -523,9 +523,17 @@ func setNAT(ctx *cli.Context, cfg *p2p.Config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// makeRPCModules splits input separated by a comma and trims excessive white
|
// splitAndTrim splits input separated by a comma
|
||||||
// space from the substrings.
|
// and trims excessive white space from the substrings.
|
||||||
func makeRPCModules(input string) []string {
|
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, ",")
|
result := strings.Split(input, ",")
|
||||||
for i, r := range result {
|
for i, r := range result {
|
||||||
result[i] = strings.TrimSpace(r)
|
result[i] = strings.TrimSpace(r)
|
||||||
|
|
@ -547,10 +555,10 @@ func setHTTP(ctx *cli.Context, cfg *node.Config) {
|
||||||
cfg.HTTPPort = ctx.GlobalInt(RPCPortFlag.Name)
|
cfg.HTTPPort = ctx.GlobalInt(RPCPortFlag.Name)
|
||||||
}
|
}
|
||||||
if ctx.GlobalIsSet(RPCCORSDomainFlag.Name) {
|
if ctx.GlobalIsSet(RPCCORSDomainFlag.Name) {
|
||||||
cfg.HTTPCors = ctx.GlobalString(RPCCORSDomainFlag.Name)
|
cfg.HTTPCors = splitAndTrim(ctx.GlobalString(RPCCORSDomainFlag.Name))
|
||||||
}
|
}
|
||||||
if ctx.GlobalIsSet(RPCApiFlag.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)
|
cfg.WSOrigins = ctx.GlobalString(WSAllowedOriginsFlag.Name)
|
||||||
}
|
}
|
||||||
if ctx.GlobalIsSet(WSApiFlag.Name) {
|
if ctx.GlobalIsSet(WSApiFlag.Name) {
|
||||||
cfg.WSModules = makeRPCModules(ctx.GlobalString(WSApiFlag.Name))
|
cfg.WSModules = splitAndTrim(ctx.GlobalString(WSApiFlag.Name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
11
node/api.go
11
node/api.go
|
|
@ -92,8 +92,13 @@ func (api *PrivateAdminAPI) StartRPC(host *string, port *int, cors *string, apis
|
||||||
if port == nil {
|
if port == nil {
|
||||||
port = &api.node.config.HTTPPort
|
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
|
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 false, err
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ type Config struct {
|
||||||
// HTTPCors is the Cross-Origin Resource Sharing header to send to requesting
|
// 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
|
// clients. Please be aware that CORS is a browser enforced security, it's fully
|
||||||
// useless for custom HTTP clients.
|
// 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.
|
// 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
|
// If the module list is empty, all RPC API endpoints designated public will be
|
||||||
|
|
|
||||||
|
|
@ -372,7 +372,7 @@ func (n *Node) stopIPC() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// startHTTP initializes and starts the HTTP RPC endpoint.
|
// 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
|
// Short circuit if the HTTP endpoint isn't being exposed
|
||||||
if endpoint == "" {
|
if endpoint == "" {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
11
rpc/http.go
11
rpc/http.go
|
|
@ -25,7 +25,6 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -140,8 +139,8 @@ func (t *httpReadWriteNopCloser) Close() error {
|
||||||
// NewHTTPServer creates a new HTTP RPC server around an API provider.
|
// NewHTTPServer creates a new HTTP RPC server around an API provider.
|
||||||
//
|
//
|
||||||
// Deprecated: Server implements http.Handler
|
// Deprecated: Server implements http.Handler
|
||||||
func NewHTTPServer(corsString string, srv *Server) *http.Server {
|
func NewHTTPServer(cors []string, srv *Server) *http.Server {
|
||||||
return &http.Server{Handler: newCorsHandler(srv, corsString)}
|
return &http.Server{Handler: newCorsHandler(srv, cors)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP serves JSON-RPC requests over HTTP.
|
// 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)
|
srv.ServeSingleRequest(codec, OptionMethodInvocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCorsHandler(srv *Server, corsString string) http.Handler {
|
func newCorsHandler(srv *Server, allowedOrigins []string) http.Handler {
|
||||||
var allowedOrigins []string
|
|
||||||
for _, domain := range strings.Split(corsString, ",") {
|
|
||||||
allowedOrigins = append(allowedOrigins, strings.TrimSpace(domain))
|
|
||||||
}
|
|
||||||
c := cors.New(cors.Options{
|
c := cors.New(cors.Options{
|
||||||
AllowedOrigins: allowedOrigins,
|
AllowedOrigins: allowedOrigins,
|
||||||
AllowedMethods: []string{"POST", "GET"},
|
AllowedMethods: []string{"POST", "GET"},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue