wechat-protocol 2.4.2

Native WeChat transport, authentication and protocol codecs
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
//! Download and decode one WeChat Channels video from freshly resolved playback material.
//!
//! The address and token come from the authenticated detail reply and are used once. The request
//! goes only to the Channels video host, pinned to a checked public address, without redirects or
//! proxies. The first 128 KiB are decoded while bytes arrive; the rest is stored as received.
use anyhow::{ensure, Result};
use std::{io::Read, net::SocketAddr, time::Duration};
use wechat_media::channels::{apply_channels_prefix, channels_prefix_keystream};

/// Upper bound on a Channels video download.
pub const MAX_CHANNELS_VIDEO_BYTES: usize = super::MAX_VIDEO_DOWNLOAD_BYTES;
const VIDEO_HOST: &str = "finder.video.qq.com";
/// Host the detail reply uses for the same objects; the player rewrites it to [`VIDEO_HOST`].
const LEGACY_HOST: &str = "wxapp.tc.qq.com";
const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 MicroMessenger";
const REFERER: &str = "https://channels.weixin.qq.com/";

/// What the CDN delivered, kept for checks and failure diagnostics. Holds no address, token or
/// key.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ChannelsDownloadFacts {
    /// Video size the detail reply advertised, when present.
    pub advertised_bytes: Option<u64>,
    /// CDN copy size the detail reply advertised, when present.
    pub advertised_cdn_bytes: Option<u64>,
    /// `Content-Length` of the response, when present.
    pub content_length: Option<u64>,
    /// Bytes actually received.
    pub received_bytes: u64,
    /// Media type of the response, when it is a plain `type/subtype`.
    pub content_type: Option<String>,
    /// Box type at offset 4 as received, when printable.
    pub encoded_box: Option<String>,
    /// Box type at offset 4 after decoding the prefix, when printable.
    pub decoded_box: Option<String>,
}

/// A downloaded Channels video: the decoded bytes and what the CDN delivered.
pub struct ChannelsVideo {
    /// Decoded MP4 bytes.
    pub bytes: Vec<u8>,
    /// Sizes, media type and box types observed during the download.
    pub facts: ChannelsDownloadFacts,
}

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

/// A downloaded video failed a check. Its message is the stable code
/// `channels_integrity_failed`; `check` names which check and `facts` say what arrived.
#[derive(Debug, Clone)]
pub struct ChannelsDownloadFailure {
    /// `empty`, `length` (fewer or more bytes than `Content-Length`) or `container`.
    pub check: &'static str,
    /// What the CDN delivered.
    pub facts: ChannelsDownloadFacts,
}

impl std::fmt::Display for ChannelsDownloadFailure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("channels_integrity_failed")
    }
}

impl std::error::Error for ChannelsDownloadFailure {}

/// The four-byte box type at offset 4, when it is printable ASCII.
pub fn channels_box_type(bytes: &[u8]) -> Option<String> {
    let kind = bytes.get(4..8)?;
    kind.iter()
        .all(|b| b.is_ascii_alphanumeric() || *b == b' ')
        .then(|| String::from_utf8_lossy(kind).into_owned())
}

/// Build the download address the way the official player does: the returned address with its
/// token appended, over HTTPS, on the video host.
pub fn channels_video_url(media: &crate::ChannelsMedia) -> Result<reqwest::Url> {
    let (url, token) = media.transport();
    let mut address = reqwest::Url::parse(&format!("{url}{token}"))
        .map_err(|_| anyhow::anyhow!("channels_unsupported_media"))?;
    ensure!(
        matches!(address.scheme(), "http" | "https")
            && address.username().is_empty()
            && address.password().is_none()
            && address.port().is_none()
            && address.fragment().is_none(),
        "channels_unsupported_media"
    );
    match address.host_str() {
        Some(LEGACY_HOST) => address
            .set_host(Some(VIDEO_HOST))
            .map_err(|_| anyhow::anyhow!("channels_unsupported_media"))?,
        Some(VIDEO_HOST) => {}
        _ => anyhow::bail!("channels_unsupported_media"),
    }
    address
        .set_scheme("https")
        .map_err(|_| anyhow::anyhow!("channels_unsupported_media"))?;
    Ok(address)
}

