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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use crate::sources::interfaces::{Error, Family, IpFuture, IpResult, Source};
use log::trace;

use std::net::SocketAddr;

use hickory_resolver::config::*;
use hickory_resolver::TokioAsyncResolver;

#[derive(Debug, Clone, Copy)]
pub enum QueryType {
    TXT,
    A,
    AAAA,
}

/// DNS Source of the external ip
///
/// It expects a DNS server to target for a query (currently only A and TXT), to retrive in the
/// reply of the message the IP.
/// A few services are known for replying with the IP of the query sender.
#[derive(Debug, Clone)]
pub struct DNSSource {
    server: String,
    record_type: QueryType,
    record: String,
}

impl DNSSource {
    pub fn new<S: Into<String>, R: Into<String>>(
        server: S,
        record_type: QueryType,
        record: R,
    ) -> Self {
        DNSSource {
            server: server.into(),
            record_type,
            record: record.into(),
        }
    }
    fn source<R: Into<String>>(
        server: String,
        record_type: QueryType,
        record: R,
    ) -> Box<dyn Source> {
        Box::new(DNSSource {
            server,
            record_type,
            record: record.into(),
        })
    }
}

impl std::fmt::Display for DNSSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "DnsSource: {:?} {:?} {}",
            self.server, self.record_type, self.record
        )
    }
}

impl DNSSource {
    async fn get_resolver(self: &DNSSource, family: Family) -> Result<TokioAsyncResolver, Error> {
        let mut resolver_opts = ResolverOpts::default();
        resolver_opts.ip_strategy = match family {
            Family::IPv4 => LookupIpStrategy::Ipv4Only,
            Family::IPv6 => LookupIpStrategy::Ipv6Only,
            Family::Any => resolver_opts.ip_strategy,
        };

        let resolver = TokioAsyncResolver::tokio(ResolverConfig::default(), resolver_opts.clone());
        let mut config = ResolverConfig::new();
        for found_ip in resolver.lookup_ip(&self.server).await?.iter() {
            let address = SocketAddr::new(found_ip, 53);
            trace!("DNS address {}", address);
            config.add_name_server(NameServerConfig {
                bind_addr: None,
                socket_addr: address,
                protocol: hickory_resolver::config::Protocol::Udp,
                tls_dns_name: None,
                trust_negative_responses: true,
            });
        }

        Ok(TokioAsyncResolver::tokio(config, resolver_opts))
    }
}

impl Source for DNSSource {
    fn get_ip(&self, family: Family) -> IpFuture<'_> {
        async fn run(_self: &DNSSource, family: Family) -> IpResult {
            if matches!(
                (family, _self.record_type),
                (Family::IPv4, QueryType::AAAA) | (Family::IPv6, QueryType::A)
            ) {
                return Err(Error::UnsupportedFamily);
            }
            trace!("Contacting {:?} for {}", _self.server, _self.record);
            let resolver = _self
                .get_resolver(match _self.record_type {
                    QueryType::A => Family::IPv4,
                    QueryType::AAAA => Family::IPv6,
                    _ => family,
                })
                .await?;

            match _self.record_type {
                QueryType::TXT => {
                    for reply in resolver.txt_lookup(_self.record.clone()).await?.iter() {
                        for txt in reply.txt_data().iter() {
                            let data = std::str::from_utf8(txt);
                            if data.is_err() {
                                continue;
                            }

                            let ip = data.unwrap().parse()?;
                            if family == Family::Any {
                                return Ok(ip);
                            } else if family == Family::IPv4 {
                                if ip.is_ipv4() {
                                    return Ok(ip);
                                }
                                return Err(Error::DnsResolutionEmpty);
                            } else {
                                // if family == Family::IPv6
                                if ip.is_ipv6() {
                                    return Ok(ip);
                                }
                                return Err(Error::UnsupportedFamily);
                            }
                        }
                    }
                }
                QueryType::A => {
                    if family == Family::IPv4 || family == Family::Any {
                        for reply in resolver.lookup_ip(_self.record.clone()).await?.iter() {
                            if reply.is_ipv4() {
                                return Ok(reply);
                            }
                        }
                    }
                    return Err(Error::UnsupportedFamily);
                }
                QueryType::AAAA => {
                    if family == Family::IPv6 || family == Family::Any {
                        for reply in resolver.lookup_ip(_self.record.clone()).await?.iter() {
                            if reply.is_ipv6() {
                                return Ok(reply);
                            }
                        }
                    }
                    return Err(Error::UnsupportedFamily);
                }
            }
            Err(Error::DnsResolutionEmpty)
        }
        Box::pin(run(self, family))
    }

    fn box_clone(&self) -> Box<dyn Source> {
        Box::new(self.clone())
    }
}

/// Returns a collection of DNS sources to use to retrieve the external ip
pub fn get_dns_sources<T>() -> T
where
    T: std::iter::FromIterator<Box<dyn Source>>,
{
    vec![
        DNSSource::source(
            String::from("resolver1.opendns.com"),
            QueryType::A,
            "myip.opendns.com",
        ),
        DNSSource::source(
            String::from("resolver1.opendns.com"),
            QueryType::AAAA,
            "myip.opendns.com",
        ),
        DNSSource::source(
            String::from("ns1.google.com"),
            QueryType::TXT,
            "o-o.myaddr.l.google.com",
        ),
    ]
    .into_iter()
    .collect()
}