summaryrefslogtreecommitdiffstats
path: root/src/client/unencrypted.rs
blob: dcd10c6c334cce82b8ebcc259f47baa858453109 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use quick_xml::{
    events::{BytesDecl, BytesStart, Event},
    name::QName,
    Reader, Writer,
};
use tokio::io::{BufReader, ReadHalf, WriteHalf};
use tokio::net::TcpStream;
use tokio_native_tls::native_tls::TlsConnector;

use crate::stanza::stream::StreamFeature;
use crate::stanza::Element;
use crate::Jabber;
use crate::Result;
use crate::{error::JabberError, stanza::stream::Stream};

pub struct JabberClient<'j> {
    reader: Reader<BufReader<ReadHalf<TcpStream>>>,
    writer: Writer<WriteHalf<TcpStream>>,
    jabber: &'j mut Jabber<'j>,
}

impl<'j> JabberClient<'j> {
    pub fn new(
        reader: Reader<BufReader<ReadHalf<TcpStream>>>,
        writer: Writer<WriteHalf<TcpStream>>,
        jabber: &'j mut Jabber<'j>,
    ) -> Self {
        Self {
            reader,
            writer,
            jabber,
        }
    }

    pub async fn start_stream(&mut self) -> Result<()> {
        // client to server
        let declaration = BytesDecl::new("1.0", None, None);
        let server = &self.jabber.server.to_owned().try_into()?;
        let stream_element =
            Stream::new_client(&self.jabber.jid, server, None, Some("en".to_string()));
        self.writer
            .write_event_async(Event::Decl(declaration))
            .await?;
        let stream_element: Element<'_> = stream_element.into();
        stream_element.write_start(&mut self.writer).await?;
        // server to client
        let mut buf = Vec::new();
        self.reader.read_event_into_async(&mut buf).await?;
        let _stream_response = Element::read_start(&mut self.reader).await?;
        Ok(())
    }

    pub async fn get_features(&mut self) -> Result<Option<Vec<StreamFeature>>> {
        if let Some(features) = Element::read(&mut self.reader).await? {
            Ok(Some(features.try_into()?))
        } else {
            Ok(None)
        }
    }

    pub async fn starttls(mut self) -> Result<super::encrypted::JabberClient<'j>> {
        let mut starttls_element = BytesStart::new("starttls");
        starttls_element.push_attribute(("xmlns", "urn:ietf:params:xml:ns:xmpp-tls"));
        self.writer
            .write_event_async(Event::Empty(starttls_element))
            .await
            .unwrap();
        let mut buf = Vec::new();
        match self.reader.read_event_into_async(&mut buf).await.unwrap() {
            Event::Empty(e) => match e.name() {
                QName(b"proceed") => {
                    let connector = TlsConnector::new().unwrap();
                    let stream = self
                        .reader
                        .into_inner()
                        .into_inner()
                        .unsplit(self.writer.into_inner());
                    if let Ok(tlsstream) = tokio_native_tls::TlsConnector::from(connector)
                        .connect(&self.jabber.server, stream)
                        .await
                    {
                        let (read, write) = tokio::io::split(tlsstream);
                        let reader = Reader::from_reader(BufReader::new(read));
                        let writer = Writer::new(write);
                        let mut client =
                            super::encrypted::JabberClient::new(reader, writer, self.jabber);
                        client.start_stream().await?;
                        return Ok(client);
                    }
                }
                QName(_) => return Err(JabberError::TlsNegotiation),
            },
            _ => return Err(JabberError::TlsNegotiation),
        }
        Err(JabberError::TlsNegotiation)
    }
}