-
Notifications
You must be signed in to change notification settings - Fork 712
Expand file tree
/
Copy pathEnrTreeCrawler.cs
More file actions
98 lines (86 loc) · 3.78 KB
/
Copy pathEnrTreeCrawler.cs
File metadata and controls
98 lines (86 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System.Runtime.CompilerServices;
using Nethermind.Logging;
namespace Nethermind.Network.Dns;
public class EnrTreeCrawler(ILogger logger)
{
private readonly ILogger _logger = logger;
public IAsyncEnumerable<string> SearchTree(string domain, CancellationToken cancellationToken = default)
{
if (domain.StartsWith("enrtree://", StringComparison.OrdinalIgnoreCase))
{
domain = domain[10..];
// Note: we have no verification of a DNS list signer!
// Following EIP-1459 "public key must be known to the client in order to verify the list"
// Thus there shall be a list of public keys that a client allows and we shall check against it
string[] pubkey_and_url = domain.Split("@");
if (pubkey_and_url.Length > 1)
{
domain = pubkey_and_url[1];
}
else
{
_logger.Warn("No 32bit encoded public key of enr tree signer");
}
}
DnsClient client = new(domain);
SearchContext searchContext = new(string.Empty);
return SearchTree(client, searchContext, cancellationToken);
}
private async IAsyncEnumerable<string> SearchTree(DnsClient client, SearchContext searchContext, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
while (searchContext.RefsToVisit.Count > 0)
{
cancellationToken.ThrowIfCancellationRequested();
string reference = searchContext.RefsToVisit.Dequeue();
await foreach (string nodeRecordText in SearchNode(client, reference, searchContext, cancellationToken).WithCancellation(cancellationToken))
{
yield return nodeRecordText;
}
}
}
private async IAsyncEnumerable<string> SearchNode(IDnsClient client, string query, SearchContext searchContext, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (searchContext.VisitedRefs.Add(query))
{
IEnumerable<string> lookupResult = await client.Lookup(query, cancellationToken);
foreach (string node in lookupResult)
{
EnrTreeNode treeNode;
try
{
treeNode = EnrTreeParser.ParseNode(node);
}
catch (Exception e) when (e is FormatException or NotSupportedException)
{
// A single malformed record from an untrusted DNS server must not abort the whole crawl.
if (_logger.IsDebug) _logger.Debug($"Skipping malformed ENR tree record from DNS query '{query}': {e.Message}");
continue;
}
foreach (string link in treeNode.Links)
{
DnsClient linkedTreeLookup = new(link);
await foreach (string nodeRecordText in SearchTree(linkedTreeLookup, searchContext, cancellationToken).WithCancellation(cancellationToken))
{
yield return nodeRecordText;
}
}
foreach (string nodeRecordText in treeNode.Records)
{
yield return nodeRecordText;
}
foreach (string nodeRef in treeNode.Refs)
{
searchContext.RefsToVisit.Enqueue(nodeRef);
}
}
}
}
private class SearchContext
{
public SearchContext(string startRef) => RefsToVisit.Enqueue(startRef);
public HashSet<string> VisitedRefs { get; } = [];
public Queue<string> RefsToVisit { get; } = new();
}
}