fix(qmc): fix key from mmkv

pull/46/head
Unlock Music Dev 2 years ago
parent a9c976f47d
commit 423767ba63
No known key found for this signature in database
GPG Key ID: 95202E10D3413A1D

@ -4,6 +4,8 @@ import (
"io" "io"
"path/filepath" "path/filepath"
"strings" "strings"
"go.uber.org/zap"
) )
type DecoderParams struct { type DecoderParams struct {
@ -11,6 +13,8 @@ type DecoderParams struct {
Extension string // required, source extension, eg. ".mp3" Extension string // required, source extension, eg. ".mp3"
FilePath string // optional, source file path FilePath string // optional, source file path
Logger *zap.Logger // required
} }
type NewDecoderFunc func(p *DecoderParams) Decoder type NewDecoderFunc func(p *DecoderParams) Decoder

@ -8,12 +8,14 @@ import (
"runtime" "runtime"
"strings" "strings"
"go.uber.org/zap"
"unlock-music.dev/mmkv" "unlock-music.dev/mmkv"
) )
var streamKeyVault mmkv.Vault 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 == "" { if file == "" {
return nil, errors.New("file path is required while reading key from mmkv") 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) return nil, fmt.Errorf("mmkv key valut not found: %w", err)
} }
} }
mgr, err := mmkv.NewManager(mmkvDir) mgr, err := mmkv.NewManager(mmkvDir)
if err != nil { if err != nil {
return nil, fmt.Errorf("init mmkv manager: %w", err) return nil, fmt.Errorf("init mmkv manager: %w", err)
@ -40,6 +43,8 @@ func readKeyFromMMKV(file string) ([]byte, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("open mmkv vault: %w", err) return nil, fmt.Errorf("open mmkv vault: %w", err)
} }
logger.Debug("mmkv vault opened", zap.Strings("keys", streamKeyVault.Keys()))
} }
buf, err := streamKeyVault.GetBytes(file) buf, err := streamKeyVault.GetBytes(file)
@ -52,9 +57,12 @@ func readKeyFromMMKV(file string) ([]byte, error) {
} }
buf, err = streamKeyVault.GetBytes(key) buf, err = streamKeyVault.GetBytes(key)
if err != nil { 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 { if len(buf) == 0 {

@ -6,9 +6,12 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"runtime"
"strconv" "strconv"
"strings" "strings"
"go.uber.org/zap"
"unlock-music.dev/cli/algo/common" "unlock-music.dev/cli/algo/common"
"unlock-music.dev/cli/internal/sniff" "unlock-music.dev/cli/internal/sniff"
) )
@ -26,6 +29,8 @@ type Decoder struct {
rawMetaExtra1 int rawMetaExtra1 int
rawMetaExtra2 int rawMetaExtra2 int
logger *zap.Logger
} }
// Read implements io.Reader, offer the decrypted audio data. // 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 { 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 { func (d *Decoder) Validate() error {
@ -99,15 +104,21 @@ func (d *Decoder) validateDecode() error {
} }
func (d *Decoder) searchKey() (err 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) fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd)
if err != nil { if err != nil {
return err 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) suffixBuf := make([]byte, 4)
if _, err := io.ReadFull(d.raw, suffixBuf); err != nil { 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 // try to use default static cipher
d.audioLen = int(fileSizeM4 + 4) d.audioLen = fileSize
return nil return nil
} }

@ -148,9 +148,10 @@ func dealDirectory(inputDir string, outputDir string, skipNoop bool, removeSourc
continue 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 { 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 return nil
@ -167,6 +168,7 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu
Reader: file, Reader: file,
Extension: filepath.Ext(inputFile), Extension: filepath.Ext(inputFile),
FilePath: inputFile, FilePath: inputFile,
Logger: logger.With(zap.String("source", inputFile)),
} }
var dec common.Decoder var dec common.Decoder

Loading…
Cancel
Save