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
use std::{
	net::ToSocketAddrs,
	task::{Context, Poll},
	vec::IntoIter,
};

use hyper::{client::connect::dns::Name, service::Service};
use ipnet::IpNet;
use trust_dns_resolver::{
	config::{ResolverConfig, ResolverOpts},
	TokioAsyncResolver,
};

#[allow(unused_imports)]
use crate::internal_prelude::*;

pub trait Resolver: Clone + Send + Sync + 'static {
	fn new_default() -> Result<Self, io::Error>;
	fn resolve(&self, host: &str) -> PinnedFut<Result<IntoIter<SocketAddr>, io::Error>>;
	fn with_net_blacklist(self, blacklist: Arc<Vec<IpNet>>) -> Self;
}

#[derive(Clone, Debug)]
pub struct AsyncHyperResolver {
	resolver:      Arc<TokioAsyncResolver>,
	net_blacklist: Arc<Vec<IpNet>>,
}

impl AsyncHyperResolver {
	pub fn new(config: ResolverConfig, options: ResolverOpts) -> Result<Self, io::Error> {
		let resolver = Arc::new(TokioAsyncResolver::tokio(config, options)?);
		Ok(Self { resolver, net_blacklist: Arc::new(vec![]) })
	}
}

impl Resolver for AsyncHyperResolver {
	fn new_default() -> Result<Self, io::Error> {
		let resolver = Arc::new(TokioAsyncResolver::tokio_from_system_conf()?);
		Ok(Self { resolver, net_blacklist: Arc::new(vec![]) })
	}

	fn with_net_blacklist(mut self, blacklist: Arc<Vec<IpNet>>) -> Self {
		self.net_blacklist = blacklist;
		self
	}

	fn resolve(&self, name: &str) -> PinnedFut<Result<IntoIter<SocketAddr>, io::Error>> {
		let resolver = self.clone();
		let s_name = String::from(name);

		Box::pin(async move {
			let r = resolver
				.resolver
				.lookup_ip(s_name.as_str())
				.await?
				.iter()
				.map(|addr| (addr, 0_u16).to_socket_addrs())
				.try_fold(Vec::new(), |mut acc, s_addr| {
					acc.extend(s_addr?);
					Ok::<_, io::Error>(acc)
				})?
				.into_iter();

			let mut out_addrs: Vec<SocketAddr> = vec![];
			for addr in r.into_iter() {
				for blacklisted_net in resolver.net_blacklist.iter() {
					if blacklisted_net.contains(&addr.ip()) {
						return Err(io::Error::new(
							io::ErrorKind::Interrupted,
							format!("resolved IP {} is blacklisted", addr.ip().to_string().as_str()),
						))
					}
				}
				out_addrs.push(addr);
			}

			Ok(out_addrs.into_iter())
		})
	}
}

#[derive(Debug, Clone)]
pub(crate) struct Adaptor<R> {
	resolver: Arc<R>,
}

impl<R: Resolver> Adaptor<R> {
	pub fn new(resolver: Arc<R>) -> Self {
		Self { resolver }
	}
}

impl<R: Resolver> Service<Name> for Adaptor<R> {
	type Error = io::Error;
	type Future = PinnedFut<Result<Self::Response, Self::Error>>;
	type Response = std::vec::IntoIter<SocketAddr>;

	fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
		Poll::Ready(Ok(()))
	}

	fn call(&mut self, name: Name) -> Self::Future {
		let adaptor = self.clone();
		Box::pin(async move { adaptor.resolver.resolve(name.as_str()).await })
	}
}

lazy_static! {
	pub static ref RESERVED_SUBNETS: Arc<Vec<IpNet>> = Arc::new({
		vec![
			// see https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
			"0.0.0.0/8",
			"10.0.0.0/8",
			"100.64.0.0/10",
			"127.0.0.0/8",
			"169.254.0.0/16",
			"172.16.0.0/12",
			"192.0.0.0/24",
			"192.0.2.0/24",
			"192.88.99.0/24",
			"192.168.0.0/16",
			"198.18.0.0/15",
			"198.51.100.0/24",
			"203.0.113.0/24",
			"224.0.0.0/4",
			"233.252.0.0/24",
			"240.0.0.0/4",
			"255.255.255.255/32",

			// see https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
			"::1/128",
			"::/128",
			"::ffff:0:0/96",
			"64:ff9b::/96",
			"64:ff9b:1::/48",
			"100::/64",
			"2001::/23",
			"2001::/32",
			"2001:1::1/128",
			"2001:1::2/128",
			"2001:2::/48",
			"2001:3::/32",
			"2001:4:112::/48",
			"2001:10::/28",
			"2001:20::/28",
			"2001:db8::/32",
			"2002::/16",
			"2620:4f:8000::/48",
			"fc00::/7",
			"fe80::/10",
		].iter().map(|net|net.parse().unwrap()).collect()
	});
}