备份、隧道生命周期、日志、会话与设置项存在一批「静默失效」问题,本次一并收敛。 正确性: - 备份归档补上 session_secret 与 frp 配置。缺密钥时换机恢复后库里所有凭据都解不开, 界面表现为「配置都在、一执行就报解密失败」;同时支持口令加密(scrypt + AES-256-GCM)。 - 修复「立即备份」必失败:tar 头用的是遍历时的文件尺寸,而 logs、frpc 配置在打包期间 还在增长,读出的字节多于声明值导致 archive/tar 报错、整份备份作废。改为打开后重新 取尺寸并用 LimitReader 兜住,被截短时补零保持条目结构完整。 - 数据库改用 VACUUM INTO 生成一致性快照。直接打包正在写入的 SQLite 文件可能得到 结构完整但内容自相矛盾的副本,恢复后才暴露。 - Cloudflare 启动改用 StartAuto,Shutdown 显式 StopAll,子进程绑定应用 ctx。此前 重启会把用户手动停掉的隧道重新拉起,退出也可能留下孤儿进程。 - Nginx 访问/错误日志加轮转(200MB 阈值、gzip 归档、USR1 重开),读取改为从文件尾 分块倒读,并跨当日归档统计。此前日志只增不减,读接口每次全量扫描。 - 过期会话改由日任务清理;删除反代规则时同步清掉流量累计,避免同名规则重建继承旧读数。 - 启动加就绪门:Nginx bootstrap 完成后再放行 FRP 对账、Agent 隧道与 Cloudflare 自启。 设置项: - 新增设置项注册表(internal/settings/registry.go),白名单与默认值从它派生, 并记录每个键的消费者,防止再出现「界面能改、后端没人读」的黑洞设置。 - 接上三个此前不生效的设置项:timezone(启动与保存时应用到 time.Local)、 theme(后端为权威源,挂载时覆盖本地偏好、顶栏切换回写)、log_retention_days (控制日志归档保留天数)。 工程质量: - gofmt 全量格式化 23 个历史文件,CI 增加格式化门禁。 - 前端引入 ESLint(flat config)与 lint 脚本,CI 增加 lint 步骤;顺带修掉 logDisplay 的 let→const 与 LogsView 模板内直接改 prop 两处问题。 - 删除无引用的 pnpm-lock.yaml,prototype/ 加入忽略列表。 - 打包版本对齐:Docker manifest 同步到 2.0.0,CI 校验 VERSION 与 package.json 一致。 验证:gofmt 0 文件、go vet、go test ./...、npm run lint/typecheck/test/build 全绿。 Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,269 @@
|
||||
package backup
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// listEntries 读出归档里的条目名,用于断言包含关系。
|
||||
func listEntries(t *testing.T, path string) []string {
|
||||
t.Helper()
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
gr, err := gzip.NewReader(file)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer gr.Close()
|
||||
tr := tar.NewReader(gr)
|
||||
names := []string{}
|
||||
for {
|
||||
header, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
return names
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
names = append(names, header.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// TestExportIncludesSessionSecretAndFrpConfig:备份必须带上凭据加密密钥与 frpc 配置。
|
||||
// 少了 session_secret,恢复后库里的 DDNS / FRP / Cloudflare / 通知凭据全部解不开。
|
||||
func TestExportIncludesSessionSecretAndFrpConfig(t *testing.T) {
|
||||
dataDir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dataDir, "havline.db"), []byte("db"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dataDir, "session_secret"), []byte("secret"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
frpDir := filepath.Join(dataDir, "frp")
|
||||
if err := os.MkdirAll(frpDir, 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(frpDir, "frpc-server-1.toml"), []byte("token = \"x\"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
path, err := New(dataDir).Export(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries := listEntries(t, path)
|
||||
has := map[string]bool{}
|
||||
for _, name := range entries {
|
||||
has[name] = true
|
||||
}
|
||||
for _, want := range []string{"havline.db", "session_secret", "frp/frpc-server-1.toml"} {
|
||||
if !has[want] {
|
||||
t.Fatalf("备份应包含 %q,实际条目:%v", want, entries)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestExportSkipsFrpBinariesAndLogs:frp/bin、frp/downloads 与 *.log 体积大且可重新获取,不进归档。
|
||||
func TestExportSkipsFrpBinariesAndLogs(t *testing.T) {
|
||||
dataDir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dataDir, "havline.db"), []byte("db"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
frpDir := filepath.Join(dataDir, "frp")
|
||||
for _, dir := range []string{filepath.Join(frpDir, "bin", "v0.70.1"), filepath.Join(frpDir, "downloads")} {
|
||||
if err := os.MkdirAll(dir, 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
files := map[string]string{
|
||||
filepath.Join(frpDir, "bin", "v0.70.1", "frpc"): "binary",
|
||||
filepath.Join(frpDir, "downloads", "frpc.zip"): "zip",
|
||||
filepath.Join(frpDir, "frpc-server-1.log"): "log",
|
||||
filepath.Join(frpDir, "frpc-server-1.toml"): "token = \"x\"\n",
|
||||
filepath.Join(frpDir, "active.json"): "{}",
|
||||
}
|
||||
for path, content := range files {
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
path, err := New(dataDir).Export(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries := listEntries(t, path)
|
||||
has := map[string]bool{}
|
||||
for _, name := range entries {
|
||||
has[name] = true
|
||||
}
|
||||
for _, skip := range []string{"frp/bin/v0.70.1/frpc", "frp/downloads/frpc.zip", "frp/frpc-server-1.log"} {
|
||||
if has[skip] {
|
||||
t.Fatalf("归档不应包含 %q,实际条目:%v", skip, entries)
|
||||
}
|
||||
}
|
||||
for _, want := range []string{"frp/frpc-server-1.toml", "frp/active.json"} {
|
||||
if !has[want] {
|
||||
t.Fatalf("归档应保留 %q,实际条目:%v", want, entries)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestExportWithPassphraseIsEncrypted:给了口令就必须是加密容器,且不带口令读不出来。
|
||||
func TestExportWithPassphraseIsEncrypted(t *testing.T) {
|
||||
dataDir := seedDataDir(t)
|
||||
path, err := New(dataDir).ExportWith(context.Background(), Options{Passphrase: "correct horse"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encrypted, err := IsEncrypted(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !encrypted {
|
||||
t.Fatal("给了口令导出的归档应被标记为加密")
|
||||
}
|
||||
|
||||
// 无口令恢复:必须明确报「需要口令」,而不是含糊的解压失败
|
||||
target := t.TempDir()
|
||||
if err := New(target).Restore(context.Background(), path); !errors.Is(err, ErrPassphraseRequired) {
|
||||
t.Fatalf("无口令恢复应报 ErrPassphraseRequired,实际 %v", err)
|
||||
}
|
||||
|
||||
// 错口令:GCM 校验失败
|
||||
if err := New(target).RestoreWith(context.Background(), path, Options{Passphrase: "wrong"}); err == nil {
|
||||
t.Fatal("错误口令不应恢复成功")
|
||||
}
|
||||
|
||||
// 正确口令:能恢复出原来的文件内容
|
||||
if err := New(target).RestoreWith(context.Background(), path, Options{Passphrase: "correct horse"}); err != nil {
|
||||
t.Fatalf("正确口令应恢复成功:%v", err)
|
||||
}
|
||||
data, err := os.ReadFile(filepath.Join(target, "havline.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(data) != "db" {
|
||||
t.Fatalf("恢复内容不符:%q", data)
|
||||
}
|
||||
}
|
||||
|
||||
// TestExportWithoutPassphraseStaysCompatible:不给口令时仍产出未加密归档,
|
||||
// 老版本与现有客户端可以直接解压。
|
||||
func TestExportWithoutPassphraseStaysCompatible(t *testing.T) {
|
||||
dataDir := seedDataDir(t)
|
||||
path, err := New(dataDir).Export(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encrypted, err := IsEncrypted(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if encrypted {
|
||||
t.Fatal("未给口令时的归档不应加密")
|
||||
}
|
||||
if _, err := New(t.TempDir()).Verify(context.Background(), path); err != nil {
|
||||
t.Fatalf("未加密归档应能通过校验:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestVerifyWithEncryptedArchiveNeedsPassphrase:加密归档校验同样需要口令,
|
||||
// 否则自动备份会把一份好备份判成坏包删掉。
|
||||
func TestVerifyWithEncryptedArchiveNeedsPassphrase(t *testing.T) {
|
||||
dataDir := seedDataDir(t)
|
||||
path, err := New(dataDir).ExportWith(context.Background(), Options{Passphrase: "pw"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := New(dataDir).Verify(context.Background(), path); !errors.Is(err, ErrPassphraseRequired) {
|
||||
t.Fatalf("无口令校验应报 ErrPassphraseRequired,实际 %v", err)
|
||||
}
|
||||
count, err := New(dataDir).VerifyWith(context.Background(), path, Options{Passphrase: "pw"})
|
||||
if err != nil {
|
||||
t.Fatalf("带口令校验应通过:%v", err)
|
||||
}
|
||||
if count == 0 {
|
||||
t.Fatal("校验应返回条目数")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMissingSessionSecretDetectsLegacyArchive:旧归档没有 session_secret,
|
||||
// 能恢复但凭据解不开,要能被识别出来提示用户。
|
||||
func TestMissingSessionSecretDetectsLegacyArchive(t *testing.T) {
|
||||
dataDir := t.TempDir()
|
||||
// 手工造一个「只有 havline.db」的旧式归档
|
||||
legacy := writeArchive(t, map[string]string{"havline.db": "db"})
|
||||
svc := New(dataDir)
|
||||
|
||||
missing, err := svc.MissingSessionSecret(context.Background(), legacy, Options{})
|
||||
if err != nil {
|
||||
t.Fatalf("识别旧归档不应报错:%v", err)
|
||||
}
|
||||
if !missing {
|
||||
t.Fatal("旧归档应被判定为缺少 session_secret")
|
||||
}
|
||||
|
||||
fresh := seedExport(t, seedDataDir(t))
|
||||
missing, err = svc.MissingSessionSecret(context.Background(), fresh, Options{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if missing {
|
||||
t.Fatal("新归档包含 session_secret,不应判定为缺失")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestoreKeepsSecretPermissions:session_secret 还原后必须是 0600,
|
||||
// 否则同机其他用户就能读到解密凭据的钥匙。
|
||||
func TestRestoreKeepsSecretPermissions(t *testing.T) {
|
||||
if os.PathSeparator == '\\' {
|
||||
t.Skip("Windows 不保留 Unix 权限位")
|
||||
}
|
||||
dataDir := seedDataDir(t)
|
||||
path, err := New(dataDir).Export(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target := t.TempDir()
|
||||
if err := New(target).Restore(context.Background(), path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
info, err := os.Stat(filepath.Join(target, "session_secret"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := info.Mode().Perm(); got != restoreSecretMode {
|
||||
t.Fatalf("session_secret 还原后权限应为 %o,实际 %o", restoreSecretMode, got)
|
||||
}
|
||||
}
|
||||
|
||||
func seedDataDir(t *testing.T) string {
|
||||
t.Helper()
|
||||
dataDir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dataDir, "havline.db"), []byte("db"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dataDir, "session_secret"), []byte("secret"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return dataDir
|
||||
}
|
||||
|
||||
func seedExport(t *testing.T, dataDir string) string {
|
||||
t.Helper()
|
||||
path, err := New(dataDir).Export(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
Reference in New Issue
Block a user