wse-client 3.1.0

Rust client for WSE servers, natively and in the browser: topics with message recovery, reconnects, MessagePack, end-to-end encryption and signed messages.
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! The application's handle on a connection.

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

use serde::Serialize;
use tokio::sync::{mpsc, oneshot, watch};

#[cfg(target_arch = "wasm32")]
use crate::browser::Driver;
use crate::config::Config;
#[cfg(not(target_arch = "wasm32"))]
use crate::connection::Driver;
use crate::error::Error;
use crate::handler::Handler;
use crate::security::Keys;

/// Where the client stands.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Status {
    /// Making the first connection.
    Connecting,
    /// The server took the connection.
    Connected,
    /// The connection was lost; making it again.
    Reconnecting,
    /// Paused by the application: no connection until it resumes.
    Paused,
    /// Stopped for good: closed by the application (`None`), or the reason.
    Closed(Option<Error>),
}

/// What the application asks of the connection, besides its messages.
pub(crate) enum Control {
    Subscribe(Vec<String>),
    Unsubscribe(Vec<String>),
    Reconnect,
    Pause(String),
    Resume(String),
    Close,
}

/// A message the application sent, on its way to the connection's task.
pub(crate) struct Queued {
    pub(crate) text: String,
    /// The user it was sent as ([`Shared::users`]): it goes nowhere once the
    /// server took a connection for another user.
    pub(crate) user: u64,
    /// A signed or live message goes on the connection it was sent on
    /// ([`Shared::connections`]), or not at all.
    pub(crate) only_on: Option<u64>,
}

/// The user and connection a message was sent for.
struct SentFor {
    user: u64,
    /// What a signed or live message goes on.
    connection: u64,
}

impl SentFor {
    fn frame(&self, text: String) -> Queued {
        Queued {
            text,
            user: self.user,
            only_on: None,
        }
    }

    /// For this connection only.
    fn live(&self, text: String) -> Queued {
        Queued {
            only_on: Some(self.connection),
            ..self.frame(text)
        }
    }
}

/// What the handles and the connection's task share.
pub(crate) struct Shared {
    /// The server took the current connection: messages may go.
    pub(crate) ready: AtomicBool,
    /// Counts the times the server took a connection for another user than
    /// the one before.
    pub(crate) users: AtomicU64,
    /// Counts the connections the server took.
    pub(crate) connections: AtomicU64,
    /// The current connection's id and user.
    pub(crate) connection: Mutex<Option<(String, Option<String>)>>,
    /// The token for the next connection attempt.
    pub(crate) token: Mutex<Option<String>>,
    /// The server's signing key: what signed messages are for.
    pub(crate) server_kid: Mutex<Option<String>>,
    /// Stops the connection's task where it stands: a close past its
    /// deadline.
    stop: Mutex<Option<futures_util::future::AbortHandle>>,
}

impl Shared {
    pub(crate) fn new(token: Option<String>) -> Self {
        Self {
            ready: AtomicBool::new(false),
            users: AtomicU64::new(0),
            connections: AtomicU64::new(0),
            connection: Mutex::new(None),
            token: Mutex::new(token),
            server_kid: Mutex::new(None),
            stop: Mutex::new(None),
        }
    }
}

/// A connection to a WSE server, made again when it is lost.
///
/// Clones share the connection, which lives as long as the application
/// holds a handle (or until [`close`](Self::close)); the client a handler
/// is given by [`connect_with`](Self::connect_with) does not keep it.
/// Sends go out in the order they were made; what the server sends goes to
/// the [`Handler`] the client was made with, or to the
/// [`Events`](crate::Events) of [`connect`](crate::connect).
///
/// A message still queued when the connection is lost goes out on the next
/// one, unless the server takes that one for another user: then it goes
/// nowhere, and the topics start over. A signed message goes out on the
/// connection it was signed on, or not at all.
#[derive(Clone)]
pub struct Client {
    shared: Arc<Shared>,
    #[cfg_attr(not(feature = "aws-lc"), allow(dead_code))]
    keys: Arc<Keys>,
    frames: mpsc::Sender<Queued>,
    control: mpsc::UnboundedSender<Control>,
    status: watch::Receiver<Status>,
    /// The application's hold on the connection; none for a handler's.
    _owner: Option<Arc<Owner>>,
}

