apfsds-transport 0.4.0

Network transports for APFSDS (mTLS, QUIC, SSH, WebSocket)
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
//! WebSocket client with Chrome handshake emulation

use futures::{SinkExt, StreamExt};
use thiserror::Error;
use tokio::net::TcpStream;
use tokio_tungstenite::{
    MaybeTlsStream, WebSocketStream, connect_async_with_config,
    tungstenite::{
        Message,
        client::IntoClientRequest,
        http::{Request, header},
        protocol::WebSocketConfig,
    },
};
use tracing::{debug, info, trace};

/// Chrome 120 User-Agent
pub const CHROME_UA: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
    AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";

/// Browser profile for fingerprint emulation
#[derive(Debug, Clone)]
pub struct BrowserProfile {
    pub user_agent: String,
    pub accept_language: String,
    pub accept_encoding: String,
    pub sec_fetch_site: String,
    pub sec_fetch_mode: String,
    pub sec_fetch_dest: String,
    pub plugin_headers: Vec<(String, String)>,
}

impl Default for BrowserProfile {
    fn default() -> Self {
        Self::chrome_120_windows()
    }
}

impl BrowserProfile {
    /// Chrome 120 on Windows (default)
    pub fn chrome_120_windows() -> Self {
        Self {
            user_agent: CHROME_UA.to_string(),
            accept_language: "en-US,en;q=0.9".to_string(),
            accept_encoding: "gzip, deflate, br".to_string(),
            sec_fetch_site: "cross-site".to_string(),
            sec_fetch_mode: "websocket".to_string(),
            sec_fetch_dest: "empty".to_string(),
            plugin_headers: Vec::new(),
        }
    }

    /// Chrome 120 with AdBlock Plus
    pub fn chrome_120_with_adblock() -> Self {
        let mut profile = Self::chrome_120_windows();
        profile.plugin_headers.push((
            "X-Client-Data".to_string(),
            "CIW2yQEIprbJAQjBtskBCKmdygEIlaHKAQiVocoBGI6jygE=".to_string(),
        ));
        profile
    }

    /// Chrome 120 on macOS
    pub fn chrome_120_macos() -> Self {
        Self {
            user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36".to_string(),
            accept_language: "en-US,en;q=0.9".to_string(),
            accept_encoding: "gzip, deflate, br".to_string(),
            sec_fetch_site: "cross-site".to_string(),
            sec_fetch_mode: "websocket".to_string(),
            sec_fetch_dest: "empty".to_string(),
            plugin_headers: Vec::new(),
        }
    }
}

#[derive(Error, Debug)]
pub enum WssClientError {
    #[error("Connection failed: {0}")]
    ConnectionFailed(String),

    #[error("Handshake failed: {0}")]
    HandshakeFailed(String),

    #[error("Send failed: {0}")]
    SendFailed(String),

    #[error("Receive failed: {0}")]
    ReceiveFailed(String),

    #[error("Connection closed")]
    ConnectionClosed,

    #[error("Invalid URL: {0}")]
    InvalidUrl(String),
}

/// WebSocket client configuration
#[derive(Debug, Clone)]
pub struct WssClientConfig {
    /// Target WebSocket URL
    pub url: String,

    /// Authorization token
    pub token: Option<String>,

    /// Browser profile for fingerprint emulation
    pub browser_profile: BrowserProfile,

    /// Cookies to send
    pub cookies: Vec<(String, String)>,

    /// Origin header (auto-generated from URL if None)
    pub origin: Option<String>,

    /// Referer header
    pub referer: Option<String>,

    /// Custom headers
    pub headers: Vec<(String, String)>,

    /// Enable compression
    pub compression: bool,

    /// Connection timeout in seconds
    pub timeout_secs: u64,
}

impl Default for WssClientConfig {
    fn default() -> Self {
        Self {
            url: String::new(),
            token: None,
            browser_profile: BrowserProfile::default(),
            cookies: Vec::new(),
            origin: None,
            referer: None,
            headers: Vec::new(),
            compression: true,
            timeout_secs: 30,
        }
    }
}

/// WebSocket client wrapper
pub struct WssClient {
    stream: WebSocketStream<MaybeTlsStream<TcpStream>>,
}

