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-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;
|
2018-12-11 01:16:40 +01:00
|
|
|
pub mod ssl;
|
2018-12-10 17:42:31 +01:00
|
|
|
|
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-03-13 20:40:11 +01:00
|
|
|
pub use self::connector::{Connector, ConnectorFactory};
|
|
|
|
pub use self::error::ConnectError;
|
|
|
|
pub use self::resolver::{Resolver, ResolverFactory};
|
|
|
|
|
|
|
|
use actix_service::{NewService, Service, ServiceExt};
|
|
|
|
use tokio_tcp::TcpStream;
|
|
|
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
2019-03-14 06:51:31 +01:00
|
|
|
use trust_dns_resolver::system_conf::read_system_conf;
|
|
|
|
|
|
|
|
pub fn start_resolver(cfg: ResolverConfig, opts: ResolverOpts) -> AsyncResolver {
|
|
|
|
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
|
|
|
tokio_current_thread::spawn(bg);
|
|
|
|
resolver
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start_default_resolver() -> AsyncResolver {
|
|
|
|
let (cfg, opts) = if let Ok((cfg, opts)) = read_system_conf() {
|
|
|
|
(cfg, opts)
|
|
|
|
} else {
|
|
|
|
(ResolverConfig::default(), ResolverOpts::default())
|
|
|
|
};
|
|
|
|
|
|
|
|
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
|
|
|
tokio_current_thread::spawn(bg);
|
|
|
|
resolver
|
|
|
|
}
|
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>
|
|
|
|
+ Clone {
|
2019-03-14 06:51:31 +01:00
|
|
|
Resolver::new(resolver).and_then(Connector::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-03-13 20:40:11 +01:00
|
|
|
) -> impl NewService<
|
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 = (),
|
|
|
|
> + Clone {
|
2019-03-14 06:51:31 +01:00
|
|
|
ResolverFactory::new(resolver).and_then(ConnectorFactory::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>
|
|
|
|
+ Clone {
|
2019-03-14 06:51:31 +01:00
|
|
|
Resolver::new(start_default_resolver()).and_then(Connector::new())
|
2019-03-13 20:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create connector service factory with default parameters
|
2019-03-13 23:37:12 +01:00
|
|
|
pub fn default_connector_factory<T: Address>() -> impl NewService<
|
|
|
|
Request = Connect<T>,
|
|
|
|
Response = Connection<T, TcpStream>,
|
2019-03-13 20:40:11 +01:00
|
|
|
Error = ConnectError,
|
|
|
|
InitError = (),
|
|
|
|
> + Clone {
|
2019-03-14 06:51:31 +01:00
|
|
|
ResolverFactory::new(start_default_resolver()).and_then(ConnectorFactory::new())
|
2019-03-13 20:40:11 +01:00
|
|
|
}
|