fix_: prevent multiple http or websocket lauches

fixes [5436](https://github.com/status-im/status-go/issues/5436)
This commit is contained in:
Godfrain Jacques 2024-06-28 13:45:21 -07:00
parent 5c349d1938
commit 9f806c5160
2 changed files with 56 additions and 0 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
}