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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
//! # hyper_trust_dns_connector //! //! A crate to make [trust-dns-resolver](https://docs.rs/trust-dns-resolver)'s //! asynchronous resolver compatible with [hyper](https://docs.rs/hyper) client, //! to use instead of the default dns threadpool. //! //! ## Features //! //! * `hyper-tls-connector` This feature includes //! [`hyper-tls`](https://docs.rs/hyper-tls/0.3/hyper_tls/) and //! [`native-tls`](https://docs.rs/native-tls/0.2/native_tls/) to //! provide a helper function to create a tls connector. //! //! ## Usage //! //! [trust-dns-resolver](https://docs.rs/trust-dns-resolver) creates a background that needs to //! be spawned on top of an executor to run dns queries. It does not need to be spawned on the //! same executor as the client, but will deadlock if spawned on top of another executor that //! runs on the same thread. //! //! ## Example //! //! ``` //! extern crate hyper_trust_dns_connector; //! extern crate hyper; //! extern crate tokio; //! //! use hyper_trust_dns_connector::new_async_http_connector; //! use hyper::{Client, Body}; //! use tokio::prelude::Future; //! use tokio::runtime::Runtime; //! //! let mut rt = Runtime::new().expect("couldn't create runtime"); //! let (async_http, background) = new_async_http_connector() //! .expect("couldn't create connector"); //! let client = Client::builder() //! .executor(rt.executor()) //! .build::<_, Body>(async_http); //! rt.spawn(background); //! let status_code = rt //! .block_on(client.get(hyper::Uri::from_static("http://httpbin.org/ip")) //! .map(|res| res.status())) //! .expect("error during the request"); //! println!("status is {:?}", status_code); //! ``` extern crate futures; extern crate hyper; extern crate trust_dns_resolver; use futures::{Async, Future, Poll}; use hyper::client::connect::dns::{Name, Resolve}; use hyper::client::HttpConnector; use std::io; use std::net::IpAddr; use trust_dns_resolver::config::{ResolverConfig, ResolverOpts}; use trust_dns_resolver::{AsyncResolver, BackgroundLookupIp}; /// Wrapper future around trust-dns-resolver's /// [`BackgroundLookupIp`](https://docs.rs/trust-dns-resolver/0.10.3/trust_dns_resolver/type.BackgroundLookupIp.html) pub struct HyperLookupFuture(BackgroundLookupIp); impl Future for HyperLookupFuture { type Item = std::vec::IntoIter<IpAddr>; type Error = io::Error; fn poll(&mut self) -> Poll<Self::Item, Self::Error> { let lookups = self.0.poll()?; Ok(match lookups { Async::NotReady => Async::NotReady, Async::Ready(lookups) => { Async::Ready(lookups.iter().collect::<Vec<IpAddr>>().into_iter()) } }) } } /// Wrapper around trust-dns-resolver's /// [`AsyncResolver`](https://docs.rs/trust-dns-resolver/0.10.3/trust_dns_resolver/struct.AsyncResolver.html) /// /// The resolver runs a bakground Task wich manages dns requests. When a new resolver is created, /// the background task is also created, it needs to be spawned on top of an executor before using the client, /// or dns requests will block. #[derive(Debug, Clone)] pub struct AsyncHyperResolver(AsyncResolver); impl AsyncHyperResolver { /// constructs a new resolver, arguments are passed to the corresponding method of /// [`AsyncResolver`](https://docs.rs/trust-dns-resolver/0.10.3/trust_dns_resolver/struct.AsyncResolver.html#method.new) pub fn new( config: ResolverConfig, options: ResolverOpts, ) -> (Self, impl Future<Item = (), Error = ()>) { let (resolver, background) = AsyncResolver::new(config, options); (Self(resolver), background) } /// constructs a new resolver from default configuration, uses the corresponding method of /// [`AsyncResolver`](https://docs.rs/trust-dns-resolver/0.10.3/trust_dns_resolver/struct.AsyncResolver.html#method.new) pub fn new_from_system_conf() -> Result<(Self, impl Future<Item = (), Error = ()>), io::Error> { let (resolver, background) = AsyncResolver::from_system_conf()?; Ok((Self(resolver), background)) } } impl Resolve for AsyncHyperResolver { type Addrs = std::vec::IntoIter<IpAddr>; type Future = HyperLookupFuture; fn resolve(&self, name: Name) -> Self::Future { HyperLookupFuture(self.0.lookup_ip(name.as_str())) } } /// A helper function to create an http connector and a dns task with the default configuration /// /// ``` /// use tokio::runtime::Runtime; /// use hyper_trust_dns_connector::new_async_http_connector; /// use hyper::{Client, Body}; /// /// let mut rt = Runtime::new().expect("couldn't create runtime"); /// /// let (async_http, background) = new_async_http_connector() /// .expect("couldn't create connector"); /// let client = Client::builder() /// .executor(rt.executor()) /// .build::<_, Body>(async_http); /// /// rt.spawn(background); /// ``` pub fn new_async_http_connector() -> Result< ( HttpConnector<AsyncHyperResolver>, impl Future<Item = (), Error = ()>, ), io::Error, > { let (resolver, background) = AsyncHyperResolver::new_from_system_conf()?; Ok((HttpConnector::new_with_resolver(resolver), background)) } /// Module to use [`hyper-tls`](https://docs.rs/hyper-tls/0.3/hyper_tls/), /// needs "hyper-tls-connector" feature enabled /// /// ## Example /// /// ``` /// use tokio::runtime::Runtime; /// use hyper_trust_dns_connector::https::new_async_https_connector; /// use hyper::{Client, Body}; /// /// let mut rt = Runtime::new().expect("couldn't create runtime"); /// /// let (https, background) = new_async_https_connector() /// .expect("couldn't create connector"); /// let client = Client::builder() /// .executor(rt.executor()) /// .build::<_, Body>(https); /// /// rt.spawn(background); /// ``` #[cfg(feature = "hyper-tls-connector")] pub mod https { extern crate hyper_tls; extern crate native_tls; use hyper_tls::HttpsConnector; use native_tls::TlsConnector; use crate::io; use crate::Future; use crate::HttpConnector; use crate::{new_async_http_connector, AsyncHyperResolver}; #[derive(Debug)] pub enum Error { NativeTls(native_tls::Error), Io(io::Error), } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { Error::NativeTls(err) => write!(f, "native_tls error : {}", err), Error::Io(err) => write!(f, "io error : {}", err), } } } impl std::error::Error for Error {} impl From<io::Error> for Error { fn from(error: io::Error) -> Self { Error::Io(error) } } impl From<native_tls::Error> for Error { fn from(error: native_tls::Error) -> Self { Error::NativeTls(error) } } /// A helper function to create an https connector from [`hyper-tls`](https://docs.rs/hyper-tls/0.3/hyper_tls/) /// and a dns task with the default configuration. pub fn new_async_https_connector() -> Result< ( HttpsConnector<HttpConnector<AsyncHyperResolver>>, impl Future<Item = (), Error = ()>, ), Error, > { let (mut http, background) = new_async_http_connector()?; http.enforce_http(false); let tls_connector = TlsConnector::new()?; Ok((HttpsConnector::from((http, tls_connector)), background)) } }