ivoryvalley 0.1.0

A transparent deduplication proxy for Mastodon and the Fediverse
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
//! WebSocket relay handlers for Mastodon streaming API.
//!
//! This module implements the WebSocket proxy that:
//! - Accepts WebSocket connections from Mastodon clients
//! - Connects to the upstream Mastodon streaming server
//! - Relays messages bidirectionally between client and upstream
//! - Filters `update` events for deduplication

use axum::{
    extract::{
        ws::{Message, WebSocket},
        Query, State, WebSocketUpgrade,
    },
    response::Response,
};
use futures_util::{SinkExt, StreamExt};
use serde::Deserialize;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_tungstenite::{connect_async, tungstenite};
use tracing::{debug, error, info, warn};

use crate::config::AppState;
use crate::db::{extract_dedup_uri, SeenUriStore};

/// Query parameters for WebSocket streaming endpoint.
///
/// # Security Note
///
/// The Mastodon streaming API authenticates WebSocket connections via
/// query parameters (e.g., `?access_token=...`) and does not support
/// using the `Authorization` header once the WebSocket upgrade has
/// occurred. This relay mirrors that behavior to remain compatible with
/// Mastodon clients.
///
/// Because query parameters may be logged by proxies and load balancers,
/// deployments using this module should:
/// - Use HTTPS between all hops (client → relay → upstream)
/// - Avoid logging full URLs containing `access_token`
/// - Prefer short-lived or least-privileged access tokens
#[derive(Debug, Deserialize)]
pub struct StreamingParams {
    /// Access token for authentication.
    ///
    /// Accepted as a query parameter to remain compatible with the Mastodon
    /// streaming API, which authenticates WebSocket streams using query
    /// parameters only.
    pub access_token: Option<String>,
    /// Stream type (user, public, etc.)
    pub stream: Option<String>,
    /// Tag for hashtag streams
    pub tag: Option<String>,
    /// List ID for list streams
    pub list: Option<String>,
}

/// State for WebSocket connections with deduplication support
#[derive(Clone)]
pub struct WebSocketState {
    pub app_state: AppState,
    pub seen_store: Arc<SeenUriStore>,
}

impl WebSocketState {
    pub fn new(app_state: AppState, seen_store: Arc<SeenUriStore>) -> Self {
        Self {
            app_state,
            seen_store,
        }
    }
}

/// Handle WebSocket upgrade requests for streaming API
pub async fn streaming_handler(
    ws: WebSocketUpgrade,
    State(state): State<WebSocketState>,
    Query(params): Query<StreamingParams>,
) -> Response {
    info!("WebSocket upgrade request received");

    // Extract what we need before the upgrade to avoid Send issues
    let upstream_url = state.app_state.config.upstream_url.clone();
    let seen_store = state.seen_store.clone();

    ws.on_upgrade(move |socket| handle_streaming(socket, upstream_url, seen_store, params))
}

