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
20 changes: 20 additions & 0 deletions crates/tw-config/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ impl ProxyKind {
}
}

/// 写错的字段名是错误,和配置的其余部分一样(见 `Config` 上那段):`passwd`
/// 被静默丢掉的话,表现是代理一直 407,而配置文件里看着什么都写了。
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProxyAuth {
pub user: String,
/// 支持 `${ENV}`,和上游的密钥同一套
pub pass: crate::Secret,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Proxy {
pub name: String,
#[serde(default, rename = "type")]
Expand Down Expand Up @@ -198,6 +202,22 @@ mod tests {
);
}

/// `typ: http` 被悄悄丢掉的话,代理按默认的 socks5h 去连一个 HTTP 代理,
/// 报的是连不上,而配置文件里看着明明写了类型。
#[test]
fn a_misspelled_proxy_field_is_an_error_that_names_it() {
let e = serde_yaml_ng::from_str::<Proxy>("{name: p, addr: '127.0.0.1:7890', typ: http}")
.unwrap_err()
.to_string();
assert!(e.contains("typ"), "{e}");
let e = serde_yaml_ng::from_str::<Proxy>(
"{name: p, addr: '127.0.0.1:7890', auth: {user: a, passwd: b}}",
)
.unwrap_err()
.to_string();
assert!(e.contains("passwd"), "{e}");
}

#[test]
fn failing_is_the_default_not_silently_going_direct() {
// 静默降级最糟的情况不是失败,是它**真的连上了**,而你以为
Expand Down
2 changes: 1 addition & 1 deletion crates/tw-control/tests/live_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ async fn the_overview_says_which_proxy_is_down_and_why() {
let dead = dead_addr();
let d = tempfile::tempdir().unwrap();
let yaml = format!(
"version: 1\nlisten:\n control:\n key: c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00\nclients:\n - name: 我\n key: tw-k\nproxies:\n - name: 代理一\n kind: http\n addr: {dead}\nproviders:\n - name: 中转\n base_url: http://127.0.0.1:9\n protocol: anthropic\n key: sk-x\n proxy: 代理一\n"
"version: 1\nlisten:\n control:\n key: c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00\nclients:\n - name: 我\n key: tw-k\nproxies:\n - name: 代理一\n type: http\n addr: {dead}\nproviders:\n - name: 中转\n base_url: http://127.0.0.1:9\n protocol: anthropic\n key: sk-x\n proxy: 代理一\n"
);
let (gw, app) = control(&d, &yaml, None);
let mut rx = gw.bus.subscribe();
Expand Down
Loading