Skip to main content

futu_opend/
hints.rs

1//! v1.4.110 P1-2: auth error hint messages (抽自 main.rs lines 1416-1604).
2//!
3//! v1.4.97 P1-D: error message classification + user-facing recovery hints.
4//! ret_type=2/15/21/45 等错误码 → 不同恢复建议 (device_id 重置 / 反刷 sleep /
5//! 防火墙开放 9595 / 平台 + 客户端版本切换).
6
7use crate::config::RuntimeConfig;
8
9/// 解析 auth error string + 给出对应 recovery hint (打到 stderr + tracing).
10///
11/// 调用方在 `Err(e) => { ... }` match arm 内调用此 fn (替代 inline if-chain).
12/// 之后调用方按需做 `if config.setup_only { return Err(...) }` 控制流.
13pub fn print_auth_error_hint(e: &futu_core::error::FutuError, config: &RuntimeConfig) {
14    let err_str = format!("{e}");
15    let hint_21 = err_str.contains("ret_type=21") || err_str.contains("验证码错");
16    let hint_15 = err_str.contains("ret_type=15") || err_str.contains("长时间没有登录");
17    // v1.4.19:识别"所有 Platform IP 都连不上"—— 几乎一定是本机
18    // 出站防火墙挡了 9595 端口(腾讯云 Lighthouse 默认不放 9595;
19    // 企业内网 / 云厂商安全组也常见)
20    let hint_firewall =
21        err_str.contains("Platform IP pool exhausted") || err_str.contains("timed out");
22    // v1.4.92 D1: 更多细分 hint —— error_code=2 / 45 / 网络 DNS / SMS 非交互
23    let hint_2 = err_str.contains("ret_type=2,")
24        || err_str.contains("ret_type=2 ")
25        || err_str.contains("账号密码不匹配");
26    let hint_45 = err_str.contains("ret_type=45") || err_str.contains("当前应用版本过低");
27    let hint_dns = err_str.contains("dns error")
28        || err_str.contains("failed to lookup")
29        || err_str.contains("Name or service not known")
30        || err_str.contains("nodename nor servname");
31    // SMS 非交互:error 是 SMS 流程相关 + stdin 不是 TTY
32    let hint_sms_noninteractive = (err_str.contains("ret_type=20")
33        || err_str.contains("require_device_verify")
34        || err_str.contains("device_verify_sig"))
35        && !std::io::IsTerminal::is_terminal(&std::io::stdin())
36        && config.verify_code.is_none();
37    if hint_21 {
38        tracing::error!(
39            error = %e,
40            "gateway init failed: SMS code mismatch (ret_type=21). \
41             Device_id may be locked after multiple wrong codes — try \
42             `futu-opend --reset-device --setup-only ...` to regenerate \
43             and re-verify via SMS."
44        );
45        // v1.4.72 BUG-009 Fix 9b (external reviewer v1.4.69 P1): setup-only + TTY
46        // 场景保持前台 wait,让用户读完错误 + 手动决定下一步(不要
47        // 立即退出让 supervisor 重启 → 重 POST /authority → 新 SMS
48        // 失效旧码 → 累计失败触发账户锁)。
49        //
50        // 判断条件:setup_only + stdin 是 tty(可交互)+ --verify-code
51        // 用过(说明这次 SMS 输错)。非交互 daemon (systemd / Docker)
52        // 保持旧行为 return Err → supervisor 决定重启策略。
53        if config.setup_only
54            && std::io::IsTerminal::is_terminal(&std::io::stdin())
55            && config.verify_code.is_some()
56        {
57            eprintln!();
58            eprintln!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
59            eprintln!("⚠️  v1.4.72 BUG-009 Fix 9b: SMS 验证码错 (ret_type=21)");
60            eprintln!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
61            eprintln!();
62            eprintln!("   daemon 不立即退出,让你有时间读完错误 + 决定下一步。");
63            eprintln!();
64            eprintln!("   下一步建议(不要盲目重跑 daemon,避免新 SMS + 账户锁):");
65            eprintln!("   1. 等 30 秒,等服务端限流过");
66            eprintln!("   2. 检查手机上最新收到的 SMS(可能有多条,用最新那条)");
67            eprintln!("   3. 重跑 daemon 带新码:");
68            eprintln!("      futu-opend --setup-only --verify-code <新 SMS 码> ...");
69            eprintln!();
70            eprintln!("   按 Enter 退出(Ctrl+C 也可)...");
71            let mut pause = String::new();
72            if let Err(err) = std::io::stdin().read_line(&mut pause) {
73                tracing::debug!(
74                    error = %err,
75                    "failed to wait for interactive SMS error acknowledgment"
76                );
77            }
78        }
79    } else if hint_15 {
80        // v1.4.74 A3 BUG-003 fix(external reviewer v1.4.71 AI tester §4.2 Layer 2):
81        // error_code=15 原本用一大段 inline text 列 5 因,用户 skim 读
82        // 很难 discharge 每一条 cause。改为结构化 stderr 输出 + tracing
83        // log 保留引用;让用户能**按优先级逐条排查**。
84        eprint!("{}", ret_type_15_hint_text());
85        tracing::error!(
86            error = %e,
87            "ret_type=15 — see stderr for 5-cause diagnostic checklist"
88        );
89    } else if hint_firewall {
90        tracing::error!(
91            error = %e,
92            "gateway init failed: all Platform connection endpoints are unreachable on port 9595. \
93             This is almost always an outbound firewall issue on your host, NOT \
94             a Futu server problem. Quick check: \
95             `nc -vz hkconn.futunn.com 9595` and `nc -vz usconn.moomoo.com 9595` — \
96             if these also fail, your host is blocking outbound TCP 9595. \
97             Fix: open 9595 in your cloud security group \
98             (Tencent Cloud Lighthouse / CVM / AWS / Aliyun all default to \
99             blocking non-standard ports)."
100        );
101    } else if hint_2 {
102        // v1.4.92 D1: error_code=2 "账号密码不匹配"
103        // 真因 4 类(按出现概率排序):
104        //   1. 密码真错(最常见)
105        //   2. --platform 选错(auth.futunn.com 收 client_type=60 / 反之 → 直接拒)
106        //   3. account 拆区号错(+86-xxx 整串发 → 服务端按号码本体查 hash 不匹配)
107        //   4. 同号在 futunn / moomoo 各注册了一个,登错了那个
108        eprintln!();
109        eprintln!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
110        eprintln!("⚠️  ret_type=2 — 账号密码不匹配");
111        eprintln!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
112        eprintln!();
113        eprintln!("💡 Hint: 这个错误可能 4 种根因(按概率):");
114        eprintln!("  1. 密码真错 → 检查 --login-pwd 或 --login-pwd-md5");
115        eprintln!("  2. --platform 选错 → futunn 系账号必须 --platform futunn,");
116        eprintln!("     moomoo (US/SG/AU/JP/CA/MY) 系账号必须 --platform moomoo");
117        eprintln!("  3. 同号双注册 → futunn / moomoo 同手机号 / 邮箱可各注册一个,");
118        eprintln!("     检查 `futu-opend --login-account ... --platform <对侧>` 是否能登");
119        eprintln!("  4. 区号写错 → 手机号格式 +86-13900000000 (区号 + dash + 号码本体)");
120        eprintln!();
121        tracing::error!(error = %e, "ret_type=2 — see stderr 4-cause hint");
122    } else if hint_45 {
123        // v1.4.92 D1: error_code=45 "当前应用版本过低"
124        eprintln!();
125        eprintln!("⚠️  ret_type=45 — 当前应用版本过低");
126        eprintln!();
127        eprintln!("💡 Hint: 服务端对海外账号 (user_attribution != 1) 严格校验,");
128        eprintln!("        客户端版本号需 ≥ 800(X-Futu-Client-Version 字段)。");
129        eprintln!("        本 Rust daemon 使用 backend 可识别的 Rust 版本号 1030。");
130        eprintln!("        若仍报 45,可能是 backend 升级了最低版本要求,");
131        eprintln!("        升级 daemon: brew upgrade futuleaf/tap/futu-opend-rs");
132        eprintln!();
133        tracing::error!(error = %e, "ret_type=45 — version too low, see stderr hint");
134    } else if hint_dns {
135        // v1.4.92 D1: DNS 解析失败 (auth.futunn.com / auth.moomoo.com)
136        eprintln!();
137        eprintln!("⚠️  网络错: DNS 解析 auth server 失败");
138        eprintln!();
139        eprintln!("💡 Hint: 检查域名解析 + 网络连通性:");
140        eprintln!("  1. ping auth.futunn.com / ping auth.moomoo.com");
141        eprintln!("  2. 若公司 / 校园 VPN 限制了海外域名 → 切换 VPN 或开放 auth.* 解析");
142        eprintln!("  3. 中国大陆 ISP 偶尔抽风 auth.moomoo.com → 试 8.8.8.8 DNS");
143        eprintln!("  4. 检查 /etc/hosts 是否有错误的 auth server override");
144        eprintln!();
145        tracing::error!(error = %e, "DNS lookup failed for auth server");
146    } else if hint_sms_noninteractive {
147        // v1.4.92 D1: SMS 验证需要交互终端 + stdin 不是 TTY (nohup / docker -d / systemd)
148        eprintln!();
149        eprintln!("⚠️  SMS 验证码需要交互式终端 (stdin 不是 TTY)");
150        eprintln!();
151        eprintln!("💡 Hint: 后台 / 守护进程模式下 SMS 输入会立即返回空字符串,");
152        eprintln!("        毒化 device_id 触发 ret_type=15 / 21。");
153        eprintln!();
154        eprintln!("  正确部署姿势 (systemd / Docker):");
155        eprintln!("  1. 前台一次完成 SMS:");
156        eprintln!("     futu-opend --setup-only --login-account X --login-pwd Y \\");
157        eprintln!("                --platform <futunn|moomoo>");
158        eprintln!("     (或用 --verify-code <已收到的码> 跳过 stdin)");
159        eprintln!("  2. 完成后凭据写入 ~/.futu-opend-rs/,daemon 后续自动跳 SMS");
160        eprintln!("  3. 启动后台 daemon:");
161        eprintln!("     futu-opend --login-account X --login-pwd Y --platform ...");
162        eprintln!();
163        tracing::error!(error = %e, "SMS required but stdin is not a TTY");
164    } else {
165        tracing::error!(error = %e, "gateway initialization failed, starting in offline mode");
166    }
167}
168
169fn ret_type_15_hint_text() -> &'static str {
170    r#"
171━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
172⚠️  Gateway init failed: server returned ret_type=15
173    ("请重新输入密码" — 协议层是 kAuthTgtgtExpired:服务端判定 tgtgt 大票过期/无效)
174━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
175
176不要反复输入同一密码。15 不等于密码一定错误,也不是 WebTCP/HTTP transport
177失败;请求已经到达 auth backend,服务端在 authority 业务层拒绝。
178
179authority raw response 的 `error` 对象可以一行定性:
180
181  - `require_device_verify=true` → 应进入 SMS/设备验证流程
182  - `delete_password=true` → C++ 会塌缩成 15,按删除密码凭据/重认证处理
183  - 裸 `error_code=15` → tgtgt 被服务端票据校验拒绝
184
185这个错误仍有若干常见触发因素,按优先级逐条排查:
186
187  [0] **有另一个 Futu OpenD 正在用这个账号**(v1.4.52 新增)
188      → 检查所有机器:`lsof -i :11111` + `ps aux | grep -i opend`
189      → 停掉另一个 OpenD(C++ 或 Rust),然后重试
190
191  [1] **短时间重复 authority / 服务端拒绝继续发票据**
192      → 等 60 秒后再重试
193
194  [2] **SMS 超限 / device_id 验证状态异常**(v1.4.57 新增)
195      → 等 3-5 分钟让限流过
196      → 手机上登录富途/moomoo App 一次清账号状态
197      → 重启 daemon 带 `--verify-code <CODE>` 避免 stdin 延迟
198
199  [3] **device_id 验证状态需要重置**(空 SMS 提交 / 长时间不用)
200      → 先试 `--reset-device --setup-only`
201      → 注意:破坏性操作,会触发二次 SMS 验证
202
203  [4] **账号级状态异常 / 不适合密码登录**
204      → 手机上登录富途/moomoo App 一次清账号状态
205      → 若账号从未在 App 登录激活,必须先激活
206
207  建议试序:[0] → [1] → [2] → [3] → [4]
208
209"#
210}
211
212#[cfg(test)]
213mod tests;