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") confTree.Set("Version", "2.2.0")
return confTree 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" nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
) )
const tomlConfigVersion = "2.2.0" const tomlConfigVersion = "2.3.0"
const ( const (
defNetworkType = nodeconfig.Mainnet defNetworkType = nodeconfig.Mainnet
@ -34,6 +34,7 @@ var defaultConfig = harmonyconfig.HarmonyConfig{
RosettaEnabled: false, RosettaEnabled: false,
IP: "127.0.0.1", IP: "127.0.0.1",
Port: nodeconfig.DefaultRPCPort, Port: nodeconfig.DefaultRPCPort,
AuthPort: nodeconfig.DefaultAuthRPCPort,
RosettaPort: nodeconfig.DefaultRosettaPort, RosettaPort: nodeconfig.DefaultRosettaPort,
}, },
WS: harmonyconfig.WsConfig{ WS: harmonyconfig.WsConfig{

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

@ -66,6 +66,7 @@ func TestHarmonyFlags(t *testing.T) {
Enabled: true, Enabled: true,
IP: "127.0.0.1", IP: "127.0.0.1",
Port: 9500, Port: 9500,
AuthPort: 9501,
RosettaEnabled: false, RosettaEnabled: false,
RosettaPort: 9700, RosettaPort: 9700,
}, },
@ -424,6 +425,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: false, RosettaEnabled: false,
IP: defaultConfig.HTTP.IP, IP: defaultConfig.HTTP.IP,
Port: defaultConfig.HTTP.Port, Port: defaultConfig.HTTP.Port,
AuthPort: defaultConfig.HTTP.AuthPort,
RosettaPort: defaultConfig.HTTP.RosettaPort, RosettaPort: defaultConfig.HTTP.RosettaPort,
}, },
}, },
@ -434,6 +436,18 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: false, RosettaEnabled: false,
IP: "8.8.8.8", IP: "8.8.8.8",
Port: 9001, 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, RosettaPort: defaultConfig.HTTP.RosettaPort,
}, },
}, },
@ -444,6 +458,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: true, RosettaEnabled: true,
IP: "8.8.8.8", IP: "8.8.8.8",
Port: 9001, Port: 9001,
AuthPort: defaultConfig.HTTP.AuthPort,
RosettaPort: 10001, RosettaPort: 10001,
}, },
}, },
@ -454,6 +469,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: true, RosettaEnabled: true,
IP: "8.8.8.8", IP: "8.8.8.8",
Port: defaultConfig.HTTP.Port, Port: defaultConfig.HTTP.Port,
AuthPort: defaultConfig.HTTP.AuthPort,
RosettaPort: 10001, RosettaPort: 10001,
}, },
}, },
@ -464,6 +480,7 @@ func TestRPCFlags(t *testing.T) {
RosettaEnabled: false, RosettaEnabled: false,
IP: nodeconfig.DefaultPublicListenIP, IP: nodeconfig.DefaultPublicListenIP,
Port: 9501, Port: 9501,
AuthPort: 9502,
RosettaPort: 9701, RosettaPort: 9701,
}, },
}, },

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

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

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

@ -48,6 +48,8 @@ const (
DefaultDNSPort = 6000 DefaultDNSPort = 6000
// DefaultRPCPort is the default rpc port. The actual port used is 9000+500 // DefaultRPCPort is the default rpc port. The actual port used is 9000+500
DefaultRPCPort = 9500 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 is the default rosetta port. The actual port used is 9000+700
DefaultRosettaPort = 9700 DefaultRosettaPort = 9700
// DefaultWSPort is the default port for web socket endpoint. The actual port used is // 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 is the port offset for RPC HTTP requests
rpcHTTPPortOffset = 500 rpcHTTPPortOffset = 500
// rpcHTTPAuthPortOffset is the port offset for RPC Auth HTTP requests
rpcHTTPAuthPortOffset = 501
// rpcHTTPPortOffset is the port offset for rosetta HTTP requests // rpcHTTPPortOffset is the port offset for rosetta HTTP requests
rosettaHTTPPortOffset = 700 rosettaHTTPPortOffset = 700
@ -123,6 +128,11 @@ func GetRPCHTTPPortFromBase(basePort int) int {
return basePort + rpcHTTPPortOffset 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 // GetRosettaHTTPPortFromBase return the rosetta HTTP port from base port
func GetRosettaHTTPPortFromBase(basePort int) int { func GetRosettaHTTPPortFromBase(basePort int) int {
return basePort + rosettaHTTPPortOffset return basePort + rosettaHTTPPortOffset

@ -53,6 +53,7 @@ var (
wsListener net.Listener wsListener net.Listener
wsHandler *rpc.Server wsHandler *rpc.Server
httpEndpoint = "" httpEndpoint = ""
httpAuthEndpoint = ""
wsEndpoint = "" wsEndpoint = ""
httpVirtualHosts = []string{"*"} httpVirtualHosts = []string{"*"}
httpTimeouts = rpc.DefaultHTTPTimeouts httpTimeouts = rpc.DefaultHTTPTimeouts
@ -71,12 +72,18 @@ func (n Version) Namespace() string {
// StartServers starts the http & ws servers // StartServers starts the http & ws servers
func StartServers(hmy *hmy.Harmony, apis []rpc.API, config nodeconfig.RPCServerConfig) error { func StartServers(hmy *hmy.Harmony, apis []rpc.API, config nodeconfig.RPCServerConfig) error {
apis = append(apis, getAPIs(hmy, config.DebugEnabled, config.RateLimiterEnabled, config.RequestsPerSecond)...) apis = append(apis, getAPIs(hmy, config.DebugEnabled, config.RateLimiterEnabled, config.RequestsPerSecond)...)
authApis := getAuthAPIs(hmy, config.DebugEnabled, config.RateLimiterEnabled, config.RequestsPerSecond)
if config.HTTPEnabled { if config.HTTPEnabled {
httpEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPPort) httpEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPPort)
if err := startHTTP(apis); err != nil { if err := startHTTP(apis); err != nil {
return err return err
} }
httpAuthEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPAuthPort)
if err := startAuthHTTP(authApis); err != nil {
return err
}
} }
if config.WSEnabled { if config.WSEnabled {
@ -120,6 +127,13 @@ func StopServers() error {
return nil 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 // getAPIs returns all the API methods for the RPC interface
func getAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelimit int) []rpc.API { func getAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelimit int) []rpc.API {
publicAPIs := []rpc.API{ publicAPIs := []rpc.API{
@ -141,10 +155,10 @@ func getAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelim
NewPublicPoolAPI(hmy, Eth), NewPublicPoolAPI(hmy, Eth),
NewPublicStakingAPI(hmy, V1), NewPublicStakingAPI(hmy, V1),
NewPublicStakingAPI(hmy, V2), 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, V1),
NewPublicDebugAPI(hmy, V2), 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) // Legacy methods (subject to removal)
v1.NewPublicLegacyAPI(hmy, "hmy"), v1.NewPublicLegacyAPI(hmy, "hmy"),
eth.NewPublicEthService(hmy, "eth"), eth.NewPublicEthService(hmy, "eth"),
@ -179,6 +193,23 @@ func startHTTP(apis []rpc.API) (err error) {
return nil 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) { func startWS(apis []rpc.API) (err error) {
wsListener, wsHandler, err = rpc.StartWSEndpoint(wsEndpoint, apis, WSModules, wsOrigins, true) wsListener, wsHandler, err = rpc.StartWSEndpoint(wsEndpoint, apis, WSModules, wsOrigins, true)
if err != nil { if err != nil {

Loading…
Cancel
Save