/// The application's handles hold one of these: when the last goes, the
/// connection is closed.
struct Owner(mpsc::UnboundedSender<Control>);

impl Drop for Owner {
    fn drop(&mut self) {
        let _ = self.0.send(Control::Close);
    }
}

impl Client {
    /// Connects, and returns once the server took the connection. What it
    /// sends goes to `handler`.
    ///
    /// The first connection is not made again when it fails: the error is
    /// returned. Later ones are, as [`Config::reconnect`] says.
    pub async fn connect<H: Handler>(config: Config, handler: H) -> Result<Self, Error> {
        Self::connect_with(config, |_| handler).await
    }

    /// [`connect`](Self::connect) with a handler that is given the client,
    /// to send from it.
    pub async fn connect_with<H, F>(
        #[cfg_attr(not(feature = "aws-lc"), allow(unused_mut))] mut config: Config,
        make: F,
    ) -> Result<Self, Error>
    where
        H: Handler,
        F: FnOnce(Client) -> H,
    {
        config.check()?;
        let keys = Arc::new(Keys::from_config(&config)?);
        // Read into the keys: the bytes of this copy go.
        #[cfg(feature = "aws-lc")]
        if let Some(mut key) = config.signing_key.take() {
            zeroize::Zeroize::zeroize(&mut key);
        }
        let shared = Arc::new(Shared::new(config.token.clone()));
        let (frames_tx, frames_rx) = mpsc::channel(config.send_queue);
        let (control_tx, control_rx) = mpsc::unbounded_channel();
        let (status_tx, status_rx) = watch::channel(Status::Connecting);
        let for_handler = Client {
            shared: Arc::clone(&shared),
            keys: Arc::clone(&keys),
            frames: frames_tx,
            control: control_tx.clone(),
            status: status_rx,
            _owner: None,
        };
        let client = Client {
            _owner: Some(Arc::new(Owner(control_tx))),
            ..for_handler.clone()
        };
        let handler = make(for_handler);
        let (first_tx, first_rx) = oneshot::channel();
        let ended = (Arc::clone(&shared), status_tx.clone());
        let (stop, stopped) = futures_util::future::AbortHandle::new_pair();
        if let Ok(mut slot) = shared.stop.lock() {
            *slot = Some(stop);
        }
        let driver = Driver::new(
            config, handler, shared, keys, frames_rx, control_rx, status_tx,
        );
        let run = async move {
            use futures_util::FutureExt;
            let driven = futures_util::future::Abortable::new(
                std::panic::AssertUnwindSafe(driver.run(Some(first_tx))).catch_unwind(),
                stopped,
            )
            .await;
            let (shared, status) = ended;
            match driven {
                Ok(Ok(())) => {}
                // A handler that panics ends the client, which says so.
                Ok(Err(_)) => {
                    shared.ready.store(false, Ordering::Release);
                    status.send_replace(Status::Closed(Some(Error::Panicked)));
                }
                // Stopped by a close past its deadline: the connection went
                // with the task.
                Err(_) => {
                    shared.ready.store(false, Ordering::Release);
                    status.send_replace(Status::Closed(None));
                }
            }
        };
        #[cfg(not(target_arch = "wasm32"))]
        tokio::spawn(run);
        #[cfg(target_arch = "wasm32")]
        wasm_bindgen_futures::spawn_local(run);
        match first_rx.await {
            Ok(Ok(())) => Ok(client),
            Ok(Err(error)) => Err(error),
            Err(_) => Err(Error::Closed),
        }
    }

    /// Sends a message `{"t": kind, "p": payload}`; fails at once when the
    /// queue is full.
    pub fn try_send<P: Serialize + ?Sized>(&self, kind: &str, payload: &P) -> Result<(), Error> {
        let for_now = self.ready()?;
        let text = encode(kind, payload)?;
        self.frames
            .try_send(for_now.frame(text))
            .map_err(|e| match e {
                mpsc::error::TrySendError::Full(_) => Error::Full,
                mpsc::error::TrySendError::Closed(_) => Error::Closed,
            })
    }

