diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index f0e2266e3..b6ce29a7a 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -124,6 +124,8 @@ var ( metricsFlag = flag.Bool("metrics", false, "Collect and upload node metrics") pushgatewayIP = flag.String("pushgateway_ip", "grafana.harmony.one", "Metrics view ip") pushgatewayPort = flag.String("pushgateway_port", "9091", "Metrics view port") + + publicRPC = flag.Bool("public_rpc", false, "Enable Public RPC Access (default: false)") ) func initSetup() { @@ -406,6 +408,7 @@ func main() { os.Exit(1) } + nodeconfig.SetPublicRPC(*publicRPC) nodeconfig.SetVersion(fmt.Sprintf("Harmony (C) 2019. %v, version %v-%v (%v %v)", path.Base(os.Args[0]), version, commit, builtBy, builtAt)) if *versionFlag { printVersion() diff --git a/internal/configs/node/config.go b/internal/configs/node/config.go index d54853dcb..b77a408cc 100644 --- a/internal/configs/node/config.go +++ b/internal/configs/node/config.go @@ -63,6 +63,7 @@ const ( ) var version string +var publicRPC bool // enable public RPC access // ConfigType is the structure of all node related configuration variables type ConfigType struct { @@ -252,3 +253,13 @@ func GetVersion() string { func GetTempDir() string { return os.TempDir() } + +// SetPublicRPC set the boolean value of public RPC access +func SetPublicRPC(v bool) { + publicRPC = v +} + +// GetPublicRPC get the boolean value of public RPC access +func GetPublicRPC() bool { + return publicRPC +} diff --git a/node/rpc.go b/node/rpc.go index 92d5c567c..c28192a2b 100644 --- a/node/rpc.go +++ b/node/rpc.go @@ -10,6 +10,7 @@ import ( "github.com/ethereum/go-ethereum/rpc" "github.com/harmony-one/harmony/hmy" + nodeconfig "github.com/harmony-one/harmony/internal/configs/node" "github.com/harmony-one/harmony/internal/hmyapi" "github.com/harmony-one/harmony/internal/hmyapi/filters" "github.com/harmony-one/harmony/internal/utils" @@ -56,12 +57,17 @@ func (node *Node) StartRPC(nodePort string) error { port, _ := strconv.Atoi(nodePort) - httpEndpoint = fmt.Sprintf(":%v", port+rpcHTTPPortOffset) + ip := "" + if !nodeconfig.GetPublicRPC() { + ip = "127.0.0.1" + } + httpEndpoint = fmt.Sprintf("%v:%v", ip, port+rpcHTTPPortOffset) + if err := node.startHTTP(httpEndpoint, apis, httpModules, httpOrigins, httpVirtualHosts, httpTimeouts); err != nil { return err } - wsEndpoint = fmt.Sprintf(":%v", port+rpcWSPortOffset) + wsEndpoint = fmt.Sprintf("%v:%v", ip, port+rpcWSPortOffset) if err := node.startWS(wsEndpoint, apis, wsModules, wsOrigins, true); err != nil { node.stopHTTP() return err