2021-12-22 10:41:34 +00:00
|
|
|
use std::convert::TryInto;
|
2021-12-22 10:58:02 +00:00
|
|
|
use std::io;
|
2021-12-13 16:06:48 +00:00
|
|
|
use std::net::SocketAddr;
|
2021-12-22 10:41:34 +00:00
|
|
|
use std::sync::Arc;
|
2022-02-01 10:31:08 +00:00
|
|
|
use std::time::Duration;
|
2021-07-22 14:01:46 +00:00
|
|
|
|
2021-12-22 10:55:51 +00:00
|
|
|
use tokio::io::{AsyncRead, AsyncWrite};
|
2021-12-22 10:33:55 +00:00
|
|
|
use tokio::net::TcpStream;
|
2021-12-22 10:58:02 +00:00
|
|
|
#[cfg(feature = "tokio-rustls")]
|
2021-12-22 10:33:55 +00:00
|
|
|
use tokio_rustls::client::TlsStream;
|
2021-12-22 10:58:02 +00:00
|
|
|
#[cfg(feature = "tokio-rustls")]
|
2021-12-22 10:41:34 +00:00
|
|
|
use tokio_rustls::rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore};
|
2021-12-22 10:58:02 +00:00
|
|
|
#[cfg(feature = "tokio-rustls")]
|
2021-12-22 10:41:34 +00:00
|
|
|
use tokio_rustls::TlsConnector;
|
|
|
|
use tracing::info;
|
2021-12-22 10:33:55 +00:00
|
|
|
|
2021-12-13 16:06:48 +00:00
|
|
|
use crate::common::{Certificate, NoExtension, PrivateKey};
|
2022-02-01 10:31:08 +00:00
|
|
|
use crate::connection::{self, EppConnection};
|
2021-12-22 09:55:48 +00:00
|
|
|
use crate::error::Error;
|
2021-12-08 15:38:58 +00:00
|
|
|
use crate::hello::{Greeting, GreetingDocument, HelloDocument};
|
2021-12-09 09:17:00 +00:00
|
|
|
use crate::request::{Command, Extension, Transaction};
|
2021-11-30 23:39:54 +00:00
|
|
|
use crate::response::Response;
|
2021-11-26 19:36:28 +00:00
|
|
|
use crate::xml::EppXml;
|
2021-12-01 13:21:43 +00:00
|
|
|
|
2022-02-01 10:41:04 +00:00
|
|
|
/// An `EppClient` provides an interface to sending EPP requests to a registry
|
|
|
|
///
|
2021-07-25 14:34:01 +00:00
|
|
|
/// Once initialized, the EppClient instance can serialize EPP requests to XML and send them
|
2022-02-01 10:41:04 +00:00
|
|
|
/// to the registry and deserialize the XML responses from the registry to local types.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use std::collections::HashMap;
|
|
|
|
/// # use std::net::ToSocketAddrs;
|
|
|
|
/// # use std::time::Duration;
|
|
|
|
/// #
|
|
|
|
/// use epp_client::EppClient;
|
|
|
|
/// use epp_client::domain::DomainCheck;
|
|
|
|
/// use epp_client::common::NoExtension;
|
|
|
|
///
|
|
|
|
/// # #[tokio::main]
|
|
|
|
/// # async fn main() {
|
|
|
|
/// // Create an instance of EppClient
|
|
|
|
/// let host = "example.com";
|
|
|
|
/// let addr = (host, 7000).to_socket_addrs().unwrap().next().unwrap();
|
|
|
|
/// let timeout = Duration::from_secs(5);
|
|
|
|
/// let mut client = match EppClient::connect("registry_name".to_string(), addr, host, None, timeout).await {
|
|
|
|
/// Ok(client) => client,
|
|
|
|
/// Err(e) => panic!("Failed to create EppClient: {}", e)
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// // Make a EPP Hello call to the registry
|
|
|
|
/// let greeting = client.hello().await.unwrap();
|
|
|
|
/// println!("{:?}", greeting);
|
|
|
|
///
|
|
|
|
/// // Execute an EPP Command against the registry with distinct request and response objects
|
|
|
|
/// let domain_check = DomainCheck { domains: &["eppdev.com", "eppdev.net"] };
|
|
|
|
/// let response = client.transact(&domain_check, "transaction-id").await.unwrap();
|
|
|
|
/// response.res_data.unwrap().list
|
|
|
|
/// .iter()
|
|
|
|
/// .for_each(|chk| println!("Domain: {}, Available: {}", chk.id, chk.available));
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The output would look like this:
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// Domain: eppdev.com, Available: 1
|
|
|
|
/// Domain: eppdev.net, Available: 1
|
|
|
|
/// ```
|
2021-12-22 10:55:51 +00:00
|
|
|
pub struct EppClient<IO> {
|
|
|
|
connection: EppConnection<IO>,
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 10:58:02 +00:00
|
|
|
#[cfg(feature = "tokio-rustls")]
|
2021-12-22 10:55:51 +00:00
|
|
|
impl EppClient<TlsStream<TcpStream>> {
|
|
|
|
/// Connect to the specified `addr` and `hostname` over TLS
|
|
|
|
///
|
|
|
|
/// The `registry` is used as a name in internal logging; `addr` provides the address to
|
|
|
|
/// connect to, `hostname` is sent as the TLS server name indication and `identity` provides
|
2022-02-01 10:31:08 +00:00
|
|
|
/// optional TLS client authentication (using) rustls as the TLS implementation.
|
|
|
|
/// The `timeout` limits the time spent on any underlying network operations.
|
2021-12-22 10:55:51 +00:00
|
|
|
///
|
|
|
|
/// Alternatively, use `EppClient::new()` with any established `AsyncRead + AsyncWrite + Unpin`
|
|
|
|
/// implementation.
|
|
|
|
pub async fn connect(
|
2021-12-13 16:06:48 +00:00
|
|
|
registry: String,
|
|
|
|
addr: SocketAddr,
|
|
|
|
hostname: &str,
|
|
|
|
identity: Option<(Vec<Certificate>, PrivateKey)>,
|
2022-02-01 10:31:08 +00:00
|
|
|
timeout: Duration,
|
2021-12-22 10:07:19 +00:00
|
|
|
) -> Result<Self, Error> {
|
2021-12-22 10:55:51 +00:00
|
|
|
info!("Connecting to server: {:?}", addr);
|
|
|
|
|
|
|
|
let mut roots = RootCertStore::empty();
|
|
|
|
roots.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
|
|
|
|
OwnedTrustAnchor::from_subject_spki_name_constraints(
|
|
|
|
ta.subject,
|
|
|
|
ta.spki,
|
|
|
|
ta.name_constraints,
|
|
|
|
)
|
|
|
|
}));
|
|
|
|
|
|
|
|
let builder = ClientConfig::builder()
|
|
|
|
.with_safe_defaults()
|
|
|
|
.with_root_certificates(roots);
|
|
|
|
|
|
|
|
let config = match identity {
|
|
|
|
Some((certs, key)) => {
|
|
|
|
let certs = certs
|
|
|
|
.into_iter()
|
2021-12-22 10:58:02 +00:00
|
|
|
.map(|cert| tokio_rustls::rustls::Certificate(cert.0))
|
2021-12-22 10:55:51 +00:00
|
|
|
.collect();
|
|
|
|
builder
|
2021-12-22 10:58:02 +00:00
|
|
|
.with_single_cert(certs, tokio_rustls::rustls::PrivateKey(key.0))
|
2021-12-22 10:55:51 +00:00
|
|
|
.map_err(|e| Error::Other(e.into()))?
|
|
|
|
}
|
|
|
|
None => builder.with_no_client_auth(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let domain = hostname.try_into().map_err(|_| {
|
|
|
|
io::Error::new(
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
format!("Invalid domain: {}", hostname),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let connector = TlsConnector::from(Arc::new(config));
|
2022-02-01 10:31:08 +00:00
|
|
|
let future = connector.connect(domain, TcpStream::connect(&addr).await?);
|
|
|
|
let stream = connection::timeout(timeout, future).await?;
|
|
|
|
Self::new(registry, stream, timeout).await
|
2021-12-22 10:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<IO: AsyncRead + AsyncWrite + Unpin> EppClient<IO> {
|
|
|
|
/// Create an `EppClient` from an already established connection
|
2022-02-01 10:31:08 +00:00
|
|
|
pub async fn new(registry: String, stream: IO, timeout: Duration) -> Result<Self, Error> {
|
2021-12-13 16:06:48 +00:00
|
|
|
Ok(Self {
|
2022-02-01 10:31:08 +00:00
|
|
|
connection: EppConnection::new(registry, stream, timeout).await?,
|
2021-12-13 15:31:06 +00:00
|
|
|
})
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 23:39:54 +00:00
|
|
|
/// Executes an EPP Hello call and returns the response as an `Greeting`
|
2021-12-22 10:07:19 +00:00
|
|
|
pub async fn hello(&mut self) -> Result<Greeting, Error> {
|
2021-12-08 15:38:58 +00:00
|
|
|
let hello_xml = HelloDocument::default().serialize()?;
|
2021-07-22 14:01:46 +00:00
|
|
|
|
|
|
|
let response = self.connection.transact(&hello_xml).await?;
|
|
|
|
|
2021-12-08 15:38:58 +00:00
|
|
|
Ok(GreetingDocument::deserialize(&response)?.data)
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 09:41:53 +00:00
|
|
|
pub async fn transact<'c, 'e, C, E>(
|
2021-11-26 21:50:22 +00:00
|
|
|
&mut self,
|
2022-01-28 09:41:53 +00:00
|
|
|
data: impl Into<RequestData<'c, 'e, C, E>>,
|
2021-11-26 21:50:22 +00:00
|
|
|
id: &str,
|
2021-12-22 09:55:48 +00:00
|
|
|
) -> Result<Response<C::Response, E::Response>, Error>
|
2021-11-26 21:50:22 +00:00
|
|
|
where
|
2022-01-28 09:41:53 +00:00
|
|
|
C: Transaction<E> + Command + 'c,
|
|
|
|
E: Extension + 'e,
|
2021-11-26 21:50:22 +00:00
|
|
|
{
|
2021-12-09 09:17:00 +00:00
|
|
|
let data = data.into();
|
|
|
|
let epp_xml = <C as Transaction<E>>::serialize_request(data.command, data.extension, id)?;
|
2021-11-26 21:50:22 +00:00
|
|
|
|
|
|
|
let response = self.connection.transact(&epp_xml).await?;
|
|
|
|
|
2021-12-09 09:17:00 +00:00
|
|
|
C::deserialize_response(&response)
|
2021-11-26 21:50:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 20:15:59 +00:00
|
|
|
/// Accepts raw EPP XML and returns the raw EPP XML response to it.
|
2021-07-26 19:27:18 +00:00
|
|
|
/// Not recommended for direct use but sometimes can be useful for debugging
|
2021-12-22 10:07:19 +00:00
|
|
|
pub async fn transact_xml(&mut self, xml: &str) -> Result<String, Error> {
|
2021-10-27 22:45:32 +00:00
|
|
|
self.connection.transact(xml).await
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 14:34:01 +00:00
|
|
|
/// Returns the greeting received on establishment of the connection in raw xml form
|
2021-07-22 14:01:46 +00:00
|
|
|
pub fn xml_greeting(&self) -> String {
|
2021-10-27 22:45:32 +00:00
|
|
|
String::from(&self.connection.greeting)
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 23:39:54 +00:00
|
|
|
/// Returns the greeting received on establishment of the connection as an `Greeting`
|
2021-12-22 09:55:48 +00:00
|
|
|
pub fn greeting(&self) -> Result<Greeting, Error> {
|
2021-12-08 15:38:58 +00:00
|
|
|
GreetingDocument::deserialize(&self.connection.greeting).map(|obj| obj.data)
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
2021-12-13 15:35:50 +00:00
|
|
|
|
2021-12-22 10:07:19 +00:00
|
|
|
pub async fn shutdown(mut self) -> Result<(), Error> {
|
2021-12-13 15:35:50 +00:00
|
|
|
self.connection.shutdown().await
|
|
|
|
}
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
2021-12-09 09:17:00 +00:00
|
|
|
|
2022-01-28 09:41:53 +00:00
|
|
|
pub struct RequestData<'c, 'e, C, E> {
|
|
|
|
command: &'c C,
|
|
|
|
extension: Option<&'e E>,
|
2021-12-09 09:17:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 09:41:53 +00:00
|
|
|
impl<'c, C: Command> From<&'c C> for RequestData<'c, 'static, C, NoExtension> {
|
|
|
|
fn from(command: &'c C) -> Self {
|
2021-12-09 09:17:00 +00:00
|
|
|
Self {
|
|
|
|
command,
|
|
|
|
extension: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 09:41:53 +00:00
|
|
|
impl<'c, 'e, C: Command, E: Extension> From<(&'c C, &'e E)> for RequestData<'c, 'e, C, E> {
|
|
|
|
fn from((command, extension): (&'c C, &'e E)) -> Self {
|
2021-12-09 09:17:00 +00:00
|
|
|
Self {
|
|
|
|
command,
|
|
|
|
extension: Some(extension),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|