    /// Sends a message `{"t": kind, "p": payload}`, waiting for room in the
    /// queue.
    pub async fn send<P: Serialize + ?Sized>(&self, kind: &str, payload: &P) -> Result<(), Error> {
        let for_now = self.ready()?;
        let text = encode(kind, payload)?;
        self.frames
            .send(for_now.frame(text))
            .await
            .map_err(|_| Error::Closed)
    }

    /// Sends a message `{"t": kind, "p": payload}` on the connection that is
    /// ready now, or not at all: one this connection did not write is
    /// dropped, never sent on a later connection. For commands that must not
    /// run late (a valve, a door, an order). Fails at once when the queue is
    /// full.
    pub fn try_send_live<P: Serialize + ?Sized>(
        &self,
        kind: &str,
        payload: &P,
    ) -> Result<(), Error> {
        let for_now = self.ready()?;
        let text = encode(kind, payload)?;
        self.frames
            .try_send(for_now.live(text))
            .map_err(|e| match e {
                mpsc::error::TrySendError::Full(_) => Error::Full,
                mpsc::error::TrySendError::Closed(_) => Error::Closed,
            })
    }

    /// [`Client::try_send_live`], waiting for room in the queue.
    pub async fn send_live<P: Serialize + ?Sized>(
        &self,
        kind: &str,
        payload: &P,
    ) -> Result<(), Error> {
        let for_now = self.ready()?;
        let text = encode(kind, payload)?;
        self.frames
            .send(for_now.live(text))
            .await
            .map_err(|_| Error::Closed)
    }

    /// Sends JSON text as it is: a message the application built itself.
    pub async fn send_raw(&self, json: impl Into<String>) -> Result<(), Error> {
        let for_now = self.ready()?;
        self.frames
            .send(for_now.frame(json.into()))
            .await
            .map_err(|_| Error::Closed)
    }

    /// Sends a signed message `{"t": kind, "p": payload}`: signed with
    /// [`Config::signing_key`] for the server's signing key, so whoever
    /// keeps it can show who sent it. `n` names it once, 1 to 128
    /// characters (an order id, say; a random one when `None`). Returns the
    /// `n` it went with: the server's signed `receipt`, or its error
    /// `SIGNATURE_REFUSED`, names it.
    #[cfg(feature = "aws-lc")]
    pub async fn send_signed<P: Serialize + ?Sized>(
        &self,
        kind: &str,
        payload: &P,
        n: Option<&str>,
    ) -> Result<String, Error> {
        let for_now = self.ready()?;
        let audience = self
            .shared
            .server_kid
            .lock()
            .ok()
            .and_then(|kid| kid.clone())
            .ok_or_else(|| Error::Signing("the server named no signing key".to_owned()))?;
        let payload =
            serde_json::value::to_raw_value(payload).map_err(|e| Error::Encode(e.to_string()))?;
        let n = match n {
            Some(n)
                if n.is_empty() || n.chars().count() > wse::protocol::signing::MAX_NONCE_LEN =>
            {
                return Err(Error::Signing(format!(
                    "n must be 1 to {} characters",
                    wse::protocol::signing::MAX_NONCE_LEN
                )));
            }
            Some(n) => n.to_owned(),
            None => {
                let mut bytes = [0u8; 16];
                wse::protocol::crypto::fill_random(&mut bytes)
                    .map_err(|_| Error::Signing("no random bytes".to_owned()))?;
                bytes.iter().map(|b| format!("{b:02x}")).collect::<String>()
            }
        };
        let text = self
            .keys
            .sign(kind, &payload, &audience, &n, crate::session::now_ms())?;
        // Signed for this connection: it goes on it or not at all.
        self.frames
            .send(for_now.live(text))
            .await
            .map_err(|_| Error::Closed)?;
        Ok(n)
    }

    /// Ends the connection for `reason` (`"background"`, `"offline"`,
    /// ...) and makes no attempt while any reason holds: topics and
    /// positions stay, and what is queued goes out first. The last reason
    /// cleared with [`resume`](Self::resume), the client connects at once
    /// and recovers what it missed.
    pub fn pause(&self, reason: impl Into<String>) -> Result<(), Error> {
        self.control(Control::Pause(reason.into()))
    }

