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
use super::SetOpt;
use curl::easy::Easy2;
use std::{
iter::FromIterator,
net::{IpAddr, SocketAddr},
time::Duration,
};
#[derive(Clone, Debug)]
pub enum DnsCache {
Disable,
Timeout(Duration),
Forever,
}
impl Default for DnsCache {
fn default() -> Self {
Duration::from_secs(60).into()
}
}
impl From<Duration> for DnsCache {
fn from(duration: Duration) -> Self {
DnsCache::Timeout(duration)
}
}
impl SetOpt for DnsCache {
#[allow(unsafe_code)]
fn set_opt<H>(&self, easy: &mut Easy2<H>) -> Result<(), curl::Error> {
let value = match self {
DnsCache::Disable => 0,
DnsCache::Timeout(duration) => duration.as_secs() as i64,
DnsCache::Forever => -1,
};
unsafe {
match curl_sys::curl_easy_setopt(easy.raw(), curl_sys::CURLOPT_DNS_CACHE_TIMEOUT, value)
{
curl_sys::CURLE_OK => Ok(()),
code => Err(curl::Error::new(code)),
}
}
}
}
#[derive(Clone, Debug, Default)]
pub struct ResolveMap(Vec<String>);
impl ResolveMap {
pub const fn new() -> Self {
ResolveMap(Vec::new())
}
pub fn add<H, A>(mut self, host: H, port: u16, addr: A) -> Self
where
H: AsRef<str>,
A: Into<IpAddr>,
{
self.0
.push(format!("{}:{}:{}", host.as_ref(), port, addr.into()));
self
}
}
impl SetOpt for ResolveMap {
fn set_opt<H>(&self, easy: &mut curl::easy::Easy2<H>) -> Result<(), curl::Error> {
let mut list = curl::easy::List::new();
for entry in self.0.iter() {
list.append(entry)?;
}
easy.resolve(list)
}
}
#[derive(Clone, Debug)]
pub(crate) struct Servers(String);
impl FromIterator<SocketAddr> for Servers {
fn from_iter<I: IntoIterator<Item = SocketAddr>>(iter: I) -> Self {
Servers(
iter.into_iter()
.map(|addr| addr.to_string())
.collect::<Vec<_>>()
.join(","),
)
}
}
impl SetOpt for Servers {
fn set_opt<H>(&self, easy: &mut Easy2<H>) -> Result<(), curl::Error> {
if let Err(e) = easy.dns_servers(&self.0) {
tracing::warn!("DNS servers could not be configured: {}", e);
}
Ok(())
}
}