logout and login added to connection initialization

This commit is contained in:
Ritesh Chitlangi 2021-07-20 02:17:57 +08:00
parent 6f9f526385
commit 0ced95dee7
6 changed files with 58 additions and 20 deletions

View File

@ -10,7 +10,7 @@ use tokio::{net::TcpStream, io::AsyncWriteExt, io::AsyncReadExt, io::split, io::
use crate::config::{CONFIG, EppClientConnection}; use crate::config::{CONFIG, EppClientConnection};
use crate::error; use crate::error;
use crate::epp::request::{EppRequest, Login}; use crate::epp::request::{EppRequest, Login, Logout};
use crate::epp::xml::EppXml; use crate::epp::xml::EppXml;
pub struct ConnectionStream { pub struct ConnectionStream {
@ -142,8 +142,8 @@ impl EppClient {
credentials: credentials credentials: credentials
}; };
let client_trid = EppRequest::generate_client_trid(&client.credentials.0)?; let client_tr_id = EppRequest::generate_client_tr_id(&client.credentials.0)?;
let login_request = Login::new(&client.credentials.0, &client.credentials.1, client_trid.as_str()); let login_request = Login::new(&client.credentials.0, &client.credentials.1, client_tr_id.as_str());
client.transact(&login_request).await?; client.transact(&login_request).await?;
@ -168,6 +168,19 @@ impl EppClient {
pub fn greeting(&self) -> String { pub fn greeting(&self) -> String {
return String::from(&self.connection.greeting) return String::from(&self.connection.greeting)
} }
pub async fn logout(&mut self) {
let client_tr_id = EppRequest::generate_client_tr_id(&self.credentials.0).unwrap();
let epp_logout = Logout::new(client_tr_id.as_str());
self.transact(&epp_logout).await;
}
}
impl Drop for EppClient {
fn drop(&mut self) {
block_on(self.logout());
}
} }
async fn epp_connect(registry_creds: &EppClientConnection) -> Result<ConnectionStream, error::Error> { async fn epp_connect(registry_creds: &EppClientConnection) -> Result<ConnectionStream, error::Error> {

View File

@ -1,3 +1,4 @@
pub mod object;
pub mod quick_xml; pub mod quick_xml;
pub mod request; pub mod request;
pub mod xml; pub mod xml;

12
src/epp/object.rs Normal file
View File

@ -0,0 +1,12 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename = "epp")]
pub struct EppObject<T> {
pub xmlns: String,
#[serde(rename = "xmlns:xsi")]
pub xmlns_xsi: String,
#[serde(rename = "xsi:schemaLocation")]
pub xsi_schema_location: String,
pub data: T,
}

View File

@ -1,10 +1,11 @@
use quick_xml::se; use quick_xml::se;
use serde::Serialize;
use std::error::Error; use std::error::Error;
use crate::epp::request::EppObject; use crate::epp::object::EppObject;
use crate::epp::xml::{EppXml, EPP_XML_HEADER}; use crate::epp::xml::{EppXml, EPP_XML_HEADER};
impl EppXml for EppObject { impl<T: Serialize> EppXml for EppObject<T> {
fn serialize(&self) -> Result<String, Box<dyn Error>> { fn serialize(&self) -> Result<String, Box<dyn Error>> {
let epp_xml = format!("{}\r\n{}", EPP_XML_HEADER, se::to_string(self)?); let epp_xml = format!("{}\r\n{}", EPP_XML_HEADER, se::to_string(self)?);

View File

@ -2,6 +2,8 @@ use serde::{Deserialize, Serialize};
use std::error::Error; use std::error::Error;
use std::time::SystemTime; use std::time::SystemTime;
use crate::epp::object::EppObject;
const EPP_XMLNS: &str = "urn:ietf:params:xml:ns:epp-1.0"; const EPP_XMLNS: &str = "urn:ietf:params:xml:ns:epp-1.0";
const EPP_XMLNS_XSI: &str = "http://www.w3.org/2001/XMLSchema-instance"; const EPP_XMLNS_XSI: &str = "http://www.w3.org/2001/XMLSchema-instance";
const EPP_XSI_SCHEMA_LOCATION: &str = "urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd"; const EPP_XSI_SCHEMA_LOCATION: &str = "urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd";
@ -37,21 +39,16 @@ pub enum RequestType {
#[serde(rename = "clTRID")] #[serde(rename = "clTRID")]
client_tr_id: StringValue, client_tr_id: StringValue,
}, },
#[serde(rename = "command")]
CommandLogout {
logout: Logout,
#[serde(rename = "clTRID")]
client_tr_id: StringValue,
},
} }
#[derive(Serialize, Deserialize, Debug, PartialEq)] impl<RequestType> EppObject<RequestType> {
#[serde(rename = "epp")] pub fn new(data: RequestType) -> EppObject<RequestType> {
pub struct EppObject {
xmlns: String,
#[serde(rename = "xmlns:xsi")]
xmlns_xsi: String,
#[serde(rename = "xsi:schemaLocation")]
xsi_schema_location: String,
data: RequestType,
}
impl EppObject {
pub fn new(data: RequestType) -> EppObject {
EppObject { EppObject {
data: data, data: data,
xmlns: EPP_XMLNS.to_string(), xmlns: EPP_XMLNS.to_string(),
@ -60,13 +57,13 @@ impl EppObject {
} }
} }
pub fn generate_client_trid(username: &str) -> Result<String, Box<dyn Error>> { pub fn generate_client_tr_id(username: &str) -> Result<String, Box<dyn Error>> {
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?; let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
Ok(format!("{}:{}", username, timestamp.as_secs())) Ok(format!("{}:{}", username, timestamp.as_secs()))
} }
} }
pub type EppRequest = EppObject; pub type EppRequest = EppObject<RequestType>;
#[derive(Serialize, Deserialize, Debug, PartialEq)] #[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
@ -165,3 +162,16 @@ impl Login {
self.services = services; self.services = services;
} }
} }
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase")]
pub struct Logout;
impl Logout {
pub fn new(client_tr_id: &str) -> EppRequest {
EppRequest::new(RequestType::CommandLogout {
logout: Logout,
client_tr_id: client_tr_id.to_string_value(),
})
}
}

1
src/epp/response.rs Normal file
View File

@ -0,0 +1 @@