feat: Enable WS and HTTP RPC servers for status desktop through config (#15168)

* feat: Enable http and websocket rpc client on desktop

* chore: static-typed structure for apiConfig

* fix_: prevent multiple http or websocket lauches

fixes [5436](https://github.com/status-im/status-go/issues/5436)

---------

Co-authored-by: Godfrain Jacques <gkounkou@gmail.com>
This commit is contained in:
Mikhail Rogachev 2024-07-02 18:01:19 +02:00 committed by GitHub
parent e199e7f9da
commit 8754b0193d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 69 additions and 4 deletions

View File

@ -0,0 +1,13 @@
import net
# Util function to test if a port is busy
proc isPortBusy*(port: Port): bool =
var sock: Socket
try:
sock = newSocket()
sock.setSockOpt(OptReuseAddr, true)
sock.bindAddr(port)
sock.close()
return false
except OSError:
return true

View File

@ -0,0 +1,43 @@
import json
include ../../../common/net_utils
type APIConfig* = object
apiModules*: string
connectorEnabled*: bool
httpEnabled*: bool
httpHost*: string
httpPort*: int
wsEnabled*: bool
wsHost*: string
wsPort*: int
proc checkAndSetPort(port: Port, isEnabled: var bool) =
if isPortBusy(port):
isEnabled = false
proc defaultAPIConfig*(): APIConfig =
result.apiModules = "connector"
result.connectorEnabled = true
result.httpEnabled = true
checkAndSetPort(Port(8545), result.httpEnabled)
result.httpHost = "0.0.0.0"
result.httpPort = 8545
result.wsEnabled = true
checkAndSetPort(Port(8586), result.wsEnabled)
result.wsHost = "0.0.0.0"
result.wsPort = 8586
proc toJson*(self: APIConfig): JsonNode =
return %* {
"apiModules": self.apiModules,
"connectorEnabled": self.connectorEnabled,
"httpEnabled": self.httpEnabled,
"httpHost": self.httpHost,
"httpPort": self.httpPort,
"wsEnabled": self.wsEnabled,
"wsHost": self.wsHost,
"wsPort": self.wsPort
}

View File

@ -1,9 +1,11 @@
import json, std/options
import ./wallet_secretes_config
import ./image_crop_rectangle
import wallet_secretes_config
import image_crop_rectangle
import api_config
export wallet_secretes_config
export image_crop_rectangle
export api_config
type
CreateAccountRequest* = object
@ -40,6 +42,7 @@ type
keycardInstanceUID*: string
keycardPairingDataFile*: string
apiConfig*: APIConfig
proc toJson*(self: CreateAccountRequest): JsonNode =
result = %*{
@ -59,6 +62,7 @@ proc toJson*(self: CreateAccountRequest): JsonNode =
"upstreamConfig": self.upstreamConfig,
"keycardInstanceUID": self.keycardInstanceUID,
"keycardPairingDataFile": self.keycardPairingDataFile,
"apiConfig": self.apiConfig
}
if self.logLevel.isSome():
@ -90,4 +94,3 @@ proc toJson*(self: CreateAccountRequest): JsonNode =
for key, value in self.walletSecretsConfig.toJson().pairs():
result[key] = value

View File

@ -1,7 +1,9 @@
import json
import json, std/options
import wallet_secretes_config
import api_config
export wallet_secretes_config
export api_config
type
LoginAccountRequest* = object
@ -14,6 +16,7 @@ type
keycardWhisperPrivateKey*: string
mnemonic*: string
walletSecretsConfig*: WalletSecretsConfig
apiConfig*: APIConfig
proc toJson*(self: LoginAccountRequest): JsonNode =
result = %* {
@ -25,6 +28,7 @@ proc toJson*(self: LoginAccountRequest): JsonNode =
"bandwidthStatsEnabled": self.bandwidthStatsEnabled,
"keycardWhisperPrivateKey": self.keycardWhisperPrivateKey,
"mnemonic": self.mnemonic,
"apiConfig": self.apiConfig
}
for key, value in self.walletSecretsConfig.toJson().pairs():
result[key] = value

View File

@ -238,6 +238,7 @@ QtObject:
torrentConfigPort: some(TORRENT_CONFIG_PORT),
keycardPairingDataFile: main_constants.KEYCARDPAIRINGDATAFILE,
walletSecretsConfig: self.buildWalletSecrets(),
apiConfig: defaultApiConfig()
)
proc createAccountAndLogin*(self: Service, password: string, displayName: string, imagePath: string, imageCropRectangle: ImageCropRectangle): string =
@ -421,6 +422,7 @@ QtObject:
mnemonic: mnemonic,
walletSecretsConfig: self.buildWalletSecrets(),
bandwidthStatsEnabled: true,
apiConfig: defaultApiConfig()
)
if main_constants.runtimeLogLevelSet():