From 849f6730b69d19eceaeffab0d0b5c53c0d2a49cc Mon Sep 17 00:00:00 2001 From: Jixun <8041017-jixunmoe@users.noreply.gitlab.com> Date: Sat, 6 Jul 2024 19:55:31 +0100 Subject: [PATCH] refactor: make mmkv key optional; change cli params name --- algo/qmc/key_mmkv.go | 11 +++++++---- cmd/um/main.go | 19 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/algo/qmc/key_mmkv.go b/algo/qmc/key_mmkv.go index 24b51cb..51c1ee3 100644 --- a/algo/qmc/key_mmkv.go +++ b/algo/qmc/key_mmkv.go @@ -80,14 +80,16 @@ func readKeyFromMMKV(file string, logger *zap.Logger) ([]byte, error) { return deriveKey(buf) } -func OpenMMKV(vaultPath string, vaultKey string, logger *zap.Logger) error { - filePath, fileName := filepath.Split(vaultPath) +func OpenMMKV(mmkvPath string, key string, logger *zap.Logger) error { + filePath, fileName := filepath.Split(mmkvPath) mgr, err := mmkv.NewManager(filepath.Dir(filePath)) if err != nil { return fmt.Errorf("init mmkv manager: %w", err) } - streamKeyVault, err = mgr.OpenVaultCrypto(fileName, vaultKey) + // If `vaultKey` is empty, the key is ignored. + streamKeyVault, err = mgr.OpenVaultCrypto(fileName, key) + if err != nil { return fmt.Errorf("open mmkv vault: %w", err) } @@ -109,6 +111,7 @@ func readKeyFromMMKVCustom(mid string) ([]byte, error) { return deriveKey(eKey) } +// / getRelativeMMKVDir get mmkv dir relative to file (legacy QQMusic for macOS behaviour) func getRelativeMMKVDir(file string) (string, error) { mmkvDir := filepath.Join(filepath.Dir(file), "../mmkv") if _, err := os.Stat(mmkvDir); err != nil { @@ -131,7 +134,7 @@ func getDefaultMMKVDir() (string, error) { mmkvDir := filepath.Join( homeDir, - "Library/Containers/com.tencent.QQMusicMac/Data", // todo: make configurable + "Library/Containers/com.tencent.QQMusicMac/Data", "Library/Application Support/QQMusicMac/mmkv", ) if _, err := os.Stat(mmkvDir); err != nil { diff --git a/cmd/um/main.go b/cmd/um/main.go index 4b74ce7..cb48457 100644 --- a/cmd/um/main.go +++ b/cmd/um/main.go @@ -5,9 +5,6 @@ import ( "context" "errors" "fmt" - "github.com/fsnotify/fsnotify" - "github.com/urfave/cli/v2" - "go.uber.org/zap" "io" "os" "os/signal" @@ -18,6 +15,10 @@ import ( "strings" "time" + "github.com/fsnotify/fsnotify" + "github.com/urfave/cli/v2" + "go.uber.org/zap" + "unlock-music.dev/cli/algo/common" _ "unlock-music.dev/cli/algo/kgm" _ "unlock-music.dev/cli/algo/kwm" @@ -49,8 +50,8 @@ func main() { Flags: []cli.Flag{ &cli.StringFlag{Name: "input", Aliases: []string{"i"}, Usage: "path to input file or dir", Required: false}, &cli.StringFlag{Name: "output", Aliases: []string{"o"}, Usage: "path to output dir", Required: false}, - &cli.StringFlag{Name: "vault-file", Aliases: []string{"db"}, Usage: "数据库文件位置 (请确保crc文件在同目录下)", Required: false}, - &cli.StringFlag{Name: "vault-key", Aliases: []string{"key"}, Usage: "数据库密钥 (length 32)", Required: false}, + &cli.StringFlag{Name: "qmc-mmkv", Aliases: []string{"db"}, Usage: "path to qmc mmkv (`.crc` file also required)", Required: false}, + &cli.StringFlag{Name: "qmc-mmkv-key", Aliases: []string{"key"}, Usage: "mmkv password (16 ascii chars)", Required: false}, &cli.BoolFlag{Name: "remove-source", Aliases: []string{"rs"}, Usage: "remove source file", Required: false, Value: false}, &cli.BoolFlag{Name: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true}, &cli.BoolFlag{Name: "update-metadata", Usage: "update metadata & album art from network", Required: false, Value: false}, @@ -131,10 +132,10 @@ func appMain(c *cli.Context) (err error) { return errors.New("output should be a writable directory") } - vaultPath := c.String("vault-file") - vaultKey := c.String("vault-key") - if vaultPath != "" && vaultKey != "" { - err := qmc.OpenMMKV(vaultPath, vaultKey, logger) + if mmkv := c.String("qmc-mmkv"); mmkv != "" { + // If key is not set, the mmkv vault will be treated as unencrypted. + key := c.String("qmc-mmkv-key") + err := qmc.OpenMMKV(mmkv, key, logger) if err != nil { return err }