連接器: 帳號密碼填了就作數(蓋掉標頭舊的 Authorization);既有連接器全欄位可編輯、改了就存自動重連

This commit is contained in:
daniel wang 2026-11-12 21:55:50 +08:00
parent 66dce03cee
commit 89e280b562
2 changed files with 55 additions and 6 deletions

View File

@ -505,8 +505,52 @@ impl App {
ui.add(egui::Label::new(RichText::new(&text).size(11.5).color(p.text3)).truncate()).on_hover_text(&text); ui.add(egui::Label::new(RichText::new(&text).size(11.5).color(p.text3)).truncate()).on_hover_text(&text);
}, },
); );
let what = if s.url.is_empty() { format!("{} {}", s.command, s.args.join(" ")) } else { s.url.clone() }; // 全部欄位都能改:改了就存(頁面尾端的簽名比對會標 dirty),
ui.label(RichText::new(what).monospace().size(11.5).color(p.text3)); // mcp_poll 偵測到簽名變動會自動重連。名稱、指令/網址、環境變數、標頭都 editable。
let mut cmd_edit = if s.url.is_empty() {
format!("{} {}", s.command, s.args.join(" ")).trim().to_string()
} else {
s.url.clone()
};
let mut env_edit: String = s.env.iter().map(|(k, v)| format!("{k}={v}")).collect::<Vec<_>>().join(" ");
let mut hdr_edit: String = s.headers.iter().map(|(k, v)| format!("{k}: {v}")).collect::<Vec<_>>().join(" ");
ui.horizontal(|ui| {
ui.label(RichText::new(t("名稱")).size(12.0).color(p.text3));
ui.add(egui::TextEdit::singleline(&mut s.name).desired_width((ui.available_width() - 44.0).max(120.0)));
});
let cmd_resp = ui.add(egui::TextEdit::singleline(&mut cmd_edit).font(egui::TextStyle::Monospace).hint_text(t("npx -y … 或 https://…/mcp")).desired_width(f32::INFINITY));
let env_resp = ui.add(egui::TextEdit::singleline(&mut env_edit).font(egui::TextStyle::Monospace).password(true).hint_text(t("環境變數:K=V K2=V2(本機用,選填)")).desired_width(f32::INFINITY));
let hide_hdr = !hdr_edit.contains('{') && !hdr_edit.is_empty();
let hdr_resp = ui.add(egui::TextEdit::singleline(&mut hdr_edit).font(egui::TextStyle::Monospace).password(hide_hdr).hint_text(t("標頭:Authorization: Bearer …(遠端用,選填)")).desired_width(f32::INFINITY));
if cmd_resp.changed() {
let c = cmd_edit.trim().to_string();
if c.starts_with("http://") || c.starts_with("https://") {
s.url = c;
s.command.clear();
s.args.clear();
} else {
let mut it = c.split_whitespace();
s.command = it.next().unwrap_or_default().to_string();
s.args = it.map(String::from).collect();
s.url.clear();
}
}
if env_resp.changed() {
s.env = env_edit
.split_whitespace()
.filter_map(|kv| kv.split_once('='))
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
}
if hdr_resp.changed() {
s.headers = hdr_edit
.split_once(':')
.map(|(k, v)| std::iter::once((k.trim().to_string(), v.trim().to_string())).collect())
.unwrap_or_default();
}
if s.name != s.name.trim() {
s.name = s.name.trim().to_string();
}
// 帳號 / 密碼: changed here, the server is connected again with the new ones. // 帳號 / 密碼: changed here, the server is connected again with the new ones.
ui.horizontal(|ui| { ui.horizontal(|ui| {
let w = ((ui.available_width() - 110.0) / 2.0).max(80.0); let w = ((ui.available_width() - 110.0) / 2.0).max(80.0);

View File

@ -67,7 +67,8 @@ impl Server {
/// - wherever 「{username}」 / 「{password}」 is written (arguments, variables' values, the /// - wherever 「{username}」 / 「{password}」 is written (arguments, variables' values, the
/// address, headers' values), that is where they go, e.g. `SCC_USER={username}`; /// address, headers' values), that is where they go, e.g. `SCC_USER={username}`;
/// - written nowhere: a command gets them as MCP_USERNAME / MCP_PASSWORD, an address gets /// - written nowhere: a command gets them as MCP_USERNAME / MCP_PASSWORD, an address gets
/// `Authorization: Basic …` (unless it has an Authorization header of its own). /// `Authorization: Basic …` — 帳號密碼填了就作數:它換掉標頭裡原有的 Authorization
/// (一條只裝得下一條,人現填的帳號密碼就是要送過去的那條)。
pub fn resolved(&self) -> Server { pub fn resolved(&self) -> Server {
let mut s = self.clone(); let mut s = self.clone();
let (user, pass) = (self.username.trim(), self.password.as_str()); let (user, pass) = (self.username.trim(), self.password.as_str());
@ -89,8 +90,10 @@ impl Server {
if s.url.trim().is_empty() { if s.url.trim().is_empty() {
s.env.entry(USER_ENV.into()).or_insert_with(|| user.to_string()); s.env.entry(USER_ENV.into()).or_insert_with(|| user.to_string());
s.env.entry(PASS_ENV.into()).or_insert_with(|| pass.to_string()); s.env.entry(PASS_ENV.into()).or_insert_with(|| pass.to_string());
} else if !s.headers.keys().any(|k| k.eq_ignore_ascii_case("authorization")) { } else {
// 帳號密碼填了就作數:換掉舊的金鑰標頭(一條 Authorization 裝不下兩條)。
use base64::Engine; use base64::Engine;
s.headers.retain(|k, _| !k.eq_ignore_ascii_case("authorization"));
s.headers.insert("Authorization".into(), format!("Basic {}", base64::engine::general_purpose::STANDARD.encode(format!("{user}:{pass}")))); s.headers.insert("Authorization".into(), format!("Basic {}", base64::engine::general_purpose::STANDARD.encode(format!("{user}:{pass}"))));
} }
} }
@ -1365,12 +1368,14 @@ for line in sys.stdin:
s.env.insert(USER_ENV.into(), "own".into()); s.env.insert(USER_ENV.into(), "own".into());
let r = s.resolved(); let r = s.resolved();
assert_eq!((r.env[USER_ENV].as_str(), r.env[PASS_ENV].as_str()), ("own", "pw")); assert_eq!((r.env[USER_ENV].as_str(), r.env[PASS_ENV].as_str()), ("own", "pw"));
// No slot, an address: Basic, unless it has its own Authorization. // No slot, an address: Basic, replacing a key header (one Authorization only).
let s = Server { url: "https://scc.example/mcp".into(), username: "amy".into(), password: "pw".into(), ..blank.clone() }; let s = Server { url: "https://scc.example/mcp".into(), username: "amy".into(), password: "pw".into(), ..blank.clone() };
assert_eq!(s.resolved().headers["Authorization"], "Basic YW15OnB3"); assert_eq!(s.resolved().headers["Authorization"], "Basic YW15OnB3");
let mut own = s.clone(); let mut own = s.clone();
own.headers.insert("authorization".into(), "Bearer k".into()); own.headers.insert("authorization".into(), "Bearer k".into());
assert_eq!(own.resolved().headers.len(), 1); let r = own.resolved();
assert_eq!(r.headers.len(), 1, "the key header makes way: {r:?}");
assert_eq!(r.headers.values().next().unwrap().as_str(), "Basic YW15OnB3");
let mut slot = s.clone(); let mut slot = s.clone();
slot.headers.insert("X-Auth".into(), "{username}:{password}".into()); slot.headers.insert("X-Auth".into(), "{username}:{password}".into());
assert_eq!(slot.resolved().headers, [("X-Auth".to_string(), "amy:pw".to_string())].into_iter().collect()); assert_eq!(slot.resolved().headers, [("X-Auth".to_string(), "amy:pw".to_string())].into_iter().collect());