clean up xml serialization

This commit is contained in:
Ritesh Chitlangi 2021-07-20 01:56:10 +08:00
parent 543378cb52
commit 6f9f526385
8 changed files with 56 additions and 45 deletions

View File

@ -9,30 +9,29 @@ use tokio_rustls::{TlsConnector, rustls::ClientConfig, webpki::DNSNameRef, clien
use tokio::{net::TcpStream, io::AsyncWriteExt, io::AsyncReadExt, io::split, io::ReadHalf, io::WriteHalf};
use crate::config::{CONFIG, EppClientConnection};
use tokio::time::{sleep, Duration};
use crate::error;
use crate::epp::request::EppRequest;
use crate::epp::request::{EppRequest, Login};
use crate::epp::xml::EppXml;
struct ConnectionStream {
pub struct ConnectionStream {
reader: ReadHalf<TlsStream<TcpStream>>,
writer: WriteHalf<TlsStream<TcpStream>>,
}
struct EppConnection {
pub struct EppConnection {
registry: String,
credentials: (String, String),
stream: ConnectionStream,
pub greeting: String,
}
pub struct EppClient {
credentials: (String, String),
connection: EppConnection,
}
impl EppConnection {
pub async fn new(
registry: String,
credentials: (String, String),
mut stream: ConnectionStream) -> Result<EppConnection, Box<dyn Error>> {
let mut buf = vec![0u8; 4096];
stream.reader.read(&mut buf).await?;
@ -40,7 +39,6 @@ impl EppConnection {
Ok(EppConnection {
registry: registry,
credentials: credentials,
stream: stream,
greeting: greeting
})
@ -66,9 +64,6 @@ impl EppConnection {
buf[..4].clone_from_slice(&len_u32);
buf[4..].clone_from_slice(&content.as_bytes());
let conv = str::from_utf8(&buf[4..])?;
println!("reconverted: {}", conv);
self.write(&buf).await
}
@ -141,8 +136,22 @@ impl Drop for EppConnection {
}
impl EppClient {
pub async fn new(connection: EppConnection, credentials: (String, String)) -> Result<EppClient, Box<dyn Error>> {
let mut client = EppClient {
connection: connection,
credentials: credentials
};
let client_trid = EppRequest::generate_client_trid(&client.credentials.0)?;
let login_request = Login::new(&client.credentials.0, &client.credentials.1, client_trid.as_str());
client.transact(&login_request).await?;
Ok(client)
}
pub async fn transact(&mut self, request: &EppRequest) -> Result<String, Box<dyn Error>> {
let epp_xml = request.to_epp_xml()?;
let epp_xml = request.serialize()?;
println!("Request:\r\n{}", epp_xml);
@ -207,11 +216,10 @@ pub async fn connect(registry: &'static str) -> Result<EppClient, Box<dyn Error>
let connection = EppConnection::new(
registry.to_string(),
credentials,
stream
).await.unwrap();
let client = EppClient { connection: connection };
let client = EppClient::new(connection, credentials).await.unwrap();
tx.send(client).unwrap();
});

View File

@ -1,2 +1,3 @@
pub mod objects;
pub mod quick_xml;
pub mod request;
pub mod xml;

View File

13
src/epp/quick_xml.rs Normal file
View File

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

View File

@ -1,8 +1,7 @@
use quick_xml::se;
use serde::{Deserialize, Serialize, Serializer};
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::time::SystemTime;
const EPP_XML_HEADER: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="no"?>"#;
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_XSI_SCHEMA_LOCATION: &str = "urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd";
@ -61,10 +60,9 @@ impl EppObject {
}
}
pub fn to_epp_xml(&self) -> Result<String, Box<dyn Error>> {
let epp_xml = format!("{}\r\n{}", EPP_XML_HEADER, se::to_string(self)?);
Ok(epp_xml)
pub fn generate_client_trid(username: &str) -> Result<String, Box<dyn Error>> {
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
Ok(format!("{}:{}", username, timestamp.as_secs()))
}
}

View File

@ -1,4 +1,8 @@
use std::error::Error;
pub const EPP_XML_HEADER: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="no"?>"#;
pub trait EppXml {
fn serialize(&self) -> Result<String, Box<dyn Error>>;
fn deserialize(&self) -> Result<Self, Box<dyn Error>>;
// fn deserialize(&self) -> Result<Self, Box<dyn Error>>;
}

View File

@ -1,11 +1,12 @@
// pub mod config;
// pub mod connection;
// pub mod epp;
// pub mod error;
// #[cfg(test)]
// mod tests {
// use super::config;
// use super::config;
// use super::connection;
// use std::str;
// #[test]
// fn config() {
@ -22,14 +23,12 @@
// #[test]
// fn connect() {
// let mut cn = aw!(connection::connect("hexonet")).unwrap();
// println!("lol");
// let contents = aw!(cn.read()).unwrap();
// match str::from_utf8(&contents) {
// Ok(v) => println!("{}", v),
// Err(e) => panic!("Error: {}", e)
// }
// aw!(cn.close());
// let mut client = match aw!(connection::connect("hexonet")) {
// Ok(client) => {
// println!("{}", client.greeting());
// client
// },
// Err(e) => panic!("Error: {}", e)
// };
// }
// }

View File

@ -17,19 +17,7 @@ async fn main() {
Err(e) => panic!("Error: {}", e)
};
// sleep(Duration::from_millis(100000)).await;
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
let cl_trid = format!("eppdev:{}", timestamp.as_secs());
let epp_login = request::Login::new("eppdev", "sh48sja#27*A", &cl_trid);
client.transact(&epp_login).await.unwrap();
let epp_hello = request::Hello::new();
client.transact(&epp_hello).await.unwrap();
//let response = client.transact(&epp_hello).await.unwrap();
//println!("{}", response);
}