From 1ba4b86c7e2a63864aa84698039e7a1d46082701 Mon Sep 17 00:00:00 2001 From: Mikhail Rogachev Date: Wed, 26 Jun 2024 16:33:22 +0200 Subject: [PATCH] feat_: Add WS settings to the node config (#5346) * feat_: add ability to enable http and ws connections from the client app * feat_: add websocket option for api config * fix_: use new api options in statusd * chore_: add test for `overrideApiConfig` --- api/default_test.go | 45 +++++++++++++++++++++++++++++ api/defaults.go | 18 +++++++++--- api/geth_backend.go | 4 +++ appdatabase/node_config_test.go | 3 ++ cmd/status-cli/util.go | 7 +++-- cmd/statusd/main.go | 11 +++++-- node/geth_node.go | 8 ++++- params/config.go | 9 ++++++ protocol/requests/create_account.go | 11 +++++-- protocol/requests/login.go | 2 ++ 10 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 api/default_test.go diff --git a/api/default_test.go b/api/default_test.go new file mode 100644 index 000000000..571b4dbf0 --- /dev/null +++ b/api/default_test.go @@ -0,0 +1,45 @@ +package api + +import ( + "testing" + + "github.com/status-im/status-go/params" + "github.com/status-im/status-go/protocol/requests" + + "github.com/stretchr/testify/require" +) + +func TestOverrideApiConfig(t *testing.T) { + newNodeConfig := ¶ms.NodeConfig{ + APIModules: "test, eth, wakuv2", + ConnectorConfig: params.ConnectorConfig{Enabled: true}, + HTTPEnabled: true, + HTTPHost: "0.0.0.0", + HTTPPort: 8545, + WSEnabled: false, + WSHost: "127.0.0.1", + WSPort: 8586, + } + + apiConfig := &requests.APIConfig{ + APIModules: "connector", + ConnectorEnabled: false, + HTTPEnabled: false, + HTTPHost: "127.0.0.1", + HTTPPort: 8080, + WSEnabled: true, + WSHost: "192.168.0.1", + WSPort: 7777, + } + + overrideApiConfig(newNodeConfig, apiConfig) + + require.Equal(t, apiConfig.APIModules, newNodeConfig.APIModules) + require.Equal(t, apiConfig.ConnectorEnabled, newNodeConfig.ConnectorConfig.Enabled) + require.Equal(t, apiConfig.HTTPEnabled, newNodeConfig.HTTPEnabled) + require.Equal(t, apiConfig.HTTPHost, newNodeConfig.HTTPHost) + require.Equal(t, apiConfig.HTTPPort, newNodeConfig.HTTPPort) + require.Equal(t, apiConfig.WSEnabled, newNodeConfig.WSEnabled) + require.Equal(t, apiConfig.WSHost, newNodeConfig.WSHost) + require.Equal(t, apiConfig.WSPort, newNodeConfig.WSPort) +} diff --git a/api/defaults.go b/api/defaults.go index 504228250..47ce776f4 100644 --- a/api/defaults.go +++ b/api/defaults.go @@ -215,6 +215,19 @@ func buildWalletConfig(request *requests.WalletSecretsConfig) params.WalletConfi return walletConfig } +func overrideApiConfig(nodeConfig *params.NodeConfig, config *requests.APIConfig) { + nodeConfig.APIModules = config.APIModules + nodeConfig.ConnectorConfig.Enabled = config.ConnectorEnabled + + nodeConfig.HTTPEnabled = config.HTTPEnabled + nodeConfig.HTTPHost = config.HTTPHost + nodeConfig.HTTPPort = config.HTTPPort + + nodeConfig.WSEnabled = config.WSEnabled + nodeConfig.WSHost = config.WSHost + nodeConfig.WSPort = config.WSPort +} + func defaultNodeConfig(installationID string, request *requests.CreateAccount, opts ...params.Option) (*params.NodeConfig, error) { // Set mainnet nodeConfig := ¶ms.NodeConfig{} @@ -339,10 +352,7 @@ func defaultNodeConfig(installationID string, request *requests.CreateAccount, o } if request.APIConfig != nil { - nodeConfig.HTTPEnabled = true - nodeConfig.HTTPHost = request.APIConfig.HTTPHost - nodeConfig.HTTPPort = request.APIConfig.HTTPPort - nodeConfig.APIModules = request.APIConfig.APIModules + overrideApiConfig(nodeConfig, request.APIConfig) } for _, opt := range opts { diff --git a/api/geth_backend.go b/api/geth_backend.go index c1cbb1dc8..2d326b1f3 100644 --- a/api/geth_backend.go +++ b/api/geth_backend.go @@ -601,6 +601,10 @@ func (b *GethStatusBackend) loginAccount(request *requests.Login) error { b.overrideNetworks(b.config, request) + if request.APIConfig != nil { + overrideApiConfig(b.config, request.APIConfig) + } + err = b.setupLogSettings() if err != nil { return errors.Wrap(err, "failed to setup log settings") diff --git a/appdatabase/node_config_test.go b/appdatabase/node_config_test.go index a36b3c106..b3b13ca0f 100644 --- a/appdatabase/node_config_test.go +++ b/appdatabase/node_config_test.go @@ -154,6 +154,9 @@ func randomNodeConfig() *params.NodeConfig { HTTPPort: randomInt(math.MaxInt64), HTTPVirtualHosts: randomStringSlice(), HTTPCors: randomStringSlice(), + WSEnabled: false, // NOTE: leaving ws field idle since we are moving away from the storing the whole config + WSHost: "", + WSPort: 0, IPCEnabled: randomBool(), IPCFile: randomString(), LogEnabled: randomBool(), diff --git a/cmd/status-cli/util.go b/cmd/status-cli/util.go index 27f9d9771..7a7d21305 100644 --- a/cmd/status-cli/util.go +++ b/cmd/status-cli/util.go @@ -138,9 +138,10 @@ func createAccountAndLogin(b *api.GethStatusBackend, name, rootDataDir, password RootDataDir: rootDataDir, LogFilePath: "log", APIConfig: &requests.APIConfig{ - APIModules: apiModules, - HTTPHost: "127.0.0.1", - HTTPPort: port, + APIModules: apiModules, + HTTPEnabled: true, + HTTPHost: "127.0.0.1", + HTTPPort: port, }, TelemetryServerURL: telemetryUrl, } diff --git a/cmd/statusd/main.go b/cmd/statusd/main.go index d341d8c10..40f2676ea 100644 --- a/cmd/statusd/main.go +++ b/cmd/statusd/main.go @@ -211,9 +211,14 @@ func main() { CustomizationColor: "0x000000", BackupDisabledDataDir: config.DataDir, APIConfig: &requests.APIConfig{ - HTTPHost: config.HTTPHost, - HTTPPort: config.HTTPPort, - APIModules: config.APIModules, + ConnectorEnabled: config.ClusterConfig.Enabled, + HTTPEnabled: config.HTTPEnabled, + HTTPHost: config.HTTPHost, + HTTPPort: config.HTTPPort, + WSEnabled: config.WSEnabled, + WSHost: config.WSHost, + WSPort: config.WSPort, + APIModules: config.APIModules, }, NetworkID: &config.NetworkID, TestOverrideNetworks: config.Networks, diff --git a/node/geth_node.go b/node/geth_node.go index 701b3039f..78aac7067 100644 --- a/node/geth_node.go +++ b/node/geth_node.go @@ -94,7 +94,6 @@ func newGethNodeConfig(config *params.NodeConfig) (*node.Config, error) { MaxPeers: maxPeers, MaxPendingPeers: maxPendingPeers, }, - HTTPModules: config.FormatAPIModules(), } if config.IPCEnabled { @@ -107,12 +106,19 @@ func newGethNodeConfig(config *params.NodeConfig) (*node.Config, error) { } if config.HTTPEnabled { + nc.HTTPModules = config.FormatAPIModules() nc.HTTPHost = config.HTTPHost nc.HTTPPort = config.HTTPPort nc.HTTPVirtualHosts = config.HTTPVirtualHosts nc.HTTPCors = config.HTTPCors } + if config.WSEnabled { + nc.WSModules = config.FormatAPIModules() + nc.WSHost = config.WSHost + nc.WSPort = config.WSPort + } + if config.ClusterConfig.Enabled { nc.P2P.BootstrapNodesV5 = parseNodesV5(config.ClusterConfig.BootNodes) nc.P2P.StaticNodes = parseNodes(config.ClusterConfig.StaticNodes) diff --git a/params/config.go b/params/config.go index 8c0cfd213..fcb64b19b 100644 --- a/params/config.go +++ b/params/config.go @@ -375,6 +375,15 @@ type NodeConfig struct { // HTTPPort is the TCP port number on which to start the Geth's HTTP RPC server. HTTPPort int + // WSEnabled specifies whether the Websocket RPC server is to be enabled by default. + WSEnabled bool + + // WSHost is the host interface on which to start Geth's Websocket RPC server. + WSHost string + + // WSPort is the TCP port number on which to start the Geth's Websocket RPC server. + WSPort int + // HTTPVirtualHosts is the list of virtual hostnames which are allowed on incoming requests. // This is by default {'localhost'}. Using this prevents attacks like // DNS rebinding, which bypasses SOP by simply masquerading as being within the same diff --git a/protocol/requests/create_account.go b/protocol/requests/create_account.go index d44f0ae84..932b4d762 100644 --- a/protocol/requests/create_account.go +++ b/protocol/requests/create_account.go @@ -21,9 +21,14 @@ type ImageCropRectangle struct { } type APIConfig struct { - APIModules string `json:"apiModules"` - HTTPHost string `json:"httpHost"` - HTTPPort int `json:"httpPort"` + APIModules string `json:"apiModules"` + ConnectorEnabled bool `json:"connectorEnabled"` + HTTPEnabled bool `json:"httpEnabled"` + HTTPHost string `json:"httpHost"` + HTTPPort int `json:"httpPort"` + WSEnabled bool `json:"wsEnabled"` + WSHost string `json:"wsHost"` + WSPort int `json:"wsPort"` } type CreateAccount struct { diff --git a/protocol/requests/login.go b/protocol/requests/login.go index 1b0f871da..02e7fe257 100644 --- a/protocol/requests/login.go +++ b/protocol/requests/login.go @@ -34,6 +34,8 @@ type Login struct { Mnemonic string `json:"mnemonic"` WalletSecretsConfig + + APIConfig *APIConfig `json:"apiConfig"` } func (c *Login) Validate() error {