2021-07-24 20:15:59 +00:00
|
|
|
//! Manages sending/receiving EppObject request and responses to the registry connection
|
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
2021-11-05 22:14:05 +00:00
|
|
|
//! ```no_run
|
|
|
|
//! use std::collections::HashMap;
|
|
|
|
//!
|
|
|
|
//! use epp_client::config::{EppClientConfig, EppClientConnection};
|
2021-07-24 20:15:59 +00:00
|
|
|
//! use epp_client::EppClient;
|
2021-11-26 22:21:38 +00:00
|
|
|
//! use epp_client::domain::check::DomainCheck;
|
2021-11-26 19:36:28 +00:00
|
|
|
//! use epp_client::generate_client_tr_id;
|
2021-11-26 22:21:38 +00:00
|
|
|
//! use epp_client::common::NoExtension;
|
2021-07-24 20:15:59 +00:00
|
|
|
//!
|
|
|
|
//! #[tokio::main]
|
|
|
|
//! async fn main() {
|
2021-11-05 22:14:05 +00:00
|
|
|
//!
|
|
|
|
//! // Create a config
|
|
|
|
//! let mut registry: HashMap<String, EppClientConnection> = HashMap::new();
|
|
|
|
//! registry.insert(
|
|
|
|
//! "registry_name".to_owned(),
|
|
|
|
//! EppClientConnection {
|
|
|
|
//! host: "example.com".to_owned(),
|
|
|
|
//! port: 700,
|
|
|
|
//! username: "username".to_owned(),
|
|
|
|
//! password: "password".to_owned(),
|
|
|
|
//! ext_uris: None,
|
|
|
|
//! tls_files: None,
|
|
|
|
//! },
|
|
|
|
//! );
|
|
|
|
//! let config = EppClientConfig { registry };
|
|
|
|
//!
|
|
|
|
//! // Create an instance of EppClient, passing the config and the registry you want to connect to
|
|
|
|
//! let mut client = match EppClient::new(&config, "registry_name").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-11-26 22:21:38 +00:00
|
|
|
//! let domain_check = DomainCheck::<NoExtension>::new(vec!["eppdev.com", "eppdev.net"]);
|
2021-11-26 22:21:38 +00:00
|
|
|
//! let response = client.transact(domain_check, generate_client_tr_id(&client).as_str()).await.unwrap();
|
2021-11-05 22:14:05 +00:00
|
|
|
//! println!("{:?}", response);
|
|
|
|
//!
|
2021-07-24 20:15:59 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
|
2021-11-02 21:34:13 +00:00
|
|
|
use std::time::SystemTime;
|
|
|
|
use std::{error::Error, fmt::Debug};
|
2021-07-22 14:01:46 +00:00
|
|
|
|
2021-11-26 22:21:38 +00:00
|
|
|
use crate::common::{EppObject, NoExtension};
|
2021-10-22 00:11:24 +00:00
|
|
|
use crate::config::EppClientConfig;
|
2021-07-22 14:01:46 +00:00
|
|
|
use crate::connection::registry::{epp_connect, EppConnection};
|
2021-11-02 21:34:13 +00:00
|
|
|
use crate::error;
|
2021-12-01 18:06:05 +00:00
|
|
|
use crate::hello::{Greeting, Hello};
|
2021-11-26 22:21:38 +00:00
|
|
|
use crate::login::Login;
|
|
|
|
use crate::logout::Logout;
|
2021-11-26 21:50:22 +00:00
|
|
|
use crate::request::{generate_client_tr_id, EppExtension, EppRequest};
|
2021-11-26 22:21:38 +00:00
|
|
|
use crate::response::{CommandResponseStatus, CommandResponseWithExtension};
|
2021-11-26 19:36:28 +00:00
|
|
|
use crate::xml::EppXml;
|
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-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,
|
2021-11-02 21:34:13 +00:00
|
|
|
Err(e) => panic!("Error in client TRID gen function: {}", e),
|
2021-07-25 14:34:01 +00:00
|
|
|
};
|
|
|
|
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
|
|
|
/// 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-10-22 00:11:24 +00:00
|
|
|
pub async fn new(
|
|
|
|
config: &EppClientConfig,
|
|
|
|
registry: &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 stream = epp_connect(registry_creds).await?;
|
|
|
|
let credentials = registry_creds.credentials();
|
|
|
|
let ext_uris = registry_creds.ext_uris();
|
|
|
|
|
|
|
|
let ext_uris =
|
|
|
|
ext_uris.map(|uris| uris.iter().map(|u| u.to_string()).collect::<Vec<String>>());
|
|
|
|
|
|
|
|
let connection = EppConnection::new(registry.to_string(), stream).await?;
|
|
|
|
EppClient::build(connection, credentials, ext_uris).await
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 14:34:01 +00:00
|
|
|
/// Makes a login request to the registry and initializes an EppClient instance with it
|
2021-11-02 21:34:13 +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 {
|
2021-10-27 22:45:32 +00:00
|
|
|
connection,
|
|
|
|
credentials,
|
|
|
|
ext_uris,
|
2021-07-22 14:01:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let client_tr_id = generate_client_tr_id(&client.credentials.0)?;
|
2021-11-26 22:21:38 +00:00
|
|
|
let login_request = Login::<NoExtension>::new(
|
2021-11-02 21:34:13 +00:00
|
|
|
&client.credentials.0,
|
|
|
|
&client.credentials.1,
|
|
|
|
&client.ext_uris,
|
|
|
|
);
|
2021-07-22 14:01:46 +00:00
|
|
|
|
2021-11-02 21:34:13 +00:00
|
|
|
client
|
2021-11-26 22:21:38 +00:00
|
|
|
.transact(login_request, client_tr_id.as_str())
|
2021-11-02 21:34:13 +00:00
|
|
|
.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-12-01 18:06:05 +00:00
|
|
|
pub async fn hello(&mut self) -> Result<Greeting, Box<dyn Error>> {
|
|
|
|
let hello = Hello::new();
|
2021-07-22 14:01:46 +00:00
|
|
|
let hello_xml = hello.serialize()?;
|
|
|
|
|
|
|
|
let response = self.connection.transact(&hello_xml).await?;
|
|
|
|
|
2021-12-01 18:06:05 +00:00
|
|
|
Ok(Greeting::deserialize(&response)?)
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 22:21:38 +00:00
|
|
|
pub async fn transact<T, E>(
|
2021-11-26 21:50:22 +00:00
|
|
|
&mut self,
|
|
|
|
request: T,
|
|
|
|
id: &str,
|
|
|
|
) -> Result<CommandResponseWithExtension<<T as EppRequest<E>>::Output, E::Response>, error::Error>
|
|
|
|
where
|
|
|
|
T: EppRequest<E> + Debug,
|
|
|
|
E: EppExtension,
|
|
|
|
{
|
|
|
|
let epp_xml = request.serialize_request(id)?;
|
|
|
|
|
|
|
|
let response = self.connection.transact(&epp_xml).await?;
|
|
|
|
|
|
|
|
T::deserialize_response(&response)
|
|
|
|
}
|
|
|
|
|
2021-10-22 00:11:24 +00:00
|
|
|
/// Fetches the username used in the registry connection
|
|
|
|
pub fn username(&self) -> String {
|
|
|
|
self.credentials.0.to_string()
|
|
|
|
}
|
|
|
|
|
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>> {
|
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-07-26 19:27:18 +00:00
|
|
|
/// Returns the greeting received on establishment of the connection as an `EppGreeting`
|
2021-12-01 18:06:05 +00:00
|
|
|
pub fn greeting(&self) -> Result<Greeting, error::Error> {
|
|
|
|
Greeting::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-11-26 22:21:38 +00:00
|
|
|
pub async fn logout(
|
|
|
|
&mut self,
|
|
|
|
) -> Result<
|
|
|
|
CommandResponseWithExtension<EppObject<CommandResponseStatus>, NoExtension>,
|
|
|
|
error::Error,
|
|
|
|
> {
|
2021-07-22 14:01:46 +00:00
|
|
|
let client_tr_id = generate_client_tr_id(&self.credentials.0).unwrap();
|
2021-11-26 22:21:38 +00:00
|
|
|
let epp_logout = Logout::<NoExtension>::new();
|
2021-07-22 14:01:46 +00:00
|
|
|
|
2021-11-26 22:21:38 +00:00
|
|
|
let response = self.transact(epp_logout, client_tr_id.as_str()).await?;
|
2021-07-22 14:01:46 +00:00
|
|
|
|
2021-11-11 16:48:07 +00:00
|
|
|
self.connection.shutdown().await?;
|
2021-11-11 15:56:01 +00:00
|
|
|
|
2021-11-11 16:48:07 +00:00
|
|
|
Ok(response)
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
}
|