fix: APN 弹窗回填密码,内容没变时不再改写基带配置
读 APN 时没取密码,打开弹窗再保存会把基带和连接里的密码清空。现在从 QMI 和 ModemManager 都读出密码, 密码框加显示/隐藏按钮。密码按原样解析,不去首尾空格;写进网络管理器配置时按 keyfile 规则转义开头空格。 基带 1 号配置内容没变就不改写:改写会让基带重新附着,原样保存也会断一次数据连接。
This commit is contained in:
@@ -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="));
|
||||
|
||||
@@ -248,11 +248,14 @@ async fn apn_set(State(app): State<Arc<AppState>>, 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,
|
||||
|
||||
@@ -907,7 +907,7 @@
|
||||
function apnDialog(data) {
|
||||
var api = (window.parent && window.parent.layer) || window.layer;
|
||||
if (!api || !api.open) return;
|
||||
var css = "<style>.apn-box{background:#fff}.apn-body{padding:12px 16px 4px}.apn-row{display:flex;align-items:center;gap:12px;margin:0 0 10px}.apn-row span{width:72px;flex:none;color:#606266}.apn-row input,.apn-row select{flex:1;height:32px;border:1px solid #dcdfe6;border-radius:3px;padding:0 8px;background:#fff;color:#303133}.apn-error{margin:0 0 8px;color:#c45656;font-size:13px}.apn-foot{display:flex;justify-content:flex-end;gap:8px;padding:10px 16px;border-top:1px solid #e4e7ed}</style>";
|
||||
var css = "<style>.apn-box{background:#fff}.apn-body{padding:12px 16px 4px}.apn-row{display:flex;align-items:center;gap:12px;margin:0 0 10px}.apn-row>span:first-child{width:72px;flex:none;color:#606266}.apn-row input,.apn-row select{flex:1;height:32px;border:1px solid #dcdfe6;border-radius:3px;padding:0 8px;background:#fff;color:#303133}.apn-secret{position:relative;flex:1;display:flex;min-width:0}.apn-secret input{min-width:0;padding-right:34px}.apn-eye{position:absolute;right:2px;top:50%;transform:translateY(-50%);width:28px;height:28px;border:0;background:transparent;color:#909399;cursor:pointer;padding:0;display:inline-flex;align-items:center;justify-content:center}.apn-eye:hover{color:#303133}.apn-eye svg{width:16px;height:16px;display:none}.apn-eye .eye-on{display:block}.apn-eye.is-open .eye-on{display:none}.apn-eye.is-open .eye-off{display:block}.apn-error{margin:0 0 8px;color:#c45656;font-size:13px}.apn-foot{display:flex;justify-content:flex-end;gap:8px;padding:10px 16px;border-top:1px solid #e4e7ed}</style>";
|
||||
var ip = data.ip || "ipv4v6";
|
||||
var auth = data.auth || "none";
|
||||
function option(value, label) { return "<option value=\"" + value + "\"" + (value === ip || value === auth ? " selected" : "") + ">" + esc(label) + "</option>"; }
|
||||
@@ -915,7 +915,8 @@
|
||||
body += "<label class=\"apn-row\"><span>" + esc(I18n.t("apn.name")) + "</span><input id=\"apn-name\" placeholder=\"" + esc(I18n.t("apn.auto")) + "\" value=\"" + esc(data.apn || "") + "\"></label>";
|
||||
body += "<label class=\"apn-row\"><span>" + esc(I18n.t("apn.ip")) + "</span><select id=\"apn-ip\">" + option("ipv4v6", I18n.t("apn.ipv4v6")) + option("ipv4", I18n.t("apn.ipv4")) + option("ipv6", I18n.t("apn.ipv6")) + "</select></label>";
|
||||
body += "<label class=\"apn-row\"><span>" + esc(I18n.t("apn.user")) + "</span><input id=\"apn-user\" value=\"" + esc(data.user || "") + "\"></label>";
|
||||
body += "<label class=\"apn-row\"><span>" + esc(I18n.t("apn.password")) + "</span><input id=\"apn-password\" type=\"password\" value=\"" + esc(data.password || "") + "\"></label>";
|
||||
var eye = "<button type=\"button\" class=\"apn-eye\" title=\"" + esc(I18n.t("wifi.show")) + "\"><svg class=\"eye-on\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\"><path d=\"M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12z\"/><circle cx=\"12\" cy=\"12\" r=\"3\"/></svg><svg class=\"eye-off\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\"><path d=\"M3 3l18 18\"/><path d=\"M10.6 6.2A10.7 10.7 0 0 1 12 6c6.5 0 10 6 10 6a18.4 18.4 0 0 1-4.1 4.6\"/><path d=\"M6.1 6.1C3.6 7.8 2 12 2 12s3.5 7 10 7a10.8 10.8 0 0 0 4.2-.8\"/><path d=\"M9.9 9.9a3 3 0 0 0 4.2 4.2\"/></svg></button>";
|
||||
body += "<label class=\"apn-row\"><span>" + esc(I18n.t("apn.password")) + "</span><span class=\"apn-secret\"><input id=\"apn-password\" type=\"password\" autocomplete=\"off\" value=\"" + esc(data.password || "") + "\">" + eye + "</span></label>";
|
||||
body += "<label class=\"apn-row\"><span>" + esc(I18n.t("apn.auth")) + "</span><select id=\"apn-auth\">" + ["none", "pap", "chap", "pap-chap"].map(function (value) { return "<option value=\"" + value + "\"" + (value === auth ? " selected" : "") + ">" + esc(I18n.t("apn." + (value === "pap-chap" ? "both" : value === "none" ? "none" : value))) + "</option>"; }).join("") + "</select></label>";
|
||||
var html = css + "<div class=\"apn-box\"><div class=\"apn-body\">" + body + "</div><div class=\"apn-foot\"><button type=\"button\" class=\"layui-btn layui-btn-primary layui-btn-sm\" id=\"apn-cancel\">" + esc(I18n.t("apn.cancel")) + "</button><button type=\"button\" class=\"layui-btn layui-btn-sm\" id=\"apn-save\">" + esc(I18n.t("apn.save")) + "</button></div></div>";
|
||||
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 () {
|
||||
|
||||
@@ -446,10 +446,12 @@ fn mm_apn_get(modem: u32) -> Result<Apn, String> {
|
||||
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<Apn> {
|
||||
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<Apn> {
|
||||
(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<String, String> {
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user