Skip to content

Commit 393465d

Browse files
authored
Merge pull request #3 from final00000000/feat/windows-hosts-compat-backup-restore
Improve Windows hosts update reliability and add desktop recovery flow
2 parents 589b7ee + 591fe0d commit 393465d

10 files changed

Lines changed: 1962 additions & 128 deletions

File tree

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ Android 版目前走的是 DNS 代理接管方案:
9797
桌面端默认提供:
9898

9999
- `开始加速 / 停止加速`
100+
- `恢复 hosts / 彻底恢复原始状态`
100101
- 一键最小化
101-
- 错误详情展示
102+
- 状态与最近操作日志展示
102103
- 当前上游、DoH、证书和域名接管范围预览
103104
- 配置和关于面板
104105

@@ -134,6 +135,28 @@ cargo run --bin linuxdo-accelerator -- init-config
134135
sudo cargo run --bin linuxdo-accelerator -- setup
135136
```
136137

138+
仅准备 `hosts` 规则:
139+
140+
```bash
141+
sudo cargo run --bin linuxdo-accelerator -- apply-hosts
142+
```
143+
144+
手动确保首次 `hosts` 基线备份存在:
145+
146+
```bash
147+
cargo run --bin linuxdo-accelerator -- backup-hosts
148+
```
149+
150+
恢复到首次接管前的 `hosts` 完整备份:
151+
152+
```bash
153+
sudo cargo run --bin linuxdo-accelerator -- restore-hosts
154+
```
155+
156+
> `backup-hosts` 默认只创建**首次基线备份**,不会覆盖已有备份。
157+
> `clean-hosts` / `restore-hosts` 仅适合在加速服务已停止时使用;
158+
> 如果目标是“尽量完整回到初始状态”,优先使用 `cleanup`
159+
137160
前台直接启动:
138161

139162
```bash
@@ -176,6 +199,9 @@ linuxdo-accelerator --config /path/to/linuxdo-accelerator.toml
176199
```
177200

178201
程序会改用该配置文件;对应的 `runtime``certs` 目录也会优先跟着这个配置目录走。
202+
`runtime` 目录中还会保存 `hosts.backup``hosts.backup.json`,用于完整恢复首次接管前的
203+
`hosts` 内容与原始文件属性。
204+
此外还会写入 `operations.log`,记录启动、停止、恢复和清理等关键操作结果,便于排查问题。
179205

180206
## Config Example
181207

@@ -207,7 +233,7 @@ server_common_name = "linux.do"
207233
- `linuxdo-accelerator`
208234
- 默认直接打开桌面 GUI
209235
- 传入命令参数后作为 CLI 使用
210-
- 负责 `setup / start / stop / status / gui` 等命令
236+
- 负责 `setup / apply-hosts / backup-hosts / restore-hosts / start / stop / status / gui` 等命令
211237
- Windows 下打包为可双击启动的 `.exe`
212238
- Linux 下由 `.desktop` 启动
213239
- macOS 下打包为 `.app / .dmg`
@@ -279,6 +305,16 @@ macOS 不再走本地交叉编译脚本,而是通过 GitHub Actions 原生构
279305
- macOS 本机编译与窗口最小化恢复链路
280306
- 证书、`hosts` 和运行状态文件统一管理
281307

308+
## Hosts Safety Notes
309+
310+
- 首次写入系统 `hosts` 前,会在 `runtime` 目录自动创建完整备份
311+
- `clean-hosts` 只删除 `linuxdo-accelerator` 自己维护的 marker block
312+
- `restore-hosts` 会用完整备份覆盖当前 `hosts`,适合停止使用后的显式恢复
313+
- `cleanup` 会优先尝试恢复首次接管前的完整备份;如果完整备份缺失、损坏、状态异常或恢复失败,会退化为仅清理 marker block;遇到损坏 / 异常备份时还会顺带清掉失效备份,避免后续继续卡在错误状态
314+
- Windows 下会在写入前自动处理 `hosts` 的只读属性,并在写入后恢复原始文件属性
315+
- Windows 下对 `hosts` 原子替换增加了共享冲突重试,降低杀软 / 系统进程短暂占用导致的恢复失败概率
316+
- 当前恢复范围以 `hosts` 内容与常见文件属性为主,不额外恢复自定义 ACL / owner
317+
282318
## Inspirations
283319

284320
- [docmirror/dev-sidecar](https://github.com/docmirror/dev-sidecar)

src/cli.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use std::path::PathBuf;
33
use anyhow::Result;
44
use clap::{Parser, Subcommand};
55

6+
use crate::config::AppConfig;
67
#[cfg(any(windows, target_os = "linux", target_os = "macos"))]
78
use crate::gui;
8-
use crate::config::AppConfig;
99
use crate::service;
1010

1111
#[derive(Debug, Parser)]
@@ -30,6 +30,8 @@ pub enum Command {
3030
Status,
3131
CleanHosts,
3232
ApplyHosts,
33+
BackupHosts,
34+
RestoreHosts,
3335
UninstallCert,
3436
Cleanup,
3537
#[command(hide = true)]
@@ -49,8 +51,8 @@ pub async fn run(cli: Cli) -> Result<()> {
4951
None | Some(Command::Gui) => {
5052
#[cfg(any(windows, target_os = "linux", target_os = "macos"))]
5153
{
52-
let config_path = service::init_config(cli.config.clone())?;
53-
gui::run(config_path)?;
54+
let config_path = service::init_config(cli.config.clone())?;
55+
gui::run(config_path)?;
5456
}
5557
#[cfg(target_os = "android")]
5658
{
@@ -87,6 +89,14 @@ pub async fn run(cli: Cli) -> Result<()> {
8789
service::apply_hosts_only(cli.config)?;
8890
println!("hosts applied");
8991
}
92+
Some(Command::BackupHosts) => {
93+
service::backup_hosts(cli.config)?;
94+
println!("hosts backup ready");
95+
}
96+
Some(Command::RestoreHosts) => {
97+
service::restore_hosts(cli.config)?;
98+
println!("hosts restored");
99+
}
90100
Some(Command::UninstallCert) => {
91101
service::uninstall_certificate(cli.config)?;
92102
println!("certificate removed");

0 commit comments

Comments
 (0)