impl WssClient {
    /// Connect to a WebSocket server with Chrome-like handshake
    pub async fn connect(config: WssClientConfig) -> Result<Self, WssClientError> {
        let request = Self::build_request(&config)?;

        let mut ws_config = WebSocketConfig::default();
        ws_config.max_message_size = Some(64 << 20); // 64MB
        ws_config.max_frame_size = Some(16 << 20); // 16MB

        debug!("Connecting to {}", config.url);

        let (stream, response) = connect_async_with_config(request, Some(ws_config), false)
            .await
            .map_err(|e| WssClientError::ConnectionFailed(e.to_string()))?;

        info!(
            "Connected to WebSocket server, status: {}",
            response.status()
        );

        Ok(Self { stream })
    }

    /// Build a Chrome-like HTTP request for WebSocket upgrade
    fn build_request(config: &WssClientConfig) -> Result<Request<()>, WssClientError> {
        let mut request = config
            .url
            .as_str()
            .into_client_request()
            .map_err(|e| WssClientError::InvalidUrl(e.to_string()))?;

        let host = request.uri().host().unwrap_or("localhost").to_string();
        let uri = request.uri().clone();

        // Generate Origin if not provided
        let origin = config.origin.clone().unwrap_or_else(|| {
            let scheme = uri.scheme_str().unwrap_or("wss");
            let origin_scheme = if scheme == "wss" { "https" } else { "http" };
            format!("{}://{}", origin_scheme, host)
        });

        let headers = request.headers_mut();

        // Chrome-like headers in correct order (order matters for fingerprinting!)

        // 1. Host (already set by into_client_request, but ensure it's correct)
        headers.insert(header::HOST, host.parse().unwrap());

        // 2. Connection: Upgrade (set by WebSocket library)

        // 3. Pragma & Cache-Control
        headers.insert(header::PRAGMA, "no-cache".parse().unwrap());
        headers.insert(header::CACHE_CONTROL, "no-cache".parse().unwrap());

        // 4. User-Agent (from browser profile)
        headers.insert(
            header::USER_AGENT,
            config.browser_profile.user_agent.parse().unwrap(),
        );

        // 5. Upgrade: websocket (set by WebSocket library)

        // 6. Origin (critical for Chrome!)
        headers.insert(header::ORIGIN, origin.parse().unwrap());

        // 7. Sec-WebSocket-Version (set by WebSocket library)

        // 8. Accept-Encoding (from browser profile)
        headers.insert(
            header::ACCEPT_ENCODING,
            config.browser_profile.accept_encoding.parse().unwrap(),
        );

        // 9. Accept-Language (from browser profile)
        headers.insert(
            header::ACCEPT_LANGUAGE,
            config.browser_profile.accept_language.parse().unwrap(),
        );

        // 10. Cookie (if provided)
        if !config.cookies.is_empty() {
            let cookie_str = config
                .cookies
                .iter()
                .map(|(k, v)| format!("{}={}", k, v))
                .collect::<Vec<_>>()
                .join("; ");
            headers.insert(header::COOKIE, cookie_str.parse().unwrap());
        }

        // 11. Sec-WebSocket-Key (set by WebSocket library)

        // 12. Sec-WebSocket-Extensions
        headers.insert(
            "Sec-WebSocket-Extensions",
            "permessage-deflate; client_max_window_bits"
                .parse()
                .unwrap(),
        );

        // 13. Sec-Fetch-* headers (Chrome 85+)
        headers.insert(
            "Sec-Fetch-Site",
            config.browser_profile.sec_fetch_site.parse().unwrap(),
        );
        headers.insert(
            "Sec-Fetch-Mode",
            config.browser_profile.sec_fetch_mode.parse().unwrap(),
        );
        headers.insert(
            "Sec-Fetch-Dest",
            config.browser_profile.sec_fetch_dest.parse().unwrap(),
        );

        // 14. Referer (if provided)
        if let Some(referer) = &config.referer {
            headers.insert(header::REFERER, referer.parse().unwrap());
        }

        // 15. Plugin headers (e.g., X-Client-Data for Chrome with sync enabled)
        for (key, value) in &config.browser_profile.plugin_headers {
            if let (Ok(name), Ok(val)) = (
                key.parse::<hyper::header::HeaderName>(),
                value.parse::<hyper::header::HeaderValue>(),
            ) {
                headers.insert(name, val);
            }
        }

        // 16. Authorization (if token is provided)
        if let Some(token) = &config.token {
            headers.insert(
                header::AUTHORIZATION,
                format!("Bearer {}", token).parse().unwrap(),
            );
        }

        // 17. Custom headers (last, to allow overrides)
        for (key, value) in &config.headers {
            if let (Ok(name), Ok(val)) = (
                key.parse::<hyper::header::HeaderName>(),
                value.parse::<hyper::header::HeaderValue>(),
            ) {
                headers.insert(name, val);
            }
        }

        Ok(request)
    }