/// Handle the streaming WebSocket connection
async fn handle_streaming(
    client_ws: WebSocket,
    upstream_url: String,
    seen_store: Arc<SeenUriStore>,
    params: StreamingParams,
) {
    // Build upstream WebSocket URL
    let upstream_ws_url = build_upstream_ws_url(&upstream_url, &params);

    info!("Connecting to upstream: {}", upstream_ws_url);

    // Split client connection early so we can send error if upstream fails
    let (mut client_sink, client_stream) = client_ws.split();

    // Connect to upstream
    let upstream_result = connect_async(&upstream_ws_url).await;

    let (upstream_ws, _response) = match upstream_result {
        Ok(conn) => conn,
        Err(e) => {
            error!("Failed to connect to upstream WebSocket: {}", e);
            // Send close frame to client with error reason
            let close_frame = axum::extract::ws::CloseFrame {
                code: 1014, // Bad Gateway equivalent
                reason: "Failed to connect to upstream server".into(),
            };
            let _ = client_sink.send(Message::Close(Some(close_frame))).await;
            return;
        }
    };

    info!("Connected to upstream WebSocket");

    // Split upstream connection
    let (mut upstream_sink, mut upstream_stream) = upstream_ws.split();

    // Create channels for message passing.
    // Buffer size of 32 is a deliberate compromise:
    // - Mastodon streaming events arrive at a modest rate
    // - Small bounded buffer smooths short bursts without unbounded memory growth
    // - Backpressure is acceptable: slowing relay is preferable to unbounded buffering
    let (client_tx, mut client_rx) = mpsc::channel::<Message>(32);
    let (upstream_tx, mut upstream_rx) = mpsc::channel::<tungstenite::Message>(32);

    // Wrap client_stream in an Option for move into task
    let mut client_stream = Some(client_stream);

    // Clone store for the filtering task
    let filter_store = seen_store.clone();

    // Task: Forward filtered messages from upstream to client
    let mut upstream_to_client = tokio::spawn(async move {
        while let Some(msg_result) = upstream_stream.next().await {
            match msg_result {
                Ok(msg) => {
                    // Convert and potentially filter the message
                    if let Some(client_msg) = filter_upstream_message(msg, &filter_store) {
                        if client_tx.send(client_msg).await.is_err() {
                            debug!("Client channel closed");
                            break;
                        }
                    }
                }
                Err(e) => {
                    warn!("Upstream WebSocket error: {}", e);
                    break;
                }
            }
        }
    });

    // Task: Forward messages from client to upstream
    let mut client_to_upstream = tokio::spawn(async move {
        let mut stream = client_stream.take().unwrap();
        while let Some(msg_result) = stream.next().await {
            match msg_result {
                Ok(msg) => {
                    // Convert axum Message to tungstenite Message
                    if let Some(upstream_msg) = convert_client_to_upstream(msg) {
                        if upstream_tx.send(upstream_msg).await.is_err() {
                            debug!("Upstream channel closed");
                            break;
                        }
                    }
                }
                Err(e) => {
                    warn!("Client WebSocket error: {}", e);
                    break;
                }
            }
        }
    });

    // Task: Send messages to client
    let mut send_to_client = tokio::spawn(async move {
        while let Some(msg) = client_rx.recv().await {
            if client_sink.send(msg).await.is_err() {
                debug!("Failed to send to client");
                break;
            }
        }
    });

    // Task: Send messages to upstream
    let mut send_to_upstream = tokio::spawn(async move {
        while let Some(msg) = upstream_rx.recv().await {
            if upstream_sink.send(msg).await.is_err() {
                debug!("Failed to send to upstream");
                break;
            }
        }
    });

    // Wait for any task to complete (connection closed), then abort the rest
    tokio::select! {
        _ = &mut upstream_to_client => info!("Upstream to client task ended"),
        _ = &mut client_to_upstream => info!("Client to upstream task ended"),
        _ = &mut send_to_client => info!("Send to client task ended"),
        _ = &mut send_to_upstream => info!("Send to upstream task ended"),
    }

    // Abort remaining tasks to prevent resource leaks
    upstream_to_client.abort();
    client_to_upstream.abort();
    send_to_client.abort();
    send_to_upstream.abort();

    info!("WebSocket connection closed");
}

/// Build the upstream WebSocket URL with query parameters
fn build_upstream_ws_url(upstream_base: &str, params: &StreamingParams) -> String {
    // Convert HTTP(S) URL to WS(S)
    let ws_base = upstream_base
        .replace("https://", "wss://")
        .replace("http://", "ws://");

    let mut url = format!("{}/api/v1/streaming", ws_base);

    // Build query string with proper URL encoding
    let mut query_parts = Vec::new();

    if let Some(ref token) = params.access_token {
        query_parts.push(format!("access_token={}", urlencoding::encode(token)));
    }
    if let Some(ref stream) = params.stream {
        query_parts.push(format!("stream={}", urlencoding::encode(stream)));
    }
    if let Some(ref tag) = params.tag {
        query_parts.push(format!("tag={}", urlencoding::encode(tag)));
    }
    if let Some(ref list) = params.list {
        query_parts.push(format!("list={}", urlencoding::encode(list)));
    }

    if !query_parts.is_empty() {
        url.push('?');
        url.push_str(&query_parts.join("&"));
    }

    url
}

