add auth-ws port (#3932)

* add auth-ws port
pull/3934/head
LuttyYang 3 years ago committed by GitHub
parent e90e90bc5e
commit 3bce74fdbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      cmd/harmony/config_migrations.go
  2. 3
      cmd/harmony/default.go
  3. 10
      cmd/harmony/flags.go
  4. 13
      cmd/harmony/flags_test.go
  5. 1
      cmd/harmony/main.go
  6. 1
      internal/configs/harmony/harmony.go
  7. 1
      internal/configs/node/config.go
  8. 10
      internal/configs/node/network.go
  9. 19
      rpc/rpc.go

@ -212,4 +212,12 @@ func init() {
return confTree return confTree
} }
migrations["2.4.0"] = func(confTree *toml.Tree) *toml.Tree {
if confTree.Get("WS.AuthPort") == nil {
confTree.Set("WS.AuthPort", defaultConfig.WS.AuthPort)
}
confTree.Set("Version", "2.5.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.4.0" const tomlConfigVersion = "2.5.0"
const ( const (
defNetworkType = nodeconfig.Mainnet defNetworkType = nodeconfig.Mainnet
@ -42,6 +42,7 @@ var defaultConfig = harmonyconfig.HarmonyConfig{
Enabled: true, Enabled: true,
IP: "127.0.0.1", IP: "127.0.0.1",
Port: nodeconfig.DefaultWSPort, Port: nodeconfig.DefaultWSPort,
AuthPort: nodeconfig.DefaultAuthWSPort,
}, },
RPCOpt: harmonyconfig.RpcOptConfig{ RPCOpt: harmonyconfig.RpcOptConfig{
DebugEnabled: false, DebugEnabled: false,

@ -74,6 +74,7 @@ var (
wsEnabledFlag, wsEnabledFlag,
wsIPFlag, wsIPFlag,
wsPortFlag, wsPortFlag,
wsAuthPortFlag,
} }
rpcOptFlags = []cli.Flag{ rpcOptFlags = []cli.Flag{
@ -653,6 +654,11 @@ var (
Usage: "port for websocket endpoint", Usage: "port for websocket endpoint",
DefValue: defaultConfig.WS.Port, DefValue: defaultConfig.WS.Port,
} }
wsAuthPortFlag = cli.IntFlag{
Name: "ws.auth-port",
Usage: "port for websocket auth endpoint",
DefValue: defaultConfig.WS.AuthPort,
}
) )
func applyWSFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) { func applyWSFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) {
@ -665,6 +671,9 @@ func applyWSFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) {
if cli.IsFlagChanged(cmd, wsPortFlag) { if cli.IsFlagChanged(cmd, wsPortFlag) {
config.WS.Port = cli.GetIntFlagValue(cmd, wsPortFlag) config.WS.Port = cli.GetIntFlagValue(cmd, wsPortFlag)
} }
if cli.IsFlagChanged(cmd, wsAuthPortFlag) {
config.WS.AuthPort = cli.GetIntFlagValue(cmd, wsAuthPortFlag)
}
} }
// rpc opt flags // rpc opt flags
@ -1381,6 +1390,7 @@ func applyLegacyMiscFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfi
config.HTTP.AuthPort = nodeconfig.GetRPCAuthHTTPPortFromBase(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)
config.WS.AuthPort = nodeconfig.GetWSAuthPortFromBase(legacyPort)
legPortStr := strconv.Itoa(legacyPort) legPortStr := strconv.Itoa(legacyPort)
syncPort, _ := strconv.Atoi(legacysync.GetSyncingPort(legPortStr)) syncPort, _ := strconv.Atoi(legacysync.GetSyncingPort(legPortStr))

@ -81,6 +81,7 @@ func TestHarmonyFlags(t *testing.T) {
Enabled: true, Enabled: true,
IP: "127.0.0.1", IP: "127.0.0.1",
Port: 9800, Port: 9800,
AuthPort: 9801,
}, },
Consensus: &harmonyconfig.ConsensusConfig{ Consensus: &harmonyconfig.ConsensusConfig{
MinPeers: 6, MinPeers: 6,
@ -532,6 +533,7 @@ func TestWSFlags(t *testing.T) {
Enabled: false, Enabled: false,
IP: defaultConfig.WS.IP, IP: defaultConfig.WS.IP,
Port: defaultConfig.WS.Port, Port: defaultConfig.WS.Port,
AuthPort: defaultConfig.WS.AuthPort,
}, },
}, },
{ {
@ -540,6 +542,16 @@ func TestWSFlags(t *testing.T) {
Enabled: true, Enabled: true,
IP: "8.8.8.8", IP: "8.8.8.8",
Port: 9001, Port: 9001,
AuthPort: defaultConfig.WS.AuthPort,
},
},
{
args: []string{"--ws", "--ws.ip", "8.8.8.8", "--ws.auth-port", "9001"},
expConfig: harmonyconfig.WsConfig{
Enabled: true,
IP: "8.8.8.8",
Port: defaultConfig.WS.Port,
AuthPort: 9001,
}, },
}, },
{ {
@ -548,6 +560,7 @@ func TestWSFlags(t *testing.T) {
Enabled: true, Enabled: true,
IP: nodeconfig.DefaultPublicListenIP, IP: nodeconfig.DefaultPublicListenIP,
Port: 9801, Port: 9801,
AuthPort: 9802,
}, },
}, },
} }

