Skip to content

Commit

Permalink
feat: Support config file export
Browse files Browse the repository at this point in the history
feat: Support config file import
feat: Windows close button minimizes the window
refactor: Crawl config file export
chore: upgrade dependencies
  • Loading branch information
snakem982 committed Dec 11, 2024
1 parent d2aeb3f commit 48a5c01
Show file tree
Hide file tree
Showing 17 changed files with 992 additions and 74 deletions.
112 changes: 106 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ package main
import (
"context"
"github.com/keybase/go-keychain"
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"
"github.com/wailsapp/wails/v2/pkg/runtime"
"os"
"os/exec"
"pandora-box/backend/cache"
"pandora-box/backend/constant"
"pandora-box/backend/meta"
isadmin "pandora-box/backend/system/admin"
"pandora-box/backend/system/open"
"pandora-box/backend/system/update"
"runtime"
"pandora-box/backend/tools"
"path/filepath"
"strings"
)

Expand All @@ -35,11 +39,7 @@ func (a *App) startup(ctx context.Context) {
}

func (a *App) IsMac() string {
if runtime.GOOS == "darwin" {
return "true"
}

return "false"
return "true"
}

func (a *App) GetMacAcStatus() string {
Expand Down Expand Up @@ -160,3 +160,103 @@ func (a *App) IsNeedUpdate() string {

return "false"
}

func (a *App) ImportConfig() string {
selection, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "选择文件 Select File",
Filters: []runtime.FileFilter{
{
DisplayName: "文件类型 Type (*.zip)",
Pattern: "*.zip",
},
},
})

if err != nil || selection == "" {
return "false"
}

secret := cache.Get(constant.SecretKey)

err = meta.Recovery(selection)

if err != nil {
return err.Error()
}

if secret != nil {
_ = cache.Put(constant.SecretKey, secret)
}

return "true"
}

func (a *App) ExportConfig() string {
homeDir := filepath.Dir(C.Path.HomeDir())
desktopPath := filepath.Join(homeDir, "Downloads")
_, err := os.Stat(desktopPath)
if !os.IsNotExist(err) {
homeDir = desktopPath
}

selection, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "选择导出位置 Select Export Directory",
DefaultDirectory: homeDir,
DefaultFilename: "Pandora-Box-Config.zip",
Filters: []runtime.FileFilter{
{
DisplayName: "文件类型 Type (*.zip)",
Pattern: "*.zip",
},
},
})

if err != nil || selection == "" {
return "false"
}

err = meta.Dump(selection)

if err != nil {
return err.Error()
}

return "true"
}

func (a *App) SfQuit() {
runtime.Quit(a.ctx)
}

func (a *App) ExportCrawl() string {

homeDir := filepath.Dir(C.Path.HomeDir())
desktopPath := filepath.Join(homeDir, "Downloads")
_, err := os.Stat(desktopPath)
if !os.IsNotExist(err) {
homeDir = desktopPath
}

selection, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "选择导出位置 Select Export Directory",
DefaultDirectory: homeDir,
DefaultFilename: "Pandora-Box-Config.yaml",
Filters: []runtime.FileFilter{
{
DisplayName: "文件类型 Type (*.yaml)",
Pattern: "*.yaml",
},
},
})

if err != nil || selection == "" {
return "false"
}

err = tools.CopyFile(filepath.Join(C.Path.HomeDir(), constant.DefaultDownload), selection)
if err != nil {
return err.Error()
}

return "true"
}
111 changes: 106 additions & 5 deletions app_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ package main

import (
"context"
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"
"github.com/wailsapp/wails/v2/pkg/runtime"
"os"
"pandora-box/backend/cache"
"pandora-box/backend/constant"
"pandora-box/backend/meta"
isadmin "pandora-box/backend/system/admin"
"pandora-box/backend/system/open"
"pandora-box/backend/system/update"
"runtime"
"pandora-box/backend/tools"
"path/filepath"
)

// App struct
Expand All @@ -32,10 +36,6 @@ func (a *App) startup(ctx context.Context) {
}

func (a *App) IsMac() string {
if runtime.GOOS == "darwin" {
return "true"
}

return "false"
}

Expand Down Expand Up @@ -95,3 +95,104 @@ func (a *App) IsNeedUpdate() string {

return "false"
}

