Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions bin/twcore/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,15 @@ fn cmd_speed(path: &Path, provider: Option<String>, proxy: Option<String>) -> 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,
);
Expand All @@ -453,12 +456,12 @@ fn cmd_speed(path: &Path, provider: Option<String>, proxy: Option<String>) -> 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 {
Expand All @@ -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()?)),
Expand All @@ -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}"),
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/tw-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
15 changes: 12 additions & 3 deletions crates/tw-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -350,9 +354,14 @@ pub enum Event {
proxy: String,
/// `unreachable` = 刚刚检查不通;`reachable` = 又通了
state: String,
/// 不通时卡在哪一步,已脱敏。通了没有
/// 不通时卡在建连的哪一步。**和原因分开** —— 「卡在到代理的 TCP
/// 握手」要去改的地方和「代理拒绝了用户名和密码」完全不同,而把
/// 两句话拼成一个字符串之后,界面就只能整句照搬
#[serde(default, skip_serializing_if = "Option::is_none")]
failed: Option<L1Stage>,
/// 不通的原因,已脱敏。通了没有
#[serde(default, skip_serializing_if = "Option::is_none")]
detail: Option<String>,
detail: Option<Msg>,
at_ms: u64,
},
/// 上游拒绝了我们的凭据(401/403),或者重新接受了。
Expand Down Expand Up @@ -1105,7 +1114,7 @@ pub struct L1Result {
pub failed: Option<L1Stage>,
/// 失败的原因
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
pub error: Option<Msg>,
}

/// 当前的配置文本,连同它的版本号。
Expand Down
3 changes: 2 additions & 1 deletion crates/tw-control/tests/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading
Loading