/// Download the post's video and return the decoded MP4 bytes.
///
/// `max_bytes` caps the transfer below [`MAX_CHANNELS_VIDEO_BYTES`]. The advertised size, when
/// the reply gives one, must fit the cap and match the received length.
pub fn fetch_channels_video(
    media: &crate::ChannelsMedia,
    max_bytes: usize,
    progress: impl FnMut(u64, Option<u64>) -> Result<()>,
) -> Result<ChannelsVideo> {
    let url = channels_video_url(media)?;
    let address = super::link_thumbnail::public_address(
        &url,
        "channels_download_failed",
        "channels_download_failed",
    )?;
    download_at(url, address, media, max_bytes, progress)
}

fn timeout_for(expected: Option<u64>) -> Duration {
    // At least two minutes, plus one second per MiB, at most thirty minutes.
    let seconds = expected.map_or(0, |bytes| bytes / (1024 * 1024));
    Duration::from_secs((120 + seconds).min(30 * 60))
}

fn download_at(
    url: reqwest::Url,
    address: SocketAddr,
    media: &crate::ChannelsMedia,
    max_bytes: usize,
    mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
) -> Result<ChannelsVideo> {
    let cap = max_bytes.min(MAX_CHANNELS_VIDEO_BYTES) as u64;
    let advertised = [media.file_size, media.cdn_file_size];
    ensure!(
        advertised.iter().flatten().all(|size| *size <= cap),
        "channels_media_too_large"
    );
    let host = url.host_str().unwrap_or_default().to_owned();
    let client = reqwest::blocking::Client::builder()
        .timeout(timeout_for(media.file_size.or(media.cdn_file_size)))
        .connect_timeout(Duration::from_secs(15))
        .redirect(reqwest::redirect::Policy::none())
        .no_proxy()
        .resolve(&host, address)
        .build()?;
    let mut response = client
        .get(url)
        .header("Accept-Encoding", "identity")
        .header("User-Agent", USER_AGENT)
        .header("Referer", REFERER)
        .send()
        .map_err(|_| anyhow::anyhow!("channels_download_failed"))?;
    if response.status() != 200 {
        anyhow::bail!("CDN HTTP status {}", response.status().as_u16());
    }
    let header = |name: &str| {
        response
            .headers()
            .get(name)
            .and_then(|value| value.to_str().ok())
            .map(str::to_ascii_lowercase)
    };
    ensure!(
        header("x-errno").is_none_or(|errno| errno.trim() == "0"),
        "channels_download_failed"
    );
    ensure!(
        header("content-encoding").is_none_or(|v| v == "identity"),
        "channels_download_failed"
    );
    ensure!(
        header("content-type").is_none_or(
            |kind| kind.starts_with("video/") || kind.starts_with("application/octet-stream")
        ),
        "channels_download_failed"
    );
    let content_type = header("content-type")
        .map(|kind| kind.split(';').next().unwrap_or_default().trim().to_owned())
        .filter(|kind| {
            kind.len() <= 64
                && kind.contains('/')
                && kind
                    .bytes()
                    .all(|b| b.is_ascii_alphanumeric() || b"/.+-".contains(&b))
        });
    let total = response.content_length();
    if let Some(length) = total {
        ensure!(length <= cap, "channels_media_too_large");
    }
    let stream = channels_prefix_keystream(media.decode_key());
    let mut bytes = Vec::with_capacity(total.unwrap_or(0).min(cap) as usize);
    let mut chunk = vec![0u8; 64 * 1024];
    let mut head = Vec::with_capacity(8);
    loop {
        progress(bytes.len() as u64, total)?;
        let n = response
            .read(&mut chunk)
            .map_err(|_| anyhow::anyhow!("channels_download_failed"))?;
        if n == 0 {
            break;
        }
        ensure!((bytes.len() + n) as u64 <= cap, "channels_media_too_large");
        if head.len() < 8 {
            let wanted = (8 - head.len()).min(n);
            head.extend_from_slice(&chunk[..wanted]);
        }
        let offset = bytes.len() as u64;
        apply_channels_prefix(&mut chunk[..n], offset, &stream);
        bytes.extend_from_slice(&chunk[..n]);
    }
    let facts = ChannelsDownloadFacts {
        advertised_bytes: media.file_size,
        advertised_cdn_bytes: media.cdn_file_size,
        content_length: total,
        received_bytes: bytes.len() as u64,
        content_type,
        encoded_box: channels_box_type(&head),
        decoded_box: channels_box_type(&bytes),
    };
    // An advertised size that differs is recorded, not fatal: the container check decides
    // whether the bytes are the expected video.
    let check = if bytes.is_empty() {
        Some("empty")
    } else if total.is_some_and(|n| n != facts.received_bytes) {
        Some("length")
    } else {
        None
    };
    if let Some(check) = check {
        return Err(ChannelsDownloadFailure { check, facts }.into());
    }
    Ok(ChannelsVideo { bytes, facts })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{io::Write, net::TcpListener};
    use wechat_media::channels::CHANNELS_PREFIX_BYTES;

    fn media(url: &str, token: &str, key: u64, size: Option<u64>) -> crate::ChannelsMedia {
        crate::ChannelsMedia::for_tests(url, token, key, size)
    }

    #[test]
    fn download_address_appends_the_token_and_moves_to_the_video_host() {
        let url = channels_video_url(&media(
            "http://wxapp.tc.qq.com/251/20302/stodownload?encfilekey=a%2Bb&idx=1",
            "&token=t%3D1&web=1",
            1,
            None,
        ))
        .unwrap();
        assert_eq!(
            url.as_str(),
            "https://finder.video.qq.com/251/20302/stodownload?encfilekey=a%2Bb&idx=1&token=t%3D1&web=1"
        );
        for bad in [
            "http://example.invalid/251/stodownload?x=1",
            "http://wxapp.tc.qq.com:8080/x",
            "http://user@wxapp.tc.qq.com/x",
            "ftp://wxapp.tc.qq.com/x",
            "not a url",
        ] {
            assert!(
                channels_video_url(&media(bad, "", 1, None)).is_err(),
                "{bad}"
            );
        }
    }

    fn serve(
        head: String,
        body: Vec<u8>,
    ) -> (reqwest::Url, SocketAddr, std::thread::JoinHandle<String>) {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let address = listener.local_addr().unwrap();
        let server = std::thread::spawn(move || {
            let (mut stream, _) = listener.accept().unwrap();
            stream
                .set_read_timeout(Some(Duration::from_secs(2)))
                .unwrap();
            let mut request = vec![];
            let mut byte = [0];
            while !request.ends_with(b"\r\n\r\n") {
                stream.read_exact(&mut byte).unwrap();
                request.push(byte[0]);
            }
            let _ = stream.write_all(head.as_bytes());
            let _ = stream.write_all(&body);
            String::from_utf8(request).unwrap()
        });
        let url =
            reqwest::Url::parse(&format!("http://127.0.0.1:{}/video?x=1", address.port())).unwrap();
        (url, address, server)
    }

    #[test]
    fn decodes_the_prefix_and_checks_length_type_and_redirects() {
        let mut plain: Vec<u8> = (0..CHANNELS_PREFIX_BYTES + 1000)
            .map(|i| (i % 253) as u8)
            .collect();
        plain[..8].copy_from_slice(&[0, 0, 0, 32, b'f', b't', b'y', b'p']);
        let mut encrypted = plain.clone();
        let stream = channels_prefix_keystream(42);
        apply_channels_prefix(&mut encrypted, 0, &stream);
        let ok = format!(
            "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: video/mp4\r\nX-ErrNo: 0\r\nContent-Length: {}\r\n\r\n",
            encrypted.len()
        );
        let (url, address, server) = serve(ok.clone(), encrypted.clone());
        let size = Some(encrypted.len() as u64);
        let mut seen = 0;
        let video = download_at(
            url,
            address,
            &media("u", "", 42, size),
            usize::MAX,
            |done, _| {
                seen = done;
                Ok(())
            },
        )
        .unwrap();
        assert_eq!(video.bytes, plain);
        assert_eq!(video.facts.received_bytes, plain.len() as u64);
        assert_eq!(video.facts.content_length, size);
        assert_eq!(video.facts.content_type.as_deref(), Some("video/mp4"));
        assert_eq!(video.facts.decoded_box.as_deref(), Some("ftyp"));
        assert_ne!(video.facts.encoded_box.as_deref(), Some("ftyp"));
        assert_eq!(seen, plain.len() as u64);
        let request = server.join().unwrap();
        assert!(
            request.contains("referer: https://channels.weixin.qq.com/\r\n")
                || request.contains("Referer: https://channels.weixin.qq.com/\r\n")
        );

        let cases = [
            ("HTTP/1.1 302 Found\r\nConnection: close\r\nLocation: http://127.0.0.1/x\r\nContent-Length: 0\r\n\r\n".to_string(), vec![], size, "CDN HTTP status 302"),
            ("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n".to_string(), vec![], size, "CDN HTTP status 403"),
            ("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\nContent-Length: 4\r\n\r\n".to_string(), b"<ht>".to_vec(), None, "channels_download_failed"),
            ("HTTP/1.1 200 OK\r\nConnection: close\r\nX-ErrNo: -5103144\r\nContent-Length: 4\r\n\r\n".to_string(), b"abcd".to_vec(), None, "channels_download_failed"),
            ("HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: video/mp4\r\n\r\n".to_string(), vec![1; 64], Some(32), "channels_media_too_large"),
        ];
        for (head, body, advertised, expected) in cases {
            let cap = if expected == "channels_media_too_large" {
                32
            } else {
                usize::MAX
            };
            let (url, address, server) = serve(head, body);
            let error = download_at(url, address, &media("u", "", 1, advertised), cap, |_, _| {
                Ok(())
            })
            .unwrap_err();
            assert_eq!(error.to_string(), expected);
            let _ = server.join();
        }
    }

    #[test]
    fn a_different_advertised_size_is_recorded_and_a_short_body_is_a_length_failure() {
        let head = "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: video/mp4\r\nContent-Length: 16\r\n\r\n";
        let (url, address, server) = serve(head.into(), vec![7; 16]);
        let video = download_at(
            url,
            address,
            &media("u", "", 1, Some(99)),
            usize::MAX,
            |_, _| Ok(()),
        )
        .unwrap();
        let _ = server.join();
        assert_eq!(video.facts.advertised_bytes, Some(99));
        assert_eq!(video.facts.received_bytes, 16);

        let (url, address, server) = serve(
            "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: video/mp4\r\n\r\n".into(),
            vec![],
        );
        let error = download_at(
            url,
            address,
            &media("u", "", 1, None),
            usize::MAX,
            |_, _| Ok(()),
        )
        .unwrap_err();
        let _ = server.join();
        let failure = error.downcast_ref::<ChannelsDownloadFailure>().unwrap();
        assert_eq!(failure.check, "empty");
        assert_eq!(error.to_string(), "channels_integrity_failed");
    }

    #[test]
    fn an_oversized_advertised_size_fails_before_connecting() {
        let address: SocketAddr = "127.0.0.1:9".parse().unwrap();
        let url = reqwest::Url::parse("http://127.0.0.1:9/v").unwrap();
        let error = download_at(url, address, &media("u", "", 1, Some(100)), 99, |_, _| {
            Ok(())
        })
        .unwrap_err();
        assert_eq!(error.to_string(), "channels_media_too_large");
    }
}