/// Filter messages from upstream, applying deduplication to update events
fn filter_upstream_message(
    msg: tungstenite::Message,
    seen_store: &SeenUriStore,
) -> Option<Message> {
    match msg {
        tungstenite::Message::Text(text) => {
            // Try to parse as streaming event, filter out duplicates
            filter_streaming_event(&text, seen_store).map(|filtered| Message::Text(filtered.into()))
        }
        tungstenite::Message::Binary(data) => Some(Message::Binary(data)),
        tungstenite::Message::Ping(data) => Some(Message::Ping(data)),
        tungstenite::Message::Pong(data) => Some(Message::Pong(data)),
        tungstenite::Message::Close(frame) => {
            let axum_frame = frame.map(|f| axum::extract::ws::CloseFrame {
                code: f.code.into(),
                reason: f.reason.to_string().into(),
            });
            Some(Message::Close(axum_frame))
        }
        tungstenite::Message::Frame(_) => None, // Raw frames not supported
    }
}

/// Filter a streaming event, returning None if it should be deduplicated
fn filter_streaming_event(text: &str, seen_store: &SeenUriStore) -> Option<String> {
    // Parse the event JSON
    let event: serde_json::Value = match serde_json::from_str(text) {
        Ok(v) => v,
        Err(_) => {
            // Not valid JSON, pass through (could be heartbeat comment line)
            return Some(text.to_string());
        }
    };

    // Check if this is an update event
    let event_type = event.get("event").and_then(|e| e.as_str());

    if event_type != Some("update") {
        // Not an update event, pass through
        return Some(text.to_string());
    }

    // Parse the payload (it's a JSON string inside the event)
    let payload_str = event.get("payload").and_then(|p| p.as_str())?;
    let payload: serde_json::Value = serde_json::from_str(payload_str).ok()?;

    // Extract the deduplication URI
    let dedup_uri = extract_dedup_uri(&payload)?;

    // Atomically check if seen and mark as seen
    match seen_store.check_and_mark(dedup_uri) {
        Ok(was_seen) => {
            if was_seen {
                debug!("Filtering duplicate status: {}", dedup_uri);
                None // Filter out duplicate
            } else {
                Some(text.to_string())
            }
        }
        Err(e) => {
            warn!("Failed to check/mark URI {}: {}", dedup_uri, e);
            // On error, pass through to avoid dropping messages
            Some(text.to_string())
        }
    }
}

