Merge pull request #3853 from LuttyYang/add_auth_api

Add RPC auth port for some auth api
pull/3858/head
Haodi 3 years ago committed by GitHub
commit dece072273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      cmd/harmony/config_migrations.go
  2. 3
      cmd/harmony/default.go
  3. 12
      cmd/harmony/flags.go
  4. 17
      cmd/harmony/flags_test.go
  5. 1
      cmd/harmony/main.go
  6. 1
      internal/configs/harmony/harmony.go
  7. 7
      internal/configs/node/config.go
  8. 10
      internal/configs/node/network.go
  9. 35
      rpc/rpc.go

@ -192,4 +192,13 @@ func init() {
confTree.Set("Version", "2.2.0")
return confTree
}
migrations["2.2.0"] = func(confTree *toml.Tree) *toml.Tree {
if confTree.Get("HTTP.AuthPort") == nil {
confTree.Set("HTTP.AuthPort", defaultConfig.HTTP.AuthPort)
}
confTree.Set("Version", "2.3.0")
return confTree
}
}

@ -5,7 +5,7 @@ import (
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
)
const tomlConfigVersion = "2.2.0"
const tomlConfigVersion = "2.3.0"
const (
defNetworkType = nodeconfig.Mainnet
@ -34,6 +34,7 @@ var defaultConfig = harmonyconfig.HarmonyConfig{
RosettaEnabled: false,
IP: "127.0.0.1",
Port: nodeconfig.DefaultRPCPort,
AuthPort: nodeconfig.DefaultAuthRPCPort,
RosettaPort: nodeconfig.DefaultRosettaPort,
},
WS: harmonyconfig.WsConfig{

@ -64,6 +64,7 @@ var (
httpRosettaEnabledFlag,
httpIPFlag,
httpPortFlag,
httpAuthPortFlag,
httpRosettaPortFlag,
}
@ -568,6 +569,11 @@ var (
Usage: "rpc port to listen for HTTP requests",
DefValue: defaultConfig.HTTP.Port,
}
httpAuthPortFlag = cli.IntFlag{
Name: "http.auth-port",
Usage: "rpc port to listen for auth HTTP requests",
DefValue: defaultConfig.HTTP.AuthPort,
}
httpRosettaEnabledFlag = cli.BoolFlag{
Name: "http.rosetta",
Usage: "enable HTTP / Rosetta requests",
@ -593,6 +599,11 @@ func applyHTTPFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) {
isRPCSpecified = true
}
if cli.IsFlagChanged(cmd, httpAuthPortFlag) {
config.HTTP.AuthPort = cli.GetIntFlagValue(cmd, httpAuthPortFlag)
isRPCSpecified = true
}
if cli.IsFlagChanged(cmd, httpRosettaPortFlag) {
config.HTTP.RosettaPort = cli.GetIntFlagValue(cmd, httpRosettaPortFlag)
isRosettaSpecified = true
@ -1336,6 +1347,7 @@ func applyLegacyMiscFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfi
legacyPort := cli.GetIntFlagValue(cmd, legacyPortFlag)
config.P2P.Port = legacyPort
config.HTTP.Port = nodeconfig.GetRPCHTTPPortFromBase(legacyPort)
config.HTTP.AuthPort = nodeconfig.GetRPCAuthHTTPPortFromBase(legacyPort)
config.HTTP.RosettaPort = nodeconfig.GetRosettaHTTPPortFromBase(legacyPort)
config.WS.Port = nodeconfig.GetWSPortFromBase(legacyPort)

@ -66,6 +66,7 @@ func TestHarmonyFlags(t *testing.T) {
Enabled: true,
IP: "127.0.0.1",
Port: 9500,
AuthPort: 9501,
RosettaEnabled: false,
RosettaPort: 9700,
},
@ -424,6 +425,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: false,
IP: defaultConfig.HTTP.IP,
Port: defaultConfig.HTTP.Port,
AuthPort: defaultConfig.HTTP.AuthPort,
RosettaPort: defaultConfig.HTTP.RosettaPort,
},
},
@ -434,6 +436,18 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: false,
IP: "8.8.8.8",
Port: 9001,
AuthPort: defaultConfig.HTTP.AuthPort,
RosettaPort: defaultConfig.HTTP.RosettaPort,
},
},
{
args: []string{"--http.ip", "8.8.8.8", "--http.auth-port", "9001"},
expConfig: harmonyconfig.HttpConfig{
Enabled: true,
RosettaEnabled: false,
IP: "8.8.8.8",
Port: defaultConfig.HTTP.Port,
AuthPort: 9001,
RosettaPort: defaultConfig.HTTP.RosettaPort,
},
},
@ -444,6 +458,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: true,
IP: "8.8.8.8",
Port: 9001,
AuthPort: defaultConfig.HTTP.AuthPort,
RosettaPort: 10001,
},
},
@ -454,6 +469,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: true,
IP: "8.8.8.8",
Port: defaultConfig.HTTP.Port,
AuthPort: defaultConfig.HTTP.AuthPort,
RosettaPort: 10001,
},
},
@ -464,6 +480,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: false,
IP: nodeconfig.DefaultPublicListenIP,
Port: 9501,
AuthPort: 9502,
RosettaPort: 9701,
},
},

