diff --git a/Cargo.lock b/Cargo.lock index c352c678..b7e828b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2793,6 +2793,7 @@ version = "0.9.1" dependencies = [ "serde", "serde_json", + "tw-types", ] [[package]] diff --git a/bin/twcore/src/main.rs b/bin/twcore/src/main.rs index bc0e2a66..08b27708 100644 --- a/bin/twcore/src/main.rs +++ b/bin/twcore/src/main.rs @@ -437,12 +437,15 @@ fn cmd_speed(path: &Path, provider: Option, proxy: Option) -> Re .proxies .iter() .find(|x| x.name == name) - .with_context(|| format!("未找到名为「{name}」的代理"))?; + .with_context(|| format!("no proxy named `{name}`"))?; let hop = tw_gateway::hop_of(px).map_err(anyhow::Error::msg)?; let (host, port) = tw_gateway::proxy_target(&cfg, &px.name); let r = tw_gateway::l1_proxy(&hop, &host, port).await; print_l1( - &format!("代理「{}」(经其连接 {host}:{port})", px.name), + &format!( + "proxy `{}` (connecting to {host}:{port} through it)", + px.name + ), None, &r, ); @@ -453,12 +456,12 @@ fn cmd_speed(path: &Path, provider: Option, proxy: Option) -> Re cfg.providers .iter() .find(|p| p.name == *n) - .with_context(|| format!("未找到名为「{n}」的上游"))?, + .with_context(|| format!("no upstream named `{n}`"))?, ], None => cfg.providers.iter().collect(), }; if targets.is_empty() { - println!("尚未配置任何上游,请先在 providers 中添加上游。"); + println!("No upstreams are configured yet. Add one under `providers`."); return Ok(()); } for p in targets { @@ -484,14 +487,16 @@ fn hop_for( // 跟随系统代理的地址要到建连时才由环境决定,我们没有那份地址 // 可以去握手。说出来,而不是假装直连测一遍给个漂亮数字。 tw_config::SYSTEM => anyhow::bail!( - "该上游使用系统代理,代理地址在建立连接时才能确定,链路测速无法测量。将代理配置为命名代理后即可测速" + "This upstream goes through the system proxy, whose address is only decided by \ + the environment when the connection is made, so a link test cannot measure it. \ + Configure the proxy as a named entry to measure this route." ), name => { let px = cfg .proxies .iter() .find(|x| x.name == name) - .with_context(|| format!("proxies 中未定义「{name}」"))?; + .with_context(|| format!("`{name}` is not defined under `proxies`"))?; let auth = match &px.auth { None => None, Some(a) => Some((a.user.clone(), a.pass.resolve()?)), @@ -507,19 +512,20 @@ fn hop_for( fn print_l1(target: &str, via: Option<&str>, r: &tw_gateway::L1Result) { let head = match via { - Some(v) => format!("{target}(经由 {v})"), + Some(v) => format!("{target} (through {v})"), None => target.to_string(), }; println!("{} {}", if r.ok { "✅" } else { "❌" }, head); + // 28 宽:最长的一个是 `TLS handshake to the proxy` for seg in &r.segments { - println!(" {:<18} {:>6} ms", seg.stage.label(), seg.ms); + println!(" {:<28} {:>6} ms", seg.stage.label(), seg.ms); } if r.ok { - println!(" {:<18} {:>6} ms", "建立连接合计", r.total_ms); + println!(" {:<28} {:>6} ms", "connect, total", r.total_ms); } if let Some(e) = &r.error { match r.failed { - Some(stage) => println!(" {}:{e}", stage.label()), + Some(stage) => println!(" {}: {e}", stage.label()), None => println!(" {e}"), } } diff --git a/crates/tw-api/Cargo.toml b/crates/tw-api/Cargo.toml index d9e1b906..a313f6e1 100644 --- a/crates/tw-api/Cargo.toml +++ b/crates/tw-api/Cargo.toml @@ -10,3 +10,7 @@ description = "Control-plane contract: request and response types, plus a client [dependencies] serde = { workspace = true } serde_json = { workspace = true } +# `Msg` 住在 tw-types:会造消息的 crate 有一半够不着 tw-api(tw-config、 +# tw-adopt 都只依赖 tw-types),而契约这一层反过来依赖它没有问题 —— +# tw-types 自己零依赖。 +tw-types = { workspace = true } diff --git a/crates/tw-api/src/lib.rs b/crates/tw-api/src/lib.rs index d46de7f4..87bb67c0 100644 --- a/crates/tw-api/src/lib.rs +++ b/crates/tw-api/src/lib.rs @@ -11,6 +11,10 @@ use serde::{Deserialize, Serialize}; +/// 给人看的话:码 + 参数 + 英文原句。见 [`tw_types::Msg`]。**协议里凡是 +/// 一句给人读的错误或说明,类型都是它**,不是 `String`。 +pub use tw_types::Msg; + /// 控制面协议版本。UI 和 CLI 连上来时检查,不匹配就明确提示「请升级 /// 客户端」,而不是以奇怪的方式失败。 pub const CONTROL_API_VERSION: u32 = 1; @@ -350,9 +354,14 @@ pub enum Event { proxy: String, /// `unreachable` = 刚刚检查不通;`reachable` = 又通了 state: String, - /// 不通时卡在哪一步,已脱敏。通了没有 + /// 不通时卡在建连的哪一步。**和原因分开** —— 「卡在到代理的 TCP + /// 握手」要去改的地方和「代理拒绝了用户名和密码」完全不同,而把 + /// 两句话拼成一个字符串之后,界面就只能整句照搬 + #[serde(default, skip_serializing_if = "Option::is_none")] + failed: Option, + /// 不通的原因,已脱敏。通了没有 #[serde(default, skip_serializing_if = "Option::is_none")] - detail: Option, + detail: Option, at_ms: u64, }, /// 上游拒绝了我们的凭据(401/403),或者重新接受了。 @@ -1105,7 +1114,7 @@ pub struct L1Result { pub failed: Option, /// 失败的原因 #[serde(default, skip_serializing_if = "Option::is_none")] - pub error: Option, + pub error: Option, } /// 当前的配置文本,连同它的版本号。 diff --git a/crates/tw-control/tests/resources.rs b/crates/tw-control/tests/resources.rs index 3337d6f5..4bd15675 100644 --- a/crates/tw-control/tests/resources.rs +++ b/crates/tw-control/tests/resources.rs @@ -552,7 +552,8 @@ async fn testing_a_proxy_checks_its_credentials_including_the_stored_ones() { .await; let v = json(&body); assert_eq!(v["ok"], false, "{body}"); - assert!(v["error"].as_str().unwrap().contains("407"), "{body}"); + // 按码断言:这条测的是「握手做完了、认证验过了」,不是那句话怎么写 + assert_eq!(v["error"]["code"], "l1.http_proxy.auth_required", "{body}"); // 存下正确的,再用「保持原样」检测 —— 用的是存着的那份 call( diff --git a/crates/tw-gateway/src/l1.rs b/crates/tw-gateway/src/l1.rs index 1f4dff27..81f15923 100644 --- a/crates/tw-gateway/src/l1.rs +++ b/crates/tw-gateway/src/l1.rs @@ -18,6 +18,7 @@ use std::time::{Duration, Instant}; use serde::{Deserialize, Serialize}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; +use tw_types::{Msg, msg}; /// 每一段都单独限时,这样超时错误能说出**卡在哪一段**。 /// 一句「8 秒超时」和「TLS 握手 8 秒没完成」的可修复性差得远。 @@ -81,15 +82,15 @@ impl Stage { /// 命令行里的说法。**界面不用它**,界面按 `step` / `peer` 自己说。 pub fn label(&self) -> String { let step = match self.step { - Step::Config => "配置", - Step::Dns => "DNS 解析", - Step::Tcp => "TCP 握手", - Step::Tls => "TLS 握手", - Step::Handshake => "代理握手", + Step::Config => "config", + Step::Dns => "DNS lookup", + Step::Tcp => "TCP handshake", + Step::Tls => "TLS handshake", + Step::Handshake => "proxy handshake", }; match (self.step, self.peer) { (Step::Handshake, _) | (_, Peer::Upstream) => step.to_string(), - (_, Peer::Proxy) => format!("{step} · 代理"), + (_, Peer::Proxy) => format!("{step} to the proxy"), } } } @@ -118,9 +119,11 @@ impl SkipReason { /// 命令行里的说法。 pub fn label(&self) -> &'static str { match self { - SkipReason::PlainHttp => "http:// 地址不进行 TLS 握手", - SkipReason::IpAddress => "地址已是 IP,无需 DNS 解析", - SkipReason::ProxyResolves => "域名由代理解析,本机不进行 DNS 解析", + SkipReason::PlainHttp => "an http:// address has no TLS handshake", + SkipReason::IpAddress => "already an IP address, so no DNS lookup", + SkipReason::ProxyResolves => { + "the proxy resolves the name, so there is no local DNS lookup" + } } } } @@ -153,7 +156,7 @@ pub struct L1Result { pub failed: Option, /// 失败的原因和下一步 #[serde(default, skip_serializing_if = "Option::is_none")] - pub error: Option, + pub error: Option, } /// 代理怎么走。调用方从 `tw_config::Proxy` 解析好再传进来 —— 解析密码 @@ -174,22 +177,22 @@ pub struct ProxyHop { /// **`system` 这里测不了**:跟随系统代理是 reqwest 在建连时才去查环境的, /// 我们没有那份地址可以去握手。说出来,而不是假装直连测一遍给个漂亮 /// 数字 —— 那个数字测的根本不是用户实际会走的路。 -pub fn hop_for( - cfg: &tw_config::Config, - p: &tw_config::Provider, -) -> Result, String> { +pub fn hop_for(cfg: &tw_config::Config, p: &tw_config::Provider) -> Result, Msg> { match p.proxy.as_str() { tw_config::DIRECT => Ok(None), - tw_config::SYSTEM => Err( - "该上游使用系统代理,代理地址在建立连接时才能确定,链路测速无法测量。如需测速,请将代理配置为命名代理" - .into(), - ), + tw_config::SYSTEM => Err(msg!( + "l1.config.system_proxy" => + "This upstream goes through the system proxy, whose address is only decided by the \ + environment when the connection is made, so a link test cannot measure it. Configure \ + the proxy as a named entry to measure this route." + )), name => { - let px = cfg - .proxies - .iter() - .find(|x| x.name == name) - .ok_or_else(|| format!("上游「{}」使用的代理「{name}」未在 proxies 中定义", p.name))?; + let px = cfg.proxies.iter().find(|x| x.name == name).ok_or_else(|| { + msg!( + "l1.config.proxy_undefined", upstream = p.name.clone(), proxy = name => + "Upstream `{upstream}` uses proxy `{proxy}`, which is not defined under `proxies`." + ) + })?; hop_of(px).map(Some) } } @@ -197,14 +200,17 @@ pub fn hop_for( /// 一个代理条目解析成一跳。**密码在这里取出来**,取不出来(环境变量没设) /// 直接说是哪个代理的哪一项。 -pub fn hop_of(px: &tw_config::Proxy) -> Result { +pub fn hop_of(px: &tw_config::Proxy) -> Result { let auth = match &px.auth { None => None, Some(a) => Some(( a.user.clone(), - a.pass - .resolve() - .map_err(|e| format!("无法读取代理「{}」的密码:{e}", px.name))?, + a.pass.resolve().map_err(|e| { + msg!( + "l1.config.proxy_password", proxy = px.name.clone(), detail = e => + "The password for proxy `{proxy}` could not be read: {detail}" + ) + })?, )), }; Ok(ProxyHop { @@ -262,14 +268,14 @@ impl Timer { fn total(&self) -> u64 { self.started.elapsed().as_millis() as u64 } - fn fail(self, stage: Stage, msg: impl std::fmt::Display) -> L1Result { + fn fail(self, stage: Stage, why: Msg) -> L1Result { L1Result { ok: false, total_ms: self.total(), segments: self.segments, skipped: self.skipped, failed: Some(stage), - error: Some(msg.to_string()), + error: Some(why), } } fn ok(self) -> L1Result { @@ -284,34 +290,49 @@ impl Timer { } } -/// 某一步超时的说法。**它同时是判据**:解析超时和解析失败要说成两句话。 -fn timed_out() -> String { - format!("{} 秒内未完成", PHASE_TIMEOUT.as_secs()) +/// 某一步超时的说法。 +const TIMED_OUT: &str = "l1.timeout"; + +fn timed_out() -> Msg { + msg!( + "l1.timeout", seconds = PHASE_TIMEOUT.as_secs() => + "did not finish within {seconds} s" + ) } -async fn phase(f: impl std::future::Future>) -> Result { +/// 这一段是超时,不是别的失败。**按码判,不按句子判** —— 上一版比的是 +/// 中文原话,于是「解析超时」和「解析失败」两句话之间隔着一次字符串相等, +/// 改一个字就会把超时说成解析失败。 +fn is_timeout(m: &Msg) -> bool { + m.code == TIMED_OUT +} + +async fn phase(f: impl std::future::Future>) -> Result { match tokio::time::timeout(PHASE_TIMEOUT, f).await { Ok(Ok(v)) => Ok(v), - Ok(Err(e)) => Err(e.to_string()), + Ok(Err(e)) => Err(msg!("l1.io", detail = e => "{detail}")), Err(_) => Err(timed_out()), } } /// 拆出 host / port / 要不要 TLS。 -fn target_of(base_url: &str) -> Result<(String, u16, bool), String> { - let u = reqwest::Url::parse(base_url).map_err(|e| format!("接口地址不是合法的 URL:{e}"))?; +fn target_of(base_url: &str) -> Result<(String, u16, bool), Msg> { + let u = reqwest::Url::parse(base_url).map_err(|e| { + msg!("l1.config.bad_url", detail = e => "The endpoint address is not a valid URL: {detail}") + })?; let tls = match u.scheme() { "https" => true, "http" => false, other => { - return Err(format!( - "接口地址的协议 {other} 不受支持,仅支持 http 和 https" + return Err(msg!( + "l1.config.unsupported_scheme", scheme = other => + "The endpoint address uses `{scheme}`, which is not supported; only http and https are." )); } }; let host = u .host_str() - .ok_or("接口地址中缺少主机名")? + .ok_or_else(|| msg!("l1.config.no_host" => "The endpoint address has no host name."))? .trim_matches(['[', ']']) .to_string(); Ok((host, u.port_or_known_default().unwrap_or(443), tls)) @@ -328,11 +349,11 @@ pub async fn l1(base_url: &str, proxy: Option<&ProxyHop>) -> L1Result { let stream: Box = match proxy { None => match connect_direct(&mut t, &host, port).await { Ok(s) => Box::new(s), - Err(r) => return r, + Err(r) => return *r, }, Some(p) => match connect_via_proxy(&mut t, p, &host, port).await { Ok(s) => s, - Err(r) => return r, + Err(r) => return *r, }, }; @@ -360,15 +381,17 @@ pub async fn l1_proxy(p: &ProxyHop, host: &str, port: u16) -> L1Result { let mut t = Timer::new(); match connect_via_proxy(&mut t, p, host, port).await { Ok(_) => t.ok(), - Err(r) => r, + Err(r) => *r, } } -async fn connect_direct(t: &mut Timer, host: &str, port: u16) -> Result { +/// **`Err` 侧装箱**:这里的「错」是一份已经量完的 `L1Result`,比一个 +/// 句柄大得多,而成功那一路每次都要把它抬着走。 +async fn connect_direct(t: &mut Timer, host: &str, port: u16) -> Result> { let dns = Stage::new(Step::Dns, Peer::Upstream); let addr = match resolve(t, host, port, dns).await { Ok(a) => a, - Err(e) => return Err(std::mem::replace(t, Timer::new()).fail(dns, e)), + Err(e) => return Err(Box::new(std::mem::replace(t, Timer::new()).fail(dns, e))), }; let tcp = Stage::new(Step::Tcp, Peer::Upstream); match phase(TcpStream::connect(addr)).await { @@ -377,13 +400,15 @@ async fn connect_direct(t: &mut Timer, host: &str, port: u16) -> Result Err(std::mem::replace(t, Timer::new()).fail(tcp, tcp_message(&e, addr))), + Err(e) => Err(Box::new( + std::mem::replace(t, Timer::new()).fail(tcp, tcp_message(&e, addr)), + )), } } /// 解析一个 `host:port`。**已经是 IP 时不记这一段** —— 记一个 0ms 的 /// DNS 解析会让人以为解析快得离谱,而实际是根本没发生。 -async fn resolve(t: &mut Timer, host: &str, port: u16, stage: Stage) -> Result { +async fn resolve(t: &mut Timer, host: &str, port: u16, stage: Stage) -> Result { if let Ok(ip) = host.parse::() { t.skip(stage, SkipReason::IpAddress); t.last = Instant::now(); @@ -395,18 +420,22 @@ async fn resolve(t: &mut Timer, host: &str, port: u16, stage: Stage) -> Result + "Resolving {host} did not finish within {seconds} s. The DNS server may be unreachable." ) } else { - format!("无法解析 {host}。请检查域名拼写;如果该地址需要经代理访问,请先配置代理") + msg!( + "l1.dns.failed", host = host => + "{host} could not be resolved. Check the spelling of the name; if this \ + address has to be reached through a proxy, configure the proxy first." + ) } })?; - let addr = it - .next() - .ok_or_else(|| format!("{host} 的解析结果中没有地址"))?; + let addr = it.next().ok_or_else( + || msg!("l1.dns.no_records", host = host => "{host} resolved, but to no addresses."), + )?; t.mark(stage); Ok(addr) } @@ -416,14 +445,14 @@ async fn connect_via_proxy( p: &ProxyHop, host: &str, port: u16, -) -> Result, L1Result> { +) -> Result, Box> { use tw_config::ProxyKind::*; let (phost, pport) = match split_hostport(&p.addr) { Ok(v) => v, Err(e) => { let stage = Stage::new(Step::Config, Peer::Proxy); - return Err(std::mem::replace(t, Timer::new()).fail(stage, e)); + return Err(Box::new(std::mem::replace(t, Timer::new()).fail(stage, e))); } }; @@ -433,7 +462,11 @@ async fn connect_via_proxy( let target_ip = if p.kind == Socks5 { match resolve(t, host, port, upstream_dns).await { Ok(a) => Some(a), - Err(e) => return Err(std::mem::replace(t, Timer::new()).fail(upstream_dns, e)), + Err(e) => { + return Err(Box::new( + std::mem::replace(t, Timer::new()).fail(upstream_dns, e), + )); + } } } else { t.skip(upstream_dns, SkipReason::ProxyResolves); @@ -443,7 +476,11 @@ async fn connect_via_proxy( let proxy_dns = Stage::new(Step::Dns, Peer::Proxy); let paddr = match resolve(t, &phost, pport, proxy_dns).await { Ok(a) => a, - Err(e) => return Err(std::mem::replace(t, Timer::new()).fail(proxy_dns, e)), + Err(e) => { + return Err(Box::new( + std::mem::replace(t, Timer::new()).fail(proxy_dns, e), + )); + } }; let proxy_tcp = Stage::new(Step::Tcp, Peer::Proxy); let tcp = match phase(TcpStream::connect(paddr)).await { @@ -453,8 +490,10 @@ async fn connect_via_proxy( s } Err(e) => { - let msg = tcp_message(&e, paddr); - return Err(std::mem::replace(t, Timer::new()).fail(proxy_tcp, msg)); + let why = tcp_message(&e, paddr); + return Err(Box::new( + std::mem::replace(t, Timer::new()).fail(proxy_tcp, why), + )); } }; @@ -466,7 +505,11 @@ async fn connect_via_proxy( t.mark(proxy_tls); s } - Err(e) => return Err(std::mem::replace(t, Timer::new()).fail(proxy_tls, e)), + Err(e) => { + return Err(Box::new( + std::mem::replace(t, Timer::new()).fail(proxy_tls, e), + )); + } } } else { Box::new(tcp) @@ -482,46 +525,70 @@ async fn connect_via_proxy( t.mark(handshake); Ok(io) } - Err(e) => Err(std::mem::replace(t, Timer::new()).fail(handshake, e)), + Err(e) => Err(Box::new( + std::mem::replace(t, Timer::new()).fail(handshake, e), + )), } } /// TCP 建连失败的系统原话是英文的,而且只有一个 errno。 /// 「Connection refused (os error 61)」和「那个端口上没有东西在听」 /// 之间隔着一次搜索。 -fn tcp_message(e: &str, addr: SocketAddr) -> String { - let l = e.to_ascii_lowercase(); +fn tcp_message(e: &Msg, addr: SocketAddr) -> Msg { + let l = e.text.to_ascii_lowercase(); if l.contains("refused") { - format!("{addr} 拒绝连接,该端口上没有服务在监听。请检查地址和端口") - } else if e == timed_out() || l.contains("timed out") { - format!("连接 {addr} 没有响应。请检查网络,或确认该地址是否需要经代理访问") + msg!( + "l1.tcp.refused", addr = addr => + "{addr} refused the connection: nothing is listening on that port. Check the address and the port." + ) + } else if is_timeout(e) || l.contains("timed out") { + msg!( + "l1.tcp.timeout", addr = addr => + "{addr} did not answer. Check the network, or whether this address has to be reached through a proxy." + ) } else if l.contains("unreachable") { - format!("无法访问 {addr} 所在的网络,请检查本机网络") + msg!( + "l1.tcp.unreachable", addr = addr => + "The network {addr} is on cannot be reached. Check this machine's network." + ) } else { - format!("无法连接 {addr}:{e}") + msg!("l1.tcp.failed", addr = addr, detail = e.text.clone() => "{addr} could not be reached: {detail}") } } -fn split_hostport(s: &str) -> Result<(String, u16), String> { +fn split_hostport(s: &str) -> Result<(String, u16), Msg> { // IPv6 字面量要带方括号 if let Some(rest) = s.strip_prefix('[') { - let (h, p) = rest - .split_once("]:") - .ok_or("IPv6 代理地址应写成 [::1]:1080 的形式")?; - return Ok((h.to_string(), p.parse().map_err(|_| "端口必须是数字")?)); + let (h, p) = rest.split_once("]:").ok_or_else(|| { + msg!( + "l1.config.proxy_addr_ipv6_form" => + "An IPv6 proxy address is written as [::1]:1080." + ) + })?; + return Ok(( + h.to_string(), + p.parse().map_err(|_| { + msg!("l1.config.proxy_port_not_a_number", port = p => "The port `{port}` is not a number.") + })?, + )); } - let (h, p) = s - .rsplit_once(':') - .ok_or("代理地址应写成 主机:端口 的形式")?; + let (h, p) = s.rsplit_once(':').ok_or_else( + || msg!("l1.config.proxy_addr_form" => "A proxy address is written as host:port."), + )?; // **裸的 IPv6 要报错,不能猜。**`fe80::1` 会被切成主机 `fe80:` 加 // 端口 `1` —— 一个语法上完全合法、语义上彻底错掉的结果,而它的表现 // 是「代理连不上」,没有任何线索指向这里。 if h.contains(':') { - return Err(format!("{s} 中的 IPv6 地址需要加方括号,应写成 [{h}]:{p}")); + return Err(msg!( + "l1.config.proxy_addr_ipv6_brackets", addr = s, host = h, port = p => + "The IPv6 address in {addr} needs brackets, as [{host}]:{port}." + )); } Ok(( h.to_string(), - p.parse().map_err(|_| format!("端口 {p} 不是数字"))?, + p.parse().map_err(|_| { + msg!("l1.config.proxy_port_not_a_number", port = p => "The port `{port}` is not a number.") + })?, )) } @@ -531,7 +598,7 @@ async fn socks5_connect( host: &str, port: u16, target_ip: Option, -) -> Result<(), String> { +) -> Result<(), Msg> { // 打招呼。**总是把「无认证」也报上去** —— 配了用户名密码但代理不要, // 只报 0x02 会被拒,而那个失败看起来像密码错了。 let greeting: &[u8] = if p.auth.is_some() { @@ -543,19 +610,25 @@ async fn socks5_connect( let mut m = [0u8; 2]; read_exact(io, &mut m).await?; if m[0] != 0x05 { - return Err(format!( - "代理的响应不是 SOCKS5 协议(版本字节 0x{:02x}),该端口上可能是 HTTP 代理", - m[0] + return Err(msg!( + "l1.socks5.not_socks5", byte = format!("0x{:02x}", m[0]) => + "The proxy did not answer SOCKS5 (version byte {byte}). An HTTP proxy may be running on that port." )); } match m[1] { 0x00 => {} 0x02 => { let Some((u, pw)) = &p.auth else { - return Err("代理要求用户名和密码认证,但该代理未配置认证信息".into()); + return Err(msg!( + "l1.socks5.auth_required" => + "The proxy asks for a user name and password, and none are configured for it." + )); }; if u.len() > 255 || pw.len() > 255 { - return Err("SOCKS5 的用户名和密码均不能超过 255 字节".into()); + return Err(msg!( + "l1.socks5.credentials_too_long" => + "A SOCKS5 user name and password are each limited to 255 bytes." + )); } let mut buf = vec![0x01, u.len() as u8]; buf.extend_from_slice(u.as_bytes()); @@ -565,11 +638,23 @@ async fn socks5_connect( let mut r = [0u8; 2]; read_exact(io, &mut r).await?; if r[1] != 0x00 { - return Err("代理拒绝了用户名和密码".into()); + return Err(msg!( + "l1.socks5.auth_rejected" => "The proxy rejected the user name and password." + )); } } - 0xFF => return Err("代理不接受所提供的认证方式".into()), - other => return Err(format!("代理要求的认证方式 0x{other:02x} 不受支持")), + 0xFF => { + return Err(msg!( + "l1.socks5.no_acceptable_auth" => + "The proxy accepts none of the authentication methods offered." + )); + } + other => { + return Err(msg!( + "l1.socks5.unsupported_auth", method = format!("0x{other:02x}") => + "The proxy asks for authentication method {method}, which is not supported." + )); + } } // CONNECT @@ -585,7 +670,10 @@ async fn socks5_connect( } None => { if host.len() > 255 { - return Err("域名超过 255 字节,无法通过 SOCKS5 发送".into()); + return Err(msg!( + "l1.socks5.host_too_long" => + "The host name is longer than 255 bytes and cannot be sent over SOCKS5." + )); } req.push(0x03); req.push(host.len() as u8); @@ -598,7 +686,7 @@ async fn socks5_connect( let mut head = [0u8; 4]; read_exact(io, &mut head).await?; if head[1] != 0x00 { - return Err(socks5_reply(head[1]).to_string()); + return Err(socks5_reply(head[1])); } // **绑定地址必须读掉**,否则它会留在流里,被后面的 TLS 当成握手数据。 let n = match head[3] { @@ -609,24 +697,39 @@ async fn socks5_connect( read_exact(io, &mut l).await?; l[0] as usize } - other => return Err(format!("代理返回了无法识别的地址类型 0x{other:02x}")), + other => { + return Err(msg!( + "l1.socks5.unknown_address_type", kind = format!("0x{other:02x}") => + "The proxy answered with address type {kind}, which is not recognized." + )); + } }; let mut rest = vec![0u8; n + 2]; read_exact(io, &mut rest).await?; Ok(()) } -fn socks5_reply(code: u8) -> &'static str { +/// SOCKS5 的拒绝理由。**每一种一个码** —— 「代理规则不允许」和「主机 +/// 不可达」要去改的地方完全不同,合成一句「代理拒绝了连接」等于把那个 +/// 区别扔掉。 +fn socks5_reply(code: u8) -> Msg { match code { - 0x01 => "代理内部错误", - 0x02 => "代理规则不允许连接该地址", - 0x03 => "网络不可达", - 0x04 => "主机不可达", - 0x05 => "目标拒绝连接", - 0x06 => "TTL 已过期", - 0x07 => "代理不支持 CONNECT", - 0x08 => "代理不支持该地址类型", - _ => "代理拒绝了连接", + 0x01 => msg!("l1.socks5.general_failure" => "The proxy reported an internal failure."), + 0x02 => { + msg!("l1.socks5.not_allowed" => "The proxy's rules do not allow a connection to this address.") + } + 0x03 => msg!("l1.socks5.network_unreachable" => "The proxy cannot reach that network."), + 0x04 => msg!("l1.socks5.host_unreachable" => "The proxy cannot reach that host."), + 0x05 => msg!("l1.socks5.connection_refused" => "The destination refused the connection."), + 0x06 => msg!("l1.socks5.ttl_expired" => "The TTL expired on the way."), + 0x07 => msg!("l1.socks5.command_unsupported" => "The proxy does not support CONNECT."), + 0x08 => { + msg!("l1.socks5.address_type_unsupported" => "The proxy does not support this address type.") + } + other => msg!( + "l1.socks5.rejected", reply = format!("0x{other:02x}") => + "The proxy refused the connection (reply {reply})." + ), } } @@ -635,7 +738,7 @@ async fn http_connect( p: &ProxyHop, host: &str, port: u16, -) -> Result<(), String> { +) -> Result<(), Msg> { let hostport = if host.contains(':') { format!("[{host}]:{port}") } else { @@ -661,7 +764,10 @@ async fn http_connect( break; } if buf.len() > 8192 { - return Err("代理的响应头超过 8 KB,该端口上可能不是 HTTP 代理".into()); + return Err(msg!( + "l1.http_proxy.headers_too_large" => + "The proxy's response headers passed 8 KB. Something other than an HTTP proxy may be running on that port." + )); } } let head = String::from_utf8_lossy(&buf); @@ -669,17 +775,26 @@ async fn http_connect( let code = line.split_whitespace().nth(1).unwrap_or(""); match code { "200" => Ok(()), - "407" => Err("代理要求认证(HTTP 407),请检查用户名和密码".into()), - "" => Err(format!("代理返回了无法识别的响应:{line}")), - c => Err(format!("代理拒绝了 CONNECT 请求(HTTP {c}):{line}")), + "407" => Err(msg!( + "l1.http_proxy.auth_required" => + "The proxy asks for authentication (HTTP 407). Check the user name and password." + )), + "" => Err(msg!( + "l1.http_proxy.unreadable_response", line = line => + "The proxy answered something unrecognized: {line}" + )), + c => Err(msg!( + "l1.http_proxy.connect_rejected", status = c, line = line => + "The proxy refused the CONNECT request (HTTP {status}): {line}" + )), } } -async fn write_all(io: &mut Box, buf: &[u8]) -> Result<(), String> { +async fn write_all(io: &mut Box, buf: &[u8]) -> Result<(), Msg> { phase(io.write_all(buf)).await } -async fn read_exact(io: &mut Box, buf: &mut [u8]) -> Result<(), String> { +async fn read_exact(io: &mut Box, buf: &mut [u8]) -> Result<(), Msg> { phase(async { io.read_exact(buf).await.map(|_| ()) }).await } @@ -695,20 +810,21 @@ pub(crate) fn tls_config() -> Arc { use rustls_platform_verifier::BuilderVerifierExt; let c = rustls::ClientConfig::builder() .with_platform_verifier() - .expect("无法创建平台证书验证器") + .expect("the platform certificate verifier could not be created") .with_no_client_auth(); Arc::new(c) }) .clone() } -async fn tls_handshake(stream: Box, host: &str) -> Result<(), String> { +async fn tls_handshake(stream: Box, host: &str) -> Result<(), Msg> { tls_handshake_boxed(stream, host).await.map(|_| ()) } -async fn tls_handshake_boxed(stream: Box, host: &str) -> Result, String> { - let name = rustls::pki_types::ServerName::try_from(host.to_string()) - .map_err(|_| format!("{host} 不是有效的 TLS 主机名"))?; +async fn tls_handshake_boxed(stream: Box, host: &str) -> Result, Msg> { + let name = rustls::pki_types::ServerName::try_from(host.to_string()).map_err( + |_| msg!("l1.tls.bad_hostname", host = host => "{host} is not a valid TLS host name."), + )?; let conn = tokio_rustls::TlsConnector::from(tls_config()); match tokio::time::timeout(PHASE_TIMEOUT, conn.connect(name, stream)).await { Ok(Ok(s)) => Ok(Box::new(s)), @@ -723,11 +839,11 @@ async fn tls_handshake_boxed(stream: Box, host: &str) -> Result String { +fn tls_message(e: &std::io::Error) -> Msg { let Some(rustls::Error::InvalidCertificate(ce)) = e.get_ref().and_then(|r| r.downcast_ref::()) else { - return e.to_string(); + return msg!("l1.tls.failed", detail = e => "The TLS handshake failed: {detail}"); }; let detail = match ce { rustls::CertificateError::Other(o) => o.to_string(), @@ -737,20 +853,31 @@ fn tls_message(e: &std::io::Error) -> String { // 塞进 `Other`,那些结构化变体在这个平台上永远不会出现 —— 按枚举 // 匹配的结果是每一种失败都拿到那句最泛的提示,包括证书只是过期时。 let d = detail.to_ascii_lowercase(); - let hint = - if d.contains("expired") || d.contains("not valid yet") || d.contains("not yet valid") { - "证书已过期或尚未生效,也可能是本机时间不准确,请先核对系统时间" - } else if d.contains("issuer") - || d.contains("self-signed") - || d.contains("self signed") - || d.contains("untrusted") - || d.contains("not trusted") - { - "证书的签发者不在系统信任列表中。如果本机安装了抓包工具或企业根证书,证书链可能已被替换" - } else { - "如果网络中有抓包工具或企业代理拦截 TLS 连接,也会出现此错误" - }; - format!("证书验证未通过:{detail}。{hint}") + if d.contains("expired") || d.contains("not valid yet") || d.contains("not yet valid") { + msg!( + "l1.tls.cert_expired", detail = detail => + "The certificate did not verify: {detail}. It has expired or is not valid yet; this \ + machine's clock may also be wrong, so check the system time first." + ) + } else if d.contains("issuer") + || d.contains("self-signed") + || d.contains("self signed") + || d.contains("untrusted") + || d.contains("not trusted") + { + msg!( + "l1.tls.cert_untrusted_issuer", detail = detail => + "The certificate did not verify: {detail}. Its issuer is not in the system trust \ + store. If an interception tool or a corporate root certificate is installed on this \ + machine, the certificate chain may have been replaced." + ) + } else { + msg!( + "l1.tls.cert_invalid", detail = detail => + "The certificate did not verify: {detail}. An interception tool or a corporate proxy \ + that terminates TLS on the network also produces this error." + ) + } } #[cfg(test)] @@ -858,7 +985,7 @@ mod tests { // 变成主机 `fe80:` 加端口 `1`,语法合法、语义全错,而表现只是 // 「代理连不上」。**报错好过悄悄猜。** let e = split_hostport("fe80::1").unwrap_err(); - assert!(e.contains("方括号"), "{e}"); + assert_eq!(e.code, "l1.config.proxy_addr_ipv6_brackets", "{e}"); assert!(split_hostport("::1:1080").is_err()); assert!(split_hostport("no-port").is_err()); } @@ -1039,7 +1166,7 @@ mod tests { let e = socks5_connect(&mut io, &p, "example.com", 443, None) .await .unwrap_err(); - assert!(e.contains("HTTP 代理"), "{e}"); + assert_eq!(e.code, "l1.socks5.not_socks5", "{e}"); } // ── 假的 HTTP CONNECT 代理 ────────────────────────────────────── @@ -1104,7 +1231,7 @@ mod tests { let mut io = dial(a).await; let p = hop(tw_config::ProxyKind::Http, a, None); let e = http_connect(&mut io, &p, "x.com", 443).await.unwrap_err(); - assert!(e.contains("用户名和密码"), "{e}"); + assert_eq!(e.code, "l1.http_proxy.auth_required", "{e}"); } #[tokio::test] @@ -1150,8 +1277,9 @@ mod tests { let r = l1_proxy(&p, "api.anthropic.com", 443).await; assert!(!r.ok); let e = r.error.unwrap(); - assert!(e.contains("没有服务在监听"), "{e}"); - assert!(!e.contains("os error"), "{e}"); + assert_eq!(e.code, "l1.tcp.refused", "{e}"); + // 系统原话(「Connection refused (os error 61)」)不往外发 + assert!(!e.text.contains("os error"), "{e}"); } #[tokio::test] diff --git a/crates/tw-gateway/src/server.rs b/crates/tw-gateway/src/server.rs index 7c44108a..9b5766e7 100644 --- a/crates/tw-gateway/src/server.rs +++ b/crates/tw-gateway/src/server.rs @@ -517,6 +517,7 @@ impl AppState { id: self.bus.next_id(), proxy: proxy.to_string(), state: "reachable".into(), + failed: None, detail: None, at_ms: now_ms(), }); @@ -555,30 +556,38 @@ impl AppState { let name = proxy.to_string(); tokio::spawn(async move { let cfg = state.config(); - let result = match cfg.proxies.iter().find(|p| p.name == name) { - Some(px) => match crate::l1::hop_of(px) { - Ok(hop) => { - let (host, port) = crate::l1::proxy_target(&cfg, &name); - let r = crate::l1::l1_proxy(&hop, &host, port).await; - if r.ok { - None - } else { - // 卡在哪一步 + 为什么。**两个都要**:一句「TCP 握手失败」 - // 说不出是地址错了还是代理没起来 - let step = r.failed.map(|f| f.label()).unwrap_or_default(); - let why = r.error.unwrap_or_else(|| "无法连接".into()); - Some(if step.is_empty() { - why + // 卡在哪一步 + 为什么。**两个都要**:一句「TCP 握手失败」说不出 + // 是地址错了还是代理没起来。两样分开发,界面自己组句 + let result: Option<(Option, tw_types::Msg)> = + match cfg.proxies.iter().find(|p| p.name == name) { + Some(px) => match crate::l1::hop_of(px) { + Ok(hop) => { + let (host, port) = crate::l1::proxy_target(&cfg, &name); + let r = crate::l1::l1_proxy(&hop, &host, port).await; + if r.ok { + None } else { - format!("{step}:{why}") - }) + Some(( + r.failed, + r.error.unwrap_or_else(|| { + tw_types::msg!( + "l1.unreachable" => "The proxy could not be reached." + ) + }), + )) + } } - } - Err(e) => Some(e), - }, - // 配置刚好在这中间改了,代理没了:不报 - None => None, - }; + Err(e) => Some(( + Some(crate::l1::Stage { + step: crate::l1::Step::Config, + peer: crate::l1::Peer::Proxy, + }), + e, + )), + }, + // 配置刚好在这中间改了,代理没了:不报 + None => None, + }; let changed = state .proxies .lock() @@ -598,19 +607,30 @@ impl AppState { return; } match &result { - Some(why) => tracing::warn!(proxy = %name, "代理不通:{why}"), - None => tracing::info!(proxy = %name, "代理又通了"), + Some((_, why)) => tracing::warn!(proxy = %name, "proxy is unreachable: {why}"), + None => tracing::info!(proxy = %name, "proxy is reachable again"), } + let (failed, detail) = match result { + Some((stage, why)) => ( + stage.map(|s| tw_api::L1Stage { + step: s.step.slug().into(), + peer: s.peer.slug().into(), + }), + Some(why), + ), + None => (None, None), + }; state.bus.emit(tw_api::Event::ProxyChanged { id: state.bus.next_id(), proxy: name, - state: if result.is_some() { + state: if detail.is_some() { "unreachable" } else { "reachable" } .into(), - detail: result, + failed, + detail, at_ms: now_ms(), }); }); diff --git a/crates/tw-gateway/tests/notices.rs b/crates/tw-gateway/tests/notices.rs index 0c326743..b92c71ab 100644 --- a/crates/tw-gateway/tests/notices.rs +++ b/crates/tw-gateway/tests/notices.rs @@ -174,7 +174,9 @@ async fn next_change( .. } if want == "proxy" => { assert_eq!(proxy, "代理一"); - return (state, detail); + // **返回码,不返回句子。**这条测试要的是「说清卡在哪一步」, + // 而句子随时会改措辞 —— 比字符串的话,改一个词就红 + return (state, detail.map(|d| d.code)); } _ => {} } @@ -232,9 +234,12 @@ async fn a_dead_proxy_is_named_as_the_cause_and_reported_again_when_it_comes_bac // 代理连上就断:请求失败,而失败的是代理不是上游 assert_ne!(ask(gw).await, 200); - let (state, detail) = next_change(&mut rx, "proxy").await; + let (state, code) = next_change(&mut rx, "proxy").await; assert_eq!(state, "unreachable"); - assert!(detail.is_some_and(|d| !d.is_empty()), "要说清卡在哪一步"); + assert!( + code.is_some_and(|c| c.starts_with("l1.")), + "要说清卡在哪一步" + ); broken.store(false, Ordering::SeqCst); assert_eq!(ask(gw).await, 200); diff --git a/crates/tw-types/src/lib.rs b/crates/tw-types/src/lib.rs index 85c76246..cc66f879 100644 --- a/crates/tw-types/src/lib.rs +++ b/crates/tw-types/src/lib.rs @@ -1,10 +1,79 @@ -//! 共享类型:请求/响应 DTO、调用上下文、网关错误。 +//! 共享类型:请求/响应 DTO、调用上下文、网关错误、给人看的话。 //! //! 这一层刻意零业务依赖 —— 它是企业版和桌面版都要认的契约, //! 任何在这里出现的第三方 crate 都会被两边同时背上。 +use std::collections::BTreeMap; + use serde::{Deserialize, Serialize}; +/// 一条给人看的话:一个稳定的码、填进句子的参数,以及英文原句。 +/// +/// **core 不翻译,只出英文。**桌面版有中英两套界面,而这些句子是从这里 +/// 发出去的。在这里翻译等于把「界面现在是什么语言」塞进一个同时服务 +/// 命令行、桌面版和企业版的网关进程 —— 那个问题在这一层没有答案。 +/// +/// 所以这里给的是码加参数:界面拿 `code` 去自己的词表里找句子,用 `args` +/// 填空。界面不认识这个码时(core 比界面新,或者第三方客户端根本没有 +/// 词表),显示 `text` —— 一句英文总好过一个码。 +/// +/// **码是契约,句子不是。**改措辞不用动码,界面那边什么都不用做;只有 +/// 语义变了才换码 —— 那时界面里那条旧文案会随着码一起失效,而不是 +/// 悄悄留在那里说着一件不再为真的事。 +/// +/// 码的写法是点分小写,从粗到细:`l1.dns.timeout`、`l1.proxy.auth_rejected`。 +/// 第一段是发出它的那一层。 +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Msg { + pub code: String, + /// 填进句子里的参数,按名字取。**值已经写成字符串** —— 界面只是 + /// 把它们放进自己那句话里,不需要知道原来是什么类型 + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub args: BTreeMap, + /// 英文原句,参数已经填好 + pub text: String, +} + +impl std::fmt::Display for Msg { + /// 命令行和日志里就用英文原句。 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.text) + } +} + +impl Msg { + /// 取一个参数。**没有就是空串** —— 调用方是在拼一句话,为一个缺席的 + /// 参数 panic 没有意义 + pub fn arg(&self, name: &str) -> &str { + self.args.get(name).map(String::as_str).unwrap_or_default() + } +} + +/// 造一条 [`Msg`]。 +/// +/// ```ignore +/// msg!("l1.dns.no_records", host = host => "`{host}` resolved, but to no addresses"); +/// ``` +/// +/// 参数先 `let` 出来,所以句子里直接写 `{host}` 就能取到,同时它们原样 +/// 进 `args` —— **两边不会说不同的话**,这正是分开写最容易出错的地方。 +#[macro_export] +macro_rules! msg { + ($code:expr $(, $name:ident = $value:expr)* $(,)? => $($fmt:tt)+) => {{ + $(let $name = $value;)* + $crate::Msg { + code: ($code).into(), + args: ::std::collections::BTreeMap::from([ + $(( + ::std::string::String::from(stringify!($name)), + ::std::string::ToString::to_string(&$name), + ),)* + ]), + text: format!($($fmt)+), + } + }}; +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ChatCompletionRequest { pub model: String, @@ -408,4 +477,36 @@ mod call_ctx_tests { Some("a@b.c") ); } + + #[test] + fn a_message_carries_its_arguments_beside_the_sentence() { + // 界面要照自己的语序重写这句话,所以参数必须单独拿得到 —— + // 从成句的英文里再切出主机名是切不准的。 + let m = msg!( + "l1.dns.timeout", host = "api.example.com", seconds = 8u64 => + "Resolving {host} did not finish within {seconds} s." + ); + assert_eq!(m.code, "l1.dns.timeout"); + assert_eq!(m.arg("host"), "api.example.com"); + assert_eq!(m.arg("seconds"), "8"); + assert_eq!( + m.text, + "Resolving api.example.com did not finish within 8 s." + ); + // 没有的参数是空串,不是 panic + assert_eq!(m.arg("port"), ""); + } + + #[test] + fn a_message_without_arguments_leaves_the_map_out_of_the_wire() { + let m = msg!("l1.config.no_host" => "The endpoint address has no host name."); + assert!(m.args.is_empty()); + let wire = serde_json::to_value(&m).unwrap(); + assert!(wire.get("args").is_none(), "{wire}"); + assert_eq!( + serde_json::from_value::(wire).unwrap(), + m, + "少了 args 也要读得回来" + ); + } }