aboutsummaryrefslogtreecommitdiffstats
path: root/jabber/src/client.rs
blob: 9d3268250488f12412dd6b2ee4aa078ab5b8fefa (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use std::{
    borrow::Borrow,
    future::Future,
    pin::pin,
    sync::Arc,
    task::{ready, Poll},
};

use futures::{FutureExt, Sink, SinkExt, Stream, StreamExt};
use jid::ParseError;
use rsasl::config::SASLConfig;
use stanza::{
    client::Stanza,
    sasl::Mechanisms,
    stream::{Feature, Features},
};
use tokio::sync::Mutex;

use crate::{
    connection::{Tls, Unencrypted},
    jabber_stream::bound_stream::{BoundJabberReader, BoundJabberStream},
    Connection, Error, JabberStream, Result, JID,
};

// feed it client stanzas, receive client stanzas
pub struct JabberClient {
    connection: Option<BoundJabberStream<Tls>>,
    jid: JID,
    // TODO: have reconnection be handled by another part, so creds don't need to be stored in object
    password: Arc<SASLConfig>,
    server: String,
}

impl JabberClient {
    pub fn new(
        jid: impl TryInto<JID, Error = ParseError>,
        password: impl ToString,
    ) -> Result<JabberClient> {
        let jid = jid.try_into()?;
        let sasl_config = SASLConfig::with_credentials(
            None,
            jid.localpart.clone().ok_or(Error::NoLocalpart)?,
            password.to_string(),
        )?;
        Ok(JabberClient {
            connection: None,
            jid: jid.clone(),
            password: sasl_config,
            server: jid.domainpart,
        })
    }

    pub fn jid(&self) -> JID {
        self.jid.clone()
    }

    pub async fn connect(&mut self) -> Result<()> {
        match &self.connection {
            Some(_) => Ok(()),
            None => {
                self.connection = Some(
                    connect_and_login(&mut self.jid, self.password.clone(), &mut self.server)
                        .await?,
                );
                Ok(())
            }
        }
    }

    pub(crate) fn into_inner(self) -> Result<BoundJabberStream<Tls>> {
        self.connection.ok_or(Error::Disconnected)
    }

    // pub async fn send_stanza(&mut self, stanza: &Stanza) -> Result<()> {
    //     match &mut self.connection {
    //         ConnectionState::Disconnected => return Err(Error::Disconnected),
    //         ConnectionState::Connecting(_connecting) => return Err(Error::Connecting),
    //         ConnectionState::Connected(jabber_stream) => {
    //             Ok(jabber_stream.send_stanza(stanza).await?)
    //         }
    //     }
    // }
}

pub async fn connect_and_login(
    jid: &mut JID,
    auth: Arc<SASLConfig>,
    server: &mut String,
) -> Result<BoundJabberStream<Tls>> {
    let mut conn_state = Connecting::start(&server).await?;
    loop {
        match conn_state {
            Connecting::InsecureConnectionEstablised(tcp_stream) => {
                conn_state = Connecting::InsecureStreamStarted(
                    JabberStream::start_stream(tcp_stream, server).await?,
                )
            }
            Connecting::InsecureStreamStarted(jabber_stream) => {
                conn_state = Connecting::InsecureGotFeatures(jabber_stream.get_features().await?)
            }
            Connecting::InsecureGotFeatures((features, jabber_stream)) => {
                match features.negotiate().ok_or(Error::Negotiation)? {
                    Feature::StartTls(_start_tls) => {
                        conn_state = Connecting::StartTls(jabber_stream)
                    }
                    // TODO: better error
                    _ => return Err(Error::TlsRequired),
                }
            }
            Connecting::StartTls(jabber_stream) => {
                conn_state =
                    Connecting::ConnectionEstablished(jabber_stream.starttls(&server).await?)
            }
            Connecting::ConnectionEstablished(tls_stream) => {
                conn_state =
                    Connecting::StreamStarted(JabberStream::start_stream(tls_stream, server).await?)
            }
            Connecting::StreamStarted(jabber_stream) => {
                conn_state = Connecting::GotFeatures(jabber_stream.get_features().await?)
            }
            Connecting::GotFeatures((features, jabber_stream)) => {
                match features.negotiate().ok_or(Error::Negotiation)? {
                    Feature::StartTls(_start_tls) => return Err(Error::AlreadyTls),
                    Feature::Sasl(mechanisms) => {
                        conn_state = Connecting::Sasl(mechanisms, jabber_stream)
                    }
                    Feature::Bind => conn_state = Connecting::Bind(jabber_stream),
                    Feature::Unknown => return Err(Error::Unsupported),
                }
            }
            Connecting::Sasl(mechanisms, jabber_stream) => {
                conn_state = Connecting::ConnectionEstablished(
                    jabber_stream.sasl(mechanisms, auth.clone()).await?,
                )
            }
            Connecting::Bind(jabber_stream) => {
                return Ok(jabber_stream.bind(jid).await?.to_bound_jabber());
            }
        }
    }
}

pub enum Connecting {
    InsecureConnectionEstablised(Unencrypted),
    InsecureStreamStarted(JabberStream<Unencrypted>),
    InsecureGotFeatures((Features, JabberStream<Unencrypted>)),
    StartTls(JabberStream<Unencrypted>),
    ConnectionEstablished(Tls),
    StreamStarted(JabberStream<Tls>),
    GotFeatures((Features, JabberStream<Tls>)),
    Sasl(Mechanisms, JabberStream<Tls>),
    Bind(JabberStream<Tls>),
}

impl Connecting {
    pub async fn start(server: &str) -> Result<Self> {
        match Connection::connect(server).await? {
            Connection::Encrypted(tls_stream) => Ok(Connecting::ConnectionEstablished(tls_stream)),
            Connection::Unencrypted(tcp_stream) => {
                Ok(Connecting::InsecureConnectionEstablised(tcp_stream))
            }
        }
    }
}

pub enum InsecureConnecting {
    Disconnected,
    ConnectionEstablished(Connection),
    PreStarttls(JabberStream<Unencrypted>),
    PreAuthenticated(JabberStream<Tls>),
    Authenticated(Tls),
    PreBound(JabberStream<Tls>),
    Bound(JabberStream<Tls>),
}

#[cfg(test)]
mod tests {
    use std::{sync::Arc, time::Duration};

    use super::JabberClient;
    use futures::{SinkExt, StreamExt};
    use stanza::{
        client::{
            iq::{Iq, IqType, Query},
            Stanza,
        },
        xep_0199::Ping,
    };
    use test_log::test;
    use tokio::{sync::Mutex, time::sleep};
    use tracing::info;

    #[test(tokio::test)]
    async fn login() {
        let mut client = JabberClient::new("test@blos.sm", "slayed").unwrap();
        client.connect().await.unwrap();
        sleep(Duration::from_secs(5)).await
    }

    #[test(tokio::test)]
    async fn ping_parallel() {
        let mut client = JabberClient::new("test@blos.sm", "slayed").unwrap();
        client.connect().await.unwrap();
        sleep(Duration::from_secs(5)).await;
        let jid = client.jid.clone();
        let server = client.server.clone();
        let (mut read, mut write) = client.into_inner().unwrap().split();

        tokio::join!(
            async {
                write
                    .write(&Stanza::Iq(Iq {
                        from: Some(jid.clone()),
                        id: "c2s1".to_string(),
                        to: Some(server.clone().try_into().unwrap()),
                        r#type: IqType::Get,
                        lang: None,
                        query: Some(Query::Ping(Ping)),
                        errors: Vec::new(),
                    }))
                    .await
                    .unwrap();
                write
                    .write(&Stanza::Iq(Iq {
                        from: Some(jid.clone()),
                        id: "c2s2".to_string(),
                        to: Some(server.clone().try_into().unwrap()),
                        r#type: IqType::Get,
                        lang: None,
                        query: Some(Query::Ping(Ping)),
                        errors: Vec::new(),
                    }))
                    .await
                    .unwrap();
            },
            async {
                for _ in 0..2 {
                    let stanza = read.read::<Stanza>().await.unwrap();
                    info!("ping reply: {:#?}", stanza);
                }
            }
        );
    }
}