    /// Send initial fake frames to emulate negotiation
    pub async fn send_initial_frames(&mut self) -> Result<(), WssClientError> {
        // Frame 1: Small text frame (like handshake ack)
        self.send_text("ping").await?;

        tokio::time::sleep(std::time::Duration::from_millis(fastrand::u64(10..50))).await;

        // Frame 2-3: Empty padding frames
        for _ in 0..2 {
            let padding: Vec<u8> = (0..fastrand::usize(100..500))
                .map(|_| fastrand::u8(..))
                .collect();
            self.send_binary(&padding).await?;

            tokio::time::sleep(std::time::Duration::from_millis(fastrand::u64(5..20))).await;
        }

        Ok(())
    }

    /// Send a binary message
    pub async fn send_binary(&mut self, data: &[u8]) -> Result<(), WssClientError> {
        trace!("Sending binary frame: {} bytes", data.len());
        self.stream
            .send(Message::Binary(data.to_vec().into()))
            .await
            .map_err(|e| WssClientError::SendFailed(e.to_string()))
    }

    /// Send a text message
    pub async fn send_text(&mut self, text: &str) -> Result<(), WssClientError> {
        trace!("Sending text frame: {}", text);
        self.stream
            .send(Message::Text(text.to_string().into()))
            .await
            .map_err(|e| WssClientError::SendFailed(e.to_string()))
    }

    /// Receive the next message
    pub async fn receive(&mut self) -> Result<Message, WssClientError> {
        match self.stream.next().await {
            Some(Ok(msg)) => {
                trace!("Received message: {:?}", msg);
                Ok(msg)
            }
            Some(Err(e)) => Err(WssClientError::ReceiveFailed(e.to_string())),
            None => Err(WssClientError::ConnectionClosed),
        }
    }

    /// Receive binary data only
    pub async fn receive_binary(&mut self) -> Result<Vec<u8>, WssClientError> {
        loop {
            match self.receive().await? {
                Message::Binary(data) => return Ok(data.to_vec()),
                Message::Ping(data) => {
                    self.stream
                        .send(Message::Pong(data))
                        .await
                        .map_err(|e| WssClientError::SendFailed(e.to_string()))?;
                }
                Message::Close(_) => return Err(WssClientError::ConnectionClosed),
                _ => continue, // Ignore text and other frames
            }
        }
    }

    /// Send ping
    pub async fn ping(&mut self, data: &[u8]) -> Result<(), WssClientError> {
        self.stream
            .send(Message::Ping(data.to_vec().into()))
            .await
            .map_err(|e| WssClientError::SendFailed(e.to_string()))
    }

    /// Close the connection
    pub async fn close(&mut self) -> Result<(), WssClientError> {
        debug!("Closing WebSocket connection");
        self.stream
            .close(None)
            .await
            .map_err(|e| WssClientError::SendFailed(e.to_string()))
    }

    /// Get mutable reference to the underlying stream
    pub fn stream_mut(&mut self) -> &mut WebSocketStream<MaybeTlsStream<TcpStream>> {
        &mut self.stream
    }

    /// Split into read and write halves
    pub fn split(
        self,
    ) -> (
        futures::stream::SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>,
        futures::stream::SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
    ) {
        self.stream.split()
    }
}

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

    #[test]
    fn test_chrome_ua() {
        assert!(CHROME_UA.contains("Chrome/120"));
        assert!(CHROME_UA.contains("Windows NT 10.0"));
    }

    #[test]
    fn test_config_default() {
        let config = WssClientConfig::default();
        assert!(config.compression);
        assert_eq!(config.timeout_secs, 30);
    }
}