func (a *App) ImportConfig() string {
selection, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "选择文件 Select File",
Filters: []runtime.FileFilter{
{
DisplayName: "文件类型 Type (*.zip)",
Pattern: "*.zip",
},
},
})

if err != nil || selection == "" {
return "false"
}

secret := cache.Get(constant.SecretKey)

err = meta.Recovery(selection)

if err != nil {
return err.Error()
}

if secret != nil {
_ = cache.Put(constant.SecretKey, secret)
}

return "true"
}

func (a *App) ExportConfig() string {
homeDir := filepath.Dir(C.Path.HomeDir())
desktopPath := filepath.Join(homeDir, "Desktop")
_, err := os.Stat(desktopPath)
if !os.IsNotExist(err) {
homeDir = desktopPath
}

selection, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "选择导出位置 Select Export Directory",
DefaultDirectory: homeDir,
DefaultFilename: "Pandora-Box-Config.zip",
Filters: []runtime.FileFilter{
{
DisplayName: "文件类型 Type (*.zip)",
Pattern: "*.zip",
},
},
})

if err != nil || selection == "" {
return "false"
}

err = meta.Dump(selection)

if err != nil {
return err.Error()
}

return "true"
}

func (a *App) SfQuit() {
_ = cache.Put(constant.QuitSignal, []byte("1"))
runtime.Quit(a.ctx)
}

func (a *App) ExportCrawl() string {

homeDir := filepath.Dir(C.Path.HomeDir())
desktopPath := filepath.Join(homeDir, "Desktop")
_, err := os.Stat(desktopPath)
if !os.IsNotExist(err) {
homeDir = desktopPath
}

selection, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "选择导出位置 Select Export Directory",
DefaultDirectory: homeDir,
DefaultFilename: "Pandora-Box-Config.yaml",
Filters: []runtime.FileFilter{
{
DisplayName: "文件类型 Type (*.yaml)",
Pattern: "*.yaml",
},
},
})

if err != nil || selection == "" {
return "false"
}

err = tools.CopyFile(filepath.Join(C.Path.HomeDir(), constant.DefaultDownload), selection)
if err != nil {
return err.Error()
}

return "true"
}
67 changes: 67 additions & 0 deletions backend/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package cache

import (
"fmt"
"github.com/metacubex/bbolt"
"github.com/metacubex/mihomo/log"
"os"
"pandora-box/backend/constant"
"strings"
)

Expand Down Expand Up @@ -65,3 +69,66 @@ func DeleteList(m map[string]any) error {
})
})
}

func Dump(dstDBPath string) error {
_, err := os.Stat(dstDBPath)
if !os.IsNotExist(err) {
_ = os.Remove(dstDBPath)
}

// 创建目标数据库
dstDB, err := bbolt.Open(dstDBPath, 0600, nil)
if err != nil {
return err
}
defer dstDB.Close()

return dstDB.Batch(func(tx *bbolt.Tx) error {
newBucket, err := tx.CreateBucketIfNotExists(BName)
if err != nil {
log.Warnln("[DumpFile] can't create bucket: %s", err.Error())
return fmt.Errorf("create bucket: %v", err)
}

return BDb.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(BName)
return b.ForEach(func(k, v []byte) error {
key := string(k)
if key == constant.SecretKey {
return nil
}

if key == constant.QuitSignal {
return nil
}

if !strings.HasPrefix(key, constant.RealIpHeader) {
return newBucket.Put(k, v)
}

return nil
})
})
})
}

func Recovery(srcDBPath string) error {
_, err := os.Stat(srcDBPath)
if os.IsNotExist(err) {
return err
}

// 打开源数据库
srcDB, err := bbolt.Open(srcDBPath, 0600, nil)
if err != nil {
return err
}
defer srcDB.Close()

return srcDB.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(BName)
return b.ForEach(func(k, v []byte) error {
return Put(string(k), v)
})
})
}
2 changes: 2 additions & 0 deletions backend/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const (
PrefixGetter = "Getter_"
RealIpHeader = "RealIp_"
SecretKey = "SecretKey_pb"
RecoverTmp = "RecoverTmp"
QuitSignal = "QuitSignal"
)

const (
Expand Down
Loading

0 comments on commit 48a5c01

Please sign in to comment.