[rpc] use a public_rpc parameter to enable public rpc

This is to fix the AWS ec2 instance no real public IP issue.

Signed-off-by: Leo Chen <leo@harmony.one>
pull/1629/head
Leo Chen 5 years ago
parent 6e7dc61f3c
commit d8e02f00c6
  1. 3
      cmd/harmony/main.go
  2. 11
      internal/configs/node/config.go
  3. 10
      node/rpc.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()

@ -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
}

@ -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

Loading…
Cancel
Save