2021-07-24 20:15:59 +00:00
|
|
|
//! Manages sending/receiving EppObject request and responses to the registry connection
|
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! use epp_client::EppClient;
|
|
|
|
//! use epp_client::epp::{EppDomainCheck, EppDomainCheckResponse};
|
2021-07-25 14:34:01 +00:00
|
|
|
//! use epp_client::epp::generate_client_tr_id;
|
2021-07-24 20:15:59 +00:00
|
|
|
//!
|
|
|
|
//! #[tokio::main]
|
|
|
|
//! async fn main() {
|
|
|
|
//! // Create an instance of EppClient, specifying the name of the registry as in
|
|
|
|
//! // the config file
|
|
|
|
//! let mut client = match EppClient::new("verisign").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
|
2021-07-25 14:34:01 +00:00
|
|
|
//! let domain_check = EppDomainCheck::new(vec!["eppdev.com", "eppdev.net"], generate_client_tr_id(&client).as_str());
|
2021-07-24 20:15:59 +00:00
|
|
|
//! let response = client.transact::<_, EppDomainCheckResponse>(&domain_check).await.unwrap();
|
|
|
|
//! println!("{:?}", response);
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
|
2021-07-22 14:01:46 +00:00
|
|
|
use futures::executor::block_on;
|
|
|
|
use std::{error::Error, fmt::Debug};
|
2021-07-25 14:34:01 +00:00
|
|
|
use std::time::SystemTime;
|
2021-07-22 14:01:46 +00:00
|
|
|
use std::sync::mpsc;
|
2021-07-23 16:47:41 +00:00
|
|
|
// use std::sync::Arc;
|
2021-07-22 14:01:46 +00:00
|
|
|
|
|
|
|
use crate::config::CONFIG;
|
|
|
|
use crate::connection::registry::{epp_connect, EppConnection};
|
|
|
|
use crate::error;
|
|
|
|
use crate::epp::request::{generate_client_tr_id, EppHello, EppLogin, EppLogout};
|
2021-07-25 18:26:02 +00:00
|
|
|
use crate::epp::response::{EppGreeting, EppCommandResponse, EppLoginResponse, EppLogoutResponse, EppCommandResponseError};
|
2021-07-22 14:01:46 +00:00
|
|
|
use crate::epp::xml::EppXml;
|
|
|
|
|
2021-07-25 14:34:01 +00:00
|
|
|
/// Connects to the registry and returns an logged-in instance of EppClient for further transactions
|
2021-07-22 14:01:46 +00:00
|
|
|
async fn connect(registry: &'static str) -> Result<EppClient, Box<dyn Error>> {
|
|
|
|
let registry_creds = match CONFIG.registry(registry) {
|
|
|
|
Some(creds) => creds,
|
|
|
|
None => return Err(format!("missing credentials for {}", registry).into())
|
|
|
|
};
|
|
|
|
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let stream = epp_connect(®istry_creds).await.unwrap();
|
|
|
|
let credentials = registry_creds.credentials();
|
2021-07-23 19:23:01 +00:00
|
|
|
let ext_uris = registry_creds.ext_uris();
|
|
|
|
|
|
|
|
let ext_uris = match ext_uris {
|
|
|
|
Some(uris) => Some(
|
|
|
|
uris
|
|
|
|
.iter()
|
|
|
|
.map(|u| u.to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
),
|
|
|
|
None => None,
|
|
|
|
};
|
2021-07-22 14:01:46 +00:00
|
|
|
|
|
|
|
let connection = EppConnection::new(
|
|
|
|
registry.to_string(),
|
|
|
|
stream
|
|
|
|
).await.unwrap();
|
|
|
|
|
2021-07-23 19:23:01 +00:00
|
|
|
let client = EppClient::build(connection, credentials, ext_uris).await.unwrap();
|
2021-07-22 14:01:46 +00:00
|
|
|
|
|
|
|
tx.send(client).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
let client = rx.recv()?;
|
|
|
|
|
|
|
|
Ok(client)
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:27:18 +00:00
|
|
|
/// Instances of the EppClient type are used to transact with the registry.
|
2021-07-25 14:34:01 +00:00
|
|
|
/// Once initialized, the EppClient instance can serialize EPP requests to XML and send them
|
|
|
|
/// to the registry and deserialize the XML responses from the registry to local types
|
2021-07-22 14:01:46 +00:00
|
|
|
pub struct EppClient {
|
|
|
|
credentials: (String, String),
|
2021-07-23 19:23:01 +00:00
|
|
|
ext_uris: Option<Vec<String>>,
|
2021-07-22 14:01:46 +00:00
|
|
|
connection: EppConnection,
|
2021-07-23 16:47:41 +00:00
|
|
|
// pub client_tr_id_fn: Arc<dyn Fn(&EppClient) -> String + Send + Sync>,
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 14:34:01 +00:00
|
|
|
/// A function to generate a simple client TRID. Should only be used for testing, library users
|
|
|
|
/// should generate a client TRID according to their own requirements
|
|
|
|
pub fn default_client_tr_id_fn(client: &EppClient) -> String {
|
|
|
|
let timestamp = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
|
|
|
|
Ok(time) => time,
|
|
|
|
Err(e) => panic!("Error in client TRID gen function: {}", e)
|
|
|
|
};
|
|
|
|
format!("{}:{}", &client.username(), timestamp.as_secs())
|
|
|
|
}
|
2021-07-23 16:47:41 +00:00
|
|
|
|
2021-07-22 14:01:46 +00:00
|
|
|
impl EppClient {
|
2021-07-24 20:15:59 +00:00
|
|
|
/// Fetches the username used in the registry connection
|
2021-07-23 16:47:41 +00:00
|
|
|
pub fn username(&self) -> String {
|
|
|
|
self.credentials.0.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
// pub fn set_client_tr_id_fn<F>(&mut self, func: F)
|
|
|
|
// where F: Fn(&EppClient) -> String + Send + Sync + 'static {
|
|
|
|
// self.client_tr_id_fn = Arc::new(func);
|
|
|
|
// }
|
|
|
|
|
2021-07-24 20:15:59 +00:00
|
|
|
/// Creates a new EppClient object and does an EPP Login to a given registry to become ready
|
|
|
|
/// for subsequent transactions on this client instance
|
2021-07-22 14:01:46 +00:00
|
|
|
pub async fn new(registry: &'static str) -> Result<EppClient, Box<dyn Error>> {
|
|
|
|
connect(registry).await
|
|
|
|
}
|
|
|
|
|
2021-07-25 14:34:01 +00:00
|
|
|
/// Makes a login request to the registry and initializes an EppClient instance with it
|
2021-07-23 19:23:01 +00:00
|
|
|
async fn build(connection: EppConnection, credentials: (String, String), ext_uris: Option<Vec<String>>) -> Result<EppClient, Box<dyn Error>> {
|
2021-07-22 14:01:46 +00:00
|
|
|
let mut client = EppClient {
|
|
|
|
connection: connection,
|
2021-07-23 16:47:41 +00:00
|
|
|
credentials: credentials,
|
2021-07-23 19:23:01 +00:00
|
|
|
ext_uris: ext_uris,
|
2021-07-23 16:47:41 +00:00
|
|
|
// client_tr_id_fn: Arc::new(default_client_tr_id_fn),
|
2021-07-22 14:01:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let client_tr_id = generate_client_tr_id(&client.credentials.0)?;
|
2021-07-23 19:23:01 +00:00
|
|
|
let login_request = EppLogin::new(&client.credentials.0, &client.credentials.1, &client.ext_uris, client_tr_id.as_str());
|
2021-07-22 14:01:46 +00:00
|
|
|
|
2021-07-25 18:26:02 +00:00
|
|
|
client.transact::<_, EppLoginResponse>(&login_request).await?;
|
2021-07-22 14:01:46 +00:00
|
|
|
|
|
|
|
Ok(client)
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:27:18 +00:00
|
|
|
/// Executes an EPP Hello call and returns the response as an `EppGreeting`
|
2021-07-22 14:01:46 +00:00
|
|
|
pub async fn hello(&mut self) -> Result<EppGreeting, Box<dyn Error>> {
|
|
|
|
let hello = EppHello::new();
|
|
|
|
let hello_xml = hello.serialize()?;
|
|
|
|
|
|
|
|
let response = self.connection.transact(&hello_xml).await?;
|
|
|
|
|
|
|
|
Ok(EppGreeting::deserialize(&response)?)
|
|
|
|
}
|
|
|
|
|
2021-07-24 20:15:59 +00:00
|
|
|
/// Accepts an EPP request object to convert to a request to send to the registry. The response from the
|
|
|
|
/// registry is deserialized to response type E and returned.
|
2021-07-22 14:01:46 +00:00
|
|
|
pub async fn transact<T: EppXml + Debug, E: EppXml + Debug>(&mut self, request: &T) -> Result<E::Output, error::Error> {
|
|
|
|
let epp_xml = request.serialize()?;
|
|
|
|
|
|
|
|
let response = self.connection.transact(&epp_xml).await?;
|
|
|
|
|
2021-07-25 18:26:02 +00:00
|
|
|
let status = EppCommandResponse::deserialize(&response)?;
|
2021-07-22 14:01:46 +00:00
|
|
|
|
|
|
|
if status.data.result.code < 2000 {
|
|
|
|
let response = E::deserialize(&response)?;
|
|
|
|
Ok(response)
|
|
|
|
} else {
|
|
|
|
let epp_error = EppCommandResponseError::deserialize(&response)?;
|
2021-07-22 15:08:01 +00:00
|
|
|
Err(error::Error::EppCommandError(epp_error))
|
2021-07-22 14:01:46 +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-07-22 14:01:46 +00:00
|
|
|
pub async fn transact_xml(&mut self, xml: &str) -> Result<String, Box<dyn Error>> {
|
|
|
|
self.connection.transact(&xml).await
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return String::from(&self.connection.greeting)
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:27:18 +00:00
|
|
|
/// Returns the greeting received on establishment of the connection as an `EppGreeting`
|
2021-07-24 19:10:40 +00:00
|
|
|
pub fn greeting(&self) -> Result<EppGreeting, error::Error> {
|
|
|
|
EppGreeting::deserialize(&self.connection.greeting)
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 14:34:01 +00:00
|
|
|
/// Sends the EPP Logout command to log out of the EPP session
|
2021-07-25 18:26:02 +00:00
|
|
|
pub async fn logout(&mut self) -> Result<EppLogoutResponse, error::Error> {
|
2021-07-22 14:01:46 +00:00
|
|
|
let client_tr_id = generate_client_tr_id(&self.credentials.0).unwrap();
|
|
|
|
let epp_logout = EppLogout::new(client_tr_id.as_str());
|
|
|
|
|
2021-07-25 18:26:02 +00:00
|
|
|
self.transact::<_, EppLogoutResponse>(&epp_logout).await
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for EppClient {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
block_on(self.logout());
|
|
|
|
}
|
|
|
|
}
|