futu_backend/auth/mod.rs
1// HTTP 认证模块 — 移植自成功项目 futuopend-rs
2//
3// 流程: salt → tgtgt → POST auth → (可能设备验证) → client_sig + client_key
4
5use base64::Engine;
6use futu_core::error::{FutuError, Result};
7
8mod auth_ip_list;
9mod broker;
10pub mod redact;
11/// v1.4.92 P1-D Tier A1: in-process relogin trigger (AuthRefresher trait + DefaultAuthRefresher).
12pub mod refresh;
13/// v1.4.93 G2 (CLAUDE.md C4 audit): RepullAuthCode broker auth_code self-heal.
14/// Triggered when broker auth_code expires (typical 30-day window) or
15/// `kAuthNoValidCid` (20029); avoids requiring a daemon restart.
16pub mod repull;
17#[doc(hidden)]
18pub mod site_config;
19mod webtcp;
20pub use broker::{
21 BrokerAuth, BrokerAuthRequest, BrokerAuthRouteCache, BrokerConfig, broker_auth, broker_config,
22 is_cpp_known_broker_id,
23};
24pub use refresh::{AuthRefresher, DefaultAuthRefresher, REFRESH_TIMEOUT};
25pub use repull::{ERROR_CODE_NO_VALID_CID, repull_auth_code, repull_auth_code_with_client_type};
26pub use webtcp::install_default_rustls_crypto_provider;
27pub mod commconfig;
28mod parse;
29mod util;
30
31/// Platform 通道后端连接点,按 `UserAttribution` 分池。
32///
33/// 完整对齐 C++ `FTLogin/Src/ftlogin/channel/impl/address.cpp:495-557`
34/// `LoadHardcodeAddress()` 里 `CONN_PLATFORM_*` 的条目。每个 IP 后面是 C++
35/// 源里的 `Region::kRegion*` 字段(gz/sh/hk/us/sg/au/jp),仅作注释。
36///
37/// v1.4.11 前只有 CN 12 个 IP,海外账号(HK/US/SG/AU/JP)首选 IP 命中 CN 池
38/// → 非大陆网络连不通 → 进 offline mode。v1.4.10 的 fallback 逻辑即使有也只
39/// 会 fallback 到其他 CN IP,依然死路。修复方式是按 `user_attribution` 选池。
40///
41/// 端口 9595 是 C++ 硬编码的标准端口。
42pub mod conn_points;
43// v1.4.110+ Tier 1 split: 顶层类型 + 2 const 抽到 types.rs (无业务逻辑).
44mod types;
45pub use types::{
46 AUTH_SERVER_PROD, AuthConfig, AuthResult, BrokerAuthCode, TGTGT_VALIDITY_SECS, UserAttribution,
47};
48// v1.4.110+ Tier 1 split: reqwest HTTP client builder 抽到 http_client.rs.
49mod http_client;
50pub use http_client::build_http_client;
51pub(crate) use http_client::build_http_client_with_resolve;
52
53mod phone;
54use phone::normalize_phone_account;
55
56mod device;
57use device::{
58 DEVICE_CODE_SIG_TTL_SECS, DEVICE_VERIFY_SIG_TTL_SECS, fresh_cached_device_code_sig,
59 fresh_cached_device_verify_sig, load_credentials, save_credentials,
60};
61pub use device::{read_or_generate_device_id, reset_device_state, tighten_secret_files_at_startup};
62
63// v1.4.106 codex 0558 F2+F3: PII fingerprint helpers — log 不再放 raw account/uid.
64use redact::{account_log_fingerprint, uid_log_fingerprint};
65
66/// v1.4.102 BUG-009 root-cause fix (codex 24 F6 抽 helper, 让 test 覆盖生产
67/// path): pre-flight 决策 — cached `dvs+dcs` 都 fresh 且 user 提供
68/// `--verify-code` 时, 跳过 `remember_login` + authority POST + req_device_code,
69/// 直接 verify_device_code with cached values.
70///
71/// 输入: 3 个 bool — `dvs_fresh` / `dcs_fresh` / `has_verify_cb`.
72/// 输出: true = pre-flight skip; false = fall through 现有 remember_login 流.
73///
74/// 详见 pitfall #59 + `authenticate_with_callback` 的 wire site.
75pub(super) fn should_skip_remember_login_for_cached_sms(
76 dvs_fresh: bool,
77 dcs_fresh: bool,
78 has_verify_cb: bool,
79) -> bool {
80 dvs_fresh && dcs_fresh && has_verify_cb
81}
82
83pub(super) fn should_fallback_to_password_auth_after_remember_error(err: &FutuError) -> bool {
84 !matches!(
85 err,
86 FutuError::ServerError { ret_type: 20, msg }
87 if msg.starts_with("remember-login device verification did not complete:")
88 )
89}
90
91fn decode_saved_rand_key_b64(rand_key_b64: &str) -> Result<Vec<u8>> {
92 let rand_key = base64::engine::general_purpose::STANDARD
93 .decode(rand_key_b64)
94 .map_err(|e| FutuError::Codec(format!("rand_key base64 decode: {e}")))?;
95 parse::validate_account_rand_key_len("cached credentials rand_key_b64", &rand_key)?;
96 Ok(rand_key)
97}
98
99/// v1.4.34: daemon-reload 升级(A' 方案)的产出。
100///
101/// 走一次 `remember_login` 用缓存凭据刷新 tgtgt,**只写回磁盘 credentials 文件**
102/// 不动 bridge 内存状态。下次 Platform / broker TCP 断线重连时自动读新 tgtgt。
103#[derive(Debug, Clone)]
104pub struct RefreshCredentialsReport {
105 /// 是否把新凭据成功写回了 credentials 文件
106 pub credentials_refreshed: bool,
107 /// 服务端返回的新 uid(大部分场景等于旧 uid,拿来 sanity check)
108 pub uid: u64,
109}
110
111/// v1.4.34: 给 `daemon-reload` 升级用——用磁盘缓存的 `(uid, tgtgt, device_sig,
112/// rand_key)` 走一次 `remember_login`,成功则把新 tgtgt 写回 credentials 文件。
113///
114/// **安全边界**:
115/// - 不保留 plaintext 密码(输入参数里没有 password)
116/// - 不动 bridge 内存 auth_result(不穿透 Bridge 字段可变性)
117/// - 只作用于磁盘文件
118///
119/// **失败场景**:
120/// - credentials 文件不存在 → `Err`(调用方应回退 "shutdown + restart")
121/// - tgtgt 过期(服务端拒)→ `Err`(同上)
122/// - 服务端返 code=20(重新 SMS 验证)→ `Err`(daemon 运行中不可能交互 SMS)
123pub async fn refresh_credentials_on_disk(
124 http: &reqwest::Client,
125 account: &str,
126 device_id: &str,
127 client_type: u8,
128 region_code: Option<&str>,
129 attribution: UserAttribution,
130) -> std::result::Result<RefreshCredentialsReport, FutuError> {
131 let cred = load_credentials(account).ok_or_else(|| {
132 FutuError::Codec(format!(
133 "no cached credentials for account '{}' to refresh; you likely \
134 need to shutdown + restart opend to re-auth from password",
135 account
136 ))
137 })?;
138 // 构造最小 AuthConfig——remember_login 内部用 account + client_type。
139 // client_type 必须沿真实平台传递:C++ TGTGT / User-Agent / AuthIPList
140 // 均使用 `AppConfig::GetClientTypeValue()` 的 40/60。
141 let minimal_config = AuthConfig {
142 auth_server: String::new(),
143 account: account.to_string(),
144 password: String::new(),
145 password_is_md5: false,
146 device_id: device_id.to_string(),
147 client_type,
148 };
149 // SavedCredentials 里存的是 base64 的 rand_key_b64,remember_login 需要
150 // 裸字节 —— 对齐 authenticate_with_callback 里的做法解码。
151 let rand_key = decode_saved_rand_key_b64(&cred.rand_key_b64)?;
152
153 // remember_login 对成功会返回 (AuthResult, Some(SavedCredentials))——
154 // 我们拿 SavedCredentials 写回文件,AuthResult 丢弃。
155 let (_auth_result, new_cred_opt) = remember_login(
156 http,
157 RememberLoginInput {
158 config: &minimal_config,
159 region_code,
160 attribution,
161 uid: cred.uid,
162 device_id,
163 device_sig: &cred.device_sig,
164 tgtgt: &cred.tgtgt,
165 rand_key: &rand_key,
166 web_sig: &cred.web_sig,
167 moomoo_web_sig: &cred.moomoo_web_sig,
168 verify_cb: None,
169 primary_webtcp: None,
170 },
171 )
172 .await?;
173 let credentials_refreshed = if let Some(new_cred) = new_cred_opt {
174 let uid = new_cred.uid;
175 // v1.4.106 codex 0558 F1: write IO 错 propagate, 不 silent drop
176 save_credentials(account, &new_cred).map_err(|e| {
177 FutuError::Codec(format!(
178 "admin reload: save_credentials failed — {e} (cred not on disk; \
179 next startup will re-auth via password)"
180 ))
181 })?;
182 // v1.4.106 codex 0558 F2+F3: log fingerprint 替代 raw account/uid
183 tracing::info!(
184 account_fp = %account_log_fingerprint(account),
185 uid_fp = %uid_log_fingerprint(uid),
186 "admin reload: credentials refreshed on disk"
187 );
188 true
189 } else {
190 // 服务端成功但没返新凭据——老的仍然有效,算不刷
191 tracing::debug!(
192 account_fp = %account_log_fingerprint(account),
193 "admin reload: remember_login ok but no fresh credentials (still valid)"
194 );
195 false
196 };
197 Ok(RefreshCredentialsReport {
198 credentials_refreshed,
199 uid: cred.uid,
200 })
201}
202
203/// v1.4.94 G4: 用持久化 credentials 重做 remember-login, 拿到 fresh `AuthResult`
204/// (含新 `client_sig` / `client_key`).
205///
206/// ## 用途
207///
208/// G4 reactive client_sig refresh 路径: 当 reconnect tcp_login 持续失败暗示
209/// `client_sig` 失效时, 调用方:
210/// 1. 先调 `AuthRefresher::refresh_qot_login()` (refresh disk creds via
211/// `refresh_credentials_on_disk`)
212/// 2. **再调本 fn** 用更新后的 disk creds 重做 remember-login → fresh AuthResult
213/// 3. 用 fresh AuthResult 替换 reconnect monitor 的本地 `auth_result` 变量
214/// 4. 下一轮 tcp_login 用 fresh `client_sig`
215///
216/// ## 与 `refresh_credentials_on_disk` 的区别
217///
218/// `refresh_credentials_on_disk` 只更新 disk creds + LoginCache, 不返
219/// `AuthResult`. 本 fn 复用其后端 logic 但额外**返 fresh AuthResult** 给调用方
220/// 用. 不重复 refresh disk (调用方已先调 refresh_qot_login).
221///
222/// ## Failure modes
223///
224/// - `load_credentials` 失败 (disk file 缺) → `Err`
225/// - `rand_key_b64` decode 失败 (磁盘 file 损坏) → `Err`
226/// - `remember_login` 失败 (服务端拒新 tgtgt / 反刷限流 / network) → `Err`
227///
228/// 任何失败 caller fallback 到旧行为 (continue with stale `client_sig`,
229/// 等下次 reconnect / G1 timer trigger / user 手动 admin reload).
230pub async fn reauth_via_remember_login(
231 http: &reqwest::Client,
232 account: &str,
233 device_id: &str,
234 client_type: u8,
235 attribution: UserAttribution,
236) -> std::result::Result<AuthResult, FutuError> {
237 let cred = device::load_credentials(account).ok_or_else(|| {
238 FutuError::Codec(format!(
239 "v1.4.94 G4 reauth: no cached credentials for account '{account}' (cannot \
240 reload AuthResult; daemon needs admin reload / restart)"
241 ))
242 })?;
243 // 派生 region_code (对齐 v1.4.13 phone account 拆分逻辑) — 与首登时
244 // `authenticate_with_callback` 入口同源, 保证 remember_login 收到的
245 // region_code 跟首登一致.
246 let (normalized_account, region_code_opt) = normalize_phone_account(account);
247 let minimal_config = AuthConfig {
248 auth_server: String::new(),
249 account: normalized_account,
250 password: String::new(),
251 password_is_md5: false,
252 device_id: device_id.to_string(),
253 client_type,
254 };
255 let rand_key = decode_saved_rand_key_b64(&cred.rand_key_b64)?;
256 let (auth_result, _new_cred_opt) = remember_login(
257 http,
258 RememberLoginInput {
259 config: &minimal_config,
260 region_code: region_code_opt.as_deref(),
261 attribution,
262 uid: cred.uid,
263 device_id,
264 device_sig: &cred.device_sig,
265 tgtgt: &cred.tgtgt,
266 rand_key: &rand_key,
267 web_sig: &cred.web_sig,
268 moomoo_web_sig: &cred.moomoo_web_sig,
269 verify_cb: None,
270 primary_webtcp: None,
271 },
272 )
273 .await?;
274 // v1.4.106 codex 0558 F2+F3: log fingerprint 替代 raw account/uid
275 tracing::info!(
276 account_fp = %account_log_fingerprint(account),
277 uid_fp = %uid_log_fingerprint(auth_result.user_id),
278 client_sig_len = auth_result.client_sig.len(),
279 "v1.4.94 G4: reauth_via_remember_login produced fresh AuthResult"
280 );
281 Ok(auth_result)
282}
283
284/// 验证码获取回调类型
285///
286/// 当需要短信验证码时调用此回调。返回 Some(code) 表示用户输入了验证码,
287/// 返回 None 表示用户取消。
288pub type VerifyCodeCallback = Box<dyn Fn() -> Option<String> + Send + Sync>;
289
290/// 完整密码鉴权(优先用保存的凭据跳过验证码)
291///
292/// CLI 模式使用 `authenticate()` 从 stdin 读取验证码;
293/// GUI 模式使用 `authenticate_with_callback()` 通过回调获取。
294pub async fn authenticate(config: &AuthConfig) -> Result<AuthResult> {
295 authenticate_with_callback(config, None).await
296}
297
298/// 完整密码鉴权(带自定义验证码回调)
299pub async fn authenticate_with_callback(
300 config: &AuthConfig,
301 verify_cb: Option<VerifyCodeCallback>,
302) -> Result<AuthResult> {
303 // v1.4.84 SEC-001: 首次 auth 启动时 stderr warn debug log 安全风险.
304 // OnceLock dedup 避免 retry 或 daemon-reload 重复打.
305 redact::emit_debug_log_security_warn_once();
306
307 let http = build_http_client(config.client_type)?;
308 let primary_webtcp = endpoints::primary_auth_webtcp_context_for_auth_server(
309 config.client_type,
310 &config.auth_server,
311 );
312 let _primary_webtcp_prefetch = primary_webtcp.as_ref().map(|context| {
313 endpoints::spawn_primary_auth_site_config_prefetch(context, 0, &config.device_id)
314 });
315
316 // v1.4.13:把 `+86-13900000000` 这种带区号的输入拆成 account 本体 + region_code。
317 // 不拆的话 moomoo 服务端按 `13900000000` 查存的 pwd_md5 对不上我们 tgtgt 里
318 // 发的整串 `+86-13900000000`,报 `error_code=2 账号密码不匹配`。对齐 C++
319 // `BasicAccountAuthInfo` 的字段约定(`auth_impl.cpp:267`)。
320 let (normalized_account, region_code) = normalize_phone_account(&config.account);
321 if region_code.is_some() {
322 // v1.4.106 codex 0558 F2: log fingerprint, 不写 raw account / phone
323 tracing::info!(
324 original_fp = %account_log_fingerprint(&config.account),
325 account_fp = %account_log_fingerprint(&normalized_account),
326 region_no = %region_code.as_deref().unwrap_or(""),
327 "parsed phone account with region code"
328 );
329 }
330 // 构造本地 effective config —— `account` 字段已归一化,后续所有流程都用它
331 let mut effective_config = config.clone();
332 effective_config.account = normalized_account;
333
334 // 尝试 remember login(用保存的凭据)
335 if let Some(cred) = load_credentials(&effective_config.account) {
336 tracing::info!("found saved credentials, trying remember-login");
337
338 // ★ rand_key 在 salt32 非空路径是 32 字节(AES-256),不能截断到 16。
339 // 截到 16 字节后用 AES-128 解密 AES-256 加密的 client_key / rand_key_new
340 // 必定失败(表现:`cbc_md5_var: last_block_size 127 > 15`)。
341 let rand_key = match decode_saved_rand_key_b64(&cred.rand_key_b64) {
342 Ok(rand_key) => rand_key,
343 Err(e) => {
344 tracing::warn!(
345 error = %e,
346 account_fp = %account_log_fingerprint(&effective_config.account),
347 "cached credentials rand_key_b64 invalid; skipping remember-login and falling back to password auth"
348 );
349 return password_auth(
350 &effective_config,
351 region_code.as_deref(),
352 &http,
353 verify_cb.as_deref(),
354 primary_webtcp.as_ref(),
355 )
356 .await;
357 }
358 };
359
360 // v1.4.102 BUG-009 真修 (root-cause fix, 用户 2026-04-28 反馈):
361 //
362 // **历史 saga**: BUG-009 SMS race 经过 v1.4.72/74/75/81 共 6 版迭代.
363 // v1.4.81 Option B 设计是 "若 cached dvs+dcs 都 fresh, 跳
364 // req_device_code 用 cached values + 用户传入 --verify-code 直接
365 // verify". 但 Option B 检查放在 `remember_login` **之后** —— 每次启动
366 // 都先跑 remember_login → POST /authority/ → code=20 → 拿新 dvs →
367 // 进 handle_device_verify with NEW dvs (而不是 cached) → req_device_code
368 // → 新 SMS 覆盖老码 → 用户输的老 SMS 失败 → code=21 累计触发账号锁.
369 //
370 // **真根因**: cached dvs+dcs 在 5min 窗口内是有效凭证, 任何 POST
371 // /authority/ 都可能 invalidate. 必须在 cache fresh + user 提供
372 // --verify-code 时 **跳过** remember_login, 直接 verify_device_code.
373 //
374 // **本次修法**: pre-flight 检查 — 若 cached dvs+dcs 都 fresh **AND**
375 // verify_cb 存在(--verify-code 提供) → 跳过 remember_login + authority
376 // POST + req_device_code, 直接 handle_device_verify(cached_dvs,
377 // cached_dcs, user verify_code).
378 //
379 // **不破坏现有 flow**: cache 不全 fresh / 无 --verify-code → fall
380 // through 到正常 remember_login (v1.4.74 及以前行为).
381 //
382 // **C++ 对齐**: auth_impl.cpp 没显式 "skip authority on cached SMS"
383 // 路径 (C++ 客户端假设交互式输入码), 但本 fix 是 daemon 长跑场景特有 —
384 // GUI app 不需要因为 SMS 是即时输入. C++ 如果有同等场景应该也这样做.
385 // v1.4.102 codex 24 F6 (P2) fix: 用 should_skip_remember_login_for_cached_sms
386 // 共享 decision fn (pub(super) in tests.rs). 之前生产 logic 与 test
387 // helper 重复 (test 测 helper 但 helper 不 wire 到生产) — 改为共享.
388 let dvs_fresh = fresh_cached_device_verify_sig(&cred);
389 let dcs_fresh = fresh_cached_device_code_sig(&cred);
390 let has_verify_cb = verify_cb.is_some();
391 if should_skip_remember_login_for_cached_sms(
392 dvs_fresh.is_some(),
393 dcs_fresh.is_some(),
394 has_verify_cb,
395 ) && let (Some(cached_dvs), Some(cached_dcs)) = (dvs_fresh, dcs_fresh)
396 {
397 tracing::info!(
398 dvs_len = cached_dvs.len(),
399 dcs_len = cached_dcs.len(),
400 ttl_secs = DEVICE_CODE_SIG_TTL_SECS,
401 attribution = ?cred.user_attribution,
402 "v1.4.102 BUG-009 root-cause fix: cached dvs+dcs both fresh AND \
403 user supplied --verify-code → SKIP remember_login + authority POST + \
404 req_device_code (would invalidate cached SMS); going DIRECTLY to \
405 verify_device_code with cached values"
406 );
407 let domain = cred.user_attribution.auth_domain();
408 return handle_device_verify(
409 &http,
410 DeviceVerifyInput {
411 config: &effective_config,
412 attribution: cred.user_attribution,
413 domain,
414 uid: cred.uid,
415 dvs: cached_dvs,
416 rand_key: &rand_key,
417 verify_cb: verify_cb.as_deref(),
418 cached_device_code_sig: Some(cached_dcs),
419 },
420 )
421 .await;
422 }
423 // Note: 任一缺失 (dvs / dcs / verify_cb) → fall through to remember_login.
424 // 现有 post-fail Option B/A path 仍存在 (cred 部分新鲜场景, e.g. dvs
425 // 新鲜 dcs 缺失 → Option A fallback).
426
427 match remember_login(
428 &http,
429 RememberLoginInput {
430 config: &effective_config,
431 region_code: region_code.as_deref(),
432 attribution: cred.user_attribution,
433 uid: cred.uid,
434 device_id: &cred.device_id,
435 device_sig: &cred.device_sig,
436 tgtgt: &cred.tgtgt,
437 rand_key: &rand_key,
438 web_sig: &cred.web_sig,
439 moomoo_web_sig: &cred.moomoo_web_sig,
440 verify_cb: verify_cb.as_deref(),
441 primary_webtcp: primary_webtcp.as_ref(),
442 },
443 )
444 .await
445 {
446 Ok((auth, new_cred)) => {
447 // 更新凭据 — v1.4.106 codex 0558 F1: write IO 错 propagate
448 if let Some(nc) = new_cred {
449 save_credentials(&effective_config.account, &nc).map_err(|e| {
450 FutuError::Codec(format!(
451 "remember_login: save_credentials failed — {e} (cred not on disk; \
452 daemon proceeds with in-memory creds, next restart will re-auth)"
453 ))
454 })?;
455 }
456 return Ok(auth);
457 }
458 Err(e) => {
459 if !should_fallback_to_password_auth_after_remember_error(&e) {
460 tracing::warn!(
461 error = %e,
462 "remember-login reached device verification and did not complete; \
463 not falling back to password/SMS auth because that can request a \
464 second SMS and mix DVS/DCS/rand_key state. Restart with \
465 --verify-code <SMS> using the same HOME."
466 );
467 return Err(e);
468 }
469 tracing::warn!(
470 error = %e,
471 "remember-login failed; cached credentials may be stale/incomplete, \
472 falling back to password/SMS auth"
473 );
474 }
475 }
476
477 // v1.4.75 BUG-009 Fix 9a 真修(Option A "探路版",CLAUDE.md 坑 #34 模式):
478 // 如果缓存的 device_verify_sig 仍新鲜(<5 min)→ **跳过 password_auth
479 // → POST /authority/ → backend 返新 dvs 覆盖老 SMS** 的破坏流程,直接
480 // 调 handle_device_verify 带 cached dvs 做 SMS 验证。
481 //
482 // **v1.4.72 Fix 9a(WARN-only 非真修)**:只 log 提示,仍走 password_auth。
483 // 用户输的老 SMS 码绑定 old dvs,但 daemon 流程已经拿到新 dvs → 服务端
484 // 对比失败 → code=21 累计失败锁账号(外部 v1.4.71 AI tester 报告 §2.5)。
485 //
486 // **v1.4.75 Option A 探路**:cached dvs fresh → 直接 handle_device_verify(cached_dvs)
487 //
488 // **agent 代码级审查确认 3/4 风险安全**(essentials/2026-04-23-1810-v1.4.75-plan.md):
489 // - Risk 1 🟢 backend 接受 cached dvs(C++ auth_impl.cpp:244/757/879 无"一次性"语义)
490 // - Risk 3 🟢 rand_key AES-256 不截断(aes_cbc_md5_decrypt_var 可变长 + unit test 已 PASS)
491 // - Risk 4 🟢 moomoo empty device_sig 无关(handle_device_verify 不用 device_sig)
492 //
493 // **v1.4.75 真机结论**:Risk 2 假设被推翻。GET
494 // `/authority/req_device_code?dvs=cached_X` 仍可能触发新 SMS,
495 // 所以 Option A 只能避开 authority re-POST 的反刷问题,不足以保住
496 // 用户手上的旧 SMS code。
497 //
498 // 当前 Option A fallback 仍是纯 "add new branch",**不破坏现有 auth 流程**:
499 // cached dvs 过期 / 不存在 → fall through 到 password_auth(v1.4.74 及以前行为)
500 // v1.4.81 BUG-009 Fix 9a Option B (优先) / Option A (fallback):
501 //
502 // - Option B (首选): cached device_code_sig + dvs 都 fresh → 跳过
503 // req_device_code 整步,直接 verify_device_code with cached dcs +
504 // 用户传入 --verify-code。**这是 BUG-009 的真修**。
505 // - Option A (fallback): 只有 cached dvs fresh(dcs 缺失或过期)→ 跳
506 // authority POST 避反刷,但 req_device_code 仍会触发新 SMS(v1.4.75
507 // 真机 verify 推翻 Risk 2 假设后,Option A 只能避 "authority POST 反
508 // 刷 15",不能避 "新 SMS 覆盖老码")。
509 //
510 // 典型 flow:
511 // - Step 1(首次启动 non-tty): password_auth → POST /authority/ code=20
512 // → persist shell (含 dvs+ts) → req_device_code (发 SMS, 拿 dcs) →
513 // persist dcs → stdin atty fail 退出
514 // - Step 2(5min 内重启带 --verify-code X): load credentials (dvs+dcs 都 fresh)
515 // → Option B 触发 → handle_device_verify(cached_dvs, cached_dcs=Some)
516 // → 跳 req_device_code → verify_device_code with X → 成功
517 // v1.4.102 codex 32 F3 (P2) fix: 同时 check dvs + dcs fresh.
518 // 之前只 check dcs fresh + cred.device_verify_sig.unwrap_or("") 直接
519 // 用 → DCS fresh 但 DVS 过期/缺失时仍走此路径用 stale/empty DVS verify.
520 // 修法: 与 pre-flight check (line 538-) 一致, 必须 both fresh.
521 if let (Some(cached_dcs), Some(cached_dvs)) = (
522 fresh_cached_device_code_sig(&cred),
523 fresh_cached_device_verify_sig(&cred),
524 ) {
525 tracing::info!(
526 dcs_len = cached_dcs.len(),
527 dvs_len = cached_dvs.len(),
528 ttl_secs = DEVICE_CODE_SIG_TTL_SECS,
529 attribution = ?cred.user_attribution,
530 "v1.4.81 BUG-009 Fix 9a Option B (v1.4.102 audit 32 F3 refine: \
531 both dvs+dcs fresh): using cached device_code_sig + \
532 device_verify_sig, skipping req_device_code entirely"
533 );
534 let domain = cred.user_attribution.auth_domain();
535 return handle_device_verify(
536 &http,
537 DeviceVerifyInput {
538 config: &effective_config,
539 attribution: cred.user_attribution,
540 domain,
541 uid: cred.uid,
542 dvs: cached_dvs,
543 rand_key: &rand_key,
544 verify_cb: verify_cb.as_deref(),
545 cached_device_code_sig: Some(cached_dcs),
546 },
547 )
548 .await;
549 }
550 if let Some(cached_dvs) = fresh_cached_device_verify_sig(&cred) {
551 tracing::warn!(
552 dvs_len = cached_dvs.len(),
553 ttl_secs = DEVICE_VERIFY_SIG_TTL_SECS,
554 attribution = ?cred.user_attribution,
555 "v1.4.75 BUG-009 Fix 9a Option A fallback: cached dvs only (no \
556 fresh device_code_sig) — skipping authority re-POST but \
557 req_device_code will still fire new SMS (known half-fix; 5min \
558 window after first-SMS lost). Will work for authority rate-limit \
559 avoidance but not SMS-code preservation."
560 );
561 let domain = cred.user_attribution.auth_domain();
562 return handle_device_verify(
563 &http,
564 DeviceVerifyInput {
565 config: &effective_config,
566 attribution: cred.user_attribution,
567 domain,
568 uid: cred.uid,
569 dvs: cached_dvs,
570 rand_key: &rand_key,
571 verify_cb: verify_cb.as_deref(),
572 cached_device_code_sig: None,
573 },
574 )
575 .await;
576 }
577 }
578
579 // 全新密码认证
580 //
581 // v1.4.17:SMS 验证码错(`error_code=21`)时自动轮换 device_id 重试,
582 // 最多 MAX_SMS_RETRIES 次。
583 //
584 // **v1.4.57 修正(外部报告 #5 第 3 层根因)**:自动轮换 device_id 反而会触发
585 // 服务端限流("5 次不同设备 30 秒内" 硬 threshold)。同事实锤:连续 2 次
586 // SMS 输错自动轮换后,正确码也被 code=1 "系统繁忙" 拒。v1.4.57 起**不再
587 // 自动轮换**(MAX_SMS_RETRIES=0),让用户手动决定:
588 // - 真是验证码输错 → 重新运行 `futu-opend --setup-only` 重试一次
589 // - 需要强制换 device_id → 显式 `--reset-device --setup-only`
590 //
591 // **tty 检测**:prompt_input 已在非 tty 时 fail fast(见 auth/util.rs:38-51),
592 // 避免空验证码毒化 device_id。v1.4.57 外部 #5 A/C 两层根因至此闭环。
593 // v1.4.57 外部 #5:直接调一次 password_auth,不再自动轮换 device_id。
594 // 如 SMS 输错 (ret_type=21),把错误返给 caller,用户再手动 retry。
595 //
596 // v1.4.17-56 的 `MAX_SMS_RETRIES=2` 自动轮换反而触发服务端限流(同一 uid
597 // "5 次不同设备 30 秒内"硬阈值),导致正确码也被 code=1 系统繁忙拒(实锤:
598 // 2026-04-22 外部用户 Telegram SMS 中继场景)。
599 //
600 // 未来若想恢复重试(e.g., CLI flag gated),restore 原 loop + 用
601 // `reset_device_state` + `read_or_generate_device_id` 轮换 device_id。
602 password_auth(
603 &effective_config,
604 region_code.as_deref(),
605 &http,
606 verify_cb.as_deref(),
607 primary_webtcp.as_ref(),
608 )
609 .await
610}
611
612// v1.4.110+ Tier 2/3 split: 5 业务 fn + 4 input struct 拆 4 子 mod.
613// Orchestrator (authenticate_with_callback / refresh_credentials_on_disk) 通过
614// 下方 use 仍可见 fn 和 input struct.
615mod device_verify;
616mod endpoints;
617mod password_auth;
618mod remember;
619
620use device_verify::{DeviceVerifyInput, handle_device_verify};
621use password_auth::password_auth;
622use remember::{RememberLoginInput, remember_login};
623
624#[cfg(test)]
625mod tests;