2019-03-13 20:40:11 +01:00
|
|
|
//! Actix connect - tcp connector service
|
2018-12-10 17:42:31 +01:00
|
|
|
//!
|
|
|
|
//! ## Package feature
|
|
|
|
//!
|
|
|
|
//! * `ssl` - enables ssl support via `openssl` crate
|
|
|
|
//! * `rust-tls` - enables ssl support via `rustls` crate
|
|
|
|
|
2019-04-23 21:58:34 +02:00
|
|
|
#![recursion_limit = "128"]
|
|
|
|
|
2019-03-13 20:40:11 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
|
|
|
mod connect;
|
2018-12-10 17:42:31 +01:00
|
|
|
mod connector;
|
2019-03-13 20:40:11 +01:00
|
|
|
mod error;
|
2018-12-10 17:42:31 +01:00
|
|
|
mod resolver;
|
2019-08-05 18:52:50 +02:00
|
|
|
mod service;
|
2018-12-11 01:16:40 +01:00
|
|
|
pub mod ssl;
|
2018-12-10 17:42:31 +01:00
|
|
|
|
2019-03-14 19:15:32 +01:00
|
|
|
#[cfg(feature = "uri")]
|
|
|
|
mod uri;
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
use actix_rt::Arbiter;
|
|
|
|
use actix_service::{pipeline, pipeline_factory, Service, ServiceFactory};
|
|
|
|
use tokio_net::tcp::TcpStream;
|
|
|
|
|
2019-04-05 19:36:57 +02:00
|
|
|
pub use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
|
|
|
pub use trust_dns_resolver::system_conf::read_system_conf;
|
2019-03-14 06:51:31 +01:00
|
|
|
pub use trust_dns_resolver::{error::ResolveError, AsyncResolver};
|
2019-03-13 20:40:11 +01:00
|
|
|
|
2019-03-13 23:37:12 +01:00
|
|
|
pub use self::connect::{Address, Connect, Connection};
|
2019-04-01 04:14:13 +02:00
|
|
|
pub use self::connector::{TcpConnector, TcpConnectorFactory};
|
2019-03-13 20:40:11 +01:00
|
|
|
pub use self::error::ConnectError;
|
|
|
|
pub use self::resolver::{Resolver, ResolverFactory};
|
2019-09-06 01:34:48 +02:00
|
|
|
pub use self::service::{ConnectService, ConnectServiceFactory, TcpConnectService};
|
2019-03-13 20:40:11 +01:00
|
|
|
|
2019-03-14 06:51:31 +01:00
|
|
|
pub fn start_resolver(cfg: ResolverConfig, opts: ResolverOpts) -> AsyncResolver {
|
|
|
|
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
2019-11-26 03:26:22 +01:00
|
|
|
actix_rt::spawn(bg);
|
2019-03-14 06:51:31 +01:00
|
|
|
resolver
|
|
|
|
}
|
|
|
|
|
2019-09-03 00:15:55 +02:00
|
|
|
struct DefaultResolver(AsyncResolver);
|
2019-04-11 18:57:21 +02:00
|
|
|
|
|
|
|
pub(crate) fn get_default_resolver() -> AsyncResolver {
|
2019-09-03 00:15:55 +02:00
|
|
|
if Arbiter::contains_item::<DefaultResolver>() {
|
|
|
|
return Arbiter::get_item(|item: &DefaultResolver| item.0.clone());
|
|
|
|
} else {
|
2019-04-11 18:57:21 +02: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())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
2019-11-26 03:26:22 +01:00
|
|
|
actix_rt::spawn(bg);
|
2019-04-11 18:57:21 +02:00
|
|
|
|
2019-09-03 00:15:55 +02:00
|
|
|
Arbiter::set_item(DefaultResolver(resolver.clone()));
|
2019-04-11 18:57:21 +02:00
|
|
|
resolver
|
2019-09-03 00:15:55 +02:00
|
|
|
}
|
2019-04-11 18:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start_default_resolver() -> AsyncResolver {
|
|
|
|
get_default_resolver()
|
2019-03-14 06:51:31 +01:00
|
|
|
}
|
2019-03-13 20:40:11 +01:00
|
|
|
|
|
|
|
/// Create tcp connector service
|
2019-03-13 23:37:12 +01:00
|
|
|
pub fn new_connector<T: Address>(
|
2019-03-14 06:51:31 +01:00
|
|
|
resolver: AsyncResolver,
|
2019-03-13 23:37:12 +01:00
|
|
|
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
|
2019-11-18 09:30:04 +01:00
|
|
|
+ Clone {
|
2019-11-14 13:38:24 +01:00
|
|
|
pipeline(Resolver::new(resolver)).and_then(TcpConnector::new())
|
2019-03-13 20:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create tcp connector service
|
2019-03-13 23:37:12 +01:00
|
|
|
pub fn new_connector_factory<T: Address>(
|
2019-03-14 06:51:31 +01:00
|
|
|
resolver: AsyncResolver,
|
2019-11-14 13:38:24 +01:00
|
|
|
) -> impl ServiceFactory<
|
2019-05-12 15:03:50 +02:00
|
|
|
Config = (),
|
2019-03-13 23:37:12 +01:00
|
|
|
Request = Connect<T>,
|
|
|
|
Response = Connection<T, TcpStream>,
|
2019-03-13 20:40:11 +01:00
|
|
|
Error = ConnectError,
|
|
|
|
InitError = (),
|
2019-11-18 09:30:04 +01:00
|
|
|
> + Clone {
|
2019-11-14 13:38:24 +01:00
|
|
|
pipeline_factory(ResolverFactory::new(resolver)).and_then(TcpConnectorFactory::new())
|
2019-03-13 20:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create connector service with default parameters
|
2019-03-13 23:37:12 +01:00
|
|
|
pub fn default_connector<T: Address>(
|
|
|
|
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
|
2019-11-18 09:30:04 +01:00
|
|
|
+ Clone {
|
2019-11-14 13:38:24 +01:00
|
|
|
pipeline(Resolver::default()).and_then(TcpConnector::new())
|
2019-03-13 20:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create connector service factory with default parameters
|
2019-11-14 13:38:24 +01:00
|
|
|
pub fn default_connector_factory<T: Address>() -> impl ServiceFactory<
|
2019-05-12 15:03:50 +02:00
|
|
|
Config = (),
|
2019-03-13 23:37:12 +01:00
|
|
|
Request = Connect<T>,
|
|
|
|
Response = Connection<T, TcpStream>,
|
2019-03-13 20:40:11 +01:00
|
|
|
Error = ConnectError,
|
|
|
|
InitError = (),
|
2019-11-18 09:30:04 +01:00
|
|
|
> + Clone {
|
2019-11-14 13:38:24 +01:00
|
|
|
pipeline_factory(ResolverFactory::default()).and_then(TcpConnectorFactory::new())
|
2019-03-13 20:40:11 +01:00
|
|
|
}
|