From 1eb177f80b13ebc450ba4f1c5e7b6b69a05d175c Mon Sep 17 00:00:00 2001 From: Lutty Date: Sun, 22 Aug 2021 15:03:58 +0800 Subject: [PATCH 1/8] add auth api and auth port --- internal/configs/node/config.go | 7 ++++--- rpc/rpc.go | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/internal/configs/node/config.go b/internal/configs/node/config.go index 11d22a6889..345c370fbd 100644 --- a/internal/configs/node/config.go +++ b/internal/configs/node/config.go @@ -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 diff --git a/rpc/rpc.go b/rpc/rpc.go index 78c37918ab..b27aacc4f7 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -71,12 +71,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 } + + httpEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPAuthPort) + if err := startAuthHTTP(authApis); err != nil { + return err + } } if config.WSEnabled { @@ -120,6 +126,10 @@ func StopServers() error { return nil } +func getAuthAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelimit int) []rpc.API { + return []rpc.API{} +} + // 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{ @@ -179,6 +189,23 @@ func startHTTP(apis []rpc.API) (err error) { return nil } +func startAuthHTTP(apis []rpc.API) (err error) { + httpListener, httpHandler, err = rpc.StartHTTPEndpoint( + httpEndpoint, apis, HTTPModules, httpOrigins, httpVirtualHosts, httpTimeouts, + ) + if err != nil { + return err + } + + utils.Logger().Info(). + Str("url", fmt.Sprintf("http://%s", httpEndpoint)). + Str("cors", strings.Join(httpOrigins, ",")). + Str("vhosts", strings.Join(httpVirtualHosts, ",")). + Msg("HTTP endpoint opened") + fmt.Printf("Started RPC server at: %v\n", httpEndpoint) + return nil +} + func startWS(apis []rpc.API) (err error) { wsListener, wsHandler, err = rpc.StartWSEndpoint(wsEndpoint, apis, WSModules, wsOrigins, true) if err != nil { From 22972a0d83ba177cbfbbb4d67d4476c568a1a7af Mon Sep 17 00:00:00 2001 From: haodi <82733821@qq.com> Date: Sun, 22 Aug 2021 20:05:12 +0800 Subject: [PATCH 2/8] split API which need be authed --- rpc/rpc.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rpc/rpc.go b/rpc/rpc.go index b27aacc4f7..29d7eb829e 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -127,7 +127,10 @@ func StopServers() error { } func getAuthAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelimit int) []rpc.API { - return []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 @@ -151,8 +154,6 @@ 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), // Legacy methods (subject to removal) From d58243b6301698eaf672c505723bcd144c2ad867 Mon Sep 17 00:00:00 2001 From: haodi <82733821@qq.com> Date: Wed, 25 Aug 2021 00:42:40 +0800 Subject: [PATCH 3/8] split API which need be authed --- rpc/rpc.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rpc/rpc.go b/rpc/rpc.go index 29d7eb829e..f2360e8994 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -156,6 +156,8 @@ func getAPIs(hmy *hmy.Harmony, debugEnable bool, rateLimiterEnable bool, ratelim NewPublicStakingAPI(hmy, V2), 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"), From 5221838f260840c3cd86f5411f588755ef9dd16c Mon Sep 17 00:00:00 2001 From: Lutty Date: Wed, 25 Aug 2021 22:24:02 +0800 Subject: [PATCH 4/8] support cli flag --- cmd/harmony/default.go | 1 + cmd/harmony/flags.go | 12 ++++++++++++ cmd/harmony/main.go | 1 + internal/configs/harmony/harmony.go | 1 + internal/configs/node/network.go | 10 ++++++++++ 5 files changed, 25 insertions(+) diff --git a/cmd/harmony/default.go b/cmd/harmony/default.go index 26133cde5c..5a927f1a3c 100644 --- a/cmd/harmony/default.go +++ b/cmd/harmony/default.go @@ -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{ diff --git a/cmd/harmony/flags.go b/cmd/harmony/flags.go index 522113d2ca..a95d226767 100644 --- a/cmd/harmony/flags.go +++ b/cmd/harmony/flags.go @@ -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) diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 786680334a..692d91239e 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -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, diff --git a/internal/configs/harmony/harmony.go b/internal/configs/harmony/harmony.go index afe76f987e..1748d1e0d7 100644 --- a/internal/configs/harmony/harmony.go +++ b/internal/configs/harmony/harmony.go @@ -136,6 +136,7 @@ type HttpConfig struct { Enabled bool IP string Port int + AuthPort int RosettaEnabled bool RosettaPort int } diff --git a/internal/configs/node/network.go b/internal/configs/node/network.go index 412d4cb537..3133ea9d26 100644 --- a/internal/configs/node/network.go +++ b/internal/configs/node/network.go @@ -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 From dd31a20d32e4e37cc007da4b20896131a3082654 Mon Sep 17 00:00:00 2001 From: Lutty Date: Thu, 26 Aug 2021 22:07:53 +0800 Subject: [PATCH 5/8] fix ci --- cmd/harmony/config_migrations.go | 9 +++++++++ cmd/harmony/default.go | 2 +- cmd/harmony/flags_test.go | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/cmd/harmony/config_migrations.go b/cmd/harmony/config_migrations.go index b0169021f3..7f0cb1de84 100644 --- a/cmd/harmony/config_migrations.go +++ b/cmd/harmony/config_migrations.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 + } } diff --git a/cmd/harmony/default.go b/cmd/harmony/default.go index 5a927f1a3c..c7c0595d2f 100644 --- a/cmd/harmony/default.go +++ b/cmd/harmony/default.go @@ -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 diff --git a/cmd/harmony/flags_test.go b/cmd/harmony/flags_test.go index 2b75063177..0c3cd6951a 100644 --- a/cmd/harmony/flags_test.go +++ b/cmd/harmony/flags_test.go @@ -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, }, }, From 23b04b4e417cbe340631412fb55592ca3e02d235 Mon Sep 17 00:00:00 2001 From: Lutty Date: Thu, 26 Aug 2021 23:02:26 +0800 Subject: [PATCH 6/8] fix bug --- rpc/rpc.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rpc/rpc.go b/rpc/rpc.go index f2360e8994..0487b8de0b 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -53,6 +53,7 @@ var ( wsListener net.Listener wsHandler *rpc.Server httpEndpoint = "" + httpAuthEndpoint = "" wsEndpoint = "" httpVirtualHosts = []string{"*"} httpTimeouts = rpc.DefaultHTTPTimeouts @@ -79,7 +80,7 @@ func StartServers(hmy *hmy.Harmony, apis []rpc.API, config nodeconfig.RPCServerC return err } - httpEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPAuthPort) + httpAuthEndpoint = fmt.Sprintf("%v:%v", config.HTTPIp, config.HTTPAuthPort) if err := startAuthHTTP(authApis); err != nil { return err } @@ -201,11 +202,11 @@ func startAuthHTTP(apis []rpc.API) (err error) { } utils.Logger().Info(). - Str("url", fmt.Sprintf("http://%s", httpEndpoint)). + Str("url", fmt.Sprintf("http://%s", httpAuthEndpoint)). Str("cors", strings.Join(httpOrigins, ",")). Str("vhosts", strings.Join(httpVirtualHosts, ",")). Msg("HTTP endpoint opened") - fmt.Printf("Started RPC server at: %v\n", httpEndpoint) + fmt.Printf("Started RPC server at: %v\n", httpAuthEndpoint) return nil } From 1182c7a6b3375689e39619d8d22f3e455c030c91 Mon Sep 17 00:00:00 2001 From: Lutty Date: Sat, 28 Aug 2021 17:17:20 +0800 Subject: [PATCH 7/8] fix bug --- rpc/rpc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpc/rpc.go b/rpc/rpc.go index 0487b8de0b..eb79706e69 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -195,7 +195,7 @@ func startHTTP(apis []rpc.API) (err error) { func startAuthHTTP(apis []rpc.API) (err error) { httpListener, httpHandler, err = rpc.StartHTTPEndpoint( - httpEndpoint, apis, HTTPModules, httpOrigins, httpVirtualHosts, httpTimeouts, + httpAuthEndpoint, apis, HTTPModules, httpOrigins, httpVirtualHosts, httpTimeouts, ) if err != nil { return err From 52d55e925900a0814f3376ea5a6d78daa2136735 Mon Sep 17 00:00:00 2001 From: Lutty Date: Sat, 28 Aug 2021 21:35:02 +0800 Subject: [PATCH 8/8] change tips --- rpc/rpc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpc/rpc.go b/rpc/rpc.go index eb79706e69..0c075c0867 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -206,7 +206,7 @@ func startAuthHTTP(apis []rpc.API) (err error) { Str("cors", strings.Join(httpOrigins, ",")). Str("vhosts", strings.Join(httpVirtualHosts, ",")). Msg("HTTP endpoint opened") - fmt.Printf("Started RPC server at: %v\n", httpAuthEndpoint) + fmt.Printf("Started Auth-RPC server at: %v\n", httpAuthEndpoint) return nil }