feat: 放宽升级与备份上限,在线升级不再装离线程序
升级 128MB,整份备份 10MB,单个配置 5MB。测试板仍整机重刷,离线程序由刷机工具带上。
This commit is contained in:
@@ -22,7 +22,7 @@ pub fn routes() -> Router<Arc<AppState>> {
|
|||||||
.route("/api/system/access", get(access_status).post(access_save))
|
.route("/api/system/access", get(access_status).post(access_save))
|
||||||
.route("/api/system/username", post(username_save))
|
.route("/api/system/username", post(username_save))
|
||||||
.route("/api/system/backup", get(backup_export))
|
.route("/api/system/backup", get(backup_export))
|
||||||
.route("/api/system/restore", post(backup_import))
|
.route("/api/system/restore", post(backup_import).layer(DefaultBodyLimit::max(system::MAX_BACKUP)))
|
||||||
.route("/api/system/factory", post(factory_reset))
|
.route("/api/system/factory", post(factory_reset))
|
||||||
.route("/api/system/time", get(time_status).post(time_set))
|
.route("/api/system/time", get(time_status).post(time_set))
|
||||||
.route("/api/system/timezone", post(timezone_set))
|
.route("/api/system/timezone", post(timezone_set))
|
||||||
|
|||||||
@@ -490,7 +490,7 @@
|
|||||||
var input = document.getElementById("import-file");
|
var input = document.getElementById("import-file");
|
||||||
var file = input.files && input.files[0];
|
var file = input.files && input.files[0];
|
||||||
if (!file) { toast(I18n.t("error.system.file"), false); return; }
|
if (!file) { toast(I18n.t("error.system.file"), false); return; }
|
||||||
if (file.size > 2 * 1024 * 1024) { toast(I18n.t("error.system.size"), false); return; }
|
if (file.size > 10 * 1024 * 1024) { toast(I18n.t("error.system.size"), false); return; }
|
||||||
var button = this;
|
var button = this;
|
||||||
ask(I18n.t("system.importAsk"), function () {
|
ask(I18n.t("system.importAsk"), function () {
|
||||||
busy(button, I18n.t("system.importing"), async function () {
|
busy(button, I18n.t("system.importing"), async function () {
|
||||||
@@ -526,7 +526,7 @@
|
|||||||
var input = document.getElementById("upgrade-file");
|
var input = document.getElementById("upgrade-file");
|
||||||
var file = input.files && input.files[0];
|
var file = input.files && input.files[0];
|
||||||
if (!file) { toast(I18n.t("error.system.file"), false); return; }
|
if (!file) { toast(I18n.t("error.system.file"), false); return; }
|
||||||
if (file.size > 32 * 1024 * 1024) { toast(I18n.t("error.system.size"), false); return; }
|
if (file.size > 128 * 1024 * 1024) { toast(I18n.t("error.system.size"), false); return; }
|
||||||
var button = this;
|
var button = this;
|
||||||
ask(I18n.t("system.upgradeAsk"), function () {
|
ask(I18n.t("system.upgradeAsk"), function () {
|
||||||
busy(button, I18n.t("system.upgrading"), async function () {
|
busy(button, I18n.t("system.upgrading"), async function () {
|
||||||
|
|||||||
+8
-60
@@ -12,9 +12,9 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
|||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
const PREFS_FILE: &str = "system.json";
|
const PREFS_FILE: &str = "system.json";
|
||||||
const MAX_BACKUP: usize = 2 * 1024 * 1024;
|
pub const MAX_BACKUP: usize = 10 * 1024 * 1024;
|
||||||
const MAX_FILE: usize = 1024 * 1024;
|
const MAX_FILE: usize = 5 * 1024 * 1024;
|
||||||
pub const MAX_IMAGE: usize = 32 * 1024 * 1024;
|
pub const MAX_IMAGE: usize = 128 * 1024 * 1024;
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
const BIN_PATH: &str = "/usr/local/bin/mifipanel";
|
const BIN_PATH: &str = "/usr/local/bin/mifipanel";
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
@@ -346,8 +346,6 @@ pub fn elf_ok(bytes: &[u8]) -> bool {
|
|||||||
|
|
||||||
const MAX_WEB_FILE: usize = 8 * 1024 * 1024;
|
const MAX_WEB_FILE: usize = 8 * 1024 * 1024;
|
||||||
const MAX_WEB_TOTAL: usize = 24 * 1024 * 1024;
|
const MAX_WEB_TOTAL: usize = 24 * 1024 * 1024;
|
||||||
const MAX_OFFLINE_FILE: usize = 8 * 1024 * 1024;
|
|
||||||
const MAX_OFFLINE_TOTAL: usize = 16 * 1024 * 1024;
|
|
||||||
const MAX_ZIP_ENTRIES: usize = 4000;
|
const MAX_ZIP_ENTRIES: usize = 4000;
|
||||||
|
|
||||||
/// 一次升级要用到的内容。
|
/// 一次升级要用到的内容。
|
||||||
@@ -357,8 +355,6 @@ pub struct Package {
|
|||||||
pub web: Vec<(String, Vec<u8>)>,
|
pub web: Vec<(String, Vec<u8>)>,
|
||||||
pub radio: Option<String>,
|
pub radio: Option<String>,
|
||||||
pub imei: Option<String>,
|
pub imei: Option<String>,
|
||||||
/// 离线程序:文件名和内容。只更新面板、不刷系统时,热点和终端靠它们。
|
|
||||||
pub offline: Vec<(String, Vec<u8>)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ZipEntry {
|
struct ZipEntry {
|
||||||
@@ -370,15 +366,15 @@ struct ZipEntry {
|
|||||||
encrypted: bool,
|
encrypted: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 上传的可以是面板程序本身,也可以是发布压缩包:程序、页面、方案库和 offline 里的离线程序。
|
/// 上传的可以是面板程序本身,也可以是带页面和方案库的压缩包,外面可以套一层目录。
|
||||||
/// 外面可以套一层目录。离线程序只按文件名写到面板自己的目录,不接受子目录。
|
/// 现在没有出货,测试板直接重刷。压缩包里的离线程序先不安装,等以后真要在线升级再接。
|
||||||
pub fn read_package(bytes: &[u8]) -> Result<Package, String> {
|
pub fn read_package(bytes: &[u8]) -> Result<Package, String> {
|
||||||
if bytes.is_empty() || bytes.len() > MAX_IMAGE {
|
if bytes.is_empty() || bytes.len() > MAX_IMAGE {
|
||||||
return Err("error.system.size".into());
|
return Err("error.system.size".into());
|
||||||
}
|
}
|
||||||
if !(bytes.len() >= 2 && bytes[0] == b'P' && bytes[1] == b'K') {
|
if !(bytes.len() >= 2 && bytes[0] == b'P' && bytes[1] == b'K') {
|
||||||
return if elf_ok(bytes) {
|
return if elf_ok(bytes) {
|
||||||
Ok(Package { elf: bytes.to_vec(), web: Vec::new(), radio: None, imei: None, offline: Vec::new() })
|
Ok(Package { elf: bytes.to_vec(), web: Vec::new(), radio: None, imei: None })
|
||||||
} else {
|
} else {
|
||||||
Err("error.system.elf".into())
|
Err("error.system.elf".into())
|
||||||
};
|
};
|
||||||
@@ -403,13 +399,10 @@ pub fn read_package(bytes: &[u8]) -> Result<Package, String> {
|
|||||||
let web_prefix = format!("{root}web/");
|
let web_prefix = format!("{root}web/");
|
||||||
let radio_name = format!("{root}radio-schemes.json");
|
let radio_name = format!("{root}radio-schemes.json");
|
||||||
let imei_name = format!("{root}imei-schemes.json");
|
let imei_name = format!("{root}imei-schemes.json");
|
||||||
let offline_prefix = format!("{root}offline/");
|
|
||||||
let mut web = Vec::new();
|
let mut web = Vec::new();
|
||||||
let mut total = 0usize;
|
let mut total = 0usize;
|
||||||
let mut offline_total = 0usize;
|
|
||||||
let mut radio = None;
|
let mut radio = None;
|
||||||
let mut imei = None;
|
let mut imei = None;
|
||||||
let mut offline = Vec::new();
|
|
||||||
for entry in &entries {
|
for entry in &entries {
|
||||||
if entry.name.ends_with('/') {
|
if entry.name.ends_with('/') {
|
||||||
continue;
|
continue;
|
||||||
@@ -424,28 +417,13 @@ pub fn read_package(bytes: &[u8]) -> Result<Package, String> {
|
|||||||
return Err("error.system.size".into());
|
return Err("error.system.size".into());
|
||||||
}
|
}
|
||||||
web.push((rel.to_string(), data));
|
web.push((rel.to_string(), data));
|
||||||
} else if let Some(name) = entry.name.strip_prefix(&offline_prefix) {
|
|
||||||
if !offline_name_ok(name) {
|
|
||||||
return Err("error.system.zip".into());
|
|
||||||
}
|
|
||||||
let data = zip_read(bytes, entry, MAX_OFFLINE_FILE)?;
|
|
||||||
offline_total += data.len();
|
|
||||||
if offline_total > MAX_OFFLINE_TOTAL {
|
|
||||||
return Err("error.system.size".into());
|
|
||||||
}
|
|
||||||
offline.push((name.to_string(), data));
|
|
||||||
} else if entry.name == radio_name {
|
} else if entry.name == radio_name {
|
||||||
radio = String::from_utf8(zip_read(bytes, entry, MAX_FILE)?).ok();
|
radio = String::from_utf8(zip_read(bytes, entry, MAX_FILE)?).ok();
|
||||||
} else if entry.name == imei_name {
|
} else if entry.name == imei_name {
|
||||||
imei = String::from_utf8(zip_read(bytes, entry, MAX_FILE)?).ok();
|
imei = String::from_utf8(zip_read(bytes, entry, MAX_FILE)?).ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Package { elf, web, radio, imei, offline })
|
Ok(Package { elf, web, radio, imei })
|
||||||
}
|
|
||||||
|
|
||||||
/// offline 里只放文件,不放子目录。名字和页面文件用同一套字符限制。
|
|
||||||
pub fn offline_name_ok(name: &str) -> bool {
|
|
||||||
!name.contains('/') && name_part_ok(name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 面板程序在压缩包里的位置决定包的根:直接放在最外层,或者只套一层目录。
|
/// 面板程序在压缩包里的位置决定包的根:直接放在最外层,或者只套一层目录。
|
||||||
@@ -537,26 +515,6 @@ fn zip_read(bytes: &[u8], entry: &ZipEntry, limit: usize) -> Result<Vec<u8>, Str
|
|||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 把升级包里的离线文件写到面板数据目录旁。可执行程序(没有扩展名的,例如 ttyd)标成 755。
|
|
||||||
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
|
|
||||||
pub fn install_offline(dir: &Path, files: &[(String, Vec<u8>)]) -> Result<(), String> {
|
|
||||||
if files.is_empty() {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
fs::create_dir_all(dir).map_err(|_| "error.system.upgrade".to_string())?;
|
|
||||||
for (name, data) in files {
|
|
||||||
if !offline_name_ok(name) {
|
|
||||||
return Err("error.system.zip".into());
|
|
||||||
}
|
|
||||||
let path = dir.join(name);
|
|
||||||
crate::store::write_atomic(&path, data).map_err(|_| "error.system.upgrade".to_string())?;
|
|
||||||
if !name.contains('.') {
|
|
||||||
mark_exec(&path).map_err(|_| "error.system.upgrade".to_string())?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 把升级包里的页面文件写进 web 目录。路径在读包时已经查过,这里再查一遍。
|
/// 把升级包里的页面文件写进 web 目录。路径在读包时已经查过,这里再查一遍。
|
||||||
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
|
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
|
||||||
pub fn install_web(web: &Path, files: &[(String, Vec<u8>)]) -> Result<(), String> {
|
pub fn install_web(web: &Path, files: &[(String, Vec<u8>)]) -> Result<(), String> {
|
||||||
@@ -685,6 +643,7 @@ fn mark_exec(path: &Path) -> std::io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 先换程序并确认新程序能跑,再换页面文件和方案库。程序没换成,其它都不动。
|
/// 先换程序并确认新程序能跑,再换页面文件和方案库。程序没换成,其它都不动。
|
||||||
|
/// 离线程序不从这里装:测试阶段整机重刷,或由刷机工具连同面板一起放上。
|
||||||
pub fn upgrade_image(bytes: &[u8], web: &Path) -> Result<String, String> {
|
pub fn upgrade_image(bytes: &[u8], web: &Path) -> Result<String, String> {
|
||||||
let package = read_package(bytes)?;
|
let package = read_package(bytes)?;
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
@@ -697,7 +656,6 @@ pub fn upgrade_image(bytes: &[u8], web: &Path) -> Result<String, String> {
|
|||||||
if let Some(text) = &package.imei {
|
if let Some(text) = &package.imei {
|
||||||
let _ = crate::imei_adapt::install_catalog(text);
|
let _ = crate::imei_adapt::install_catalog(text);
|
||||||
}
|
}
|
||||||
install_offline(Path::new("/usr/local/share/mifipanel/offline"), &package.offline)?;
|
|
||||||
Ok(version)
|
Ok(version)
|
||||||
}
|
}
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
@@ -1432,7 +1390,6 @@ mod tests {
|
|||||||
("panel\\mifipanel.service", b"[Unit]", false),
|
("panel\\mifipanel.service", b"[Unit]", false),
|
||||||
("panel\\radio-schemes.json", radio, false),
|
("panel\\radio-schemes.json", radio, false),
|
||||||
("panel\\offline\\hostapd_2.10-24_arm64.deb", b"deb", false),
|
("panel\\offline\\hostapd_2.10-24_arm64.deb", b"deb", false),
|
||||||
("panel\\offline\\ttyd", b"ttyd-bin", false),
|
|
||||||
]);
|
]);
|
||||||
let package = read_package(&release).unwrap();
|
let package = read_package(&release).unwrap();
|
||||||
assert_eq!(package.elf, good);
|
assert_eq!(package.elf, good);
|
||||||
@@ -1441,15 +1398,6 @@ mod tests {
|
|||||||
assert_eq!(package.web[0].1, en);
|
assert_eq!(package.web[0].1, en);
|
||||||
assert_eq!(package.radio.as_deref(), Some(std::str::from_utf8(radio).unwrap()));
|
assert_eq!(package.radio.as_deref(), Some(std::str::from_utf8(radio).unwrap()));
|
||||||
assert!(package.imei.is_none());
|
assert!(package.imei.is_none());
|
||||||
let offline: Vec<&str> = package.offline.iter().map(|item| item.0.as_str()).collect();
|
|
||||||
assert_eq!(offline, vec!["hostapd_2.10-24_arm64.deb", "ttyd"]);
|
|
||||||
assert_eq!(package.offline[1].1, b"ttyd-bin");
|
|
||||||
let offline_dir = scratch();
|
|
||||||
install_offline(&offline_dir, &package.offline).unwrap();
|
|
||||||
assert_eq!(fs::read(offline_dir.join("hostapd_2.10-24_arm64.deb")).unwrap(), b"deb");
|
|
||||||
assert_eq!(fs::read(offline_dir.join("ttyd")).unwrap(), b"ttyd-bin");
|
|
||||||
assert!(install_offline(&offline_dir, &[("../ttyd".into(), b"x".to_vec())]).is_err());
|
|
||||||
assert!(read_package(&zip_many(&[("mifipanel", &good, false), ("offline/nested/ttyd", b"x", false)])).is_err());
|
|
||||||
// 页面文件只落在 web 目录里面。
|
// 页面文件只落在 web 目录里面。
|
||||||
let dir = scratch();
|
let dir = scratch();
|
||||||
install_web(&dir, &package.web).unwrap();
|
install_web(&dir, &package.web).unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user