From 423767ba63645974fc11f924fa5af5907c345ed8 Mon Sep 17 00:00:00 2001 From: Unlock Music Dev Date: Mon, 5 Dec 2022 08:54:40 +0800 Subject: [PATCH] fix(qmc): fix key from mmkv --- algo/common/dispatch.go | 4 ++++ algo/qmc/key_mmkv.go | 12 ++++++++++-- algo/qmc/qmc.go | 25 ++++++++++++++++++------- cmd/um/main.go | 6 ++++-- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/algo/common/dispatch.go b/algo/common/dispatch.go index 263ad64..f37d35a 100644 --- a/algo/common/dispatch.go +++ b/algo/common/dispatch.go @@ -4,6 +4,8 @@ import ( "io" "path/filepath" "strings" + + "go.uber.org/zap" ) type DecoderParams struct { @@ -11,6 +13,8 @@ type DecoderParams struct { Extension string // required, source extension, eg. ".mp3" FilePath string // optional, source file path + + Logger *zap.Logger // required } type NewDecoderFunc func(p *DecoderParams) Decoder diff --git a/algo/qmc/key_mmkv.go b/algo/qmc/key_mmkv.go index 062ba52..72b3815 100644 --- a/algo/qmc/key_mmkv.go +++ b/algo/qmc/key_mmkv.go @@ -8,12 +8,14 @@ import ( "runtime" "strings" + "go.uber.org/zap" "unlock-music.dev/mmkv" ) var streamKeyVault mmkv.Vault -func readKeyFromMMKV(file string) ([]byte, error) { +// TODO: move to factory +func readKeyFromMMKV(file string, logger *zap.Logger) ([]byte, error) { if file == "" { return nil, errors.New("file path is required while reading key from mmkv") } @@ -31,6 +33,7 @@ func readKeyFromMMKV(file string) ([]byte, error) { return nil, fmt.Errorf("mmkv key valut not found: %w", err) } } + mgr, err := mmkv.NewManager(mmkvDir) if err != nil { return nil, fmt.Errorf("init mmkv manager: %w", err) @@ -40,6 +43,8 @@ func readKeyFromMMKV(file string) ([]byte, error) { if err != nil { return nil, fmt.Errorf("open mmkv vault: %w", err) } + + logger.Debug("mmkv vault opened", zap.Strings("keys", streamKeyVault.Keys())) } buf, err := streamKeyVault.GetBytes(file) @@ -52,9 +57,12 @@ func readKeyFromMMKV(file string) ([]byte, error) { } buf, err = streamKeyVault.GetBytes(key) if err != nil { - // TODO: logger + logger.Warn("read key from mmkv", zap.String("key", key), zap.Error(err)) } } + // TODO: use editorial judgement to select the best match + // since MacOS may change some characters in the file name. + // eg. "ぜ" -> "ぜ" } if len(buf) == 0 { diff --git a/algo/qmc/qmc.go b/algo/qmc/qmc.go index 987d01a..a775069 100644 --- a/algo/qmc/qmc.go +++ b/algo/qmc/qmc.go @@ -6,9 +6,12 @@ import ( "errors" "fmt" "io" + "runtime" "strconv" "strings" + "go.uber.org/zap" + "unlock-music.dev/cli/algo/common" "unlock-music.dev/cli/internal/sniff" ) @@ -26,6 +29,8 @@ type Decoder struct { rawMetaExtra1 int rawMetaExtra2 int + + logger *zap.Logger } // Read implements io.Reader, offer the decrypted audio data. @@ -40,7 +45,7 @@ func (d *Decoder) Read(p []byte) (int, error) { } func NewDecoder(p *common.DecoderParams) common.Decoder { - return &Decoder{raw: p.Reader, params: p} + return &Decoder{raw: p.Reader, params: p, logger: p.Logger} } func (d *Decoder) Validate() error { @@ -99,15 +104,21 @@ func (d *Decoder) validateDecode() error { } func (d *Decoder) searchKey() (err error) { - if d.params.Extension == ".mflach" { - d.decodedKey, err = readKeyFromMMKV(d.params.FilePath) - return err - } - fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd) if err != nil { return err } + fileSize := int(fileSizeM4) + 4 + + //goland:noinspection GoBoolExpressions + if runtime.GOOS == "darwin" { + d.decodedKey, err = readKeyFromMMKV(d.params.FilePath, d.logger) + if err == nil { + d.audioLen = fileSize + return + } + d.logger.Warn("read key from mmkv failed", zap.Error(err)) + } suffixBuf := make([]byte, 4) if _, err := io.ReadFull(d.raw, suffixBuf); err != nil { @@ -127,7 +138,7 @@ func (d *Decoder) searchKey() (err error) { } // try to use default static cipher - d.audioLen = int(fileSizeM4 + 4) + d.audioLen = fileSize return nil } diff --git a/cmd/um/main.go b/cmd/um/main.go index 01cf963..4eae5e8 100644 --- a/cmd/um/main.go +++ b/cmd/um/main.go @@ -148,9 +148,10 @@ func dealDirectory(inputDir string, outputDir string, skipNoop bool, removeSourc continue } - err := tryDecFile(filepath.Join(inputDir, item.Name()), outputDir, allDec, removeSource) + filePath := filepath.Join(inputDir, item.Name()) + err := tryDecFile(filePath, outputDir, allDec, removeSource) if err != nil { - logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err)) + logger.Error("conversion failed", zap.String("source", filePath), zap.Error(err)) } } return nil @@ -167,6 +168,7 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu Reader: file, Extension: filepath.Ext(inputFile), FilePath: inputFile, + Logger: logger.With(zap.String("source", inputFile)), } var dec common.Decoder