2022-10-11 03:58:44 +00:00
|
|
|
import
|
|
|
|
std/[strutils, sequtils, tables],
|
|
|
|
confutils,
|
|
|
|
chronos,
|
|
|
|
stew/shims/net,
|
2024-02-12 14:28:22 +00:00
|
|
|
chronicles/topics_registry,
|
|
|
|
os
|
2022-10-11 03:58:44 +00:00
|
|
|
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
|
2024-02-12 14:28:22 +00:00
|
|
|
./certsgenerator,
|
2023-08-09 17:11:50 +00:00
|
|
|
../../waku/waku_enr,
|
|
|
|
../../waku/node/peer_manager,
|
|
|
|
../../waku/waku_core,
|
2024-03-03 00:59:53 +00:00
|
|
|
../../waku/waku_node,
|
|
|
|
../../waku/factory/builder
|
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
|
2024-02-12 14:28:22 +00:00
|
|
|
const CertsDirectory = "./certs"
|
2023-04-11 15:05:12 +00:00
|
|
|
|
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",
|
2023-09-26 13:02:12 +00:00
|
|
|
defaultValue: LogLevel.INFO,
|
2022-10-11 03:58:44 +00:00
|
|
|
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
|
|
|
|
2023-09-26 13:02:12 +00:00
|
|
|
ping* {.
|
|
|
|
desc: "Ping the peer node to measure latency",
|
|
|
|
defaultValue: true,
|
|
|
|
name: "ping" .}: bool
|
|
|
|
|
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:
|
2023-09-21 11:12:14 +00:00
|
|
|
raise newException(ValueError, "Invalid timeout value")
|
2022-10-11 03:58:44 +00:00
|
|
|
|
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-09-26 13:02:12 +00:00
|
|
|
proc pingNode(node: WakuNode, peerInfo: RemotePeerInfo): Future[void] {.async, gcsafe.} =
|
|
|
|
try:
|
|
|
|
let conn = await node.switch.dial(peerInfo.peerId, peerInfo.addrs, PingCodec)
|
|
|
|
let pingDelay = await node.libp2pPing.ping(conn)
|
|
|
|
info "Peer response time (ms)", peerId = peerInfo.peerId, ping=pingDelay.millis
|
|
|
|
|
|
|
|
except CatchableError:
|
|
|
|
var msg = getCurrentExceptionMsg()
|
|
|
|
if msg == "Future operation cancelled!":
|
|
|
|
msg = "timedout"
|
|
|
|
error "Failed to ping the peer", peer=peerInfo, err=msg
|
|
|
|
|
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 = @[
|
2023-12-14 06:16:39 +00:00
|
|
|
initTAddress(parseIpAddress("1.1.1.1"), Port(53)),
|
|
|
|
initTAddress(parseIpAddress("1.0.0.1"), Port(53))]
|
2022-10-18 15:13:42 +00:00
|
|
|
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-12-14 06:16:39 +00:00
|
|
|
bindIp = parseIpAddress("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()
|
2024-02-12 14:28:22 +00:00
|
|
|
keyPath = if conf.websocketSecureKeyPath.len > 0:
|
|
|
|
conf.websocketSecureKeyPath
|
|
|
|
else:
|
|
|
|
CertsDirectory & "/key.pem"
|
|
|
|
certPath = if conf.websocketSecureCertPath.len > 0:
|
|
|
|
conf.websocketSecureCertPath
|
|
|
|
else:
|
|
|
|
CertsDirectory & "/cert.pem"
|
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,
|
|
|
|
)
|
|
|
|
|
2023-08-04 05:22:31 +00:00
|
|
|
var enrBuilder = EnrBuilder.init(nodeKey)
|
|
|
|
|
|
|
|
let recordRes = enrBuilder.build()
|
|
|
|
let record =
|
|
|
|
if recordRes.isErr():
|
|
|
|
error "failed to create enr record", error=recordRes.error
|
|
|
|
quit(QuitFailure)
|
|
|
|
else: recordRes.get()
|
|
|
|
|
2023-04-11 15:05:12 +00:00
|
|
|
if isWss and (conf.websocketSecureKeyPath.len == 0 or
|
|
|
|
conf.websocketSecureCertPath.len == 0):
|
2024-02-12 14:28:22 +00:00
|
|
|
info "WebSocket Secure requires key and certificate. Generating them"
|
|
|
|
if not dirExists(CertsDirectory):
|
|
|
|
createDir(CertsDirectory)
|
|
|
|
if generateSelfSignedCertificate(certPath, keyPath) != 0:
|
|
|
|
error "Error generating key and certificate"
|
|
|
|
return 1
|
2023-04-11 15:05:12 +00:00
|
|
|
|
2023-08-04 05:22:31 +00:00
|
|
|
builder.withRecord(record)
|
2023-04-11 15:05:12 +00:00
|
|
|
builder.withNetworkConfiguration(netConfig.tryGet())
|
|
|
|
builder.withSwitchConfiguration(
|
2024-02-12 14:28:22 +00:00
|
|
|
secureKey = some(keyPath),
|
|
|
|
secureCert = some(certPath),
|
2023-04-11 15:05:12 +00:00
|
|
|
nameResolver = resolver,
|
|
|
|
)
|
|
|
|
|
2023-04-05 12:27:11 +00:00
|
|
|
let node = builder.build().tryGet()
|
2022-10-11 03:58:44 +00:00
|
|
|
|
2023-09-26 13:02:12 +00:00
|
|
|
if conf.ping:
|
|
|
|
try:
|
|
|
|
await mountLibp2pPing(node)
|
|
|
|
except CatchableError:
|
|
|
|
error "failed to mount libp2p ping protocol: " & getCurrentExceptionMsg()
|
|
|
|
return 1
|
|
|
|
|
2022-10-11 03:58:44 +00:00
|
|
|
await node.start()
|
|
|
|
|
2023-09-26 13:02:12 +00:00
|
|
|
var pingFut:Future[bool]
|
|
|
|
if conf.ping:
|
|
|
|
pingFut = pingNode(node, peer).withTimeout(conf.timeout)
|
|
|
|
|
2022-10-11 03:58:44 +00:00
|
|
|
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
|
2023-08-11 08:00:19 +00:00
|
|
|
return 1
|
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
|
|
|
|
2023-09-26 13:02:12 +00:00
|
|
|
if conf.ping:
|
|
|
|
discard await pingFut
|
|
|
|
|
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
|