@ -320,6 +320,7 @@ func setupNodeAndRun(hc harmonyconfig.HarmonyConfig) {
WSEnabled: hc.WS.Enabled, WSEnabled: hc.WS.Enabled,
WSIp: hc.WS.IP, WSIp: hc.WS.IP,
WSPort: hc.WS.Port, WSPort: hc.WS.Port,
WSAuthPort: hc.WS.AuthPort,
DebugEnabled: hc.RPCOpt.DebugEnabled, DebugEnabled: hc.RPCOpt.DebugEnabled,
RateLimiterEnabled: hc.RPCOpt.RateLimterEnabled, RateLimiterEnabled: hc.RPCOpt.RateLimterEnabled,
RequestsPerSecond: hc.RPCOpt.RequestsPerSecond, RequestsPerSecond: hc.RPCOpt.RequestsPerSecond,

@ -148,6 +148,7 @@ type WsConfig struct {
Enabled bool Enabled bool
IP string IP string
Port int Port int
AuthPort int
} }
type RpcOptConfig struct { type RpcOptConfig struct {

@ -106,6 +106,7 @@ type RPCServerConfig struct {
WSEnabled bool WSEnabled bool
WSIp string WSIp string
WSPort int WSPort int
WSAuthPort int
DebugEnabled bool DebugEnabled bool

@ -54,6 +54,8 @@ const (
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
DefaultWSPort = 9800 DefaultWSPort = 9800
// DefaultAuthWSPort is the default port for web socket auth endpoint. The actual port used is
DefaultAuthWSPort = 9801
// DefaultPrometheusPort is the default prometheus port. The actual port used is 9000+900 // DefaultPrometheusPort is the default prometheus port. The actual port used is 9000+900
DefaultPrometheusPort = 9900 DefaultPrometheusPort = 9900
// DefaultP2PConcurrency is the default P2P concurrency, 0 means is set the default value of P2P Discovery, the actual value is 10 // DefaultP2PConcurrency is the default P2P concurrency, 0 means is set the default value of P2P Discovery, the actual value is 10
@ -79,6 +81,9 @@ const (
// rpcWSPortOffSet is the port offset for RPC websocket requests // rpcWSPortOffSet is the port offset for RPC websocket requests
rpcWSPortOffSet = 800 rpcWSPortOffSet = 800
// rpcWSAuthPortOffSet is the port offset for RPC Auth websocket requests
rpcWSAuthPortOffSet = 801
// prometheusHTTPPortOffset is the port offset for prometheus HTTP requests // prometheusHTTPPortOffset is the port offset for prometheus HTTP requests
prometheusHTTPPortOffset = 900 prometheusHTTPPortOffset = 900
) )
@ -144,6 +149,11 @@ func GetWSPortFromBase(basePort int) int {
return basePort + rpcWSPortOffSet return basePort + rpcWSPortOffSet
} }
// GetWSAuthPortFromBase return the Websocket port from the base auth port
func GetWSAuthPortFromBase(basePort int) int {
return basePort + rpcWSAuthPortOffSet
}
// GetPrometheusHTTPPortFromBase return the prometheus HTTP port from base port // GetPrometheusHTTPPortFromBase return the prometheus HTTP port from base port
func GetPrometheusHTTPPortFromBase(basePort int) int { func GetPrometheusHTTPPortFromBase(basePort int) int {
return basePort + prometheusHTTPPortOffset return basePort + prometheusHTTPPortOffset

@ -55,6 +55,7 @@ var (
httpEndpoint = "" httpEndpoint = ""
httpAuthEndpoint = "" httpAuthEndpoint = ""
wsEndpoint = "" wsEndpoint = ""
wsAuthEndpoint = ""
httpVirtualHosts = []string{"*"} httpVirtualHosts = []string{"*"}
httpTimeouts = rpc.DefaultHTTPTimeouts httpTimeouts = rpc.DefaultHTTPTimeouts
httpOrigins = []string{"*"} httpOrigins = []string{"*"}
@ -91,6 +92,11 @@ func StartServers(hmy *hmy.Harmony, apis []rpc.API, config nodeconfig.RPCServerC
if err := startWS(apis); err != nil { if err := startWS(apis); err != nil {
return err return err
} }
wsAuthEndpoint = fmt.Sprintf("%v:%v", config.WSIp, config.WSAuthPort)
if err := startAuthWS(apis); err != nil {
return err
}
} }
return nil return nil
@ -220,3 +226,16 @@ func startWS(apis []rpc.API) (err error) {
fmt.Printf("Started WS server at: %v\n", wsEndpoint) fmt.Printf("Started WS server at: %v\n", wsEndpoint)
return nil return nil
} }
func startAuthWS(apis []rpc.API) (err error) {
wsListener, wsHandler, err = rpc.StartWSEndpoint(wsAuthEndpoint, apis, WSModules, wsOrigins, true)
if err != nil {
return err
}
utils.Logger().Info().
Str("url", fmt.Sprintf("ws://%s", wsListener.Addr())).
Msg("WebSocket endpoint opened")
fmt.Printf("Started Auth-WS server at: %v\n", wsAuthEndpoint)
return nil
}

Loading…
Cancel
Save