2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2021-08-18 07:40:12 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-08-18 07:40:12 +00:00
|
|
|
|
|
|
|
import std/[sugar, sets, sequtils, strutils]
|
2023-05-18 08:24:17 +00:00
|
|
|
import chronos, chronicles, stew/endians2
|
2021-08-18 07:40:12 +00:00
|
|
|
import ".."/[multiaddress, multicodec]
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "libp2p nameresolver"
|
|
|
|
|
|
|
|
type NameResolver* = ref object of RootObj
|
|
|
|
|
|
|
|
method resolveTxt*(
|
|
|
|
self: NameResolver, address: string
|
|
|
|
): Future[seq[string]] {.async, base.} =
|
|
|
|
## Get TXT record
|
2022-09-26 09:03:24 +00:00
|
|
|
##
|
2021-08-18 07:40:12 +00:00
|
|
|
|
|
|
|
doAssert(false, "Not implemented!")
|
|
|
|
|
|
|
|
method resolveIp*(
|
|
|
|
self: NameResolver, address: string, port: Port, domain: Domain = Domain.AF_UNSPEC
|
|
|
|
): Future[seq[TransportAddress]] {.async, base.} =
|
|
|
|
## Resolve the specified address
|
2022-09-26 09:03:24 +00:00
|
|
|
##
|
2021-08-18 07:40:12 +00:00
|
|
|
|
|
|
|
doAssert(false, "Not implemented!")
|
|
|
|
|
2021-11-08 12:02:03 +00:00
|
|
|
proc getHostname*(ma: MultiAddress): string =
|
2022-09-26 09:03:24 +00:00
|
|
|
let
|
|
|
|
firstPart = ma[0].valueOr:
|
|
|
|
return ""
|
|
|
|
fpSplitted = ($firstPart).split('/', 2)
|
|
|
|
if fpSplitted.len > 2:
|
|
|
|
fpSplitted[2]
|
2021-11-08 12:02:03 +00:00
|
|
|
else:
|
|
|
|
""
|
2021-08-18 07:40:12 +00:00
|
|
|
|
2022-09-26 09:03:24 +00:00
|
|
|
proc resolveOneAddress(
|
2021-08-18 07:40:12 +00:00
|
|
|
self: NameResolver, ma: MultiAddress, domain: Domain = Domain.AF_UNSPEC, prefix = ""
|
|
|
|
): Future[seq[MultiAddress]] {.async.} =
|
|
|
|
#Resolve a single address
|
|
|
|
var pbuf: array[2, byte]
|
|
|
|
|
|
|
|
var dnsval = getHostname(ma)
|
|
|
|
|
|
|
|
if ma[1].tryGet().protoArgument(pbuf).tryGet() == 0:
|
|
|
|
raise newException(MaError, "Incorrect port number")
|
|
|
|
let
|
|
|
|
port = Port(fromBytesBE(uint16, pbuf))
|
|
|
|
resolvedAddresses = await self.resolveIp(prefix & dnsval, port, domain)
|
2022-09-26 09:03:24 +00:00
|
|
|
|
2021-08-18 07:40:12 +00:00
|
|
|
return collect(newSeqOfCap(4)):
|
|
|
|
for address in resolvedAddresses:
|
|
|
|
var createdAddress = MultiAddress.init(address).tryGet()[0].tryGet()
|
|
|
|
for part in ma:
|
2022-09-26 09:03:24 +00:00
|
|
|
if DNS.match(part.tryGet()):
|
|
|
|
continue
|
2021-08-18 07:40:12 +00:00
|
|
|
createdAddress &= part.tryGet()
|
|
|
|
createdAddress
|
|
|
|
|
2022-09-26 09:03:24 +00:00
|
|
|
proc resolveDnsAddr*(
|
2021-08-18 07:40:12 +00:00
|
|
|
self: NameResolver, ma: MultiAddress, depth: int = 0
|
2022-09-26 09:03:24 +00:00
|
|
|
): Future[seq[MultiAddress]] {.async.} =
|
|
|
|
if not DNSADDR.matchPartial(ma):
|
|
|
|
return @[ma]
|
2021-08-18 07:40:12 +00:00
|
|
|
|
|
|
|
trace "Resolving dnsaddr", ma
|
|
|
|
if depth > 6:
|
|
|
|
info "Stopping DNSADDR recursion, probably malicious", ma
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
var dnsval = getHostname(ma)
|
|
|
|
|
|
|
|
let txt = await self.resolveTxt("_dnsaddr." & dnsval)
|
|
|
|
|
|
|
|
trace "txt entries", txt
|
|
|
|
|
|
|
|
var result: seq[MultiAddress]
|
|
|
|
for entry in txt:
|
|
|
|
if not entry.startsWith("dnsaddr="):
|
|
|
|
continue
|
|
|
|
let entryValue = MultiAddress.init(entry[8 ..^ 1]).tryGet()
|
|
|
|
|
2022-09-26 09:03:24 +00:00
|
|
|
if entryValue.contains(multiCodec("p2p")).tryGet() and
|
|
|
|
ma.contains(multiCodec("p2p")).tryGet():
|
|
|
|
if entryValue[multiCodec("p2p")] != ma[multiCodec("p2p")]:
|
|
|
|
continue
|
2021-08-18 07:40:12 +00:00
|
|
|
|
2022-09-26 09:03:24 +00:00
|
|
|
let resolved = await self.resolveDnsAddr(entryValue, depth + 1)
|
|
|
|
for r in resolved:
|
|
|
|
result.add(r)
|
2021-08-18 07:40:12 +00:00
|
|
|
|
|
|
|
if result.len == 0:
|
2022-09-26 09:03:24 +00:00
|
|
|
debug "Failed to resolve a DNSADDR", ma
|
|
|
|
return @[]
|
2021-08-18 07:40:12 +00:00
|
|
|
return result
|
|
|
|
|
2021-11-08 12:02:03 +00:00
|
|
|
proc resolveMAddress*(
|
|
|
|
self: NameResolver, address: MultiAddress
|
|
|
|
): Future[seq[MultiAddress]] {.async.} =
|
2021-08-18 07:40:12 +00:00
|
|
|
var res = initOrderedSet[MultiAddress]()
|
|
|
|
|
2021-11-08 12:02:03 +00:00
|
|
|
if not DNS.matchPartial(address):
|
|
|
|
res.incl(address)
|
|
|
|
else:
|
2023-06-28 14:44:58 +00:00
|
|
|
let code = address[0].tryGet().protoCode().tryGet()
|
2021-11-08 12:02:03 +00:00
|
|
|
let seq =
|
|
|
|
case code
|
|
|
|
of multiCodec("dns"):
|
2022-09-26 09:03:24 +00:00
|
|
|
await self.resolveOneAddress(address)
|
2021-11-08 12:02:03 +00:00
|
|
|
of multiCodec("dns4"):
|
2022-09-26 09:03:24 +00:00
|
|
|
await self.resolveOneAddress(address, Domain.AF_INET)
|
2021-11-08 12:02:03 +00:00
|
|
|
of multiCodec("dns6"):
|
2022-09-26 09:03:24 +00:00
|
|
|
await self.resolveOneAddress(address, Domain.AF_INET6)
|
2021-11-08 12:02:03 +00:00
|
|
|
of multiCodec("dnsaddr"):
|
|
|
|
await self.resolveDnsAddr(address)
|
|
|
|
else:
|
2023-06-28 14:44:58 +00:00
|
|
|
assert false
|
2021-11-08 12:02:03 +00:00
|
|
|
@[address]
|
|
|
|
for ad in seq:
|
|
|
|
res.incl(ad)
|
2021-08-18 07:40:12 +00:00
|
|
|
return res.toSeq
|