diff --git a/.gitattributes b/.gitattributes index e9891d7..0dfba90 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,3 +3,4 @@ *.service text eol=lf *.nmconnection text eol=lf image/**/*.conf text eol=lf +mifipanel/offline/ttyd binary diff --git a/flasher/package-release.ps1 b/flasher/package-release.ps1 index 9fcdd75..33af0b3 100644 --- a/flasher/package-release.ps1 +++ b/flasher/package-release.ps1 @@ -21,6 +21,7 @@ $service = Join-Path $source 'mifipanel.service' $radio = Join-Path $source 'radio-schemes.json' $imei = Join-Path $source 'imei-schemes.json' $offline = Join-Path $source 'offline' +$ttyd = Join-Path $offline 'ttyd' $panel = Join-Path $flasher 'panel' $dist = Join-Path $flasher 'dist' $zip = Join-Path $dist 'panel.zip' @@ -52,6 +53,9 @@ if (Test-Path -LiteralPath $offline -PathType Container) { $missing += "缺少 hostapd 的 deb:$offline\hostapd*.deb" } } +if (-not (Test-Path -LiteralPath $ttyd -PathType Leaf)) { + $missing += "缺少网页终端程序:$ttyd" +} if ($missing.Count -gt 0) { [Console]::Error.WriteLine('打包失败。本脚本不会交叉编译,请先备好下面这些文件。') @@ -78,6 +82,12 @@ Copy-Item -LiteralPath $imei -Destination (Join-Path $panel 'imei-schemes.json') foreach ($deb in $debs) { Copy-Item -LiteralPath $deb.FullName -Destination (Join-Path $offlineDir $deb.Name) -Force } +Copy-Item -LiteralPath $ttyd -Destination (Join-Path $offlineDir 'ttyd') -Force +Copy-Item -LiteralPath $ttyd -Destination (Join-Path $panel 'ttyd') -Force +$license = Join-Path $offline 'LICENSE-ttyd' +if (Test-Path -LiteralPath $license -PathType Leaf) { + Copy-Item -LiteralPath $license -Destination (Join-Path $offlineDir 'LICENSE-ttyd') -Force +} $stagedBin = Join-Path $panel 'mifipanel' $stagedWeb = Join-Path $panel 'web' diff --git a/flasher/src/adb.rs b/flasher/src/adb.rs index 2c14d26..edb0ecf 100644 --- a/flasher/src/adb.rs +++ b/flasher/src/adb.rs @@ -370,14 +370,17 @@ pub fn install_panel(device: &AdbDevice, report: &dyn Fn(&str)) -> Result<(), St if let Some(path) = &bundle.imei { push(device, path, "/usr/local/share/mifipanel/imei-schemes.json")?; } - if let Some(path) = &bundle.deb { - let name = path.file_name().and_then(|item| item.to_str()).unwrap_or("hostapd.deb"); + for path in &bundle.offline { + let Some(name) = path.file_name().and_then(|item| item.to_str()) else { continue }; + if name.contains('/') || name.contains('\\') || name.contains("..") { + continue; + } push(device, path, &format!("/usr/local/share/mifipanel/offline/{name}"))?; } report("正在启动服务"); shell( device, - "systemctl stop mifipanel.service >/dev/null 2>&1 || true; mv /tmp/mifipanel.new /usr/local/bin/mifipanel && chmod 755 /usr/local/bin/mifipanel && chmod 644 /etc/systemd/system/mifipanel.service && systemctl daemon-reload && systemctl enable mifipanel.service && systemctl restart mifipanel.service", + "systemctl stop mifipanel.service >/dev/null 2>&1 || true; mv /tmp/mifipanel.new /usr/local/bin/mifipanel && chmod 755 /usr/local/bin/mifipanel && chmod 644 /etc/systemd/system/mifipanel.service && { chmod 755 /usr/local/share/mifipanel/offline/ttyd >/dev/null 2>&1 || true; } && systemctl daemon-reload && systemctl enable mifipanel.service && systemctl restart mifipanel.service", )?; Ok(()) } @@ -435,7 +438,7 @@ struct PanelBundle { service: std::path::PathBuf, radio: Option, imei: Option, - deb: Option, + offline: Vec, } fn panel_bundle() -> Result { @@ -456,19 +459,24 @@ fn panel_bundle() -> Result { let web = root.join("web"); let service = root.join("mifipanel.service"); if bin.is_file() && web.is_dir() && service.is_file() { - let offline = root.join("offline"); - let deb = std::fs::read_dir(&offline).ok().and_then(|entries| { - entries.flatten().map(|entry| entry.path()).find(|path| { - path.file_name().and_then(|name| name.to_str()).is_some_and(|name| name.starts_with("hostapd") && name.ends_with(".deb")) - }) - }); + let offline_dir = root.join("offline"); + let mut offline = std::fs::read_dir(&offline_dir).ok().map(|entries| { + let mut files: Vec<_> = entries.flatten().map(|entry| entry.path()).filter(|path| path.is_file()).collect(); + files.sort(); + files + }).unwrap_or_default(); + // 终端程序有时和离线包分开放。两种位置都带走,避免只更新面板时丢掉它。 + let beside = root.join("ttyd"); + if beside.is_file() && !offline.iter().any(|path| path.file_name() == beside.file_name()) { + offline.push(beside); + } return Ok(PanelBundle { bin, web, service, radio: root.join("radio-schemes.json").is_file().then(|| root.join("radio-schemes.json")), imei: root.join("imei-schemes.json").is_file().then(|| root.join("imei-schemes.json")), - deb, + offline, }); } } diff --git a/image/debian/UFI003_MB_V02/build-image.sh b/image/debian/UFI003_MB_V02/build-image.sh index 8347d58..cf3f59b 100644 --- a/image/debian/UFI003_MB_V02/build-image.sh +++ b/image/debian/UFI003_MB_V02/build-image.sh @@ -225,6 +225,13 @@ if [ -x /panel/mifipanel ] && [ -f /panel/mifipanel.service ] && [ -d /panel/web log "install panel" install -d -m 755 /mnt/r/usr/local/bin /mnt/r/usr/local/share/mifipanel /mnt/r/var/lib/mifipanel install -m 755 /panel/mifipanel /mnt/r/usr/local/bin/mifipanel + if [ ! -f /panel/ttyd ]; then + echo "缺少 /panel/ttyd,网页终端没有程序可用。" >&2 + exit 1 + fi + install -m 755 /panel/ttyd /mnt/r/usr/local/bin/ttyd + install -d -m 755 /mnt/r/usr/local/share/mifipanel/offline + install -m 755 /panel/ttyd /mnt/r/usr/local/share/mifipanel/offline/ttyd rm -rf /mnt/r/usr/local/share/mifipanel/web cp -a /panel/web /mnt/r/usr/local/share/mifipanel/web install -m 644 /panel/mifipanel.service /mnt/r/etc/systemd/system/mifipanel.service diff --git a/image/debian/UFI003_MB_V02/update-panel.sh b/image/debian/UFI003_MB_V02/update-panel.sh index 35cbdaa..94c4af9 100644 --- a/image/debian/UFI003_MB_V02/update-panel.sh +++ b/image/debian/UFI003_MB_V02/update-panel.sh @@ -1,5 +1,5 @@ #!/bin/sh -# 把打包好的面板(程序、服务文件、页面)写进现成的 rootfs.img,不重做整个镜像。 +# 把打包好的面板(程序、终端、服务文件、页面)写进现成的 rootfs.img,不重做整个镜像。 # 镜像大小不变。先运行 flasher\package-release.ps1 生成 flasher\panel,再在仓库根目录执行: # # docker run --rm -v "${PWD}\flasher\devices\UFI003_MB_V02\images:/img" ` @@ -19,6 +19,7 @@ for need in "$IMG" "$PANEL/mifipanel" "$PANEL/mifipanel.service"; do [ -f "$need" ] || { echo "缺少 $need" >&2; exit 1; } done [ -d "$PANEL/web" ] || { echo "缺少 $PANEL/web" >&2; exit 1; } +[ -f "$PANEL/ttyd" ] || { echo "缺少 $PANEL/ttyd" >&2; exit 1; } size_before=$(stat -c %s "$IMG") cmds=/tmp/update-panel.cmds @@ -37,6 +38,19 @@ for dir in /usr/local/bin /usr/local/share /usr/local/share/mifipanel "$WEB_DEST echo "mkdir $dir" >> "$cmds" done put "$PANEL/mifipanel" /usr/local/bin/mifipanel 0100755 +put "$PANEL/ttyd" /usr/local/bin/ttyd 0100755 +if [ -d "$PANEL/offline" ]; then + echo "mkdir /usr/local/share/mifipanel/offline" >> "$cmds" + find "$PANEL/offline" -maxdepth 1 -type f | sort > /tmp/offline.list + while read -r file; do + name=$(basename "$file") + mode=0100644 + if [ "$name" = "ttyd" ]; then + mode=0100755 + fi + put "$file" "/usr/local/share/mifipanel/offline/$name" "$mode" + done < /tmp/offline.list +fi put "$PANEL/mifipanel.service" /etc/systemd/system/mifipanel.service 0100644 ( cd "$PANEL/web" && find . -type d | sort ) | while read -r dir; do [ "$dir" = "." ] || echo "mkdir $WEB_DEST/${dir#./}" >> "$cmds" @@ -72,6 +86,17 @@ check() { cmp -s "$1" /tmp/out/one || { echo "写入后内容不一致:$2" >&2; exit 1; } } check "$PANEL/mifipanel" /usr/local/bin/mifipanel +check "$PANEL/ttyd" /usr/local/bin/ttyd +debugfs -R "stat /usr/local/bin/ttyd" "$IMG" 2>/dev/null | grep -q "Mode: 0755" \ + || { echo "终端程序权限不是 755" >&2; exit 1; } +if [ -f /tmp/offline.list ]; then + while read -r file; do + name=$(basename "$file") + check "$file" "/usr/local/share/mifipanel/offline/$name" + done < /tmp/offline.list + debugfs -R "stat /usr/local/share/mifipanel/offline/ttyd" "$IMG" 2>/dev/null | grep -q "Mode: 0755" \ + || { echo "离线终端程序权限不是 755" >&2; exit 1; } +fi check "$PANEL/mifipanel.service" /etc/systemd/system/mifipanel.service count=0 while read -r file; do @@ -86,6 +111,7 @@ debugfs -R "ls /etc/systemd/system/multi-user.target.wants" "$IMG" 2>/dev/null | || { echo "镜像里面板没有设成开机启动" >&2; exit 1; } size_after=$(stat -c %s "$IMG") [ "$size_before" = "$size_after" ] || { echo "镜像大小变了:$size_before -> $size_after" >&2; exit 1; } -echo "已更新:程序、服务文件、页面 $count 个;镜像大小 $size_after 字节,文件系统干净" +echo "已更新:程序、终端、服务文件、页面 $count 个;镜像大小 $size_after 字节,文件系统干净" dumpe2fs -h "$IMG" 2>/dev/null | awk '/Free blocks/' sha256sum "$PANEL/mifipanel" | awk '{print "程序 " $1}' +sha256sum "$PANEL/ttyd" | awk '{print "终端 " $1}' diff --git a/mifipanel/Cargo.lock b/mifipanel/Cargo.lock index 8d8f279..9dc0c56 100644 --- a/mifipanel/Cargo.lock +++ b/mifipanel/Cargo.lock @@ -737,13 +737,15 @@ dependencies = [ [[package]] name = "mifipanel" -version = "1.0.0" +version = "1.1.0" dependencies = [ "axum", "base64 0.22.1", "flate2", "futures-util", "hmac", + "hyper", + "hyper-util", "lettre", "libc", "serde", diff --git a/mifipanel/Cargo.toml b/mifipanel/Cargo.toml index 36e0f98..0f1d863 100644 --- a/mifipanel/Cargo.toml +++ b/mifipanel/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mifipanel" -version = "1.0.0" +version = "1.1.0" edition = "2021" description = "随身 WiFi、CPE 和手机上的短信与设备管理面板" @@ -15,6 +15,8 @@ ureq = { version = "2", features = ["json"] } tower-http = { version = "0.5", features = ["fs"] } futures-util = "0.3" hmac = "0.12" +hyper = { version = "1", features = ["http1", "server"] } +hyper-util = { version = "0.1", features = ["tokio"] } base64 = "0.22" zbus = { version = "5", default-features = false, features = ["tokio"] } libc = "0.2" diff --git a/mifipanel/offline/LICENSE-ttyd b/mifipanel/offline/LICENSE-ttyd new file mode 100644 index 0000000..ff33579 --- /dev/null +++ b/mifipanel/offline/LICENSE-ttyd @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016-2022 Shuanglei Tao + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/mifipanel/offline/ttyd b/mifipanel/offline/ttyd new file mode 100755 index 0000000..ba5b315 Binary files /dev/null and b/mifipanel/offline/ttyd differ diff --git a/mifipanel/src/hotspot.rs b/mifipanel/src/hotspot.rs index 7cc09bd..4ab52da 100644 --- a/mifipanel/src/hotspot.rs +++ b/mifipanel/src/hotspot.rs @@ -759,10 +759,28 @@ fn dnsmasq_bin() -> &'static str { } fn offline_debs() -> Vec { - vec![ + let mut out = vec![ PathBuf::from("/usr/local/share/mifipanel/offline/hostapd_2.10-24_arm64.deb"), PathBuf::from("/usr/local/share/mifipanel/hostapd_2.10-24_arm64.deb"), - ] + ]; + out.extend(hostapd_debs_in(Path::new("/usr/local/share/mifipanel/offline"))); + out.sort(); + out.dedup(); + out +} + +fn hostapd_debs_in(dir: &Path) -> Vec { + let Ok(entries) = std::fs::read_dir(dir) else { return Vec::new() }; + let mut out = Vec::new(); + for entry in entries.flatten() { + let path = entry.path(); + let Some(name) = path.file_name().and_then(|item| item.to_str()) else { continue }; + if name.starts_with("hostapd") && name.ends_with(".deb") { + out.push(path); + } + } + out.sort(); + out } fn sort_bands(bands: &mut Vec) { @@ -1261,6 +1279,20 @@ fn cstr(data: &[u8]) -> String { mod tests { use super::*; + #[test] + fn offline_directory_is_searched_for_hostapd() { + let dir = std::env::temp_dir().join(format!("mifipanel-deb-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("hostapd_2.10-24_arm64.deb"), b"deb").unwrap(); + std::fs::write(dir.join("ttyd"), b"bin").unwrap(); + std::fs::write(dir.join("notes.txt"), b"no").unwrap(); + let found = hostapd_debs_in(&dir); + assert_eq!(found.len(), 1); + assert!(found[0].ends_with("hostapd_2.10-24_arm64.deb")); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn splits_subnets_and_writes_hostapd() { assert_eq!(subnet_indexes(["2.4", "5"].into_iter()), vec![0, 1]); diff --git a/mifipanel/src/http.rs b/mifipanel/src/http.rs index cc9a96d..be2df3e 100644 --- a/mifipanel/src/http.rs +++ b/mifipanel/src/http.rs @@ -38,6 +38,8 @@ pub struct AppState { pub events: broadcast::Sender, pub data: PathBuf, pub web: PathBuf, + /// ttyd 只听这个本机地址。测试可以换成自己的模拟端口。 + pub ttyd: std::net::SocketAddr, session_seq: AtomicU64, } @@ -54,6 +56,7 @@ impl AppState { events, web, data, + ttyd: crate::terminal::listen_addr(), session_seq: AtomicU64::new(1), } } @@ -71,6 +74,7 @@ pub fn router(state: Arc, web: PathBuf) -> Router { .merge(crate::api_wifi::routes()) .merge(crate::api_cell::routes()) .merge(crate::api_system::routes()) + .merge(crate::terminal::routes()) .route("/events", get(events)) .nest_service("/layuimini", ServeDir::new(web.join("layuimini"))) .nest_service("/moban2", ServeDir::new(web.join("moban2"))) diff --git a/mifipanel/src/main.rs b/mifipanel/src/main.rs index e30c517..cfe4033 100644 --- a/mifipanel/src/main.rs +++ b/mifipanel/src/main.rs @@ -20,6 +20,7 @@ mod settings; mod sms_ops; mod store; mod system; +mod terminal; mod usb; mod wifi; @@ -108,6 +109,7 @@ async fn server() -> Result<(), String> { api_wifi::start(&app); api_cell::start(&app); api_system::start(&app); + terminal::start(); let publisher = Arc::clone(&app); tokio::spawn(async move { publish_metrics(publisher).await }); tokio::spawn(async move { diff --git a/mifipanel/src/pages/shell.html b/mifipanel/src/pages/shell.html index 6477fdd..f87352f 100644 --- a/mifipanel/src/pages/shell.html +++ b/mifipanel/src/pages/shell.html @@ -21,7 +21,7 @@ - +