-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(p2p): dnsaddr recursive resolution #2204
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1d1560f
chore(p2p): dnsaddr recursive resolution
rymnc 599b49e
fix: unwrap
rymnc 4f4b662
fix: use same version of hickory resolver as from libp2p-dns
rymnc 2fde308
Merge branch 'master' into fix/p2p
rymnc 903dfac
fix: changelog
rymnc 13c57f2
fix: unwrapping in test helpers
rymnc 82ec625
fix: move things around, remove trait
rymnc 5b4671f
fix: remove async where not required
rymnc 0482f80
fix: naming
rymnc 8c77c1d
chore: deterministic dnsaddr
rymnc 0504c1a
fix: comment
rymnc dcc3367
Merge branch 'master' into fix/p2p
xgreenx c273cba
chore: cleanup result
rymnc 689b822
Merge branch 'master' into fix/p2p
xgreenx 92f5f22
fix: use mainnet urls
rymnc cfd5503
fix: clippy
rymnc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use anyhow::anyhow; | ||
use hickory_resolver::TokioAsyncResolver; | ||
use libp2p::Multiaddr; | ||
use std::pin::Pin; | ||
|
||
/// The prefix for `dnsaddr` protocol TXT record lookups. | ||
const DNSADDR_PREFIX: &str = "_dnsaddr."; | ||
/// The maximum number of DNS lookups when dialing. | ||
/// This limit is for preventing malicious or misconfigured DNS records from causing infinite recursion. | ||
const MAX_DNS_LOOKUPS: usize = 10; | ||
|
||
pub(crate) struct DnsResolver { | ||
resolver: TokioAsyncResolver, | ||
} | ||
|
||
impl DnsResolver { | ||
pub(crate) async fn lookup_dnsaddr( | ||
&self, | ||
addr: &str, | ||
) -> anyhow::Result<Vec<Multiaddr>> { | ||
self.resolve_recursive(addr, 0).await | ||
} | ||
|
||
pub(crate) async fn new() -> anyhow::Result<Box<Self>> { | ||
let resolver = TokioAsyncResolver::tokio_from_system_conf()?; | ||
Ok(Box::new(Self { resolver })) | ||
} | ||
|
||
/// Internal method to handle recursive DNS lookups. | ||
fn resolve_recursive<'a>( | ||
&'a self, | ||
addr: &'a str, | ||
depth: usize, | ||
) -> Pin< | ||
Box<dyn std::future::Future<Output = anyhow::Result<Vec<Multiaddr>>> + Send + 'a>, | ||
> { | ||
Box::pin(async move { | ||
if depth >= MAX_DNS_LOOKUPS { | ||
return Err(anyhow!("Maximum DNS lookup depth exceeded")); | ||
} | ||
|
||
let mut multiaddrs = vec![]; | ||
let dns_lookup_url = format!("{}{}", DNSADDR_PREFIX, addr); | ||
let txt_records = self.resolver.txt_lookup(dns_lookup_url).await?; | ||
|
||
for record in txt_records { | ||
let parsed = record.to_string(); | ||
if !parsed.starts_with("dnsaddr") { | ||
continue; | ||
} | ||
|
||
let dnsaddr_value = parsed | ||
.split("dnsaddr=") | ||
.nth(1) | ||
.ok_or_else(|| anyhow!("Invalid DNS address: {:?}", parsed))?; | ||
|
||
// Check if the parsed value is a multiaddress or another dnsaddr. | ||
if dnsaddr_value.starts_with("/dnsaddr") { | ||
let nested_dnsaddr = dnsaddr_value | ||
.split('/') | ||
.nth(2) | ||
.ok_or_else(|| anyhow!("Invalid nested dnsaddr"))?; | ||
// Recursively resolve the nested dnsaddr | ||
#[allow(clippy::arithmetic_side_effects)] | ||
let nested_addrs = | ||
self.resolve_recursive(nested_dnsaddr, depth + 1).await?; | ||
multiaddrs.extend(nested_addrs); | ||
} else if let Ok(multiaddr) = dnsaddr_value.parse::<Multiaddr>() { | ||
multiaddrs.push(multiaddr); | ||
} | ||
} | ||
|
||
Ok(multiaddrs) | ||
}) | ||
} | ||
} | ||
|
||
#[allow(non_snake_case)] | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::collections::HashSet; | ||
|
||
#[tokio::test] | ||
async fn dns_resolver__parses_all_multiaddresses_from_mainnet_dnsaddr_entry() { | ||
// given | ||
let resolver = DnsResolver::new().await.unwrap(); | ||
let expected_multiaddrs: HashSet<Multiaddr> = [ | ||
"/dns/p2p-mainnet.fuel.network/tcp/30336/p2p/16Uiu2HAkxjhwNYtwawWUexYn84MsrA9ivFWkNHmiF4hSieoNP7Jd", | ||
"/dns/p2p-mainnet.fuel.network/tcp/30337/p2p/16Uiu2HAmQunK6Dd81BXh3rW2ZsszgviPgGMuHw39vv2XxbkuCfaw", | ||
"/dns/p2p-mainnet.fuel.network/tcp/30333/p2p/16Uiu2HAkuiLZNrfecgDYHJZV5LoEtCXqqRCqHY3yLBqs4LQk8jJg", | ||
"/dns/p2p-mainnet.fuel.network/tcp/30334/p2p/16Uiu2HAkzYNa6yMykppS1ij69mKoKjrZEr11oHGiM5Mpc8nKjVDM", | ||
"/dns/p2p-mainnet.fuel.network/tcp/30335/p2p/16Uiu2HAm5yqpTv1QVk3SepUYzeKXTWMuE2VqMWHq5qQLPR2Udg6s" | ||
].iter().map(|s| s.parse().unwrap()).collect(); | ||
|
||
// when | ||
// run a `dig +short txt _dnsaddr.mainnet.fuel.network` to get the TXT records | ||
let multiaddrs = resolver | ||
.lookup_dnsaddr("mainnet.fuel.network") | ||
.await | ||
.unwrap(); | ||
// then | ||
for multiaddr in multiaddrs.iter() { | ||
assert!( | ||
expected_multiaddrs.contains(multiaddr), | ||
"Unexpected multiaddr: {:?}", | ||
multiaddr | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ pub mod behavior; | |
pub mod codecs; | ||
pub mod config; | ||
pub mod discovery; | ||
mod dnsaddr_resolution; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I'd move this declaration to a separate block than the public modules. |
||
pub mod gossipsub; | ||
pub mod heartbeat; | ||
pub mod heavy_task_processor; | ||
|
@@ -23,6 +24,7 @@ pub use libp2p::{ | |
Multiaddr, | ||
PeerId, | ||
}; | ||
use tracing::warn; | ||
|
||
#[cfg(feature = "test-helpers")] | ||
pub mod network_service { | ||
|
@@ -38,6 +40,13 @@ impl TryPeerId for Multiaddr { | |
fn try_to_peer_id(&self) -> Option<PeerId> { | ||
self.iter().last().and_then(|p| match p { | ||
Protocol::P2p(peer_id) => Some(peer_id), | ||
Protocol::Dnsaddr(multiaddr) => { | ||
warn!( | ||
"synchronous recursive dnsaddr resolution is not yet supported: {:?}", | ||
multiaddr | ||
); | ||
None | ||
} | ||
_ => None, | ||
}) | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we switch this to
core-test.fuellabs.net
? The records on themainnet.fuel.network
record can and will change over time, but I just setup thecore-test
DNS record to be static.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for setting this up. I created this follow-up issue to use the
core-test.fuellabs.net
hostname in the test, since this PR got auto-merged after my approval.