Skip to main content

wechat_protocol/
channels_video.rs

1//! Download and decode one WeChat Channels video from freshly resolved playback material.
2//!
3//! The address and token come from the authenticated detail reply and are used once. The request
4//! goes only to the Channels video host, pinned to a checked public address, without redirects or
5//! proxies. The first 128 KiB are decoded while bytes arrive; the rest is stored as received.
6use anyhow::{ensure, Result};
7use std::{io::Read, net::SocketAddr, time::Duration};
8use wechat_media::channels::{apply_channels_prefix, channels_prefix_keystream};
9
10/// Upper bound on a Channels video download.
11pub const MAX_CHANNELS_VIDEO_BYTES: usize = super::MAX_VIDEO_DOWNLOAD_BYTES;
12const VIDEO_HOST: &str = "finder.video.qq.com";
13/// Host the detail reply uses for the same objects; the player rewrites it to [`VIDEO_HOST`].
14const LEGACY_HOST: &str = "wxapp.tc.qq.com";
15const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 MicroMessenger";
16const REFERER: &str = "https://channels.weixin.qq.com/";
17
18/// What the CDN delivered, kept for checks and failure diagnostics. Holds no address, token or
19/// key.
20#[derive(Debug, Clone, Default, PartialEq, Eq)]
21pub struct ChannelsDownloadFacts {
22    /// Video size the detail reply advertised, when present.
23    pub advertised_bytes: Option<u64>,
24    /// CDN copy size the detail reply advertised, when present.
25    pub advertised_cdn_bytes: Option<u64>,
26    /// `Content-Length` of the response, when present.
27    pub content_length: Option<u64>,
28    /// Bytes actually received.
29    pub received_bytes: u64,
30    /// Media type of the response, when it is a plain `type/subtype`.
31    pub content_type: Option<String>,
32    /// Box type at offset 4 as received, when printable.
33    pub encoded_box: Option<String>,
34    /// Box type at offset 4 after decoding the prefix, when printable.
35    pub decoded_box: Option<String>,
36}
37
38/// A downloaded Channels video: the decoded bytes and what the CDN delivered.
39pub struct ChannelsVideo {
40    /// Decoded MP4 bytes.
41    pub bytes: Vec<u8>,
42    /// Sizes, media type and box types observed during the download.
43    pub facts: ChannelsDownloadFacts,
44}
45
46impl std::fmt::Debug for ChannelsVideo {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        f.debug_struct("ChannelsVideo")
49            .field("bytes", &self.bytes.len())
50            .field("facts", &self.facts)
51            .finish()
52    }
53}
54
55/// A downloaded video failed a check. Its message is the stable code
56/// `channels_integrity_failed`; `check` names which check and `facts` say what arrived.
57#[derive(Debug, Clone)]
58pub struct ChannelsDownloadFailure {
59    /// `empty`, `length` (fewer or more bytes than `Content-Length`) or `container`.
60    pub check: &'static str,
61    /// What the CDN delivered.
62    pub facts: ChannelsDownloadFacts,
63}
64
65impl std::fmt::Display for ChannelsDownloadFailure {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        f.write_str("channels_integrity_failed")
68    }
69}
70
71impl std::error::Error for ChannelsDownloadFailure {}
72
73/// The four-byte box type at offset 4, when it is printable ASCII.
74pub fn channels_box_type(bytes: &[u8]) -> Option<String> {
75    let kind = bytes.get(4..8)?;
76    kind.iter()
77        .all(|b| b.is_ascii_alphanumeric() || *b == b' ')
78        .then(|| String::from_utf8_lossy(kind).into_owned())
79}
80
81/// Build the download address the way the official player does: the returned address with its
82/// token appended, over HTTPS, on the video host.
83pub fn channels_video_url(media: &crate::ChannelsMedia) -> Result<reqwest::Url> {
84    let (url, token) = media.transport();
85    let mut address = reqwest::Url::parse(&format!("{url}{token}"))
86        .map_err(|_| anyhow::anyhow!("channels_unsupported_media"))?;
87    ensure!(
88        matches!(address.scheme(), "http" | "https")
89            && address.username().is_empty()
90            && address.password().is_none()
91            && address.port().is_none()
92            && address.fragment().is_none(),
93        "channels_unsupported_media"
94    );
95    match address.host_str() {
96        Some(LEGACY_HOST) => address
97            .set_host(Some(VIDEO_HOST))
98            .map_err(|_| anyhow::anyhow!("channels_unsupported_media"))?,
99        Some(VIDEO_HOST) => {}
100        _ => anyhow::bail!("channels_unsupported_media"),
101    }
102    address
103        .set_scheme("https")
104        .map_err(|_| anyhow::anyhow!("channels_unsupported_media"))?;
105    Ok(address)
106}
107
108/// Download the post's video and return the decoded MP4 bytes.
109///
110/// `max_bytes` caps the transfer below [`MAX_CHANNELS_VIDEO_BYTES`]. The advertised size, when
111/// the reply gives one, must fit the cap and match the received length.
112pub fn fetch_channels_video(
113    media: &crate::ChannelsMedia,
114    max_bytes: usize,
115    progress: impl FnMut(u64, Option<u64>) -> Result<()>,
116) -> Result<ChannelsVideo> {
117    let url = channels_video_url(media)?;
118    let address = super::link_thumbnail::public_address(
119        &url,
120        "channels_download_failed",
121        "channels_download_failed",
122    )?;
123    download_at(url, address, media, max_bytes, progress)
124}
125
126fn timeout_for(expected: Option<u64>) -> Duration {
127    // At least two minutes, plus one second per MiB, at most thirty minutes.
128    let seconds = expected.map_or(0, |bytes| bytes / (1024 * 1024));
129    Duration::from_secs((120 + seconds).min(30 * 60))
130}
131
132fn download_at(
133    url: reqwest::Url,
134    address: SocketAddr,
135    media: &crate::ChannelsMedia,
136    max_bytes: usize,
137    mut progress: impl FnMut(u64, Option<u64>) -> Result<()>,
138) -> Result<ChannelsVideo> {
139    let cap = max_bytes.min(MAX_CHANNELS_VIDEO_BYTES) as u64;
140    let advertised = [media.file_size, media.cdn_file_size];
141    ensure!(
142        advertised.iter().flatten().all(|size| *size <= cap),
143        "channels_media_too_large"
144    );
145    let host = url.host_str().unwrap_or_default().to_owned();
146    let client = reqwest::blocking::Client::builder()
147        .timeout(timeout_for(media.file_size.or(media.cdn_file_size)))
148        .connect_timeout(Duration::from_secs(15))
149        .redirect(reqwest::redirect::Policy::none())
150        .no_proxy()
151        .resolve(&host, address)
152        .build()?;
153    let mut response = client
154        .get(url)
155        .header("Accept-Encoding", "identity")
156        .header("User-Agent", USER_AGENT)
157        .header("Referer", REFERER)
158        .send()
159        .map_err(|_| anyhow::anyhow!("channels_download_failed"))?;
160    if response.status() != 200 {
161        anyhow::bail!("CDN HTTP status {}", response.status().as_u16());
162    }
163    let header = |name: &str| {
164        response
165            .headers()
166            .get(name)
167            .and_then(|value| value.to_str().ok())
168            .map(str::to_ascii_lowercase)
169    };
170    ensure!(
171        header("x-errno").is_none_or(|errno| errno.trim() == "0"),
172        "channels_download_failed"
173    );
174    ensure!(
175        header("content-encoding").is_none_or(|v| v == "identity"),
176        "channels_download_failed"
177    );
178    ensure!(
179        header("content-type").is_none_or(
180            |kind| kind.starts_with("video/") || kind.starts_with("application/octet-stream")
181        ),
182        "channels_download_failed"
183    );
184    let content_type = header("content-type")
185        .map(|kind| kind.split(';').next().unwrap_or_default().trim().to_owned())
186        .filter(|kind| {
187            kind.len() <= 64
188                && kind.contains('/')
189                && kind
190                    .bytes()
191                    .all(|b| b.is_ascii_alphanumeric() || b"/.+-".contains(&b))
192        });
193    let total = response.content_length();
194    if let Some(length) = total {
195        ensure!(length <= cap, "channels_media_too_large");
196    }
197    let stream = channels_prefix_keystream(media.decode_key());
198    let mut bytes = Vec::with_capacity(total.unwrap_or(0).min(cap) as usize);
199    let mut chunk = vec![0u8; 64 * 1024];
200    let mut head = Vec::with_capacity(8);
201    loop {
202        progress(bytes.len() as u64, total)?;
203        let n = response
204            .read(&mut chunk)
205            .map_err(|_| anyhow::anyhow!("channels_download_failed"))?;
206        if n == 0 {
207            break;
208        }
209        ensure!((bytes.len() + n) as u64 <= cap, "channels_media_too_large");
210        if head.len() < 8 {
211            let wanted = (8 - head.len()).min(n);
212            head.extend_from_slice(&chunk[..wanted]);
213        }
214        let offset = bytes.len() as u64;
215        apply_channels_prefix(&mut chunk[..n], offset, &stream);
216        bytes.extend_from_slice(&chunk[..n]);
217    }
218    let facts = ChannelsDownloadFacts {
219        advertised_bytes: media.file_size,
220        advertised_cdn_bytes: media.cdn_file_size,
221        content_length: total,
222        received_bytes: bytes.len() as u64,
223        content_type,
224        encoded_box: channels_box_type(&head),
225        decoded_box: channels_box_type(&bytes),
226    };
227    // An advertised size that differs is recorded, not fatal: the container check decides
228    // whether the bytes are the expected video.
229    let check = if bytes.is_empty() {
230        Some("empty")
231    } else if total.is_some_and(|n| n != facts.received_bytes) {
232        Some("length")
233    } else {
234        None
235    };
236    if let Some(check) = check {
237        return Err(ChannelsDownloadFailure { check, facts }.into());
238    }
239    Ok(ChannelsVideo { bytes, facts })
240}
241
242#[cfg(test)]
243mod tests {
244    use super::*;
245    use std::{io::Write, net::TcpListener};
246    use wechat_media::channels::CHANNELS_PREFIX_BYTES;
247
248    fn media(url: &str, token: &str, key: u64, size: Option<u64>) -> crate::ChannelsMedia {
249        crate::ChannelsMedia::for_tests(url, token, key, size)
250    }
251
252    #[test]
253    fn download_address_appends_the_token_and_moves_to_the_video_host() {
254        let url = channels_video_url(&media(
255            "http://wxapp.tc.qq.com/251/20302/stodownload?encfilekey=a%2Bb&idx=1",
256            "&token=t%3D1&web=1",
257            1,
258            None,
259        ))
260        .unwrap();
261        assert_eq!(
262            url.as_str(),
263            "https://finder.video.qq.com/251/20302/stodownload?encfilekey=a%2Bb&idx=1&token=t%3D1&web=1"
264        );
265        for bad in [
266            "http://example.invalid/251/stodownload?x=1",
267            "http://wxapp.tc.qq.com:8080/x",
268            "http://user@wxapp.tc.qq.com/x",
269            "ftp://wxapp.tc.qq.com/x",
270            "not a url",
271        ] {
272            assert!(
273                channels_video_url(&media(bad, "", 1, None)).is_err(),
274                "{bad}"
275            );
276        }
277    }
278
279    fn serve(
280        head: String,
281        body: Vec<u8>,
282    ) -> (reqwest::Url, SocketAddr, std::thread::JoinHandle<String>) {
283        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
284        let address = listener.local_addr().unwrap();
285        let server = std::thread::spawn(move || {
286            let (mut stream, _) = listener.accept().unwrap();
287            stream
288                .set_read_timeout(Some(Duration::from_secs(2)))
289                .unwrap();
290            let mut request = vec![];
291            let mut byte = [0];
292            while !request.ends_with(b"\r\n\r\n") {
293                stream.read_exact(&mut byte).unwrap();
294                request.push(byte[0]);
295            }
296            let _ = stream.write_all(head.as_bytes());
297            let _ = stream.write_all(&body);
298            String::from_utf8(request).unwrap()
299        });
300        let url =
301            reqwest::Url::parse(&format!("http://127.0.0.1:{}/video?x=1", address.port())).unwrap();
302        (url, address, server)
303    }
304
305    #[test]
306    fn decodes_the_prefix_and_checks_length_type_and_redirects() {
307        let mut plain: Vec<u8> = (0..CHANNELS_PREFIX_BYTES + 1000)
308            .map(|i| (i % 253) as u8)
309            .collect();
310        plain[..8].copy_from_slice(&[0, 0, 0, 32, b'f', b't', b'y', b'p']);
311        let mut encrypted = plain.clone();
312        let stream = channels_prefix_keystream(42);
313        apply_channels_prefix(&mut encrypted, 0, &stream);
314        let ok = format!(
315            "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: video/mp4\r\nX-ErrNo: 0\r\nContent-Length: {}\r\n\r\n",
316            encrypted.len()
317        );
318        let (url, address, server) = serve(ok.clone(), encrypted.clone());
319        let size = Some(encrypted.len() as u64);
320        let mut seen = 0;
321        let video = download_at(
322            url,
323            address,
324            &media("u", "", 42, size),
325            usize::MAX,
326            |done, _| {
327                seen = done;
328                Ok(())
329            },
330        )
331        .unwrap();
332        assert_eq!(video.bytes, plain);
333        assert_eq!(video.facts.received_bytes, plain.len() as u64);
334        assert_eq!(video.facts.content_length, size);
335        assert_eq!(video.facts.content_type.as_deref(), Some("video/mp4"));
336        assert_eq!(video.facts.decoded_box.as_deref(), Some("ftyp"));
337        assert_ne!(video.facts.encoded_box.as_deref(), Some("ftyp"));
338        assert_eq!(seen, plain.len() as u64);
339        let request = server.join().unwrap();
340        assert!(
341            request.contains("referer: https://channels.weixin.qq.com/\r\n")
342                || request.contains("Referer: https://channels.weixin.qq.com/\r\n")
343        );
344
345        let cases = [
346            ("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"),
347            ("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n".to_string(), vec![], size, "CDN HTTP status 403"),
348            ("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"),
349            ("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"),
350            ("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"),
351        ];
352        for (head, body, advertised, expected) in cases {
353            let cap = if expected == "channels_media_too_large" {
354                32
355            } else {
356                usize::MAX
357            };
358            let (url, address, server) = serve(head, body);
359            let error = download_at(url, address, &media("u", "", 1, advertised), cap, |_, _| {
360                Ok(())
361            })
362            .unwrap_err();
363            assert_eq!(error.to_string(), expected);
364            let _ = server.join();
365        }
366    }
367
368    #[test]
369    fn a_different_advertised_size_is_recorded_and_a_short_body_is_a_length_failure() {
370        let head = "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: video/mp4\r\nContent-Length: 16\r\n\r\n";
371        let (url, address, server) = serve(head.into(), vec![7; 16]);
372        let video = download_at(
373            url,
374            address,
375            &media("u", "", 1, Some(99)),
376            usize::MAX,
377            |_, _| Ok(()),
378        )
379        .unwrap();
380        let _ = server.join();
381        assert_eq!(video.facts.advertised_bytes, Some(99));
382        assert_eq!(video.facts.received_bytes, 16);
383
384        let (url, address, server) = serve(
385            "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: video/mp4\r\n\r\n".into(),
386            vec![],
387        );
388        let error = download_at(
389            url,
390            address,
391            &media("u", "", 1, None),
392            usize::MAX,
393            |_, _| Ok(()),
394        )
395        .unwrap_err();
396        let _ = server.join();
397        let failure = error.downcast_ref::<ChannelsDownloadFailure>().unwrap();
398        assert_eq!(failure.check, "empty");
399        assert_eq!(error.to_string(), "channels_integrity_failed");
400    }
401
402    #[test]
403    fn an_oversized_advertised_size_fails_before_connecting() {
404        let address: SocketAddr = "127.0.0.1:9".parse().unwrap();
405        let url = reqwest::Url::parse("http://127.0.0.1:9/v").unwrap();
406        let error = download_at(url, address, &media("u", "", 1, Some(100)), 99, |_, _| {
407            Ok(())
408        })
409        .unwrap_err();
410        assert_eq!(error.to_string(), "channels_media_too_large");
411    }
412}