2019-03-13 12:40:11 -07:00
|
|
|
//! Actix connect - tcp connector service
|
2018-12-10 08:42:31 -08:00
|
|
|
//!
|
|
|
|
//! ## Package feature
|
|
|
|
//!
|
2019-12-02 22:30:09 +06:00
|
|
|
//! * `openssl` - enables ssl support via `openssl` crate
|
|
|
|
//! * `rustls` - enables ssl support via `rustls` crate
|
|
|
|
#![deny(rust_2018_idioms, warnings)]
|
|
|
|
#![allow(clippy::type_complexity)]
|
2019-04-23 21:58:34 +02:00
|
|
|
#![recursion_limit = "128"]
|
|
|
|
|
2019-03-13 12:40:11 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
|
|
|
mod connect;
|
2018-12-10 08:42:31 -08:00
|
|
|
mod connector;
|
2019-03-13 12:40:11 -07:00
|
|
|
mod error;
|
2019-12-05 16:40:24 +06:00
|
|
|
mod resolve;
|
2019-08-05 09:52:50 -07:00
|
|
|
mod service;
|
2018-12-10 16:16:40 -08:00
|
|
|
pub mod ssl;
|
2018-12-10 08:42:31 -08:00
|
|
|
|
2019-03-14 11:15:32 -07:00
|
|
|
#[cfg(feature = "uri")]
|
|
|
|
mod uri;
|
|
|
|
|
2019-12-02 11:43:52 +06:00
|
|
|
use actix_rt::{net::TcpStream, Arbiter};
|
2019-11-18 14:30:04 +06:00
|
|
|
use actix_service::{pipeline, pipeline_factory, Service, ServiceFactory};
|
2019-12-05 16:40:24 +06:00
|
|
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
|
|
|
use trust_dns_resolver::system_conf::read_system_conf;
|
2020-02-25 23:44:59 -03:00
|
|
|
use trust_dns_resolver::TokioAsyncResolver as AsyncResolver;
|
2019-12-05 16:40:24 +06:00
|
|
|
|
|
|
|
pub mod resolver {
|
|
|
|
pub use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
|
|
|
pub use trust_dns_resolver::system_conf::read_system_conf;
|
|
|
|
pub use trust_dns_resolver::{error::ResolveError, AsyncResolver};
|
|
|
|
}
|
2019-03-13 12:40:11 -07:00
|
|
|
|
2019-03-13 15:37:12 -07:00
|
|
|
pub use self::connect::{Address, Connect, Connection};
|
2019-03-31 19:14:13 -07:00
|
|
|
pub use self::connector::{TcpConnector, TcpConnectorFactory};
|
2019-03-13 12:40:11 -07:00
|
|
|
pub use self::error::ConnectError;
|
2019-12-05 16:40:24 +06:00
|
|
|
pub use self::resolve::{Resolver, ResolverFactory};
|
2019-09-05 16:34:48 -07:00
|
|
|
pub use self::service::{ConnectService, ConnectServiceFactory, TcpConnectService};
|
2019-03-13 12:40:11 -07:00
|
|
|
|
2020-02-25 23:44:59 -03:00
|
|
|
pub async fn start_resolver(
|
|
|
|
cfg: ResolverConfig,
|
|
|
|
opts: ResolverOpts,
|
|
|
|
) -> Result<AsyncResolver, ConnectError> {
|
|
|
|
Ok(AsyncResolver::tokio(cfg, opts).await?)
|
2019-03-13 22:51:31 -07:00
|
|
|
}
|
|
|
|
|
2019-09-02 15:15:55 -07:00
|
|
|
struct DefaultResolver(AsyncResolver);
|
2019-04-11 09:57:21 -07:00
|
|
|
|
2020-02-25 23:44:59 -03:00
|
|
|
pub(crate) async fn get_default_resolver() -> Result<AsyncResolver, ConnectError> {
|
2019-09-02 15:15:55 -07:00
|
|
|
if Arbiter::contains_item::<DefaultResolver>() {
|
2020-02-25 23:44:59 -03:00
|
|
|
Ok(Arbiter::get_item(|item: &DefaultResolver| item.0.clone()))
|
2019-09-02 15:15:55 -07:00
|
|
|
} else {
|
2019-04-11 09:57:21 -07:00
|
|
|
let (cfg, opts) = match read_system_conf() {
|
|
|
|
Ok((cfg, opts)) => (cfg, opts),
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("TRust-DNS can not load system config: {}", e);
|
|
|
|
(ResolverConfig::default(), ResolverOpts::default())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-25 23:44:59 -03:00
|
|
|
let resolver = AsyncResolver::tokio(cfg, opts).await?;
|
2019-04-11 09:57:21 -07:00
|
|
|
|
2019-09-02 15:15:55 -07:00
|
|
|
Arbiter::set_item(DefaultResolver(resolver.clone()));
|
2020-02-25 23:44:59 -03:00
|
|
|
Ok(resolver)
|
2019-09-02 15:15:55 -07:00
|
|
|
}
|
2019-04-11 09:57:21 -07:00
|
|
|
}
|
|
|
|
|
2020-02-25 23:44:59 -03:00
|
|
|
pub async fn start_default_resolver() -> Result<AsyncResolver, ConnectError> {
|
|
|
|
get_default_resolver().await
|
2019-03-13 22:51:31 -07:00
|
|
|
}
|
2019-03-13 12:40:11 -07:00
|
|
|
|
|
|
|
/// Create tcp connector service
|
2020-01-15 11:58:06 -08:00
|
|
|
pub fn new_connector<T: Address + 'static>(
|
2019-03-13 22:51:31 -07:00
|
|
|
resolver: AsyncResolver,
|
2019-03-13 15:37:12 -07:00
|
|
|
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
|
2019-11-18 14:30:04 +06:00
|
|
|
+ Clone {
|
2019-11-14 18:38:24 +06:00
|
|
|
pipeline(Resolver::new(resolver)).and_then(TcpConnector::new())
|
2019-03-13 12:40:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create tcp connector service
|
2020-01-15 11:58:06 -08:00
|
|
|
pub fn new_connector_factory<T: Address + 'static>(
|
2019-03-13 22:51:31 -07:00
|
|
|
resolver: AsyncResolver,
|
2019-11-14 18:38:24 +06:00
|
|
|
) -> impl ServiceFactory<
|
2019-05-12 06:03:50 -07:00
|
|
|
Config = (),
|
2019-03-13 15:37:12 -07:00
|
|
|
Request = Connect<T>,
|
|
|
|
Response = Connection<T, TcpStream>,
|
2019-03-13 12:40:11 -07:00
|
|
|
Error = ConnectError,
|
|
|
|
InitError = (),
|
2019-11-18 14:30:04 +06:00
|
|
|
> + Clone {
|
2019-11-14 18:38:24 +06:00
|
|
|
pipeline_factory(ResolverFactory::new(resolver)).and_then(TcpConnectorFactory::new())
|
2019-03-13 12:40:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create connector service with default parameters
|
2020-01-15 11:58:06 -08:00
|
|
|
pub fn default_connector<T: Address + 'static>(
|
2019-03-13 15:37:12 -07:00
|
|
|
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
|
2019-11-18 14:30:04 +06:00
|
|
|
+ Clone {
|
2019-11-14 18:38:24 +06:00
|
|
|
pipeline(Resolver::default()).and_then(TcpConnector::new())
|
2019-03-13 12:40:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create connector service factory with default parameters
|
2020-01-15 11:58:06 -08:00
|
|
|
pub fn default_connector_factory<T: Address + 'static>() -> impl ServiceFactory<
|
2019-05-12 06:03:50 -07:00
|
|
|
Config = (),
|
2019-03-13 15:37:12 -07:00
|
|
|
Request = Connect<T>,
|
|
|
|
Response = Connection<T, TcpStream>,
|
2019-03-13 12:40:11 -07:00
|
|
|
Error = ConnectError,
|
|
|
|
InitError = (),
|
2019-11-18 14:30:04 +06:00
|
|
|
> + Clone {
|
2019-11-14 18:38:24 +06:00
|
|
|
pipeline_factory(ResolverFactory::default()).and_then(TcpConnectorFactory::new())
|
2019-03-13 12:40:11 -07:00
|
|
|
}
|