rust_ipfs/ipns/
dnslink.rs

1use crate::error::Error;
2use crate::p2p::DnsResolver;
3use crate::path::IpfsPath;
4
5use tracing_futures::Instrument;
6
7#[cfg(not(target_arch = "wasm32"))]
8pub async fn resolve<'a>(
9    resolver: DnsResolver,
10    domain: &str,
11    mut path: impl Iterator<Item = &'a str>,
12) -> Result<IpfsPath, Error> {
13    use hickory_resolver::AsyncResolver;
14    use std::borrow::Cow;
15    use std::str::FromStr;
16
17    let span = tracing::trace_span!("dnslink", %domain);
18
19    async move {
20        // allow using non fqdn names (using the local search path suffices)
21        let searched = Some(Cow::Borrowed(domain));
22
23        let prefix = "_dnslink.";
24        let prefixed = if !domain.starts_with(prefix) {
25            let mut next = String::with_capacity(domain.len() + prefix.len());
26            next.push_str(prefix);
27            next.push_str(domain);
28            Some(Cow::Owned(next))
29        } else {
30            None
31        };
32
33        let searched = searched.into_iter().chain(prefixed.into_iter());
34
35        // FIXME: this uses caching trust-dns resolver even though it's discarded right away
36        // when trust-dns support lands in future libp2p-dns investigate if we could share one, no need
37        // to have multiple related caches.
38        let (config, opt) = resolver.into();
39        let resolver = AsyncResolver::tokio(config, opt);
40
41        // previous implementation searched $domain and _dnslink.$domain concurrently. not sure did
42        // `domain` assume fqdn names or not, but local suffices were not being searched on windows at
43        // least. they are probably waste of time most of the time.
44        for domain in searched {
45            let res = match resolver.txt_lookup(&*domain).await {
46                Ok(res) => res,
47                Err(e) => {
48                    tracing::debug!("resolving dnslink of {:?} failed: {}", domain, e);
49                    continue;
50                }
51            };
52
53            let mut paths =
54                res.iter()
55                    .flat_map(|txt| txt.iter())
56                    .filter_map(|txt| {
57                        if txt.starts_with(b"dnslink=") {
58                            Some(&txt[b"dnslink=".len()..])
59                        } else {
60                            None
61                        }
62                    })
63                    .map(|suffix| {
64                        std::str::from_utf8(suffix)
65                            .map_err(Error::from)
66                            .and_then(IpfsPath::from_str)
67                            .and_then(|mut internal_path| {
68                                internal_path.path.push_split(path.by_ref()).map_err(|_| {
69                                    crate::path::IpfsPathError::InvalidPath("".into())
70                                })?;
71                                Ok(internal_path)
72                            })
73                    });
74
75            if let Some(Ok(x)) = paths.next() {
76                tracing::trace!("dnslink found for {:?}", domain);
77                return Ok(x);
78            }
79
80            tracing::trace!("zero TXT records found for {:?}", domain);
81        }
82
83        Err(anyhow::anyhow!("failed to resolve {:?}", domain))
84    }
85    .instrument(span)
86    .await
87}
88
89#[cfg(target_arch = "wasm32")]
90pub async fn resolve<'a>(
91    _: DnsResolver,
92    domain: &str,
93    _: impl Iterator<Item = &'a str>,
94) -> Result<IpfsPath, Error> {
95    let span = tracing::trace_span!("dnslink", %domain);
96    async move { anyhow::bail!("failed to resolve {domain}: unimplemented") }
97        .instrument(span)
98        .await
99}
100
101#[cfg(test)]
102mod tests {
103    use super::resolve;
104
105    #[tokio::test]
106    async fn resolve_ipfs_io() {
107        tracing_subscriber::fmt::init();
108        let res = resolve(
109            crate::p2p::DnsResolver::Cloudflare,
110            "ipfs.io",
111            std::iter::empty(),
112        )
113        .await
114        .unwrap()
115        .to_string();
116        assert_eq!(res, "/ipns/website.ipfs.io");
117    }
118
119    #[tokio::test]
120    async fn resolve_website_ipfs_io() {
121        let res = resolve(
122            crate::p2p::DnsResolver::Cloudflare,
123            "website.ipfs.io",
124            std::iter::empty(),
125        )
126        .await
127        .unwrap();
128
129        assert!(
130            matches!(res.root(), crate::path::PathRoot::Ipld(_)),
131            "expected an /ipfs/cid path"
132        );
133    }
134}