    /// Clears `reason`; once none is left the client connects again.
    pub fn resume(&self, reason: impl Into<String>) -> Result<(), Error> {
        self.control(Control::Resume(reason.into()))
    }

    /// Subscribes to topics, on this connection and every later one; after
    /// a reconnect the client asks for them from where it left off.
    pub fn subscribe<I, T>(&self, topics: I) -> Result<(), Error>
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        let topics = topics.into_iter().map(Into::into).collect();
        self.control(Control::Subscribe(topics))
    }

    /// Unsubscribes, and forgets where the client stood in the topics.
    pub fn unsubscribe<I, T>(&self, topics: I) -> Result<(), Error>
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        let topics = topics.into_iter().map(Into::into).collect();
        self.control(Control::Unsubscribe(topics))
    }

    /// Drops the connection and makes it again at once.
    pub fn reconnect(&self) -> Result<(), Error> {
        self.control(Control::Reconnect)
    }

    /// The token for the next connection attempt.
    pub fn set_token(&self, token: impl Into<String>) {
        if let Ok(mut current) = self.shared.token.lock() {
            *current = Some(token.into());
        }
    }

    /// Closes the connection for good, and waits until it is.
    pub async fn close(&self) {
        let _ = self.control.send(Control::Close);
        self.closed().await;
    }

    /// Closes the client, giving what is queued `deadline` to go out: past
    /// it the connection is dropped where it stands (a link that takes
    /// nothing holds a close for as long as the system keeps the
    /// connection).
    #[cfg(not(target_arch = "wasm32"))]
    pub async fn close_within(&self, deadline: std::time::Duration) {
        let _ = self.control.send(Control::Close);
        if tokio::time::timeout(deadline, self.closed()).await.is_err() {
            if let Some(stop) = self.shared.stop.lock().ok().and_then(|slot| slot.clone()) {
                stop.abort();
            }
            self.closed().await;
        }
    }

    /// Waits until the client stops for good; the reason, unless the
    /// application closed it.
    pub async fn closed(&self) -> Option<Error> {
        let mut status = self.status.clone();
        let closed = status
            .wait_for(|s| matches!(s, Status::Closed(_)))
            .await
            .map(|s| s.clone());
        match closed {
            Ok(Status::Closed(error)) => error,
            _ => None,
        }
    }

    pub fn status(&self) -> Status {
        self.status.borrow().clone()
    }

    /// The server took the current connection: messages go out.
    pub fn is_connected(&self) -> bool {
        self.shared.ready.load(Ordering::Acquire)
    }

    /// The current connection's id, as the server named it.
    pub fn connection_id(&self) -> Option<String> {
        let connection = self.shared.connection.lock().ok()?;
        connection.as_ref().map(|(id, _)| id.clone())
    }

    /// The user the server took the connection for.
    pub fn user_id(&self) -> Option<String> {
        let connection = self.shared.connection.lock().ok()?;
        connection.as_ref().and_then(|(_, user)| user.clone())
    }

    /// The user and connection a message is sent for, read before the flag:
    /// one taken in between leaves the message with those before it, which
    /// it does not go out for.
    fn ready(&self) -> Result<SentFor, Error> {
        let sent_for = SentFor {
            user: self.shared.users.load(Ordering::Acquire),
            connection: self.shared.connections.load(Ordering::Acquire),
        };
        if self.shared.ready.load(Ordering::Acquire) {
            Ok(sent_for)
        } else if matches!(*self.status.borrow(), Status::Closed(_)) {
            Err(Error::Closed)
        } else {
            Err(Error::NotConnected)
        }
    }

    fn control(&self, control: Control) -> Result<(), Error> {
        self.control.send(control).map_err(|_| Error::Closed)
    }
}

impl std::fmt::Debug for Client {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Client")
            .field("status", &self.status())
            .field("connection_id", &self.connection_id())
            .finish()
    }
}

/// `{"t": kind, "p": payload}` in one pass.
fn encode<P: Serialize + ?Sized>(kind: &str, payload: &P) -> Result<String, Error> {
    #[derive(Serialize)]
    struct Out<'a, P: ?Sized> {
        t: &'a str,
        p: &'a P,
    }
    serde_json::to_string(&Out {
        t: kind,
        p: payload,
    })
    .map_err(|e| Error::Encode(e.to_string()))
}