refactor: make mmkv key optional; change cli params name

This commit is contained in:
Jixun 2024-07-06 19:55:31 +01:00
parent 43f96e5d89
commit 849f6730b6
2 changed files with 17 additions and 13 deletions

View File

@ -80,14 +80,16 @@ func readKeyFromMMKV(file string, logger *zap.Logger) ([]byte, error) {
return deriveKey(buf) return deriveKey(buf)
} }
func OpenMMKV(vaultPath string, vaultKey string, logger *zap.Logger) error { func OpenMMKV(mmkvPath string, key string, logger *zap.Logger) error {
filePath, fileName := filepath.Split(vaultPath) filePath, fileName := filepath.Split(mmkvPath)
mgr, err := mmkv.NewManager(filepath.Dir(filePath)) mgr, err := mmkv.NewManager(filepath.Dir(filePath))
if err != nil { if err != nil {
return fmt.Errorf("init mmkv manager: %w", err) 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 { if err != nil {
return fmt.Errorf("open mmkv vault: %w", err) return fmt.Errorf("open mmkv vault: %w", err)
} }
@ -109,6 +111,7 @@ func readKeyFromMMKVCustom(mid string) ([]byte, error) {
return deriveKey(eKey) return deriveKey(eKey)
} }
// / getRelativeMMKVDir get mmkv dir relative to file (legacy QQMusic for macOS behaviour)
func getRelativeMMKVDir(file string) (string, error) { func getRelativeMMKVDir(file string) (string, error) {
mmkvDir := filepath.Join(filepath.Dir(file), "../mmkv") mmkvDir := filepath.Join(filepath.Dir(file), "../mmkv")
if _, err := os.Stat(mmkvDir); err != nil { if _, err := os.Stat(mmkvDir); err != nil {
@ -131,7 +134,7 @@ func getDefaultMMKVDir() (string, error) {
mmkvDir := filepath.Join( mmkvDir := filepath.Join(
homeDir, homeDir,
"Library/Containers/com.tencent.QQMusicMac/Data", // todo: make configurable "Library/Containers/com.tencent.QQMusicMac/Data",
"Library/Application Support/QQMusicMac/mmkv", "Library/Application Support/QQMusicMac/mmkv",
) )
if _, err := os.Stat(mmkvDir); err != nil { if _, err := os.Stat(mmkvDir); err != nil {

View File

@ -5,9 +5,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/fsnotify/fsnotify"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"io" "io"
"os" "os"
"os/signal" "os/signal"
@ -18,6 +15,10 @@ import (
"strings" "strings"
"time" "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/common"
_ "unlock-music.dev/cli/algo/kgm" _ "unlock-music.dev/cli/algo/kgm"
_ "unlock-music.dev/cli/algo/kwm" _ "unlock-music.dev/cli/algo/kwm"
@ -49,8 +50,8 @@ func main() {
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "input", Aliases: []string{"i"}, Usage: "path to input file or dir", Required: false}, &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: "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: "qmc-mmkv", Aliases: []string{"db"}, Usage: "path to qmc mmkv (`.crc` file also required)", Required: false},
&cli.StringFlag{Name: "vault-key", Aliases: []string{"key"}, Usage: "数据库密钥 (length 32)", 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: "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: "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}, &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") return errors.New("output should be a writable directory")
} }
vaultPath := c.String("vault-file") if mmkv := c.String("qmc-mmkv"); mmkv != "" {
vaultKey := c.String("vault-key") // If key is not set, the mmkv vault will be treated as unencrypted.
if vaultPath != "" && vaultKey != "" { key := c.String("qmc-mmkv-key")
err := qmc.OpenMMKV(vaultPath, vaultKey, logger) err := qmc.OpenMMKV(mmkv, key, logger)
if err != nil { if err != nil {
return err return err
} }