Skip to content

Commit

Permalink
add auth-ws port (#3932)
Browse files Browse the repository at this point in the history
* add auth-ws port
  • Loading branch information
LuttyYang authored Nov 18, 2021
1 parent e90e90b commit 3bce74f
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 22 deletions.
8 changes: 8 additions & 0 deletions cmd/harmony/config_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,12 @@ func init() {
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
}
}
9 changes: 5 additions & 4 deletions cmd/harmony/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
)

const tomlConfigVersion = "2.4.0"
const tomlConfigVersion = "2.5.0"

const (
defNetworkType = nodeconfig.Mainnet
Expand Down Expand Up @@ -39,9 +39,10 @@ var defaultConfig = harmonyconfig.HarmonyConfig{
RosettaPort: nodeconfig.DefaultRosettaPort,
},
WS: harmonyconfig.WsConfig{
Enabled: true,
IP: "127.0.0.1",
Port: nodeconfig.DefaultWSPort,
Enabled: true,
IP: "127.0.0.1",
Port: nodeconfig.DefaultWSPort,
AuthPort: nodeconfig.DefaultAuthWSPort,
},
RPCOpt: harmonyconfig.RpcOptConfig{
DebugEnabled: false,
Expand Down
10 changes: 10 additions & 0 deletions cmd/harmony/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var (
wsEnabledFlag,
wsIPFlag,
wsPortFlag,
wsAuthPortFlag,
}

rpcOptFlags = []cli.Flag{
Expand Down Expand Up @@ -653,6 +654,11 @@ var (
Usage: "port for websocket endpoint",
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) {
Expand All @@ -665,6 +671,9 @@ func applyWSFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) {
if cli.IsFlagChanged(cmd, wsPortFlag) {
config.WS.Port = cli.GetIntFlagValue(cmd, wsPortFlag)
}
if cli.IsFlagChanged(cmd, wsAuthPortFlag) {
config.WS.AuthPort = cli.GetIntFlagValue(cmd, wsAuthPortFlag)
}
}

// rpc opt flags
Expand Down Expand Up @@ -1381,6 +1390,7 @@ func applyLegacyMiscFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfi
config.HTTP.AuthPort = nodeconfig.GetRPCAuthHTTPPortFromBase(legacyPort)
config.HTTP.RosettaPort = nodeconfig.GetRosettaHTTPPortFromBase(legacyPort)
config.WS.Port = nodeconfig.GetWSPortFromBase(legacyPort)
config.WS.AuthPort = nodeconfig.GetWSAuthPortFromBase(legacyPort)

legPortStr := strconv.Itoa(legacyPort)
syncPort, _ := strconv.Atoi(legacysync.GetSyncingPort(legPortStr))
Expand Down
37 changes: 25 additions & 12 deletions cmd/harmony/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ func TestHarmonyFlags(t *testing.T) {
RequestsPerSecond: 1000,
},
WS: harmonyconfig.WsConfig{
Enabled: true,
IP: "127.0.0.1",
Port: 9800,
Enabled: true,
IP: "127.0.0.1",
Port: 9800,
AuthPort: 9801,
},
Consensus: &harmonyconfig.ConsensusConfig{
MinPeers: 6,
Expand Down Expand Up @@ -529,25 +530,37 @@ func TestWSFlags(t *testing.T) {
{
args: []string{"--ws=false"},
expConfig: harmonyconfig.WsConfig{
Enabled: false,
IP: defaultConfig.WS.IP,
Port: defaultConfig.WS.Port,
Enabled: false,
IP: defaultConfig.WS.IP,
Port: defaultConfig.WS.Port,
AuthPort: defaultConfig.WS.AuthPort,
},
},
{
args: []string{"--ws", "--ws.ip", "8.8.8.8", "--ws.port", "9001"},
expConfig: harmonyconfig.WsConfig{
Enabled: true,
IP: "8.8.8.8",
Port: 9001,
Enabled: true,
IP: "8.8.8.8",
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,
},
},
{
args: []string{"--ip", "8.8.8.8", "--port", "9001", "--public_rpc"},
expConfig: harmonyconfig.WsConfig{
Enabled: true,
IP: nodeconfig.DefaultPublicListenIP,
Port: 9801,
Enabled: true,
IP: nodeconfig.DefaultPublicListenIP,
Port: 9801,
AuthPort: 9802,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions cmd/harmony/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func setupNodeAndRun(hc harmonyconfig.HarmonyConfig) {
WSEnabled: hc.WS.Enabled,
WSIp: hc.WS.IP,
WSPort: hc.WS.Port,
WSAuthPort: hc.WS.AuthPort,
DebugEnabled: hc.RPCOpt.DebugEnabled,
RateLimiterEnabled: hc.RPCOpt.RateLimterEnabled,
RequestsPerSecond: hc.RPCOpt.RequestsPerSecond,
Expand Down
7 changes: 4 additions & 3 deletions internal/configs/harmony/harmony.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ type HttpConfig struct {
}

type WsConfig struct {
Enabled bool
IP string
Port int
Enabled bool
IP string
Port int
AuthPort int
}

type RpcOptConfig struct {
Expand Down
7 changes: 4 additions & 3 deletions internal/configs/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ type RPCServerConfig struct {
HTTPPort int
HTTPAuthPort int

WSEnabled bool
WSIp string
WSPort int
WSEnabled bool
WSIp string
WSPort int
WSAuthPort int

DebugEnabled bool

Expand Down
10 changes: 10 additions & 0 deletions internal/configs/node/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const (
DefaultRosettaPort = 9700
// DefaultWSPort is the default port for web socket endpoint. The actual port used is
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 = 9900
// DefaultP2PConcurrency is the default P2P concurrency, 0 means is set the default value of P2P Discovery, the actual value is 10
Expand All @@ -79,6 +81,9 @@ const (
// rpcWSPortOffSet is the port offset for RPC websocket requests
rpcWSPortOffSet = 800

// rpcWSAuthPortOffSet is the port offset for RPC Auth websocket requests
rpcWSAuthPortOffSet = 801

// prometheusHTTPPortOffset is the port offset for prometheus HTTP requests
prometheusHTTPPortOffset = 900
)
Expand Down Expand Up @@ -144,6 +149,11 @@ func GetWSPortFromBase(basePort int) int {
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
func GetPrometheusHTTPPortFromBase(basePort int) int {
return basePort + prometheusHTTPPortOffset
Expand Down
19 changes: 19 additions & 0 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var (
httpEndpoint = ""
httpAuthEndpoint = ""
wsEndpoint = ""
wsAuthEndpoint = ""
httpVirtualHosts = []string{"*"}
httpTimeouts = rpc.DefaultHTTPTimeouts
httpOrigins = []string{"*"}
Expand Down Expand Up @@ -91,6 +92,11 @@ func StartServers(hmy *hmy.Harmony, apis []rpc.API, config nodeconfig.RPCServerC
if err := startWS(apis); err != nil {
return err
}

wsAuthEndpoint = fmt.Sprintf("%v:%v", config.WSIp, config.WSAuthPort)
if err := startAuthWS(apis); err != nil {
return err
}
}

return nil
Expand Down Expand Up @@ -220,3 +226,16 @@ func startWS(apis []rpc.API) (err error) {
fmt.Printf("Started WS server at: %v\n", wsEndpoint)
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
}

0 comments on commit 3bce74f

Please sign in to comment.