summaryrefslogtreecommitdiffstats
path: root/src/client/encrypted.rs
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2023-06-19 19:23:54 +0100
committerLibravatar cel 🌸 <cel@blos.sm>2023-06-19 19:23:54 +0100
commit6a5e39c60ad74c1cba84daa7c845c8f0237a5d28 (patch)
tree7115e5dc2679bc053acb9b28fed41d3175ed2817 /src/client/encrypted.rs
parentabc3ffa7363ebd30101e15db7e02a7d44f323df9 (diff)
downloadluz-6a5e39c60ad74c1cba84daa7c845c8f0237a5d28.tar.gz
luz-6a5e39c60ad74c1cba84daa7c845c8f0237a5d28.tar.bz2
luz-6a5e39c60ad74c1cba84daa7c845c8f0237a5d28.zip
implement starttls
Diffstat (limited to 'src/client/encrypted.rs')
-rw-r--r--src/client/encrypted.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/client/encrypted.rs b/src/client/encrypted.rs
new file mode 100644
index 0000000..08439b2
--- /dev/null
+++ b/src/client/encrypted.rs
@@ -0,0 +1,59 @@
+use quick_xml::{
+ events::{BytesDecl, BytesStart, Event},
+ Reader, Writer,
+};
+use tokio::io::{BufReader, ReadHalf, WriteHalf};
+use tokio::net::TcpStream;
+use tokio_native_tls::TlsStream;
+
+use crate::Jabber;
+use crate::Result;
+
+pub struct JabberClient<'j> {
+ reader: Reader<BufReader<ReadHalf<TlsStream<TcpStream>>>>,
+ writer: Writer<WriteHalf<TlsStream<TcpStream>>>,
+ jabber: &'j mut Jabber<'j>,
+}
+
+impl<'j> JabberClient<'j> {
+ pub fn new(
+ reader: Reader<BufReader<ReadHalf<TlsStream<TcpStream>>>>,
+ writer: Writer<WriteHalf<TlsStream<TcpStream>>>,
+ jabber: &'j mut Jabber<'j>,
+ ) -> Self {
+ Self {
+ reader,
+ writer,
+ jabber,
+ }
+ }
+
+ pub async fn start_stream(&mut self) -> Result<()> {
+ let declaration = BytesDecl::new("1.0", None, None);
+ let mut stream_element = BytesStart::new("stream:stream");
+ stream_element.push_attribute(("from".as_bytes(), self.jabber.jid.to_string().as_bytes()));
+ stream_element.push_attribute(("to".as_bytes(), self.jabber.server.as_bytes()));
+ stream_element.push_attribute(("version", "1.0"));
+ stream_element.push_attribute(("xml:lang", "en"));
+ stream_element.push_attribute(("xmlns", "jabber:client"));
+ stream_element.push_attribute(("xmlns:stream", "http://etherx.jabber.org/streams"));
+ self.writer
+ .write_event_async(Event::Decl(declaration))
+ .await;
+ self.writer
+ .write_event_async(Event::Start(stream_element))
+ .await
+ .unwrap();
+ let mut buf = Vec::new();
+ loop {
+ match self.reader.read_event_into_async(&mut buf).await.unwrap() {
+ Event::Start(e) => {
+ println!("{:?}", e);
+ break;
+ }
+ e => println!("decl: {:?}", e),
+ };
+ }
+ Ok(())
+ }
+}