Skip to main content

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