Integrate DNS resolution (#709)

* Update submodule

* Add DNS Client library

* Fork and update dnsclient submodule

* Integrate DNS resolution
This commit is contained in:
Hanno Cornelius 2021-08-25 13:57:35 +02:00 committed by GitHub
parent c222d83bcc
commit 0db4107ae2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 69 additions and 4 deletions

5
.gitmodules vendored
View File

@ -135,3 +135,8 @@
url = https://github.com/status-im/nim-dnsdisc.git
ignore = untracked
branch = main
[submodule "vendor/dnsclient.nim"]
path = vendor/dnsclient.nim
url = https://github.com/jm-clius/dnsclient.nim.git
ignore = untracked
branch = master

View File

@ -19,8 +19,10 @@ import libp2p/[switch, # manage transports, a single entry poi
protobuf/minprotobuf, # message serialisation/deserialisation from and to protobufs
protocols/protocol, # define the protocol base type
protocols/secure/secio, # define the protocol of secure input / output, allows encrypted communication that uses public keys to validate signed messages instead of a certificate authority like in TLS
nameresolving/dnsresolver,# define DNS resolution
muxers/muxer] # define an interface for stream multiplexing, allowing peers to offer many protocols over a single connection
import ../../waku/v2/node/[wakunode2, waku_payload],
../../waku/v2/node/./dnsdisc/waku_dnsdisc,
../../waku/v2/utils/peers,
../../waku/common/utils/nat,
./config_chat2
@ -344,6 +346,30 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} =
if conf.staticnodes.len > 0:
await connectToNodes(chat, conf.staticnodes)
elif conf.dnsDiscovery and conf.dnsDiscoveryUrl != "":
# Discover nodes via DNS
debug "Discovering nodes using Waku DNS discovery", url=conf.dnsDiscoveryUrl
var nameServers: seq[TransportAddress]
for ip in conf.dnsDiscoveryNameServers:
nameServers.add(initTAddress(ip, Port(53))) # Assume all servers use port 53
let dnsResolver = DnsResolver.new(nameServers)
proc resolver(domain: string): Future[string] {.async, gcsafe.} =
trace "resolving", domain=domain
let resolved = await dnsResolver.resolveTxt(domain)
return resolved[0] # Use only first answer
var wakuDnsDiscovery = WakuDnsDiscovery.init(conf.dnsDiscoveryUrl,
resolver)
if wakuDnsDiscovery.isOk:
let discoveredPeers = wakuDnsDiscovery.get().findPeers()
if discoveredPeers.isOk:
info "Connecting to discovered peers"
waitFor chat.node.connectToNodes(discoveredPeers.get())
else:
warn "Failed to init Waku DNS discovery"
elif conf.fleet != Fleet.none:
# Connect to at least one random fleet node
echo "No static peers configured. Choosing one at random from " & $conf.fleet & " fleet..."

View File

@ -186,6 +186,23 @@ type
defaultValue: false
name: "metrics-logging" }: bool
## DNS discovery config
dnsDiscovery* {.
desc: "Enable discovering nodes via DNS"
defaultValue: false
name: "dns-discovery" }: bool
dnsDiscoveryUrl* {.
desc: "URL for DNS node list in format 'enrtree://<key>@<fqdn>'",
defaultValue: ""
name: "dns-discovery-url" }: string
dnsDiscoveryNameServers* {.
desc: "DNS name server IPs to query. Argument may be repeated."
defaultValue: @[ValidIpAddress.init("1.1.1.1"), ValidIpAddress.init("1.0.0.1")]
name: "dns-discovery-name-server" }: seq[ValidIpAddress]
## Chat2 configuration
fleet* {.

1
vendor/dnsclient.nim vendored Submodule

@ -0,0 +1 @@
Subproject commit c3ddd26a2eece2a7bb558cb67d2f92846f9b8402

2
vendor/nim-libp2p vendored

@ -1 +1 @@
Subproject commit c1b2d45d1b562df1af29012e58f33ff8bff597ac
Subproject commit f274bfe19db5a39ffbca177b52db7e8a7eb44537

View File

@ -192,6 +192,11 @@ type
desc: "URL for DNS node list in format 'enrtree://<key>@<fqdn>'",
defaultValue: ""
name: "dns-discovery-url" }: string
dnsDiscoveryNameServers* {.
desc: "DNS name server IPs to query. Argument may be repeated."
defaultValue: @[ValidIpAddress.init("1.1.1.1"), ValidIpAddress.init("1.0.0.1")]
name: "dns-discovery-name-server" }: seq[ValidIpAddress]
# NOTE: Keys are different in nim-libp2p
proc parseCmdArg*(T: type crypto.PrivateKey, p: TaintedString): T =

View File

@ -9,6 +9,7 @@ import
libp2p/crypto/crypto,
libp2p/protocols/ping,
libp2p/protocols/pubsub/gossipsub,
libp2p/nameresolving/dnsresolver,
libp2p/builders,
../protocol/[waku_relay, waku_message],
../protocol/waku_store/waku_store,
@ -850,11 +851,21 @@ when isMainModule:
# Connect to discovered nodes
if conf.dnsDiscovery and conf.dnsDiscoveryUrl != "":
# @ TODO: this is merely POC integration with an empty resolver
debug "Waku DNS Discovery enabled. Using empty resolver."
debug "Discovering nodes using Waku DNS discovery", url=conf.dnsDiscoveryUrl
var nameServers: seq[TransportAddress]
for ip in conf.dnsDiscoveryNameServers:
nameServers.add(initTAddress(ip, Port(53))) # Assume all servers use port 53
let dnsResolver = DnsResolver.new(nameServers)
proc resolver(domain: string): Future[string] {.async, gcsafe.} =
trace "resolving", domain=domain
let resolved = await dnsResolver.resolveTxt(domain)
return resolved[0] # Use only first answer
var wakuDnsDiscovery = WakuDnsDiscovery.init(conf.dnsDiscoveryUrl,
emptyResolver) # TODO: Add DNS resolver
resolver)
if wakuDnsDiscovery.isOk:
let discoveredPeers = wakuDnsDiscovery.get().findPeers()
if discoveredPeers.isOk: