rpc: add wildcard support for rpcallowedhosts + go fmt

This commit is contained in:
Martin Holst Swende 2018-01-24 11:40:23 +01:00
parent 226aef18c8
commit 5d0c42e35f
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
4 changed files with 6 additions and 5 deletions

View file

@ -385,7 +385,7 @@ var (
}
RPCAllowedHostsFlag = cli.StringFlag{
Name: "rpcallowedhosts",
Usage: "Comma separated list of hostnames from which to accept requests (server enforced)",
Usage: "Comma separated list of hostnames from which to accept requests (server enforced). Set to * to disable this protection.",
Value: "localhost,127.0.0.1",
}
RPCApiFlag = cli.StringFlag{

View file

@ -142,7 +142,7 @@ func (api *PrivateAdminAPI) StartRPC(host *string, port *int, cors *string, apis
}
allowedHosts := api.node.config.HTTPHosts
if hosts != nil{
if hosts != nil {
allowedHosts = nil
for _, host := range strings.Split(*host, ",") {
allowedHosts = append(allowedHosts, strings.TrimSpace(host))

View file

@ -395,7 +395,7 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
}
go rpc.NewHTTPServer(cors, allowedHosts, handler).Serve(listener)
n.log.Info(fmt.Sprintf("HTTP endpoint opened: http://%s", endpoint))
n.log.Info(fmt.Sprintf("HTTP config: cors %v, hosts%v", cors,allowedHosts))
n.log.Info(fmt.Sprintf("HTTP config: cors %v, hosts%v", cors, allowedHosts))
// All listeners booted successfully
n.httpEndpoint = endpoint
n.httpListener = listener

View file

@ -143,7 +143,7 @@ func (t *httpReadWriteNopCloser) Close() error {
// NewHTTPServer creates a new HTTP RPC server around an API provider.
//
// Deprecated: Server implements http.Handler
func NewHTTPServer(cors []string, hosts[] string, srv *Server) *http.Server {
func NewHTTPServer(cors []string, hosts []string, srv *Server) *http.Server {
// Wrap the CORS-handler within a host-handler
handler := newCorsHandler(srv, cors)
handler = newHostHandler(hosts, handler)
@ -224,8 +224,9 @@ func (h *hostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
hostpart := parseHost(r.Host)
requestAllowed := false
for _, allowedHost := range h.AllowedHosts {
if strings.ToLower(allowedHost) == hostpart {
if strings.ToLower(allowedHost) == hostpart || allowedHost == "*" {
requestAllowed = true
break
}
}
if !requestAllowed {