/// Convert client message to upstream tungstenite message
fn convert_client_to_upstream(msg: Message) -> Option<tungstenite::Message> {
    match msg {
        Message::Text(text) => Some(tungstenite::Message::Text(text.to_string().into())),
        Message::Binary(data) => Some(tungstenite::Message::Binary(data)),
        Message::Ping(data) => Some(tungstenite::Message::Ping(data)),
        Message::Pong(data) => Some(tungstenite::Message::Pong(data)),
        Message::Close(frame) => {
            let tung_frame = frame.map(|f| tungstenite::protocol::CloseFrame {
                code: tungstenite::protocol::frame::coding::CloseCode::from(f.code),
                reason: f.reason.to_string().into(),
            });
            Some(tungstenite::Message::Close(tung_frame))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_build_upstream_ws_url_basic() {
        let params = StreamingParams {
            access_token: None,
            stream: None,
            tag: None,
            list: None,
        };

        let url = build_upstream_ws_url("https://mastodon.social", &params);
        assert_eq!(url, "wss://mastodon.social/api/v1/streaming");
    }

    #[test]
    fn test_build_upstream_ws_url_with_token() {
        let params = StreamingParams {
            access_token: Some("test_token".to_string()),
            stream: Some("user".to_string()),
            tag: None,
            list: None,
        };

        let url = build_upstream_ws_url("https://mastodon.social", &params);
        assert_eq!(
            url,
            "wss://mastodon.social/api/v1/streaming?access_token=test_token&stream=user"
        );
    }

    #[test]
    fn test_build_upstream_ws_url_with_hashtag() {
        let params = StreamingParams {
            access_token: Some("token".to_string()),
            stream: Some("hashtag".to_string()),
            tag: Some("rust".to_string()),
            list: None,
        };

        let url = build_upstream_ws_url("https://mastodon.social", &params);
        assert!(url.contains("stream=hashtag"));
        assert!(url.contains("tag=rust"));
    }

    #[test]
    fn test_build_upstream_ws_url_http_to_ws() {
        let params = StreamingParams {
            access_token: None,
            stream: None,
            tag: None,
            list: None,
        };

        let url = build_upstream_ws_url("http://localhost:3000", &params);
        assert_eq!(url, "ws://localhost:3000/api/v1/streaming");
    }

    #[test]
    fn test_filter_streaming_event_passes_non_update() {
        let store = SeenUriStore::open(":memory:").unwrap();

        // Notification event should pass through
        let event = r#"{"event":"notification","payload":"{\"id\":\"123\"}"}"#;
        let result = filter_streaming_event(event, &store);
        assert_eq!(result, Some(event.to_string()));

        // Delete event should pass through
        let delete_event = r#"{"event":"delete","payload":"123456"}"#;
        let result = filter_streaming_event(delete_event, &store);
        assert_eq!(result, Some(delete_event.to_string()));
    }

    #[test]
    fn test_filter_streaming_event_deduplicates_updates() {
        let store = SeenUriStore::open(":memory:").unwrap();

        let event = r#"{"event":"update","payload":"{\"id\":\"123\",\"uri\":\"https://mastodon.social/users/test/statuses/123\"}"}"#;

        // First time should pass through
        let result = filter_streaming_event(event, &store);
        assert!(result.is_some());

        // Second time should be filtered
        let result = filter_streaming_event(event, &store);
        assert!(result.is_none());
    }

    #[test]
    fn test_filter_streaming_event_deduplicates_reblogs() {
        let store = SeenUriStore::open(":memory:").unwrap();

        // Original status
        let original = r#"{"event":"update","payload":"{\"id\":\"123\",\"uri\":\"https://mastodon.social/users/original/statuses/123\"}"}"#;

        // Reblog of the same status
        let reblog = r#"{"event":"update","payload":"{\"id\":\"456\",\"uri\":\"https://mastodon.social/users/booster/statuses/456\",\"reblog\":{\"id\":\"123\",\"uri\":\"https://mastodon.social/users/original/statuses/123\"}}"}"#;

        // Original passes through
        let result = filter_streaming_event(original, &store);
        assert!(result.is_some());

        // Reblog is filtered (same underlying content)
        let result = filter_streaming_event(reblog, &store);
        assert!(result.is_none());
    }

    #[test]
    fn test_filter_streaming_event_passes_invalid_json() {
        let store = SeenUriStore::open(":memory:").unwrap();

        // Heartbeat comment line (not JSON)
        let heartbeat = ":";
        let result = filter_streaming_event(heartbeat, &store);
        assert_eq!(result, Some(heartbeat.to_string()));

        // Invalid JSON passes through
        let invalid = "not json at all";
        let result = filter_streaming_event(invalid, &store);
        assert_eq!(result, Some(invalid.to_string()));
    }

    #[test]
    fn test_filter_streaming_event_different_statuses_pass() {
        let store = SeenUriStore::open(":memory:").unwrap();

        let event1 =
            r#"{"event":"update","payload":"{\"id\":\"1\",\"uri\":\"https://example.com/1\"}"}"#;
        let event2 =
            r#"{"event":"update","payload":"{\"id\":\"2\",\"uri\":\"https://example.com/2\"}"}"#;

        // Both different statuses should pass
        assert!(filter_streaming_event(event1, &store).is_some());
        assert!(filter_streaming_event(event2, &store).is_some());
    }
}