Skip to main content

huddle_core/network/
server.rs

1//! Client connector to the centralized `huddle-server`.
2//!
3//! huddle's primary transport is libp2p (mDNS on the LAN, gossipsub
4//! across direct/relayed connections). This module adds a *second* path:
5//! a WebSocket to a single canonical server that the operator hosts. The
6//! server is reachable only as a **Tor v3 onion**, so `.onion` URLs are
7//! dialed through Tor's local SOCKS5 proxy; plain `ws://host:port` URLs
8//! (used in tests) are dialed directly.
9//!
10//! The server is a dumb ciphertext mover: we hand it the same opaque
11//! huddle wire bytes we would have published on a gossipsub topic,
12//! tagged with the cleartext `room` id, base64-encoded. It fans them out
13//! to the room's other members and queues them for offline ones. All
14//! encryption/authentication stays in the layers above — this module
15//! never inspects the payload.
16
17use base64::engine::general_purpose::STANDARD as B64;
18use base64::Engine;
19use futures::{SinkExt, StreamExt};
20use serde::{Deserialize, Serialize};
21use tokio::sync::mpsc;
22use tokio_tungstenite::tungstenite::Message as WsMessage;
23use tokio_tungstenite::WebSocketStream;
24use tracing::warn;
25
26use crate::error::{HuddleError, Result};
27
28/// Messages we send to the server. Mirrors `huddle-server`'s `ClientMsg`.
29#[derive(Debug, Serialize)]
30#[serde(tag = "type", rename_all = "snake_case")]
31enum ClientMsg {
32    Hello { fingerprint: String, rooms: Vec<String> },
33    Subscribe { room: String },
34    Unsubscribe { room: String },
35    Publish { room: String, id: String, payload_b64: String },
36    Fetch,
37    Ping,
38}
39
40/// Messages the server sends back. Mirrors `huddle-server`'s `ServerMsg`.
41#[derive(Debug, Deserialize)]
42#[serde(tag = "type", rename_all = "snake_case")]
43enum ServerMsg {
44    // The server echoes our fingerprint on `ready`, but we already know
45    // our own identity, so we keep only the tag and let serde ignore the
46    // extra field.
47    Ready,
48    Message { room: String, id: String, payload_b64: String },
49    Sent { id: String, delivered: usize, queued: usize },
50    Pong,
51    Error { message: String },
52}
53
54/// What the connector surfaces to the rest of huddle-core. The caller
55/// drives these into the same path that handles a received gossipsub
56/// message (decode → decrypt → `AppEvent`).
57#[derive(Debug, Clone)]
58pub enum ServerEvent {
59    /// Handshake complete; the mailbox (if any) will follow as `Message`s.
60    Ready,
61    /// Delivery receipt for one of our `publish` calls: how many of the
62    /// room's other members received it live vs. were queued because they
63    /// were offline. Lets the UI mark a message delivered/pending.
64    Sent { id: String, delivered: usize, queued: usize },
65    /// A room message delivered (live or from the offline mailbox).
66    Message { room: String, id: String, payload: Vec<u8> },
67    /// The socket closed; the caller may choose to reconnect.
68    Disconnected,
69}
70
71/// A live connection to the server. Cloneable handle; cloning shares the
72/// same underlying socket.
73#[derive(Clone)]
74pub struct ServerClient {
75    out_tx: mpsc::UnboundedSender<ClientMsg>,
76}
77
78impl ServerClient {
79    /// Open a connection, send the initial `hello`, and return the client
80    /// plus a stream of [`ServerEvent`]s.
81    ///
82    /// - `url`: `ws://<onion>:80/ws` in production, or `ws://127.0.0.1:8787/ws` in tests.
83    /// - `socks`: `Some("127.0.0.1:9050")` to route through Tor (**required** for `.onion`);
84    ///   `None` to dial directly.
85    pub async fn connect(
86        url: &str,
87        socks: Option<&str>,
88        fingerprint: String,
89        rooms: Vec<String>,
90    ) -> Result<(Self, mpsc::UnboundedReceiver<ServerEvent>)> {
91        match socks {
92            Some(proxy) => {
93                let proxy: std::net::SocketAddr = proxy
94                    .parse()
95                    .map_err(|e| HuddleError::Network(format!("bad socks address: {e}")))?;
96                let target = host_port_from_ws_url(url)?;
97                let stream = tokio_socks::tcp::Socks5Stream::connect(proxy, target.as_str())
98                    .await
99                    .map_err(|e| HuddleError::Network(format!("tor socks connect: {e}")))?;
100                let (ws, _resp) = tokio_tungstenite::client_async(url, stream)
101                    .await
102                    .map_err(|e| HuddleError::Network(format!("ws handshake: {e}")))?;
103                Ok(Self::spawn(ws, fingerprint, rooms))
104            }
105            None => {
106                let (ws, _resp) = tokio_tungstenite::connect_async(url)
107                    .await
108                    .map_err(|e| HuddleError::Network(format!("ws connect: {e}")))?;
109                Ok(Self::spawn(ws, fingerprint, rooms))
110            }
111        }
112    }
113
114    /// Spawn the read/write pumps for an established socket. Generic over
115    /// the inner stream so the Tor-SOCKS and direct paths (different
116    /// stream types) share one implementation.
117    fn spawn<S>(
118        ws: WebSocketStream<S>,
119        fingerprint: String,
120        rooms: Vec<String>,
121    ) -> (Self, mpsc::UnboundedReceiver<ServerEvent>)
122    where
123        S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
124    {
125        let (mut sink, mut stream) = ws.split();
126        let (out_tx, mut out_rx) = mpsc::unbounded_channel::<ClientMsg>();
127        let (ev_tx, ev_rx) = mpsc::unbounded_channel::<ServerEvent>();
128
129        // Announce ourselves first; the server replies `ready` and then
130        // flushes any queued mailbox messages.
131        let _ = out_tx.send(ClientMsg::Hello { fingerprint, rooms });
132
133        // Writer pump: serialize outgoing messages onto the socket. When
134        // `out_rx` ends (every `ServerClient` handle was dropped) we must
135        // actively close the socket — otherwise the reader task below keeps
136        // the connection alive and the server never sees us disconnect (so
137        // it never marks us offline / starts mailboxing). Closing here sends
138        // a WebSocket Close frame; the server then tears down and our reader
139        // observes the end of stream.
140        tokio::spawn(async move {
141            while let Some(msg) = out_rx.recv().await {
142                let json = match serde_json::to_string(&msg) {
143                    Ok(j) => j,
144                    Err(_) => continue,
145                };
146                if sink.send(WsMessage::Text(json.into())).await.is_err() {
147                    return;
148                }
149            }
150            let _ = sink.close().await;
151        });
152
153        // Reader pump: parse server messages into ServerEvents.
154        tokio::spawn(async move {
155            while let Some(frame) = stream.next().await {
156                let frame = match frame {
157                    Ok(f) => f,
158                    Err(_) => break,
159                };
160                let text = match frame {
161                    WsMessage::Text(t) => t.as_str().to_string(),
162                    WsMessage::Binary(b) => String::from_utf8_lossy(&b).into_owned(),
163                    WsMessage::Close(_) => break,
164                    _ => continue,
165                };
166                match serde_json::from_str::<ServerMsg>(&text) {
167                    Ok(ServerMsg::Ready) => {
168                        let _ = ev_tx.send(ServerEvent::Ready);
169                    }
170                    Ok(ServerMsg::Sent { id, delivered, queued }) => {
171                        let _ = ev_tx.send(ServerEvent::Sent { id, delivered, queued });
172                    }
173                    Ok(ServerMsg::Message { room, id, payload_b64 }) => {
174                        match B64.decode(payload_b64.as_bytes()) {
175                            Ok(payload) => {
176                                let _ = ev_tx.send(ServerEvent::Message { room, id, payload });
177                            }
178                            Err(e) => warn!(error = %e, "server sent undecodable payload"),
179                        }
180                    }
181                    Ok(ServerMsg::Error { message }) => warn!(%message, "huddle-server error"),
182                    Ok(ServerMsg::Pong) => {}
183                    Err(e) => warn!(error = %e, "unparseable server message"),
184                }
185            }
186            let _ = ev_tx.send(ServerEvent::Disconnected);
187        });
188
189        (Self { out_tx }, ev_rx)
190    }
191
192    /// Send a room's opaque wire bytes to the server for fan-out.
193    pub fn publish(&self, room: &str, id: &str, payload: &[u8]) -> Result<()> {
194        self.send(ClientMsg::Publish {
195            room: room.to_string(),
196            id: id.to_string(),
197            payload_b64: B64.encode(payload),
198        })
199    }
200
201    /// Assert membership of a room so the server mailboxes us when offline.
202    pub fn subscribe(&self, room: &str) -> Result<()> {
203        self.send(ClientMsg::Subscribe { room: room.to_string() })
204    }
205
206    pub fn unsubscribe(&self, room: &str) -> Result<()> {
207        self.send(ClientMsg::Unsubscribe { room: room.to_string() })
208    }
209
210    /// Ask the server to re-drain our mailbox.
211    pub fn fetch(&self) -> Result<()> {
212        self.send(ClientMsg::Fetch)
213    }
214
215    pub fn ping(&self) -> Result<()> {
216        self.send(ClientMsg::Ping)
217    }
218
219    fn send(&self, msg: ClientMsg) -> Result<()> {
220        self.out_tx
221            .send(msg)
222            .map_err(|_| HuddleError::Network("server connection closed".to_string()))
223    }
224}
225
226/// Extract `host:port` from a `ws://`/`wss://` URL for the SOCKS target,
227/// defaulting to port 80 when none is given (matches the onion's
228/// `HiddenServicePort 80`).
229fn host_port_from_ws_url(url: &str) -> Result<String> {
230    let rest = url
231        .strip_prefix("ws://")
232        .or_else(|| url.strip_prefix("wss://"))
233        .ok_or_else(|| HuddleError::Network(format!("expected ws:// url, got {url}")))?;
234    let authority = rest.split('/').next().unwrap_or(rest);
235    if authority.is_empty() {
236        return Err(HuddleError::Network(format!("no host in url: {url}")));
237    }
238    if authority.contains(':') {
239        Ok(authority.to_string())
240    } else {
241        Ok(format!("{authority}:80"))
242    }
243}
244
245#[cfg(test)]
246mod tests {
247    use super::host_port_from_ws_url;
248
249    #[test]
250    fn parses_host_port() {
251        assert_eq!(host_port_from_ws_url("ws://abc.onion/ws").unwrap(), "abc.onion:80");
252        assert_eq!(
253            host_port_from_ws_url("ws://127.0.0.1:8787/ws").unwrap(),
254            "127.0.0.1:8787"
255        );
256        assert_eq!(host_port_from_ws_url("wss://h:443").unwrap(), "h:443");
257        assert!(host_port_from_ws_url("http://x").is_err());
258    }
259}