2022-10-11 03:58:44 +00:00
|
|
|
import
|
|
|
|
std/[strutils, sequtils, tables],
|
|
|
|
confutils,
|
|
|
|
chronos,
|
|
|
|
stew/shims/net,
|
|
|
|
chronicles/topics_registry
|
|
|
|
import
|
|
|
|
libp2p/protocols/ping,
|
2022-10-18 15:13:42 +00:00
|
|
|
libp2p/crypto/[crypto, secp],
|
2023-04-11 15:05:12 +00:00
|
|
|
libp2p/nameresolving/dnsresolver,
|
|
|
|
libp2p/multicodec
|
2022-10-11 03:58:44 +00:00
|
|
|
import
|
2023-02-06 09:03:30 +00:00
|
|
|
../../waku/v2/node/peer_manager,
|
2023-04-24 14:37:54 +00:00
|
|
|
../../waku/v2/waku_core,
|
|
|
|
../../waku/v2/waku_node
|
2022-10-11 03:58:44 +00:00
|
|
|
|
|
|
|
# protocols and their tag
|
|
|
|
const ProtocolsTable = {
|
|
|
|
"store": "/vac/waku/store/",
|
|
|
|
"relay": "/vac/waku/relay/",
|
|
|
|
"lightpush": "/vac/waku/lightpush/",
|
|
|
|
"filter": "/vac/waku/filter/",
|
|
|
|
}.toTable
|
|
|
|
|
2023-04-11 15:05:12 +00:00
|
|
|
const WebSocketPortOffset = 1000
|
|
|
|
|
2022-10-11 03:58:44 +00:00
|
|
|
# cli flags
|
|
|
|
type
|
|
|
|
WakuCanaryConf* = object
|
|
|
|
address* {.
|
|
|
|
desc: "Multiaddress of the peer node to attempt to dial",
|
|
|
|
defaultValue: "",
|
|
|
|
name: "address",
|
2023-04-11 15:05:12 +00:00
|
|
|
abbr: "a".}: string
|
2022-10-11 03:58:44 +00:00
|
|
|
|
|
|
|
timeout* {.
|
|
|
|
desc: "Timeout to consider that the connection failed",
|
|
|
|
defaultValue: chronos.seconds(10),
|
|
|
|
name: "timeout",
|
2023-04-11 15:05:12 +00:00
|
|
|
abbr: "t".}: chronos.Duration
|
2022-10-11 03:58:44 +00:00
|
|
|
|
|
|
|
protocols* {.
|
|
|
|
desc: "Protocol required to be supported: store,relay,lightpush,filter (can be used multiple times)",
|
|
|
|
name: "protocol",
|
2023-04-11 15:05:12 +00:00
|
|
|
abbr: "p".}: seq[string]
|
2022-10-11 03:58:44 +00:00
|
|
|
|
|
|
|
logLevel* {.
|
|
|
|
desc: "Sets the log level",
|
|
|
|
defaultValue: LogLevel.DEBUG,
|
|
|
|
name: "log-level",
|
2023-04-11 15:05:12 +00:00
|
|
|
abbr: "l".}: LogLevel
|
2022-10-11 03:58:44 +00:00
|
|
|
|
2022-11-05 11:56:41 +00:00
|
|
|
nodePort* {.
|
|
|
|
desc: "Listening port for waku node",
|
|
|
|
defaultValue: 60000,
|
|
|
|
name: "node-port",
|
2023-04-11 15:05:12 +00:00
|
|
|
abbr: "np".}: uint16
|
|
|
|
|
|
|
|
## websocket secure config
|
|
|
|
websocketSecureKeyPath* {.
|
|
|
|
desc: "Secure websocket key path: '/path/to/key.txt' ",
|
|
|
|
defaultValue: ""
|
|
|
|
name: "websocket-secure-key-path".}: string
|
2022-11-05 11:56:41 +00:00
|
|
|
|
2023-04-11 15:05:12 +00:00
|
|
|
websocketSecureCertPath* {.
|
|
|
|
desc: "Secure websocket Certificate path: '/path/to/cert.txt' ",
|
|
|
|
defaultValue: ""
|
|
|
|
name: "websocket-secure-cert-path".}: string
|
2022-10-11 03:58:44 +00:00
|
|
|
|
2022-11-02 13:55:48 +00:00
|
|
|
proc parseCmdArg*(T: type chronos.Duration, p: string): T =
|
2022-10-11 03:58:44 +00:00
|
|
|
try:
|
2023-04-11 15:05:12 +00:00
|
|
|
result = chronos.seconds(parseInt(p))
|
2023-02-07 09:45:25 +00:00
|
|
|
except CatchableError:
|
2022-10-11 03:58:44 +00:00
|
|
|
raise newException(ConfigurationError, "Invalid timeout value")
|
|
|
|
|
2022-11-02 13:55:48 +00:00
|
|
|
proc completeCmdArg*(T: type chronos.Duration, val: string): seq[string] =
|
2022-10-11 03:58:44 +00:00
|
|
|
return @[]
|
|
|
|
|
|
|
|
# checks if rawProtocols (skipping version) are supported in nodeProtocols
|
|
|
|
proc areProtocolsSupported(
|
|
|
|
rawProtocols: seq[string],
|
|
|
|
nodeProtocols: seq[string]): bool =
|
|
|
|
|
|
|
|
var numOfSupportedProt: int = 0
|
|
|
|
|
|
|
|
for nodeProtocol in nodeProtocols:
|
|
|
|
for rawProtocol in rawProtocols:
|
|
|
|
let protocolTag = ProtocolsTable[rawProtocol]
|
|
|
|
if nodeProtocol.startsWith(protocolTag):
|
2023-04-11 15:05:12 +00:00
|
|
|
info "Supported protocol ok", expected = protocolTag,
|
|
|
|
supported = nodeProtocol
|
2022-10-11 03:58:44 +00:00
|
|
|
numOfSupportedProt += 1
|
|
|
|
break
|
|
|
|
|
|
|
|
if numOfSupportedProt == rawProtocols.len:
|
|
|
|
return true
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
2023-02-06 11:53:05 +00:00
|
|
|
proc main(rng: ref HmacDrbgContext): Future[int] {.async.} =
|
2022-10-11 03:58:44 +00:00
|
|
|
let conf: WakuCanaryConf = WakuCanaryConf.load()
|
|
|
|
|
2022-10-18 15:13:42 +00:00
|
|
|
# create dns resolver
|
|
|
|
let
|
|
|
|
nameServers = @[
|
|
|
|
initTAddress(ValidIpAddress.init("1.1.1.1"), Port(53)),
|
|
|
|
initTAddress(ValidIpAddress.init("1.0.0.1"), Port(53))]
|
|
|
|
resolver: DnsResolver = DnsResolver.new(nameServers)
|
|
|
|
|
2022-10-11 03:58:44 +00:00
|
|
|
if conf.logLevel != LogLevel.NONE:
|
|
|
|
setLogLevel(conf.logLevel)
|
|
|
|
|
|
|
|
# ensure input protocols are valid
|
|
|
|
for p in conf.protocols:
|
2022-11-24 13:11:23 +00:00
|
|
|
if p notin ProtocolsTable:
|
2023-04-11 15:05:12 +00:00
|
|
|
error "invalid protocol", protocol = p, valid = ProtocolsTable
|
2022-10-11 03:58:44 +00:00
|
|
|
raise newException(ConfigurationError, "Invalid cli flag values" & p)
|
|
|
|
|
|
|
|
info "Cli flags",
|
2023-04-11 15:05:12 +00:00
|
|
|
address = conf.address,
|
|
|
|
timeout = conf.timeout,
|
|
|
|
protocols = conf.protocols,
|
|
|
|
logLevel = conf.logLevel
|
2022-10-11 03:58:44 +00:00
|
|
|
|
2023-04-12 09:29:11 +00:00
|
|
|
let peerRes = parsePeerInfo(conf.address)
|
|
|
|
if peerRes.isErr():
|
|
|
|
error "Couldn't parse 'conf.address'", error = peerRes.error
|
|
|
|
return 1
|
|
|
|
|
|
|
|
let peer = peerRes.value
|
|
|
|
|
2022-10-11 03:58:44 +00:00
|
|
|
let
|
|
|
|
nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2023-04-05 12:27:11 +00:00
|
|
|
bindIp = ValidIpAddress.init("0.0.0.0")
|
2023-04-11 15:05:12 +00:00
|
|
|
wsBindPort = Port(conf.nodePort + WebSocketPortOffset)
|
2023-04-05 12:27:11 +00:00
|
|
|
nodeTcpPort = Port(conf.nodePort)
|
2023-04-11 15:05:12 +00:00
|
|
|
isWs = peer.addrs[0].contains(multiCodec("ws")).get()
|
|
|
|
isWss = peer.addrs[0].contains(multiCodec("wss")).get()
|
2023-04-05 12:27:11 +00:00
|
|
|
|
|
|
|
var builder = WakuNodeBuilder.init()
|
|
|
|
builder.withNodeKey(nodeKey)
|
2023-04-11 15:05:12 +00:00
|
|
|
|
|
|
|
let netConfig = NetConfig.init(
|
|
|
|
bindIp = bindIp,
|
|
|
|
bindPort = nodeTcpPort,
|
|
|
|
wsBindPort = wsBindPort,
|
|
|
|
wsEnabled = isWs,
|
|
|
|
wssEnabled = isWss,
|
|
|
|
)
|
|
|
|
|
|
|
|
if isWss and (conf.websocketSecureKeyPath.len == 0 or
|
|
|
|
conf.websocketSecureCertPath.len == 0):
|
|
|
|
error "WebSocket Secure requires key and certificate, see --help"
|
|
|
|
return 1
|
|
|
|
|
|
|
|
builder.withNetworkConfiguration(netConfig.tryGet())
|
|
|
|
builder.withSwitchConfiguration(
|
|
|
|
secureKey = some(conf.websocketSecureKeyPath),
|
|
|
|
secureCert = some(conf.websocketSecureCertPath),
|
|
|
|
nameResolver = resolver,
|
|
|
|
)
|
|
|
|
|
2023-04-05 12:27:11 +00:00
|
|
|
let node = builder.build().tryGet()
|
2022-10-11 03:58:44 +00:00
|
|
|
|
|
|
|
await node.start()
|
|
|
|
|
|
|
|
let timedOut = not await node.connectToNodes(@[peer]).withTimeout(conf.timeout)
|
|
|
|
if timedOut:
|
2023-04-11 15:05:12 +00:00
|
|
|
error "Timedout after", timeout = conf.timeout
|
2022-10-11 03:58:44 +00:00
|
|
|
|
|
|
|
let lp2pPeerStore = node.switch.peerStore
|
2022-11-24 13:11:23 +00:00
|
|
|
let conStatus = node.peerManager.peerStore[ConnectionBook][peer.peerId]
|
2022-10-11 03:58:44 +00:00
|
|
|
|
|
|
|
if conStatus in [Connected, CanConnect]:
|
|
|
|
let nodeProtocols = lp2pPeerStore[ProtoBook][peer.peerId]
|
|
|
|
if not areProtocolsSupported(conf.protocols, nodeProtocols):
|
2023-04-11 15:05:12 +00:00
|
|
|
error "Not all protocols are supported", expected = conf.protocols,
|
|
|
|
supported = nodeProtocols
|
2022-10-11 03:58:44 +00:00
|
|
|
return 1
|
|
|
|
elif conStatus == CannotConnect:
|
|
|
|
error "Could not connect", peerId = peer.peerId
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
when isMainModule:
|
2023-02-06 11:53:05 +00:00
|
|
|
let rng = crypto.newRng()
|
|
|
|
let status = waitFor main(rng)
|
2022-10-11 03:58:44 +00:00
|
|
|
if status == 0:
|
|
|
|
info "The node is reachable and supports all specified protocols"
|
|
|
|
else:
|
|
|
|
error "The node has some problems (see logs)"
|
|
|
|
quit status
|