2022-02-01 10:31:08 +00:00
|
|
|
use std::time::Duration;
|
2021-07-22 14:01:46 +00:00
|
|
|
|
2023-08-02 13:30:52 +00:00
|
|
|
use tracing::{debug, error};
|
2021-12-22 10:33:55 +00:00
|
|
|
|
2023-08-02 13:30:52 +00:00
|
|
|
use crate::common::NoExtension;
|
2023-08-02 13:31:45 +00:00
|
|
|
#[cfg(feature = "rustls")]
|
2023-08-02 13:30:52 +00:00
|
|
|
use crate::common::{Certificate, PrivateKey};
|
2022-02-04 21:38:45 +00:00
|
|
|
pub use crate::connection::Connector;
|
2023-08-02 13:30:52 +00:00
|
|
|
use crate::connection::EppConnection;
|
2021-12-22 09:55:48 +00:00
|
|
|
use crate::error::Error;
|
2022-11-21 19:34:25 +00:00
|
|
|
use crate::hello::{Greeting, Hello};
|
|
|
|
use crate::request::{Command, CommandWrapper, Extension, Transaction};
|
|
|
|
use crate::response::{Response, ResponseStatus};
|
2022-03-12 19:36:46 +00:00
|
|
|
use crate::xml;
|
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;
|
|
|
|
/// #
|
2022-11-21 19:34:25 +00:00
|
|
|
/// use instant_epp::EppClient;
|
|
|
|
/// use instant_epp::domain::DomainCheck;
|
|
|
|
/// use instant_epp::common::NoExtension;
|
2022-02-01 10:41:04 +00:00
|
|
|
///
|
2023-08-02 13:31:45 +00:00
|
|
|
/// # #[cfg(feature = "rustls")]
|
2022-02-01 10:41:04 +00:00
|
|
|
/// # #[tokio::main]
|
|
|
|
/// # async fn main() {
|
|
|
|
/// // Create an instance of EppClient
|
|
|
|
/// let timeout = Duration::from_secs(5);
|
2022-12-12 12:56:52 +00:00
|
|
|
/// let mut client = match EppClient::connect("registry_name".to_string(), ("example.com".to_owned(), 7000), None, timeout).await {
|
2022-02-01 10:41:04 +00:00
|
|
|
/// 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();
|
2022-11-21 19:34:25 +00:00
|
|
|
/// response
|
|
|
|
/// .res_data()
|
|
|
|
/// .unwrap()
|
|
|
|
/// .list
|
2022-02-01 10:41:04 +00:00
|
|
|
/// .iter()
|
2022-11-21 19:34:25 +00:00
|
|
|
/// .for_each(|chk| println!("Domain: {}, Available: {}", chk.inner.id, chk.inner.available));
|
2022-02-01 10:41:04 +00:00
|
|
|
/// # }
|
2023-08-02 13:30:52 +00:00
|
|
|
/// #
|
2023-08-02 13:31:45 +00:00
|
|
|
/// # #[cfg(not(feature = "rustls"))]
|
2023-08-02 13:30:52 +00:00
|
|
|
/// # fn main() {}
|
2022-02-01 10:41:04 +00:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The output would look like this:
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// Domain: eppdev.com, Available: 1
|
|
|
|
/// Domain: eppdev.net, Available: 1
|
|
|
|
/// ```
|
2022-02-04 21:38:45 +00:00
|
|
|
pub struct EppClient<C: Connector> {
|
|
|
|
connection: EppConnection<C>,
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 13:31:45 +00:00
|
|
|
#[cfg(feature = "rustls")]
|
2022-02-04 21:38:45 +00:00
|
|
|
impl EppClient<RustlsConnector> {
|
2021-12-22 10:55:51 +00:00
|
|
|
/// Connect to the specified `addr` and `hostname` over TLS
|
|
|
|
///
|
2022-12-12 12:56:52 +00:00
|
|
|
/// The `registry` is used as a name in internal logging; `host` provides the host name
|
|
|
|
/// and port to connect to), `hostname` is sent as the TLS server name indication and
|
|
|
|
/// `identity` provides 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,
|
2022-12-12 12:56:52 +00:00
|
|
|
server: (String, u16),
|
2021-12-13 16:06:48 +00:00
|
|
|
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> {
|
2022-12-12 12:56:52 +00:00
|
|
|
let connector = RustlsConnector::new(server, identity).await?;
|
2022-02-04 21:38:45 +00:00
|
|
|
Self::new(connector, registry, timeout).await
|
2021-12-22 10:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 21:38:45 +00:00
|
|
|
impl<C: Connector> EppClient<C> {
|
2021-12-22 10:55:51 +00:00
|
|
|
/// Create an `EppClient` from an already established connection
|
2022-02-04 21:38:45 +00:00
|
|
|
pub async fn new(connector: C, registry: String, timeout: Duration) -> Result<Self, Error> {
|
2021-12-13 16:06:48 +00:00
|
|
|
Ok(Self {
|
2022-02-04 21:38:45 +00:00
|
|
|
connection: EppConnection::new(connector, registry, timeout).await?,
|
2021-12-13 15:31:06 +00:00
|
|
|
})
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 23:34:47 +00:00
|
|
|
/// Executes an EPP Hello call and returns the response as a `Greeting`
|
2021-12-22 10:07:19 +00:00
|
|
|
pub async fn hello(&mut self) -> Result<Greeting, Error> {
|
2022-11-21 19:34:25 +00:00
|
|
|
let xml = xml::serialize(Hello)?;
|
2021-07-22 14:01:46 +00:00
|
|
|
|
2022-03-12 19:36:46 +00:00
|
|
|
debug!("{}: hello: {}", self.connection.registry, &xml);
|
|
|
|
let response = self.connection.transact(&xml)?.await?;
|
2022-03-10 23:34:47 +00:00
|
|
|
debug!("{}: greeting: {}", self.connection.registry, &response);
|
2021-07-22 14:01:46 +00:00
|
|
|
|
2022-11-21 19:34:25 +00:00
|
|
|
xml::deserialize::<Greeting>(&response)
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 21:38:45 +00:00
|
|
|
pub async fn transact<'c, 'e, Cmd, Ext>(
|
2021-11-26 21:50:22 +00:00
|
|
|
&mut self,
|
2022-02-04 21:38:45 +00:00
|
|
|
data: impl Into<RequestData<'c, 'e, Cmd, Ext>>,
|
2021-11-26 21:50:22 +00:00
|
|
|
id: &str,
|
2022-02-04 21:38:45 +00:00
|
|
|
) -> Result<Response<Cmd::Response, Ext::Response>, Error>
|
2021-11-26 21:50:22 +00:00
|
|
|
where
|
2022-02-04 21:38:45 +00:00
|
|
|
Cmd: Transaction<Ext> + Command + 'c,
|
|
|
|
Ext: Extension + 'e,
|
2021-11-26 21:50:22 +00:00
|
|
|
{
|
2021-12-09 09:17:00 +00:00
|
|
|
let data = data.into();
|
2022-11-21 19:34:25 +00:00
|
|
|
let document = CommandWrapper::new(data.command, data.extension, id);
|
2022-03-12 19:36:46 +00:00
|
|
|
let xml = xml::serialize(&document)?;
|
2021-11-26 21:50:22 +00:00
|
|
|
|
2022-03-10 11:50:09 +00:00
|
|
|
debug!("{}: request: {}", self.connection.registry, &xml);
|
|
|
|
let response = self.connection.transact(&xml)?.await?;
|
2022-03-10 23:34:47 +00:00
|
|
|
debug!("{}: response: {}", self.connection.registry, &response);
|
2021-11-26 21:50:22 +00:00
|
|
|
|
2022-11-21 19:34:25 +00:00
|
|
|
let rsp = match xml::deserialize::<Response<Cmd::Response, Ext::Response>>(&response) {
|
|
|
|
Ok(rsp) => rsp,
|
|
|
|
Err(e) => {
|
|
|
|
error!(%response, "failed to deserialize response for transaction: {e}");
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if rsp.result.code.is_success() {
|
|
|
|
return Ok(rsp);
|
2022-02-06 19:27:48 +00:00
|
|
|
}
|
2022-03-12 17:12:16 +00:00
|
|
|
|
2022-11-21 19:38:56 +00:00
|
|
|
let err = crate::error::Error::Command(Box::new(ResponseStatus {
|
2022-11-21 19:34:25 +00:00
|
|
|
result: rsp.result,
|
|
|
|
tr_ids: rsp.tr_ids,
|
2022-11-21 19:38:56 +00:00
|
|
|
}));
|
2022-03-12 17:12:16 +00:00
|
|
|
|
|
|
|
Err(err)
|
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> {
|
2022-03-09 16:49:35 +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> {
|
2022-11-21 19:34:25 +00:00
|
|
|
xml::deserialize::<Greeting>(&self.connection.greeting)
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
2021-12-13 15:35:50 +00:00
|
|
|
|
2022-02-05 20:59:44 +00:00
|
|
|
pub async fn reconnect(&mut self) -> Result<(), Error> {
|
|
|
|
self.connection.reconnect().await
|
|
|
|
}
|
|
|
|
|
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-02-07 16:07:16 +00:00
|
|
|
#[derive(Debug)]
|
2022-01-28 09:41:53 +00:00
|
|
|
pub struct RequestData<'c, 'e, C, E> {
|
2022-03-10 11:50:09 +00:00
|
|
|
pub(crate) command: &'c C,
|
|
|
|
pub(crate) 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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-04 21:38:45 +00:00
|
|
|
|
2022-02-05 21:07:53 +00:00
|
|
|
// Manual impl because this does not depend on whether `C` and `E` are `Clone`
|
|
|
|
impl<'c, 'e, C, E> Clone for RequestData<'c, 'e, C, E> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
command: self.command,
|
|
|
|
extension: self.extension,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manual impl because this does not depend on whether `C` and `E` are `Copy`
|
|
|
|
impl<'c, 'e, C, E> Copy for RequestData<'c, 'e, C, E> {}
|
|
|
|
|
2023-08-02 13:31:45 +00:00
|
|
|
#[cfg(feature = "rustls")]
|
2023-08-02 13:30:52 +00:00
|
|
|
use rustls_connector::RustlsConnector;
|
2022-02-04 21:38:45 +00:00
|
|
|
|
2023-08-02 13:31:45 +00:00
|
|
|
#[cfg(feature = "rustls")]
|
2023-08-02 13:30:52 +00:00
|
|
|
mod rustls_connector {
|
|
|
|
use std::io;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use tokio::net::lookup_host;
|
|
|
|
use tokio::net::TcpStream;
|
|
|
|
use tokio_rustls::client::TlsStream;
|
|
|
|
use tokio_rustls::rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore, ServerName};
|
|
|
|
use tokio_rustls::TlsConnector;
|
|
|
|
use tracing::info;
|
|
|
|
|
|
|
|
use crate::common::{Certificate, PrivateKey};
|
|
|
|
use crate::connection::{self, Connector};
|
|
|
|
use crate::error::Error;
|
|
|
|
|
|
|
|
pub struct RustlsConnector {
|
|
|
|
inner: TlsConnector,
|
|
|
|
domain: ServerName,
|
2022-12-12 12:56:52 +00:00
|
|
|
server: (String, u16),
|
2022-02-04 21:38:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 13:30:52 +00:00
|
|
|
impl RustlsConnector {
|
|
|
|
pub async fn new(
|
|
|
|
server: (String, u16),
|
|
|
|
identity: Option<(Vec<Certificate>, PrivateKey)>,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
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()
|
|
|
|
.map(|cert| tokio_rustls::rustls::Certificate(cert.0))
|
|
|
|
.collect();
|
|
|
|
builder
|
|
|
|
.with_client_auth_cert(certs, tokio_rustls::rustls::PrivateKey(key.0))
|
|
|
|
.map_err(|e| Error::Other(e.into()))?
|
|
|
|
}
|
|
|
|
None => builder.with_no_client_auth(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let domain = server.0.as_str().try_into().map_err(|_| {
|
|
|
|
io::Error::new(
|
2022-12-12 12:56:52 +00:00
|
|
|
io::ErrorKind::InvalidInput,
|
2023-08-02 13:30:52 +00:00
|
|
|
format!("Invalid domain: {}", server.0),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
inner: TlsConnector::from(Arc::new(config)),
|
|
|
|
domain,
|
|
|
|
server,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-12-12 12:56:52 +00:00
|
|
|
|
2023-08-02 13:30:52 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Connector for RustlsConnector {
|
|
|
|
type Connection = TlsStream<TcpStream>;
|
|
|
|
|
|
|
|
async fn connect(&self, timeout: Duration) -> Result<Self::Connection, Error> {
|
|
|
|
info!("Connecting to server: {}:{}", self.server.0, self.server.1);
|
|
|
|
let addr = match lookup_host(&self.server).await?.next() {
|
|
|
|
Some(addr) => addr,
|
|
|
|
None => {
|
|
|
|
return Err(Error::Io(io::Error::new(
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
format!("Invalid host: {}", &self.server.0),
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let stream = TcpStream::connect(addr).await?;
|
|
|
|
let future = self.inner.connect(self.domain.clone(), stream);
|
|
|
|
connection::timeout(timeout, future).await
|
|
|
|
}
|
2022-02-04 21:38:45 +00:00
|
|
|
}
|
|
|
|
}
|