@ -313,6 +313,7 @@ func setupNodeAndRun(hc harmonyconfig.HarmonyConfig) {
HTTPEnabled: hc.HTTP.Enabled,
HTTPIp: hc.HTTP.IP,
HTTPPort: hc.HTTP.Port,
HTTPAuthPort: hc.HTTP.AuthPort,
WSEnabled: hc.WS.Enabled,
WSIp: hc.WS.IP,
WSPort: hc.WS.Port,

@ -136,6 +136,7 @@ type HttpConfig struct {
Enabled bool
IP string
Port int
AuthPort int
RosettaEnabled bool
RosettaPort int
}

@ -98,9 +98,10 @@ type ConfigType struct {
// RPCServerConfig is the config for rpc listen addresses
type RPCServerConfig struct {
HTTPEnabled bool
HTTPIp string
HTTPPort int
HTTPEnabled bool
HTTPIp string
HTTPPort int
HTTPAuthPort int
WSEnabled bool
WSIp string

@ -48,6 +48,8 @@ const (
DefaultDNSPort = 6000
// DefaultRPCPort is the default rpc port. The actual port used is 9000+500
DefaultRPCPort = 9500
// DefaultAuthRPCPort is the default rpc auth port. The actual port used is 9000+501
DefaultAuthRPCPort = 9501
// DefaultRosettaPort is the default rosetta port. The actual port used is 9000+700
DefaultRosettaPort = 9700
// DefaultWSPort is the default port for web socket endpoint. The actual port used is
@ -67,6 +69,9 @@ const (
// rpcHTTPPortOffset is the port offset for RPC HTTP requests
rpcHTTPPortOffset = 500
// rpcHTTPAuthPortOffset is the port offset for RPC Auth HTTP requests
rpcHTTPAuthPortOffset = 501
// rpcHTTPPortOffset is the port offset for rosetta HTTP requests
rosettaHTTPPortOffset = 700
@ -123,6 +128,11 @@ func GetRPCHTTPPortFromBase(basePort int) int {
return basePort + rpcHTTPPortOffset
}
// GetRPCAuthHTTPPortFromBase return the rpc HTTP port from base port
func GetRPCAuthHTTPPortFromBase(basePort int) int {
return basePort + rpcHTTPAuthPortOffset
}
// GetRosettaHTTPPortFromBase return the rosetta HTTP port from base port
func GetRosettaHTTPPortFromBase(basePort int) int {
return basePort + rosettaHTTPPortOffset

@ -53,6 +53,7 @@ var (
wsListener net.Listener
wsHandler *rpc.Server
httpEndpoint = ""
httpAuthEndpoint = ""
wsEndpoint = ""
httpVirtualHosts = []string{"*"}
httpTimeouts = rpc.DefaultHTTPTimeouts
@ -71,12 +72,18 @@ func (n Version) Namespace() string {
// StartServers starts the http & ws servers
func StartServers(hmy *hmy.Harmony, apis []rpc.API, config nodeconfig.RPCServerConfig) error {
apis = append(apis, getAPIs(hmy, config.DebugEnabled, config.RateLimiterEnabled, config.RequestsPerSecond)...)
authApis := getAuthAPIs(hmy, config.DebugEnabled, config.RateLimiterEnabled, config.RequestsPerSecond)
if config.HTTPEnabled {
httpEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPPort)
if err := startHTTP(apis); err != nil {
return err
}
httpAuthEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPAuthPort)
if err := startAuthHTTP(authApis); err != nil {
return err
}
}
if config.WSEnabled {
@ -120,6 +127,13 @@ func StopServers() error {
return nil
}
func getAuthAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelimit int) []rpc.API {
return []rpc.API{
NewPublicTraceAPI(hmy, Debug), // Debug version means geth trace rpc
NewPublicTraceAPI(hmy, Trace), // Trace version means parity trace rpc
}
}
// getAPIs returns all the API methods for the RPC interface
func getAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelimit int) []rpc.API {
publicAPIs := []rpc.API{
@ -141,10 +155,10 @@ func getAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelim
NewPublicPoolAPI(hmy, Eth),
NewPublicStakingAPI(hmy, V1),
NewPublicStakingAPI(hmy, V2),
NewPublicTraceAPI(hmy, Debug), // Debug version means geth trace rpc
NewPublicTraceAPI(hmy, Trace), // Trace version means parity trace rpc
NewPublicDebugAPI(hmy, V1),
NewPublicDebugAPI(hmy, V2),
NewPublicTraceAPI(hmy, Debug), // Debug version means geth trace rpc
NewPublicTraceAPI(hmy, Trace), // Trace version means parity trace rpc
// Legacy methods (subject to removal)
v1.NewPublicLegacyAPI(hmy, "hmy"),
eth.NewPublicEthService(hmy, "eth"),
@ -179,6 +193,23 @@ func startHTTP(apis []rpc.API) (err error) {
return nil
}
func startAuthHTTP(apis []rpc.API) (err error) {
httpListener, httpHandler, err = rpc.StartHTTPEndpoint(
httpAuthEndpoint, apis, HTTPModules, httpOrigins, httpVirtualHosts, httpTimeouts,
)
if err != nil {
return err
}
utils.Logger().Info().
Str("url", fmt.Sprintf("http://%s", httpAuthEndpoint)).
Str("cors", strings.Join(httpOrigins, ",")).
Str("vhosts", strings.Join(httpVirtualHosts, ",")).
Msg("HTTP endpoint opened")
fmt.Printf("Started Auth-RPC server at: %v\n", httpAuthEndpoint)
return nil
}
func startWS(apis []rpc.API) (err error) {
wsListener, wsHandler, err = rpc.StartWSEndpoint(wsEndpoint, apis, WSModules, wsOrigins, true)
if err != nil {

Loading…
Cancel
Save