1mod account;
7mod acw;
8mod config;
9mod error;
10mod notes;
11
12pub use account::{
13 is_upload_allowed_ext, unescape_list, Account, DisplayEntry, EntryKind, FileInfo, FolderInfo,
14 ListEntry, UploadPart, UploadResult,
15};
16pub use acw::{calc_acw_sc_v2, extract_arg1, is_acw_challenge, solve_from_html, MASK, PERM};
17pub use config::{
18 config_keys, config_path_used, default_config_path, format_split_name, get_config,
19 get_config_value, load_config, save_config, set_config_cache, set_config_value, Config,
20};
21pub use notes::{
22 clean_share_desc, extract_share_description, format_convert_note, format_part_note,
23 format_raw_note, parse_convert_note, parse_file_note, parse_part_note, ConvertMeta,
24 FileNote, PartMeta, NOTE_VERSION,
25};
26pub const VERSION: &str = env!("CARGO_PKG_VERSION");
28pub use error::{Error, Result};
29
30use regex::Regex;
31use reqwest::blocking::Client as HttpClient;
32use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, ACCEPT_LANGUAGE, CONTENT_TYPE, COOKIE, REFERER, SET_COOKIE, USER_AGENT};
33use reqwest::redirect::Policy;
34use serde::Deserialize;
35use std::collections::HashMap;
36use std::fs::File;
37use std::io::{Read, Write};
38use std::path::{Path, PathBuf};
39use std::thread;
40use std::time::Duration;
41use url::Url;
42
43const DEFAULT_UA: &str =
44 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
45const CDN_RISK_WAIT: Duration = Duration::from_millis(2200);
46
47#[derive(Debug, Clone)]
49pub struct ParseOptions {
50 pub password: Option<String>,
51 pub resolve_direct: bool,
52}
53
54impl Default for ParseOptions {
55 fn default() -> Self {
56 Self {
57 password: None,
58 resolve_direct: true,
59 }
60 }
61}
62
63impl ParseOptions {
64 pub fn new() -> Self {
65 Self::default()
66 }
67
68 pub fn password(mut self, pwd: impl Into<String>) -> Self {
69 self.password = Some(pwd.into());
70 self
71 }
72
73 pub fn resolve_direct(mut self, v: bool) -> Self {
74 self.resolve_direct = v;
75 self
76 }
77}
78
79#[derive(Debug, Clone)]
81pub struct ParseResult {
82 pub fid: String,
83 pub filename: Option<String>,
84 pub password_protected: bool,
85 pub cdn_domain: String,
86 pub telecom: String,
87 pub unicom: String,
88 pub normal: String,
89 pub direct: Option<String>,
90 pub saved_path: Option<PathBuf>,
91 pub description: String,
93 pub note_kind: String,
95 pub orig_name: String,
97}
98
99pub struct Client {
101 http: HttpClient,
102 cookies: HashMap<String, String>,
103 origin: String,
104 last_filename: Option<String>,
105}
106
107impl Client {
108 pub fn new() -> Result<Self> {
109 let mut headers = HeaderMap::new();
110 headers.insert(USER_AGENT, HeaderValue::from_static(DEFAULT_UA));
111 headers.insert(
112 ACCEPT,
113 HeaderValue::from_static(
114 "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
115 ),
116 );
117 headers.insert(
118 ACCEPT_LANGUAGE,
119 HeaderValue::from_static("zh-CN,zh;q=0.9,en;q=0.8"),
120 );
121
122 let http = HttpClient::builder()
123 .default_headers(headers)
124 .redirect(Policy::limited(10))
125 .timeout(Duration::from_secs(30))
126 .build()?;
127
128 Ok(Self {
129 http,
130 cookies: HashMap::new(),
131 origin: String::new(),
132 last_filename: None,
133 })
134 }
135
136 fn origin_of(url: &str) -> Result<String> {
137 let u = Url::parse(url)?;
138 Ok(format!(
139 "{}://{}",
140 u.scheme(),
141 u.host_str().unwrap_or_default()
142 ))
143 }
144
145 fn store_cookies(&mut self, headers: &HeaderMap) {
146 for val in headers.get_all(SET_COOKIE) {
147 if let Ok(s) = val.to_str() {
148 if let Some(pair) = s.split(';').next() {
149 if let Some((k, v)) = pair.split_once('=') {
150 self.cookies
151 .insert(k.trim().to_string(), v.trim().to_string());
152 }
153 }
154 }
155 }
156 }
157
158 fn cookie_header(&self) -> Option<String> {
159 if self.cookies.is_empty() {
160 return None;
161 }
162 Some(
163 self.cookies
164 .iter()
165 .map(|(k, v)| format!("{k}={v}"))
166 .collect::<Vec<_>>()
167 .join("; "),
168 )
169 }
170
171 fn get_html(&mut self, url: &str, referer: Option<&str>) -> Result<String> {
172 let mut req = self.http.get(url);
173 if let Some(r) = referer {
174 req = req.header(REFERER, r);
175 }
176 if let Some(c) = self.cookie_header() {
177 req = req.header(COOKIE, c);
178 }
179 let resp = req.send()?;
180 self.store_cookies(resp.headers());
181 if !resp.status().is_success() {
182 return Err(Error::Http(format!("GET {} -> {}", url, resp.status())));
183 }
184 let mut text = resp.text()?;
185
186 if is_acw_challenge(&text) {
187 let (_arg1, token) = solve_from_html(&text)?;
188 self.cookies
189 .insert("acw_sc__v2".into(), token);
190 let mut req2 = self.http.get(url);
191 if let Some(r) = referer {
192 req2 = req2.header(REFERER, r);
193 }
194 if let Some(c) = self.cookie_header() {
195 req2 = req2.header(COOKIE, c);
196 }
197 let resp2 = req2.send()?;
198 self.store_cookies(resp2.headers());
199 if !resp2.status().is_success() {
200 return Err(Error::Http(format!(
201 "GET {} after acw -> {}",
202 url,
203 resp2.status()
204 )));
205 }
206 text = resp2.text()?;
207 if is_acw_challenge(&text) {
208 return Err(Error::Acw("still on challenge page after cookie".into()));
209 }
210 }
211 Ok(text)
212 }
213
214 pub fn is_password_protected(html: &str) -> bool {
215 const MARKERS: &[&str] = &[
216 "id=\"passwddiv\"",
217 "id='passwddiv'",
218 "id=\"pwd\"",
219 "function down_p(",
220 "passwddiv-input",
221 "输入密码",
222 ];
223 MARKERS.iter().any(|m| html.contains(m))
224 }
225
226 fn extract_pwd_sign(html: &str) -> Option<String> {
227 let re = Regex::new(r"'sign'\s*:\s*'([^']+)'").ok()?;
228 let mut valid = Vec::new();
229 for cap in re.captures_iter(html) {
230 let s = cap.get(1)?.as_str();
231 if s.len() > 20 && !s.contains('<') && !s.contains('>') {
232 valid.push(s.to_string());
233 }
234 }
235 valid.pop()
236 }
237
238 fn extract_pwd_fid(html: &str) -> Option<String> {
239 let re = Regex::new(r"ajaxm\.php\?file=(\d+)").ok()?;
240 let mut last = None;
241 for cap in re.captures_iter(html) {
242 last = cap.get(1).map(|m| m.as_str().to_string());
243 }
244 if last.is_some() {
245 return last;
246 }
247 let re2 = Regex::new(r"[?&]f=(\d+)").ok()?;
248 re2.captures(html)
249 .and_then(|c| c.get(1).map(|m| m.as_str().to_string()))
250 }
251
252 fn re_first(html: &str, pat: &str) -> Option<String> {
253 Regex::new(pat)
254 .ok()?
255 .captures(html)
256 .and_then(|c| c.get(1).map(|m| m.as_str().to_string()))
257 }
258
259 pub fn parse(&mut self, share_url: &str, opts: ParseOptions) -> Result<ParseResult> {
261 self.last_filename = None;
262 self.origin = Self::origin_of(share_url)?;
263
264 let html = self.get_html(share_url, None)?;
265
266 let (fid, links, password_protected) = if Self::is_password_protected(&html) {
267 let password = opts
268 .password
269 .as_deref()
270 .filter(|s| !s.is_empty())
271 .ok_or(Error::PasswordRequired)?;
272
273 let fid = Self::extract_pwd_fid(&html)
274 .ok_or_else(|| Error::Parse("password page: missing fid".into()))?;
275 let sign = Self::extract_pwd_sign(&html)
276 .ok_or_else(|| Error::Parse("password page: missing sign".into()))?;
277 let mut kdns =
278 Self::re_first(&html, r"var\s+kdns\s*=\s*(\d+)").unwrap_or_else(|| "1".into());
279 if let Some(k) = Self::re_first(&html, r"var\s+killdns\s*=\s*(\d+)") {
280 kdns = k;
281 }
282 let links = self.ajaxm_password(&fid, &sign, &kdns, password, share_url)?;
283 (fid, links, true)
284 } else {
285 let fid = Self::re_first(&html, r"var\s+fid\s*=\s*(\d+)\s*;")
286 .or_else(|| Self::re_first(&html, r#"data-id="(\d+)""#))
287 .or_else(|| Self::re_first(&html, r"fid\s*=\s*'(\d+)'"))
288 .ok_or_else(|| Error::Parse("public page: missing fid".into()))?;
289
290 let fn_path = Self::re_first(&html, r#"iframe[^>]*src="(/fn\?[^"]*)""#)
291 .or_else(|| Self::re_first(&html, r"src='(/fn\?[^']*)'"))
292 .ok_or_else(|| Error::Parse("public page: missing fn path".into()))?;
293
294 if let Some(name) =
295 Self::re_first(&html, r"<title>(.*?)(?:\s*-\s*蓝奏云)?</title>")
296 {
297 let name = name.trim().to_string();
298 if name != "文件" && name != "蓝奏云" {
299 self.last_filename = Some(name);
300 }
301 }
302
303 let fn_url = format!("{}{}", self.origin, fn_path);
304 let fn_html = self.get_html(&fn_url, Some(&format!("{}/", self.origin)))?;
305 let wp_sign = Self::re_first(&fn_html, r"var\s+wp_sign\s*=\s*'([^']*)'")
306 .ok_or_else(|| Error::Parse("fn page: missing wp_sign".into()))?;
307 let ajaxdata = Self::re_first(&fn_html, r"var\s+ajaxdata\s*=\s*'([^']*)'")
308 .ok_or_else(|| Error::Parse("fn page: missing ajaxdata".into()))?;
309 let mut kdns =
310 Self::re_first(&fn_html, r"var\s+kdns\s*=\s*(\d+)").unwrap_or_else(|| "1".into());
311 if let Some(k) = Self::re_first(&fn_html, r"var\s+killdns\s*=\s*(\d+)") {
312 kdns = k;
313 }
314
315 let links = self.ajaxm_public(&fid, &wp_sign, &ajaxdata, &kdns, &fn_path)?;
316 (fid, links, false)
317 };
318
319 let desc = extract_share_description(&html);
320 let mut note_kind = String::new();
321 let mut orig_name = String::new();
322 if let Some(n) = parse_file_note(&desc) {
323 note_kind = n.kind.clone();
324 orig_name = n.name.clone();
325 }
326
327 let mut result = ParseResult {
328 fid,
329 filename: self.last_filename.clone(),
330 password_protected,
331 cdn_domain: links.dom.clone(),
332 telecom: links.telecom.clone(),
333 unicom: links.unicom.clone(),
334 normal: links.normal.clone(),
335 direct: None,
336 saved_path: None,
337 description: clean_share_desc(&desc),
338 note_kind,
339 orig_name,
340 };
341
342 if opts.resolve_direct {
343 result.direct = Some(
344 self.resolve_direct_url(&links.telecom)
345 .or_else(|_| self.resolve_direct_url(&links.normal))?,
346 );
347 }
348
349 Ok(result)
350 }
351
352 fn ajaxm_public(
353 &mut self,
354 fid: &str,
355 wp_sign: &str,
356 ajaxdata: &str,
357 kdns: &str,
358 fn_path: &str,
359 ) -> Result<LinkSet> {
360 let api = format!("{}/ajaxm.php?file={fid}", self.origin);
361 let body = [
362 ("action", "downprocess"),
363 ("websignkey", ajaxdata),
364 ("signs", ajaxdata),
365 ("sign", wp_sign),
366 ("websign", ""),
367 ("kd", kdns),
368 ("ves", "1"),
369 ];
370 let referer = format!("{}{}", self.origin, fn_path);
371 self.post_ajaxm(&api, &body, &referer, kdns)
372 }
373
374 fn ajaxm_password(
375 &mut self,
376 fid: &str,
377 sign: &str,
378 kdns: &str,
379 password: &str,
380 share_url: &str,
381 ) -> Result<LinkSet> {
382 let api = format!("{}/ajaxm.php?file={fid}", self.origin);
383 let body = [
384 ("action", "downprocess"),
385 ("sign", sign),
386 ("kd", kdns),
387 ("p", password),
388 ];
389 self.post_ajaxm(&api, &body, share_url, kdns)
390 }
391
392 fn post_ajaxm(
393 &mut self,
394 api: &str,
395 body: &[(&str, &str)],
396 referer: &str,
397 kdns: &str,
398 ) -> Result<LinkSet> {
399 let mut req = self
400 .http
401 .post(api)
402 .header(CONTENT_TYPE, "application/x-www-form-urlencoded")
403 .header(REFERER, referer)
404 .header("Origin", &self.origin)
405 .header("X-Requested-With", "XMLHttpRequest")
406 .form(body);
407 if let Some(c) = self.cookie_header() {
408 req = req.header(COOKIE, c);
409 }
410 let resp = req.send()?;
411 self.store_cookies(resp.headers());
412 let status = resp.status();
413 let raw = resp
414 .text()
415 .map_err(|e| Error::Parse(format!("ajaxm read: {e}")))?;
416 if !status.is_success() {
417 return Err(Error::Http(format!(
418 "ajaxm {api} -> {status} body={}",
419 &raw[..raw.len().min(200)]
420 )));
421 }
422 let data: AjaxmResp = serde_json::from_str(&raw).map_err(|e| {
423 Error::Parse(format!(
424 "ajaxm non-json: {e}; body={}",
425 &raw[..raw.len().min(300)]
426 ))
427 })?;
428 if data.zt_string() != "1" {
429 return Err(Error::Parse(format!(
430 "ajaxm failed zt={} info={:?}",
431 data.zt_string(),
432 data.inf_string()
433 )));
434 }
435 let mut dom = data
436 .dom_string()
437 .ok_or_else(|| Error::Parse("ajaxm missing dom".into()))?;
438 let url = data
439 .url_string()
440 .ok_or_else(|| Error::Parse("ajaxm missing url".into()))?;
441 if let Some(inf) = data.inf_string() {
442 let inf = inf.trim().to_string();
443 let looks_name = inf.contains('.')
445 || inf.contains('_')
446 || inf.contains('-')
447 || inf.contains(' ')
448 || !inf.chars().all(|c| c.is_ascii_digit());
449 if !inf.is_empty() && inf != "文件" && looks_name {
450 self.last_filename = Some(inf);
451 }
452 }
453 if kdns == "0" {
454 dom = "https://slssctm.dmpdmp.com".into();
455 }
456 let base = format!("{dom}/file/{url}");
457 let normal = if base.contains('?') {
458 format!("{base}&toolsdown")
459 } else {
460 format!("{base}?toolsdown")
461 };
462 Ok(LinkSet {
463 dom,
464 telecom: base.clone(),
465 unicom: base,
466 normal,
467 })
468 }
469
470 pub fn resolve_direct_url(&mut self, cdn_url: &str) -> Result<String> {
476 self.resolve_direct_url_with_referer(cdn_url, None)
477 }
478
479 pub fn resolve_direct_url_with_referer(
480 &mut self,
481 cdn_url: &str,
482 share_referer: Option<&str>,
483 ) -> Result<String> {
484 let referer = share_referer
485 .map(|s| s.to_string())
486 .unwrap_or_else(|| {
487 if self.origin.is_empty() {
488 "https://www.lanzou.com/".to_string()
489 } else {
490 format!("{}/", self.origin)
491 }
492 });
493 let mut req = self
494 .http
495 .get(cdn_url)
496 .header(REFERER, referer)
497 .header(ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9,en;q=0.8")
498 .header(
499 ACCEPT,
500 "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
501 )
502 .header("Sec-Fetch-Dest", "document")
503 .header("Sec-Fetch-Mode", "navigate")
504 .header("Sec-Fetch-Site", "cross-site");
505 if let Some(c) = self.cookie_header() {
506 req = req.header(COOKIE, c);
507 }
508 let resp = req.send()?;
509 self.store_cookies(resp.headers());
510 if !resp.status().is_success() {
511 return Err(Error::Cdn(format!("status {}", resp.status())));
512 }
513 let ct = resp
514 .headers()
515 .get(CONTENT_TYPE)
516 .and_then(|v| v.to_str().ok())
517 .unwrap_or("")
518 .to_string();
519
520 let mut bytes = Vec::new();
521 let mut r = resp;
522 let mut take = [0u8; 512];
523 let n = r.read(&mut take)?;
524 bytes.extend_from_slice(&take[..n]);
525 if looks_like_file(&bytes, &ct) {
526 return Ok(cdn_url.to_string());
527 }
528 r.read_to_end(&mut bytes)?;
529 let text = String::from_utf8_lossy(&bytes);
530 if let Some(code) = extract_cdn_error(&text) {
531 return Err(Error::Cdn(format!(
532 "cdn offline ERROR:{code} (file may be expired/unavailable)"
533 )));
534 }
535 if is_cdn_risk_page(&text) {
536 return self.resolve_via_ajax(cdn_url, &text);
537 }
538 Err(Error::Cdn(format!(
539 "unknown CDN response ct={ct} head={:?}",
540 &bytes[..bytes.len().min(60)]
541 )))
542 }
543
544 fn resolve_via_ajax(&mut self, cdn_url: &str, risk_html: &str) -> Result<String> {
545 let file_token = Self::re_first(risk_html, r"'file'\s*:\s*'([^']+)'")
546 .ok_or_else(|| Error::Cdn("risk page missing file".into()))?;
547 let sign = Self::re_first(risk_html, r"'sign'\s*:\s*'([^']+)'")
548 .ok_or_else(|| Error::Cdn("risk page missing sign".into()))?;
549 let ajax_url = Url::parse(cdn_url)?.join("ajax.php")?;
550 let origin = Self::origin_of(cdn_url)?;
551
552 let mut last_err = String::new();
553 for _ in 0..2 {
554 thread::sleep(CDN_RISK_WAIT);
555 let mut req = self
556 .http
557 .post(ajax_url.as_str())
558 .header(REFERER, cdn_url)
559 .header("Origin", &origin)
560 .header("X-Requested-With", "XMLHttpRequest")
561 .header(
562 CONTENT_TYPE,
563 "application/x-www-form-urlencoded; charset=UTF-8",
564 )
565 .header(ACCEPT, "application/json, text/javascript, */*; q=0.01")
566 .form(&[
567 ("file", file_token.as_str()),
568 ("el", "2"),
569 ("sign", sign.as_str()),
570 ]);
571 if let Some(c) = self.cookie_header() {
572 req = req.header(COOKIE, c);
573 }
574 let resp = req.send()?;
575 self.store_cookies(resp.headers());
576 let data: AjaxRiskResp = match resp.json() {
577 Ok(d) => d,
578 Err(e) => {
579 last_err = format!("non-json: {e}");
580 continue;
581 }
582 };
583 let zt = data.zt_string();
584 let final_url = data.url.unwrap_or_default();
585 if zt == "1" && !final_url.is_empty() && !final_url.starts_with('?') {
586 let final_url = if let Some(rest) = final_url.strip_prefix("//") {
587 format!("https://{rest}")
588 } else if final_url.starts_with('/') {
589 format!("{origin}{final_url}")
590 } else {
591 final_url
592 };
593 return Ok(final_url);
594 }
595 last_err = format!("zt={zt} url={final_url}");
596 }
597 Err(Error::Cdn(format!("ajax risk failed: {last_err}")))
598 }
599
600 pub fn download_share(
603 &mut self,
604 share_url: &str,
605 password: Option<&str>,
606 dest_dir: impl AsRef<Path>,
607 filename: Option<&str>,
608 ) -> Result<PathBuf> {
609 let dest_dir = dest_dir.as_ref();
610 let mut last = Error::Cdn("download share failed".into());
611 for attempt in 1..=3 {
612 if attempt > 1 {
613 thread::sleep(Duration::from_secs(attempt as u64));
614 }
615 let mut nc = Client::new()?;
616 let opts = ParseOptions {
617 password: password.map(|s| s.to_string()),
618 resolve_direct: false,
619 };
620 let res = match nc.parse(share_url, opts) {
621 Ok(r) => r,
622 Err(e) => {
623 last = e;
624 continue;
625 }
626 };
627 let name = filename
628 .map(|s| s.to_string())
629 .or(res.filename.clone());
630 for cdn in [&res.telecom, &res.normal] {
631 if cdn.is_empty() {
632 continue;
633 }
634 match nc.download_cdn_stream(cdn, share_url, dest_dir, name.as_deref()) {
635 Ok(p) => return Ok(p),
636 Err(e) => last = e,
637 }
638 }
639 }
640 Err(last)
641 }
642
643 fn download_cdn_stream(
645 &mut self,
646 cdn_url: &str,
647 share_referer: &str,
648 dest_dir: &Path,
649 filename: Option<&str>,
650 ) -> Result<PathBuf> {
651 let mut req = self
652 .http
653 .get(cdn_url)
654 .header(REFERER, share_referer)
655 .header(ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9,en;q=0.8")
656 .header(
657 ACCEPT,
658 "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
659 )
660 .header("Sec-Fetch-Dest", "document")
661 .header("Sec-Fetch-Mode", "navigate")
662 .header("Sec-Fetch-Site", "cross-site")
663 .timeout(Duration::from_secs(120 * 60));
664 if let Some(c) = self.cookie_header() {
665 req = req.header(COOKIE, c);
666 }
667 let resp = req.send()?;
668 self.store_cookies(resp.headers());
669 if !resp.status().is_success() {
670 return Err(Error::Cdn(format!("status {}", resp.status())));
671 }
672 let ct = resp
673 .headers()
674 .get(CONTENT_TYPE)
675 .and_then(|v| v.to_str().ok())
676 .unwrap_or("")
677 .to_string();
678 let content_disp = resp
679 .headers()
680 .get("content-disposition")
681 .cloned();
682 let content_length = resp.content_length().unwrap_or(0);
683
684 let mut head = vec![0u8; 512];
685 let mut r = resp;
686 let n = r.read(&mut head)?;
687 head.truncate(n);
688 if looks_like_file(&head, &ct) {
689 return self.save_download_stream(
691 ChainReader {
692 head: Some(head),
693 rest: r,
694 },
695 dest_dir,
696 filename,
697 content_length,
698 content_disp.as_ref(),
699 cdn_url,
700 );
701 }
702 let mut body = head;
703 r.read_to_end(&mut body)?;
704 let text = String::from_utf8_lossy(&body);
705 if let Some(code) = extract_cdn_error(&text) {
706 return Err(Error::Cdn(format!(
707 "cdn offline ERROR:{code} (file may be expired/unavailable)"
708 )));
709 }
710 if is_cdn_risk_page(&text) {
711 let final_url = self.resolve_via_ajax(cdn_url, &text)?;
712 return self.download(
713 &final_url,
714 dest_dir,
715 filename,
716 Some(share_referer),
717 );
718 }
719 Err(Error::Cdn(format!(
720 "unknown CDN response ct={ct} head={:?}",
721 &body[..body.len().min(60)]
722 )))
723 }
724
725 fn save_download_stream<R: Read>(
726 &mut self,
727 mut reader: R,
728 dest_dir: &Path,
729 filename: Option<&str>,
730 content_length: u64,
731 content_disp: Option<&reqwest::header::HeaderValue>,
732 raw_url: &str,
733 ) -> Result<PathBuf> {
734 std::fs::create_dir_all(dest_dir)?;
735 let name = filename
736 .map(|s| s.to_string())
737 .or_else(|| filename_from_cd(content_disp))
738 .or_else(|| filename_from_url(raw_url))
739 .or_else(|| self.last_filename.clone())
740 .unwrap_or_else(|| format!("lanzou_{}.bin", now_ts()));
741 let name = Path::new(&name)
742 .file_name()
743 .and_then(|s| s.to_str())
744 .unwrap_or("download.bin")
745 .to_string();
746 let mut out_path = dest_dir.join(&name);
747 if out_path.exists() {
748 let stem = out_path
749 .file_stem()
750 .and_then(|s| s.to_str())
751 .unwrap_or("file");
752 let ext = out_path
753 .extension()
754 .and_then(|s| s.to_str())
755 .map(|e| format!(".{e}"))
756 .unwrap_or_default();
757 let mut i = 1;
758 loop {
759 let candidate = dest_dir.join(format!("{stem}({i}){ext}"));
760 if !candidate.exists() {
761 out_path = candidate;
762 break;
763 }
764 i += 1;
765 }
766 }
767 let mut file = File::create(&out_path)?;
768 let mut buf = [0u8; 64 * 1024];
769 let mut read: u64 = 0;
770 let mut last = std::time::Instant::now() - std::time::Duration::from_secs(1);
771 let mut last_pct: i32 = -1;
772 let label = name.clone();
773 let total = content_length;
774 if total == 0 {
775 eprint!("\r[download] {label} ...");
776 }
777 loop {
778 let n = reader.read(&mut buf)?;
779 if n == 0 {
780 break;
781 }
782 file.write_all(&buf[..n])?;
783 read += n as u64;
784 if total > 0 {
785 let now = std::time::Instant::now();
786 let pct_i = (read * 100 / total) as i32;
787 if now.duration_since(last).as_millis() >= 200 || pct_i != last_pct || read >= total {
788 last = now;
789 last_pct = pct_i;
790 let pct = read as f64 * 100.0 / total as f64;
791 eprint!(
792 "\r[download] {label} {pct:5.1}% {}/{} ",
793 human_bytes(read),
794 human_bytes(total)
795 );
796 }
797 }
798 }
799 if total > 0 {
800 eprintln!(
801 "\r[download] {label} 100.0% {}/{} ",
802 human_bytes(total),
803 human_bytes(total)
804 );
805 } else {
806 eprintln!("\r[download] {label} done ");
807 }
808 Ok(out_path)
809 }
810
811 pub fn download(
813 &mut self,
814 url: &str,
815 dest_dir: impl AsRef<Path>,
816 filename: Option<&str>,
817 referer: Option<&str>,
818 ) -> Result<PathBuf> {
819 let dest_dir = dest_dir.as_ref();
820 std::fs::create_dir_all(dest_dir)?;
821
822 let referer = referer.map(|s| s.to_string()).unwrap_or_else(|| {
823 if self.origin.is_empty() {
824 "https://www.lanzou.com/".into()
825 } else {
826 format!("{}/", self.origin)
827 }
828 });
829
830 let mut req = self
831 .http
832 .get(url)
833 .header(REFERER, referer)
834 .header(ACCEPT, "*/*")
835 .header(ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9,en;q=0.8")
836 .timeout(Duration::from_secs(120));
837 if let Some(c) = self.cookie_header() {
838 req = req.header(COOKIE, c);
839 }
840 let resp = req.send()?;
841 self.store_cookies(resp.headers());
842 if !resp.status().is_success() {
843 return Err(Error::Http(format!("download status {}", resp.status())));
844 }
845
846 let name = filename
847 .map(|s| s.to_string())
848 .or_else(|| filename_from_cd(resp.headers().get("content-disposition")))
849 .or_else(|| filename_from_url(url))
850 .or_else(|| self.last_filename.clone())
851 .unwrap_or_else(|| format!("lanzou_{}.bin", now_ts()));
852
853 let name = Path::new(&name)
854 .file_name()
855 .and_then(|s| s.to_str())
856 .unwrap_or("download.bin")
857 .to_string();
858
859 let mut out_path = dest_dir.join(&name);
860 if out_path.exists() {
861 let stem = out_path
862 .file_stem()
863 .and_then(|s| s.to_str())
864 .unwrap_or("file");
865 let ext = out_path
866 .extension()
867 .and_then(|s| s.to_str())
868 .map(|e| format!(".{e}"))
869 .unwrap_or_default();
870 let mut i = 1;
871 loop {
872 let candidate = dest_dir.join(format!("{stem}({i}){ext}"));
873 if !candidate.exists() {
874 out_path = candidate;
875 break;
876 }
877 i += 1;
878 }
879 }
880
881 let total = resp.content_length().unwrap_or(0);
882 let mut file = File::create(&out_path)?;
883 let mut reader = resp;
884 let mut buf = [0u8; 64 * 1024];
885 let mut read: u64 = 0;
886 let mut last = std::time::Instant::now() - std::time::Duration::from_secs(1);
887 let mut last_pct: i32 = -1;
888 let label = name.clone();
889 if total == 0 {
890 eprint!("\r[download] {label} ...");
891 }
892 loop {
893 let n = reader.read(&mut buf)?;
894 if n == 0 {
895 break;
896 }
897 file.write_all(&buf[..n])?;
898 read += n as u64;
899 if total > 0 {
900 let now = std::time::Instant::now();
901 let pct_i = (read * 100 / total) as i32;
902 if now.duration_since(last).as_millis() >= 200 || pct_i != last_pct || read >= total {
903 last = now;
904 last_pct = pct_i;
905 let pct = read as f64 * 100.0 / total as f64;
906 eprint!(
907 "\r[download] {label} {pct:5.1}% {}/{} ",
908 human_bytes(read),
909 human_bytes(total)
910 );
911 }
912 }
913 }
914 if total > 0 {
915 eprintln!(
916 "\r[download] {label} 100.0% {}/{} ",
917 human_bytes(total),
918 human_bytes(total)
919 );
920 } else {
921 eprintln!("\r[download] {label} done ");
922 }
923 Ok(out_path)
924 }
925}
926
927#[derive(Debug)]
928struct LinkSet {
929 dom: String,
930 telecom: String,
931 unicom: String,
932 normal: String,
933}
934
935#[derive(Debug, Deserialize)]
936struct AjaxmResp {
937 #[serde(default)]
938 zt: serde_json::Value,
939 #[serde(default)]
940 dom: serde_json::Value,
941 #[serde(default)]
942 url: serde_json::Value,
943 #[serde(default)]
944 inf: serde_json::Value,
945}
946
947impl AjaxmResp {
948 fn zt_string(&self) -> String {
949 json_to_string(&self.zt)
950 }
951
952 fn dom_string(&self) -> Option<String> {
953 let s = json_to_string(&self.dom);
954 if s.is_empty() || s == "null" {
955 None
956 } else {
957 Some(s)
958 }
959 }
960
961 fn url_string(&self) -> Option<String> {
962 let s = json_to_string(&self.url);
963 if s.is_empty() || s == "null" {
964 None
965 } else {
966 Some(s)
967 }
968 }
969
970 fn inf_string(&self) -> Option<String> {
971 let s = json_to_string(&self.inf);
972 if s.is_empty() || s == "null" {
973 None
974 } else {
975 Some(s)
976 }
977 }
978}
979
980fn json_to_string(v: &serde_json::Value) -> String {
981 match v {
982 serde_json::Value::Null => String::new(),
983 serde_json::Value::String(s) => s.clone(),
984 serde_json::Value::Number(n) => n.to_string(),
985 serde_json::Value::Bool(b) => b.to_string(),
986 other => other.to_string(),
987 }
988}
989
990#[derive(Debug, Deserialize)]
991struct AjaxRiskResp {
992 #[serde(default)]
993 zt: serde_json::Value,
994 #[serde(default)]
995 url: Option<String>,
996}
997
998impl AjaxRiskResp {
999 fn zt_string(&self) -> String {
1000 match &self.zt {
1001 serde_json::Value::String(s) => s.clone(),
1002 serde_json::Value::Number(n) => n.to_string(),
1003 other => other.to_string(),
1004 }
1005 }
1006}
1007
1008
1009fn is_cdn_risk_page(html: &str) -> bool {
1010 (html.contains("ajax.php") && html.contains("down_r"))
1011 || (html.contains("系统发现您的网络异常") && html.contains("ajax.php"))
1012 || (html.contains("ajax.php") && html.contains("el"))
1013}
1014
1015fn extract_cdn_error(html: &str) -> Option<String> {
1017 if let Some(caps) = Regex::new(r"(?i)ERROR\s*[::]\s*(\d+)")
1018 .ok()
1019 .and_then(|re| re.captures(html))
1020 {
1021 return Some(caps.get(1)?.as_str().to_string());
1022 }
1023 if html.contains("class=\"off0\"") || html.contains("class=\"off1\"") {
1024 return Some("offline".into());
1025 }
1026 None
1027}
1028
1029struct ChainReader<R> {
1031 head: Option<Vec<u8>>,
1032 rest: R,
1033}
1034
1035impl<R: Read> Read for ChainReader<R> {
1036 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
1037 if let Some(ref mut head) = self.head {
1038 if !head.is_empty() {
1039 let n = head.len().min(buf.len());
1040 buf[..n].copy_from_slice(&head[..n]);
1041 head.drain(..n);
1042 if head.is_empty() {
1043 self.head = None;
1044 }
1045 return Ok(n);
1046 }
1047 self.head = None;
1048 }
1049 self.rest.read(buf)
1050 }
1051}
1052
1053fn looks_like_file(chunk: &[u8], content_type: &str) -> bool {
1054 let ct = content_type.to_ascii_lowercase();
1055 if ct.contains("octet-stream")
1056 || ct.contains("zip")
1057 || ct.contains("msword")
1058 || ct.contains("excel")
1059 {
1060 return true;
1061 }
1062 if ct.contains("application/") && !ct.contains("html") && !ct.contains("json") {
1063 return true;
1064 }
1065 if chunk.len() >= 2 && &chunk[..2] == b"PK" {
1066 return true;
1067 }
1068 if chunk.starts_with(&[0x1f, 0x8b]) {
1069 return true;
1070 }
1071 if chunk.len() >= 4 && chunk[..4] == [0xd0, 0xcf, 0x11, 0xe0] {
1072 return true;
1073 }
1074 if chunk.starts_with(b"%PDF") {
1075 return true;
1076 }
1077 let head = String::from_utf8_lossy(&chunk[..chunk.len().min(20)]).to_ascii_lowercase();
1078 let t = head.trim_start();
1079 if t.starts_with("<!doctype") || t.starts_with("<html") || t.starts_with("<script") {
1080 return false;
1081 }
1082 false
1083}
1084
1085fn filename_from_cd(h: Option<&reqwest::header::HeaderValue>) -> Option<String> {
1086 let s = h?.to_str().ok()?;
1087 if let Some(rest) = s.split("filename*=UTF-8''").nth(1) {
1088 let v = rest.split(';').next()?.trim().trim_matches('"');
1089 return Some(urlencoding::decode(v).ok()?.into_owned());
1090 }
1091 if let Some(idx) = s.find("filename=") {
1092 let v = s[idx + 9..].split(';').next()?.trim().trim_matches('"');
1093 return Some(v.to_string());
1094 }
1095 None
1096}
1097
1098fn filename_from_url(url: &str) -> Option<String> {
1099 let u = Url::parse(url).ok()?;
1100 for key in ["fileName", "filename", "fn", "name"] {
1101 if let Some(v) = u
1102 .query_pairs()
1103 .find(|(k, _)| k == key)
1104 .map(|(_, v)| v.into_owned())
1105 {
1106 if !v.is_empty() {
1107 return Some(v);
1108 }
1109 }
1110 }
1111 let name = Path::new(u.path()).file_name()?.to_str()?.to_string();
1112 if name.is_empty() || name == "file" || name == "ajax.php" {
1113 None
1114 } else {
1115 Some(name)
1116 }
1117}
1118
1119fn now_ts() -> u64 {
1120 use std::time::{SystemTime, UNIX_EPOCH};
1121 SystemTime::now()
1122 .duration_since(UNIX_EPOCH)
1123 .map(|d| d.as_secs())
1124 .unwrap_or(0)
1125}
1126
1127
1128pub(crate) fn human_bytes(n: u64) -> String {
1129 if n < 1024 {
1130 return format!("{n}B");
1131 }
1132 let mut f = n as f64;
1133 for u in ["KB", "MB", "GB", "TB"] {
1134 f /= 1024.0;
1135 if f < 1024.0 {
1136 return format!("{f:.1}{u}");
1137 }
1138 }
1139 format!("{:.1}PB", f / 1024.0)
1140}