From ea0d3436d28fc12fbf0a242da4ae0f7d789bc381 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Mon, 30 Nov 2026 16:49:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20APN=20=E5=BC=B9=E7=AA=97=E5=9B=9E?= =?UTF-8?q?=E5=A1=AB=E5=AF=86=E7=A0=81=EF=BC=8C=E5=86=85=E5=AE=B9=E6=B2=A1?= =?UTF-8?q?=E5=8F=98=E6=97=B6=E4=B8=8D=E5=86=8D=E6=94=B9=E5=86=99=E5=9F=BA?= =?UTF-8?q?=E5=B8=A6=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 读 APN 时没取密码,打开弹窗再保存会把基带和连接里的密码清空。现在从 QMI 和 ModemManager 都读出密码, 密码框加显示/隐藏按钮。密码按原样解析,不去首尾空格;写进网络管理器配置时按 keyfile 规则转义开头空格。 基带 1 号配置内容没变就不改写:改写会让基带重新附着,原样保存也会断一次数据连接。 --- mifipanel/src/cellular.rs | 50 ++++++++++++++++++++++++++++---- mifipanel/src/classic.rs | 13 +++++---- mifipanel/src/pages/welcome.html | 14 +++++++-- mifipanel/src/radio_adapt.rs | 28 ++++++++++++++++-- 4 files changed, 90 insertions(+), 15 deletions(-) diff --git a/mifipanel/src/cellular.rs b/mifipanel/src/cellular.rs index 7577478..7f3d897 100644 --- a/mifipanel/src/cellular.rs +++ b/mifipanel/src/cellular.rs @@ -544,15 +544,15 @@ pub(crate) fn render_profile(profile: &WanProfile) -> String { let on = if profile.enabled { "true" } else { "false" }; let mut gsm = String::from("auto-config=false\n"); if !profile.apn.is_empty() { - gsm.push_str(&format!("apn={}\n", profile.apn)); + gsm.push_str(&format!("apn={}\n", keyfile_escape(&profile.apn))); if !profile.user.is_empty() { - gsm.push_str(&format!("username={}\n", profile.user)); + gsm.push_str(&format!("username={}\n", keyfile_escape(&profile.user))); } // 没有密码时标成不需要,否则填了用户名的连接会一直等密码。 if profile.password.is_empty() { gsm.push_str("password-flags=4\n"); } else { - gsm.push_str(&format!("password={}\npassword-flags=0\n", profile.password)); + gsm.push_str(&format!("password={}\npassword-flags=0\n", keyfile_escape(&profile.password))); } } let (v4, v6) = match profile.ip.as_str() { @@ -569,6 +569,7 @@ pub(crate) fn parse_profile(text: &str) -> WanProfile { let mut v4 = String::new(); let mut v6 = String::new(); for raw in text.lines() { + let raw = raw.trim_end_matches('\r'); let line = raw.trim(); if line.is_empty() || line.starts_with('#') { continue; @@ -577,8 +578,10 @@ pub(crate) fn parse_profile(text: &str) -> WanProfile { section = line[1..line.len() - 1].to_string(); continue; } - let Some((key, value)) = line.split_once('=') else { continue }; - let value = value.trim(); + // 值只去掉等号后面的空白,结尾的空格是值的一部分(密码里可能有)。 + let Some((key, value)) = raw.trim_start().split_once('=') else { continue }; + let value = keyfile_unescape(value.trim_start()); + let value = value.as_str(); match (section.as_str(), key.trim()) { ("connection", "uuid") if !value.is_empty() => profile.uuid = value.to_string(), ("connection", "autoconnect") => profile.enabled = !matches!(value, "false" | "no" | "0"), @@ -598,6 +601,35 @@ pub(crate) fn parse_profile(text: &str) -> WanProfile { profile } +/// 网络管理器的配置文件读值时会吃掉开头的空格,开头的空格要写成 \s,反斜杠要写成 \\。 +fn keyfile_escape(value: &str) -> String { + let escaped = value.replace('\\', "\\\\").replace('\t', "\\t"); + match escaped.strip_prefix(' ') { + Some(rest) => format!("\\s{rest}"), + None => escaped, + } +} + +fn keyfile_unescape(value: &str) -> String { + let mut out = String::new(); + let mut chars = value.chars(); + while let Some(ch) = chars.next() { + if ch != '\\' { + out.push(ch); + continue; + } + match chars.next() { + Some('s') => out.push(' '), + Some('t') => out.push('\t'), + Some('n') => out.push('\n'), + Some('r') => out.push('\r'), + Some(other) => out.push(other), + None => out.push('\\'), + } + } + out +} + /// 读 `nmcli -t -f GENERAL.STATE,IP4.ADDRESS,IP6.ADDRESS connection show cellular` 的输出。 /// 连接没激活时没有 GENERAL.STATE 这一行,算未连接。地址优先给 IPv4。 pub(crate) fn parse_show(text: &str) -> (&'static str, String) { @@ -734,7 +766,7 @@ fn maintain_wan_linux() { } /// 基带 1 号配置同时是 LTE 附着用的接入点。它里面存着 APN 时,基带不接受空 APN 拨号 -/// (null-apn-disallowed),所以连接配置没写 APN 就照抄基带的。基带读不出密码,只抄 APN 和用户名。 +/// (null-apn-disallowed),所以连接配置没写 APN 就照抄基带的。 #[cfg(target_os = "linux")] fn adopt_modem_apn() { let mut profile = read_profile(); @@ -747,6 +779,7 @@ fn adopt_modem_apn() { } profile.apn = modem.apn; profile.user = modem.user; + profile.password = modem.password; profile.ip = modem.ip; if write_profile(&profile).is_ok() { let _ = load_profile(); @@ -1035,6 +1068,11 @@ mod tests { assert!(text.contains("autoconnect=false\n")); assert!(text.contains("method=disabled\n")); assert_eq!(parse_profile(&text), profile); + let spaced = WanProfile { password: " pw it's 1 ".into(), ..profile.clone() }; + let text = render_profile(&spaced); + assert!(text.contains("\npassword=\\spw it's 1 \n")); + assert_eq!(parse_profile(&text), spaced); + assert_eq!(parse_profile(&text.replace('\n', "\r\n")), spaced); let open = render_profile(&WanProfile { password: String::new(), ..profile.clone() }); assert!(open.contains("password-flags=4\n")); assert!(!open.contains("password=")); diff --git a/mifipanel/src/classic.rs b/mifipanel/src/classic.rs index dd1ec1d..ff7a31d 100644 --- a/mifipanel/src/classic.rs +++ b/mifipanel/src/classic.rs @@ -248,11 +248,14 @@ async fn apn_set(State(app): State>, headers: HeaderMap, axum::Jso let wan = (body.apn.clone(), body.ip.clone(), body.user.clone(), body.password.clone()); let changed = block_modem(Arc::clone(&app), move |_| { let before = crate::radio_adapt::apn_get().ok(); - crate::radio_adapt::apn_set(&body.apn, &body.ip, &body.user, &body.password, &body.auth)?; - // 基带读不出密码,填了密码就当作改过。 - Ok(before.map_or(true, |old| { - old.apn != body.apn || old.ip != body.ip || old.user != body.user || old.auth != body.auth || !body.password.is_empty() - })) + let changed = before.map_or(true, |old| { + old.apn != body.apn || old.ip != body.ip || old.user != body.user || old.auth != body.auth || old.password != body.password + }); + // 1 号配置也是附着用的,内容没变也去改写的话基带会重新附着,数据连接白白断一次。 + if changed { + crate::radio_adapt::apn_set(&body.apn, &body.ip, &body.user, &body.password, &body.auth)?; + } + Ok(changed) }).await; let changed = match changed { Ok(changed) => changed, diff --git a/mifipanel/src/pages/welcome.html b/mifipanel/src/pages/welcome.html index bbd589b..783e65d 100644 --- a/mifipanel/src/pages/welcome.html +++ b/mifipanel/src/pages/welcome.html @@ -907,7 +907,7 @@ function apnDialog(data) { var api = (window.parent && window.parent.layer) || window.layer; if (!api || !api.open) return; - var css = ""; + var css = ""; var ip = data.ip || "ipv4v6"; var auth = data.auth || "none"; function option(value, label) { return ""; } @@ -915,7 +915,8 @@ body += ""; body += ""; body += ""; - body += ""; + var eye = ""; + body += ""; body += ""; var html = css + "
" + body + "
"; var view = window.parent || window; @@ -931,6 +932,15 @@ if (content) { content.style.overflow = "visible"; content.style.padding = "0"; content.style.height = "auto"; } var cancel = root.querySelector("#apn-cancel"); if (cancel) cancel.onclick = function () { api.close(index); }; + var eyeButton = root.querySelector(".apn-eye"); + if (eyeButton) eyeButton.onclick = function (event) { + event.preventDefault(); + var input = root.querySelector("#apn-password"); + var shown = input.type === "password"; + input.type = shown ? "text" : "password"; + eyeButton.classList.toggle("is-open", shown); + eyeButton.title = I18n.t(shown ? "wifi.hide" : "wifi.show"); + }; var save = root.querySelector("#apn-save"); if (!save) return; save.onclick = function () { diff --git a/mifipanel/src/radio_adapt.rs b/mifipanel/src/radio_adapt.rs index c5078e2..be14723 100644 --- a/mifipanel/src/radio_adapt.rs +++ b/mifipanel/src/radio_adapt.rs @@ -446,10 +446,12 @@ fn mm_apn_get(modem: u32) -> Result { let mut apn = field(&text, "modem.3gpp.eps.initial-bearer.settings.apn"); let mut ip = field(&text, "modem.3gpp.eps.initial-bearer.settings.ip-type"); let mut user = field(&text, "modem.3gpp.eps.initial-bearer.settings.user"); + let mut password = field(&text, "modem.3gpp.eps.initial-bearer.settings.password"); if apn == "--" { apn.clear(); } if user == "--" { user.clear(); } + if password == "--" { password.clear(); } if ip.is_empty() || ip == "--" { ip = "ipv4v6".into(); } - Ok(Apn { apn, ip, user, password: String::new(), auth: "none".into() }) + Ok(Apn { apn, ip, user, password, auth: "none".into() }) } #[cfg(target_os = "linux")] @@ -495,6 +497,7 @@ fn qmi_apn_get() -> Option { let mut apn = quoted_after(&text, "APN:"); let pdp = quoted_after(&text, "PDP type:"); let user = quoted_after(&text, "Username:"); + let password = quoted_exact(&text, "Password:"); let auth = quoted_after(&text, "Auth:"); if apn == "''" { apn.clear(); } let ip = match pdp.as_str() { @@ -509,7 +512,7 @@ fn qmi_apn_get() -> Option { (false, true) => "chap", _ => "none", }; - Some(Apn { apn, ip: ip.into(), user, password: String::new(), auth: auth.into() }) + Some(Apn { apn, ip: ip.into(), user, password, auth: auth.into() }) } #[cfg(target_os = "linux")] @@ -568,6 +571,19 @@ fn quoted_after(text: &str, label: &str) -> String { String::new() } +/// 密码里可以有空格和单引号,只去掉 qmicli 包在外面的那一对单引号,里面原样保留。 +fn quoted_exact(text: &str, label: &str) -> String { + for line in text.lines() { + let Some(rest) = line.trim_start().strip_prefix(label) else { continue }; + let rest = rest.strip_prefix(' ').unwrap_or(rest).trim_end_matches(['\r', '\n']); + return match rest.strip_prefix('\'').and_then(|inner| inner.strip_suffix('\'')) { + Some(inner) => inner.to_string(), + None => rest.trim().to_string(), + }; + } + String::new() +} + #[cfg(target_os = "linux")] fn mmcli_kv(modem: u32) -> Result { let mut cmd = std::process::Command::new("mmcli"); @@ -626,6 +642,14 @@ mod tests { assert_eq!(validate_apn("cmnet", "ipv5", "", "", "none"), Err("error.apn.ip".into())); } + #[test] + fn profile_password_is_read_exactly() { + let text = "\t\tUsername: 'probeuser'\n\t\tPassword: ' it's a pw '\n\t\tAuth: 'none'\n"; + assert_eq!(quoted_exact(text, "Password:"), " it's a pw "); + assert_eq!(quoted_exact("\t\tPassword: ''\n", "Password:"), ""); + assert_eq!(quoted_exact("\t\tUsername: ''\n", "Password:"), ""); + } + #[test] fn embedded_catalog_parses() { let schemes = parse_schemes(EMBEDDED).unwrap();