Compare commits
14 Commits
9647ca15fd
...
14c9d49d46
Author | SHA1 | Date | |
---|---|---|---|
14c9d49d46 | |||
6f1cdc5b4f | |||
8fd6763b29 | |||
2880c63bf7 | |||
67ff0c44cd | |||
4365628bff | |||
b275b552ed | |||
4ecaef1ff9 | |||
e2fc56ddb2 | |||
a2c55721cc | |||
e6ba9f0bfa | |||
21bd246d6c | |||
f05ae61aff | |||
7b37e4dd3c |
@ -1,12 +1,17 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Decoder interface {
|
||||
Validate() error
|
||||
Decode() error
|
||||
GetCoverImage() []byte
|
||||
GetAudioData() []byte
|
||||
GetAudioExt() string
|
||||
GetMeta() Meta
|
||||
io.Reader
|
||||
}
|
||||
|
||||
type CoverImageGetter interface {
|
||||
GetCoverImage(ctx context.Context) ([]byte, error)
|
||||
}
|
||||
|
||||
type Meta interface {
|
||||
@ -14,3 +19,7 @@ type Meta interface {
|
||||
GetTitle() string
|
||||
GetAlbum() string
|
||||
}
|
||||
|
||||
type StreamDecoder interface {
|
||||
Decrypt(buf []byte, offset int)
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"io"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type NewDecoderFunc func([]byte) Decoder
|
||||
type NewDecoderFunc func(rd io.ReadSeeker) Decoder
|
||||
|
||||
type decoderItem struct {
|
||||
noop bool
|
||||
|
@ -2,46 +2,39 @@ package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type RawDecoder struct {
|
||||
file []byte
|
||||
rd io.ReadSeeker
|
||||
|
||||
audioExt string
|
||||
}
|
||||
|
||||
func NewRawDecoder(file []byte) Decoder {
|
||||
return &RawDecoder{file: file}
|
||||
func NewRawDecoder(rd io.ReadSeeker) Decoder {
|
||||
return &RawDecoder{rd: rd}
|
||||
}
|
||||
|
||||
func (d *RawDecoder) Validate() error {
|
||||
for ext, sniffer := range snifferRegistry {
|
||||
if sniffer(d.file) {
|
||||
d.audioExt = strings.ToLower(ext)
|
||||
return nil
|
||||
}
|
||||
header := make([]byte, 16)
|
||||
if _, err := io.ReadFull(d.rd, header); err != nil {
|
||||
return fmt.Errorf("read file header failed: %v", err)
|
||||
}
|
||||
if _, err := d.rd.Seek(0, io.SeekStart); err != nil {
|
||||
return fmt.Errorf("seek file failed: %v", err)
|
||||
}
|
||||
return errors.New("audio doesn't recognized")
|
||||
}
|
||||
|
||||
func (d RawDecoder) Decode() error {
|
||||
var ok bool
|
||||
d.audioExt, ok = SniffAll(header)
|
||||
if !ok {
|
||||
return errors.New("raw: sniff audio type failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d RawDecoder) GetCoverImage() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d RawDecoder) GetAudioData() []byte {
|
||||
return d.file
|
||||
}
|
||||
|
||||
func (d RawDecoder) GetAudioExt() string {
|
||||
return d.audioExt
|
||||
}
|
||||
|
||||
func (d RawDecoder) GetMeta() Meta {
|
||||
return nil
|
||||
func (d *RawDecoder) Read(p []byte) (n int, err error) {
|
||||
return d.rd.Read(p)
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
111
algo/kgm/kgm.go
111
algo/kgm/kgm.go
@ -1,90 +1,57 @@
|
||||
package kgm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
"github.com/unlock-music/cli/internal/logging"
|
||||
)
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
var (
|
||||
vprHeader = []byte{
|
||||
0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43,
|
||||
0x91, 0xAA, 0xBD, 0xD0, 0x7A, 0xF5, 0x36, 0x31}
|
||||
kgmHeader = []byte{
|
||||
0x7C, 0xD5, 0x32, 0xEB, 0x86, 0x02, 0x7F, 0x4B,
|
||||
0xA8, 0xAF, 0xA6, 0x8E, 0x0F, 0xFF, 0x99, 0x14}
|
||||
ErrKgmMagicHeader = errors.New("kgm/vpr magic header not matched")
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
)
|
||||
|
||||
type Decoder struct {
|
||||
file []byte
|
||||
key []byte
|
||||
isVpr bool
|
||||
audio []byte
|
||||
header header
|
||||
cipher common.StreamDecoder
|
||||
|
||||
rd io.ReadSeeker
|
||||
offset int
|
||||
}
|
||||
|
||||
func NewDecoder(file []byte) common.Decoder {
|
||||
return &Decoder{
|
||||
file: file,
|
||||
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
||||
return &Decoder{rd: rd}
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() (err error) {
|
||||
if err := d.header.FromFile(d.rd); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// TODO; validate crypto version
|
||||
|
||||
func (d Decoder) GetCoverImage() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Decoder) GetAudioData() []byte {
|
||||
return d.audio
|
||||
}
|
||||
|
||||
func (d Decoder) GetAudioExt() string {
|
||||
return "" // use sniffer
|
||||
}
|
||||
|
||||
func (d Decoder) GetMeta() common.Meta {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
if bytes.Equal(kgmHeader, d.file[:len(kgmHeader)]) {
|
||||
d.isVpr = false
|
||||
} else if bytes.Equal(vprHeader, d.file[:len(vprHeader)]) {
|
||||
d.isVpr = true
|
||||
} else {
|
||||
return ErrKgmMagicHeader
|
||||
}
|
||||
|
||||
d.key = d.file[0x1c:0x2c]
|
||||
d.key = append(d.key, 0x00)
|
||||
_ = d.file[0x2c:0x3c] //todo: key2
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode() error {
|
||||
headerLen := binary.LittleEndian.Uint32(d.file[0x10:0x14])
|
||||
dataEncrypted := d.file[headerLen:]
|
||||
lenData := len(dataEncrypted)
|
||||
initMask()
|
||||
if fullMaskLen < lenData {
|
||||
logging.Log().Warn("The file is too large and the processed audio is incomplete, " +
|
||||
"please report to us about this file at https://github.com/unlock-music/cli/issues")
|
||||
lenData = fullMaskLen
|
||||
}
|
||||
d.audio = make([]byte, lenData)
|
||||
|
||||
for i := 0; i < lenData; i++ {
|
||||
med8 := dataEncrypted[i] ^ d.key[i%17] ^ maskV2PreDef[i%(16*17)] ^ maskV2[i>>4]
|
||||
d.audio[i] = med8 ^ (med8&0xf)<<4
|
||||
}
|
||||
if d.isVpr {
|
||||
for i := 0; i < lenData; i++ {
|
||||
d.audio[i] ^= maskDiffVpr[i%17]
|
||||
switch d.header.CryptoVersion {
|
||||
case 3:
|
||||
d.cipher, err = newKgmCryptoV3(&d.header)
|
||||
if err != nil {
|
||||
return fmt.Errorf("kgm init crypto v3: %w", err)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("kgm: unsupported crypto version %d", d.header.CryptoVersion)
|
||||
}
|
||||
|
||||
// prepare for read
|
||||
if _, err := d.rd.Seek(int64(d.header.AudioOffset), io.SeekStart); err != nil {
|
||||
return fmt.Errorf("kgm seek to audio: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) Read(buf []byte) (int, error) {
|
||||
n, err := d.rd.Read(buf)
|
||||
if n > 0 {
|
||||
d.cipher.Decrypt(buf[:n], d.offset)
|
||||
d.offset += n
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Kugou
|
||||
common.RegisterDecoder("kgm", false, NewDecoder)
|
||||
|
Binary file not shown.
64
algo/kgm/kgm_header.go
Normal file
64
algo/kgm/kgm_header.go
Normal file
@ -0,0 +1,64 @@
|
||||
package kgm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
var (
|
||||
vprHeader = []byte{
|
||||
0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43,
|
||||
0x91, 0xAA, 0xBD, 0xD0, 0x7A, 0xF5, 0x36, 0x31,
|
||||
}
|
||||
kgmHeader = []byte{
|
||||
0x7C, 0xD5, 0x32, 0xEB, 0x86, 0x02, 0x7F, 0x4B,
|
||||
0xA8, 0xAF, 0xA6, 0x8E, 0x0F, 0xFF, 0x99, 0x14,
|
||||
}
|
||||
|
||||
ErrKgmMagicHeader = errors.New("kgm magic header not matched")
|
||||
)
|
||||
|
||||
// header is the header of a KGM file.
|
||||
type header struct {
|
||||
MagicHeader []byte // 0x00-0x0f: magic header
|
||||
AudioOffset uint32 // 0x10-0x13: offset of audio data
|
||||
CryptoVersion uint32 // 0x14-0x17: crypto version
|
||||
CryptoSlot uint32 // 0x18-0x1b: crypto key slot
|
||||
CryptoTestData []byte // 0x1c-0x2b: crypto test data
|
||||
CryptoKey []byte // 0x2c-0x3b: crypto key
|
||||
}
|
||||
|
||||
func (h *header) FromFile(rd io.ReadSeeker) error {
|
||||
if _, err := rd.Seek(0, io.SeekStart); err != nil {
|
||||
return fmt.Errorf("kgm seek start: %w", err)
|
||||
}
|
||||
|
||||
buf := make([]byte, 0x3c)
|
||||
if _, err := io.ReadFull(rd, buf); err != nil {
|
||||
return fmt.Errorf("kgm read header: %w", err)
|
||||
}
|
||||
|
||||
return h.FromBytes(buf)
|
||||
}
|
||||
|
||||
func (h *header) FromBytes(buf []byte) error {
|
||||
if len(buf) < 0x3c {
|
||||
return errors.New("invalid kgm header length")
|
||||
}
|
||||
|
||||
h.MagicHeader = buf[:0x10]
|
||||
if !bytes.Equal(kgmHeader, h.MagicHeader) && !bytes.Equal(vprHeader, h.MagicHeader) {
|
||||
return ErrKgmMagicHeader
|
||||
}
|
||||
|
||||
h.AudioOffset = binary.LittleEndian.Uint32(buf[0x10:0x14])
|
||||
h.CryptoVersion = binary.LittleEndian.Uint32(buf[0x14:0x18])
|
||||
h.CryptoSlot = binary.LittleEndian.Uint32(buf[0x18:0x1c])
|
||||
h.CryptoTestData = buf[0x1c:0x2c]
|
||||
h.CryptoKey = buf[0x2c:0x3c]
|
||||
|
||||
return nil
|
||||
}
|
55
algo/kgm/kgm_v3.go
Normal file
55
algo/kgm/kgm_v3.go
Normal file
@ -0,0 +1,55 @@
|
||||
package kgm
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
)
|
||||
|
||||
// kgmCryptoV3 is kgm file crypto v3
|
||||
type kgmCryptoV3 struct {
|
||||
slotBox []byte
|
||||
fileBox []byte
|
||||
}
|
||||
|
||||
var kgmV3Slot2Key = map[uint32][]byte{
|
||||
1: {0x6C, 0x2C, 0x2F, 0x27},
|
||||
}
|
||||
|
||||
func newKgmCryptoV3(header *header) (common.StreamDecoder, error) {
|
||||
c := &kgmCryptoV3{}
|
||||
|
||||
slotKey, ok := kgmV3Slot2Key[header.CryptoSlot]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("kgm3: unknown crypto slot %d", header.CryptoSlot)
|
||||
}
|
||||
c.slotBox = kugouMD5(slotKey)
|
||||
|
||||
c.fileBox = append(kugouMD5(header.CryptoKey), 0x6b)
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (d *kgmCryptoV3) Decrypt(b []byte, offset int) {
|
||||
for i := 0; i < len(b); i++ {
|
||||
b[i] ^= d.fileBox[(offset+i)%len(d.fileBox)]
|
||||
b[i] ^= b[i] << 4
|
||||
b[i] ^= d.slotBox[(offset+i)%len(d.slotBox)]
|
||||
b[i] ^= xorCollapseUint32(uint32(offset + i))
|
||||
}
|
||||
}
|
||||
|
||||
func xorCollapseUint32(i uint32) byte {
|
||||
return byte(i) ^ byte(i>>8) ^ byte(i>>16) ^ byte(i>>24)
|
||||
}
|
||||
|
||||
func kugouMD5(b []byte) []byte {
|
||||
digest := md5.Sum(b)
|
||||
ret := make([]byte, 16)
|
||||
for i := 0; i < md5.Size; i += 2 {
|
||||
ret[i] = digest[14-i]
|
||||
ret[i+1] = digest[14-i+1]
|
||||
}
|
||||
return ret
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
package kgm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"github.com/ulikunitz/xz"
|
||||
"github.com/unlock-music/cli/internal/logging"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
)
|
||||
|
||||
var maskDiffVpr = []byte{
|
||||
0x25, 0xDF, 0xE8, 0xA6, 0x75, 0x1E, 0x75, 0x0E,
|
||||
0x2F, 0x80, 0xF3, 0x2D, 0xB8, 0xB6, 0xE3, 0x11,
|
||||
0x00}
|
||||
|
||||
var maskV2PreDef = []byte{
|
||||
0xB8, 0xD5, 0x3D, 0xB2, 0xE9, 0xAF, 0x78, 0x8C, 0x83, 0x33, 0x71, 0x51, 0x76, 0xA0, 0xCD, 0x37,
|
||||
0x2F, 0x3E, 0x35, 0x8D, 0xA9, 0xBE, 0x98, 0xB7, 0xE7, 0x8C, 0x22, 0xCE, 0x5A, 0x61, 0xDF, 0x68,
|
||||
0x69, 0x89, 0xFE, 0xA5, 0xB6, 0xDE, 0xA9, 0x77, 0xFC, 0xC8, 0xBD, 0xBD, 0xE5, 0x6D, 0x3E, 0x5A,
|
||||
0x36, 0xEF, 0x69, 0x4E, 0xBE, 0xE1, 0xE9, 0x66, 0x1C, 0xF3, 0xD9, 0x02, 0xB6, 0xF2, 0x12, 0x9B,
|
||||
0x44, 0xD0, 0x6F, 0xB9, 0x35, 0x89, 0xB6, 0x46, 0x6D, 0x73, 0x82, 0x06, 0x69, 0xC1, 0xED, 0xD7,
|
||||
0x85, 0xC2, 0x30, 0xDF, 0xA2, 0x62, 0xBE, 0x79, 0x2D, 0x62, 0x62, 0x3D, 0x0D, 0x7E, 0xBE, 0x48,
|
||||
0x89, 0x23, 0x02, 0xA0, 0xE4, 0xD5, 0x75, 0x51, 0x32, 0x02, 0x53, 0xFD, 0x16, 0x3A, 0x21, 0x3B,
|
||||
0x16, 0x0F, 0xC3, 0xB2, 0xBB, 0xB3, 0xE2, 0xBA, 0x3A, 0x3D, 0x13, 0xEC, 0xF6, 0x01, 0x45, 0x84,
|
||||
0xA5, 0x70, 0x0F, 0x93, 0x49, 0x0C, 0x64, 0xCD, 0x31, 0xD5, 0xCC, 0x4C, 0x07, 0x01, 0x9E, 0x00,
|
||||
0x1A, 0x23, 0x90, 0xBF, 0x88, 0x1E, 0x3B, 0xAB, 0xA6, 0x3E, 0xC4, 0x73, 0x47, 0x10, 0x7E, 0x3B,
|
||||
0x5E, 0xBC, 0xE3, 0x00, 0x84, 0xFF, 0x09, 0xD4, 0xE0, 0x89, 0x0F, 0x5B, 0x58, 0x70, 0x4F, 0xFB,
|
||||
0x65, 0xD8, 0x5C, 0x53, 0x1B, 0xD3, 0xC8, 0xC6, 0xBF, 0xEF, 0x98, 0xB0, 0x50, 0x4F, 0x0F, 0xEA,
|
||||
0xE5, 0x83, 0x58, 0x8C, 0x28, 0x2C, 0x84, 0x67, 0xCD, 0xD0, 0x9E, 0x47, 0xDB, 0x27, 0x50, 0xCA,
|
||||
0xF4, 0x63, 0x63, 0xE8, 0x97, 0x7F, 0x1B, 0x4B, 0x0C, 0xC2, 0xC1, 0x21, 0x4C, 0xCC, 0x58, 0xF5,
|
||||
0x94, 0x52, 0xA3, 0xF3, 0xD3, 0xE0, 0x68, 0xF4, 0x00, 0x23, 0xF3, 0x5E, 0x0A, 0x7B, 0x93, 0xDD,
|
||||
0xAB, 0x12, 0xB2, 0x13, 0xE8, 0x84, 0xD7, 0xA7, 0x9F, 0x0F, 0x32, 0x4C, 0x55, 0x1D, 0x04, 0x36,
|
||||
0x52, 0xDC, 0x03, 0xF3, 0xF9, 0x4E, 0x42, 0xE9, 0x3D, 0x61, 0xEF, 0x7C, 0xB6, 0xB3, 0x93, 0x50,
|
||||
}
|
||||
|
||||
//go:embed kgm.v2.mask
|
||||
var maskV2Xz []byte
|
||||
|
||||
var maskV2 []byte
|
||||
var fullMaskLen int
|
||||
var initMaskOK = false
|
||||
|
||||
//todo: decompress mask on demand
|
||||
func initMask() {
|
||||
if initMaskOK {
|
||||
return
|
||||
}
|
||||
maskReader, err := xz.NewReader(bytes.NewReader(maskV2Xz))
|
||||
if err != nil {
|
||||
logging.Log().Fatal("load kgm mask failed", zap.Error(err))
|
||||
}
|
||||
maskV2, err = io.ReadAll(maskReader)
|
||||
if err != nil {
|
||||
logging.Log().Fatal("load kgm mask failed", zap.Error(err))
|
||||
}
|
||||
fullMaskLen = len(maskV2) * 16
|
||||
initMaskOK = true
|
||||
}
|
110
algo/kwm/kwm.go
110
algo/kwm/kwm.go
@ -2,107 +2,73 @@ package kwm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
)
|
||||
|
||||
var (
|
||||
magicHeader = []byte{
|
||||
0x79, 0x65, 0x65, 0x6C, 0x69, 0x6F, 0x6E, 0x2D,
|
||||
0x6B, 0x75, 0x77, 0x6F, 0x2D, 0x74, 0x6D, 0x65}
|
||||
ErrKwFileSize = errors.New("kwm invalid file size")
|
||||
ErrKwMagicHeader = errors.New("kwm magic header not matched")
|
||||
)
|
||||
|
||||
const magicHeader = "yeelion-kuwo-tme"
|
||||
const keyPreDefined = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
|
||||
|
||||
type Decoder struct {
|
||||
file []byte
|
||||
cipher common.StreamDecoder
|
||||
|
||||
rd io.ReadSeeker
|
||||
offset int
|
||||
|
||||
key []byte
|
||||
outputExt string
|
||||
bitrate int
|
||||
mask []byte
|
||||
|
||||
audio []byte
|
||||
}
|
||||
|
||||
func (d *Decoder) GetCoverImage() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) GetAudioData() []byte {
|
||||
return d.audio
|
||||
}
|
||||
|
||||
func (d *Decoder) GetAudioExt() string {
|
||||
return "." + d.outputExt
|
||||
}
|
||||
|
||||
func (d *Decoder) GetMeta() common.Meta {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewDecoder(data []byte) common.Decoder {
|
||||
//todo: Notice the input data will be changed for now
|
||||
return &Decoder{file: data}
|
||||
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
||||
return &Decoder{rd: rd}
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
lenData := len(d.file)
|
||||
if lenData < 1024 {
|
||||
return ErrKwFileSize
|
||||
}
|
||||
if !bytes.Equal(magicHeader, d.file[:16]) {
|
||||
return ErrKwMagicHeader
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateMask(key []byte) []byte {
|
||||
keyInt := binary.LittleEndian.Uint64(key)
|
||||
keyStr := strconv.FormatUint(keyInt, 10)
|
||||
keyStrTrim := padOrTruncate(keyStr, 32)
|
||||
mask := make([]byte, 32)
|
||||
for i := 0; i < 32; i++ {
|
||||
mask[i] = keyPreDefined[i] ^ keyStrTrim[i]
|
||||
}
|
||||
return mask
|
||||
}
|
||||
|
||||
func (d *Decoder) parseBitrateAndType() {
|
||||
bitType := string(bytes.TrimRight(d.file[0x30:0x38], string(byte(0))))
|
||||
charPos := 0
|
||||
for charPos = range bitType {
|
||||
if !unicode.IsNumber(rune(bitType[charPos])) {
|
||||
break
|
||||
}
|
||||
}
|
||||
var err error
|
||||
d.bitrate, err = strconv.Atoi(bitType[:charPos])
|
||||
header := make([]byte, 0x400) // kwm header is fixed to 1024 bytes
|
||||
_, err := io.ReadFull(d.rd, header)
|
||||
if err != nil {
|
||||
d.bitrate = 0
|
||||
return fmt.Errorf("kwm read header: %w", err)
|
||||
}
|
||||
d.outputExt = strings.ToLower(bitType[charPos:])
|
||||
|
||||
// check magic header, 0x00 - 0x0F
|
||||
if !bytes.Equal([]byte(magicHeader), header[:len(magicHeader)]) {
|
||||
return errors.New("kwm magic header not matched")
|
||||
}
|
||||
|
||||
d.cipher = newKwmCipher(header[0x18:0x20]) // Crypto Key, 0x18 - 0x1F
|
||||
d.bitrate, d.outputExt = parseBitrateAndType(header[0x30:0x38]) // Bitrate & File Extension, 0x30 - 0x38
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode() error {
|
||||
d.parseBitrateAndType()
|
||||
func parseBitrateAndType(header []byte) (int, string) {
|
||||
tmp := strings.TrimRight(string(header), "\x00")
|
||||
sep := strings.IndexFunc(tmp, func(r rune) bool {
|
||||
return !unicode.IsDigit(r)
|
||||
})
|
||||
|
||||
d.mask = generateMask(d.file[0x18:0x20])
|
||||
bitrate, _ := strconv.Atoi(tmp[:sep]) // just ignore the error
|
||||
outputExt := strings.ToLower(tmp[sep:])
|
||||
return bitrate, outputExt
|
||||
}
|
||||
|
||||
d.audio = d.file[1024:]
|
||||
dataLen := len(d.audio)
|
||||
for i := 0; i < dataLen; i++ {
|
||||
d.audio[i] ^= d.mask[i&0x1F] //equals: [i % 32]
|
||||
func (d *Decoder) Read(b []byte) (int, error) {
|
||||
n, err := d.rd.Read(b)
|
||||
if n > 0 {
|
||||
d.cipher.Decrypt(b[:n], d.offset)
|
||||
d.offset += n
|
||||
}
|
||||
return nil
|
||||
return n, err
|
||||
}
|
||||
|
||||
func padOrTruncate(raw string, length int) string {
|
||||
|
31
algo/kwm/kwm_cipher.go
Normal file
31
algo/kwm/kwm_cipher.go
Normal file
@ -0,0 +1,31 @@
|
||||
package kwm
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type kwmCipher struct {
|
||||
mask []byte
|
||||
}
|
||||
|
||||
func newKwmCipher(key []byte) *kwmCipher {
|
||||
return &kwmCipher{mask: generateMask(key)}
|
||||
}
|
||||
|
||||
func generateMask(key []byte) []byte {
|
||||
keyInt := binary.LittleEndian.Uint64(key)
|
||||
keyStr := strconv.FormatUint(keyInt, 10)
|
||||
keyStrTrim := padOrTruncate(keyStr, 32)
|
||||
mask := make([]byte, 32)
|
||||
for i := 0; i < 32; i++ {
|
||||
mask[i] = keyPreDefined[i] ^ keyStrTrim[i]
|
||||
}
|
||||
return mask
|
||||
}
|
||||
|
||||
func (c kwmCipher) Decrypt(buf []byte, offset int) {
|
||||
for i := range buf {
|
||||
buf[i] ^= c.mask[(offset+i)&0x1F] // equivalent: [i % 32]
|
||||
}
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
package ncm
|
||||
|
||||
import (
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
"strings"
|
||||
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
)
|
||||
|
||||
type RawMeta interface {
|
||||
|
272
algo/ncm/ncm.go
272
algo/ncm/ncm.go
@ -2,40 +2,44 @@ package ncm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
"github.com/unlock-music/cli/internal/logging"
|
||||
"github.com/unlock-music/cli/internal/utils"
|
||||
"go.uber.org/zap"
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
"unlock-music.dev/cli/internal/utils"
|
||||
)
|
||||
|
||||
const magicHeader = "CTENFDAM"
|
||||
|
||||
var (
|
||||
magicHeader = []byte{
|
||||
0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D}
|
||||
keyCore = []byte{
|
||||
0x68, 0x7a, 0x48, 0x52, 0x41, 0x6d, 0x73, 0x6f,
|
||||
0x35, 0x6b, 0x49, 0x6e, 0x62, 0x61, 0x78, 0x57}
|
||||
0x35, 0x6b, 0x49, 0x6e, 0x62, 0x61, 0x78, 0x57,
|
||||
}
|
||||
keyMeta = []byte{
|
||||
0x23, 0x31, 0x34, 0x6C, 0x6A, 0x6B, 0x5F, 0x21,
|
||||
0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28}
|
||||
0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28,
|
||||
}
|
||||
)
|
||||
|
||||
func NewDecoder(data []byte) common.Decoder {
|
||||
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
||||
return &Decoder{
|
||||
file: data,
|
||||
fileLen: uint32(len(data)),
|
||||
rd: rd,
|
||||
}
|
||||
}
|
||||
|
||||
type Decoder struct {
|
||||
file []byte
|
||||
fileLen uint32
|
||||
rd io.ReadSeeker
|
||||
offset int
|
||||
|
||||
cipher common.StreamDecoder
|
||||
|
||||
key []byte
|
||||
box []byte
|
||||
@ -46,91 +50,128 @@ type Decoder struct {
|
||||
|
||||
cover []byte
|
||||
audio []byte
|
||||
|
||||
offsetKey uint32
|
||||
offsetMeta uint32
|
||||
offsetCover uint32
|
||||
offsetAudio uint32
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
if !bytes.Equal(magicHeader, d.file[:len(magicHeader)]) {
|
||||
return errors.New("ncm magic header not match")
|
||||
if err := d.validateMagicHeader(); err != nil {
|
||||
return err
|
||||
}
|
||||
d.offsetKey = 8 + 2
|
||||
|
||||
if _, err := d.rd.Seek(2, io.SeekCurrent); err != nil { // 2 bytes gap
|
||||
return fmt.Errorf("ncm seek file: %w", err)
|
||||
}
|
||||
|
||||
keyData, err := d.readKeyData()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := d.readMetaData(); err != nil {
|
||||
return fmt.Errorf("read meta date failed: %w", err)
|
||||
}
|
||||
|
||||
if _, err := d.rd.Seek(5, io.SeekCurrent); err != nil { // 5 bytes gap
|
||||
return fmt.Errorf("ncm seek gap: %w", err)
|
||||
}
|
||||
|
||||
if err := d.readCoverData(); err != nil {
|
||||
return fmt.Errorf("parse ncm cover file failed: %w", err)
|
||||
}
|
||||
|
||||
if err := d.parseMeta(); err != nil {
|
||||
return fmt.Errorf("parse meta failed: %w", err)
|
||||
}
|
||||
|
||||
d.cipher = newNcmCipher(keyData)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) readKeyData() error {
|
||||
if d.offsetKey == 0 || d.offsetKey+4 > d.fileLen {
|
||||
return errors.New("invalid cover file offset")
|
||||
func (d *Decoder) validateMagicHeader() error {
|
||||
header := make([]byte, len(magicHeader)) // 0x00 - 0x07
|
||||
if _, err := d.rd.Read(header); err != nil {
|
||||
return fmt.Errorf("ncm read magic header: %w", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal([]byte(magicHeader), header) {
|
||||
return errors.New("ncm magic header not match")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) readKeyData() ([]byte, error) {
|
||||
bKeyLen := make([]byte, 4) //
|
||||
if _, err := io.ReadFull(d.rd, bKeyLen); err != nil {
|
||||
return nil, fmt.Errorf("ncm read key length: %w", err)
|
||||
}
|
||||
bKeyLen := d.file[d.offsetKey : d.offsetKey+4]
|
||||
iKeyLen := binary.LittleEndian.Uint32(bKeyLen)
|
||||
d.offsetMeta = d.offsetKey + 4 + iKeyLen
|
||||
|
||||
bKeyRaw := make([]byte, iKeyLen)
|
||||
if _, err := io.ReadFull(d.rd, bKeyRaw); err != nil {
|
||||
return nil, fmt.Errorf("ncm read key data: %w", err)
|
||||
}
|
||||
for i := uint32(0); i < iKeyLen; i++ {
|
||||
bKeyRaw[i] = d.file[i+4+d.offsetKey] ^ 0x64
|
||||
bKeyRaw[i] ^= 0x64
|
||||
}
|
||||
|
||||
d.key = utils.PKCS7UnPadding(utils.DecryptAes128Ecb(bKeyRaw, keyCore))[17:]
|
||||
return nil
|
||||
return utils.PKCS7UnPadding(utils.DecryptAES128ECB(bKeyRaw, keyCore))[17:], nil
|
||||
}
|
||||
|
||||
func (d *Decoder) readMetaData() error {
|
||||
if d.offsetMeta == 0 || d.offsetMeta+4 > d.fileLen {
|
||||
return errors.New("invalid meta file offset")
|
||||
bMetaLen := make([]byte, 4) //
|
||||
if _, err := io.ReadFull(d.rd, bMetaLen); err != nil {
|
||||
return fmt.Errorf("ncm read key length: %w", err)
|
||||
}
|
||||
bMetaLen := d.file[d.offsetMeta : d.offsetMeta+4]
|
||||
iMetaLen := binary.LittleEndian.Uint32(bMetaLen)
|
||||
d.offsetCover = d.offsetMeta + 4 + iMetaLen
|
||||
|
||||
if iMetaLen == 0 {
|
||||
return errors.New("no any meta file found")
|
||||
return nil // no meta data
|
||||
}
|
||||
|
||||
// Why sub 22: Remove "163 key(Don't modify):"
|
||||
bKeyRaw := make([]byte, iMetaLen-22)
|
||||
for i := uint32(0); i < iMetaLen-22; i++ {
|
||||
bKeyRaw[i] = d.file[d.offsetMeta+4+22+i] ^ 0x63
|
||||
bMetaRaw := make([]byte, iMetaLen)
|
||||
if _, err := io.ReadFull(d.rd, bMetaRaw); err != nil {
|
||||
return fmt.Errorf("ncm read meta data: %w", err)
|
||||
}
|
||||
bMetaRaw = bMetaRaw[22:] // skip "163 key(Don't modify):"
|
||||
for i := 0; i < len(bMetaRaw); i++ {
|
||||
bMetaRaw[i] ^= 0x63
|
||||
}
|
||||
|
||||
cipherText, err := base64.StdEncoding.DecodeString(string(bKeyRaw))
|
||||
cipherText, err := base64.StdEncoding.DecodeString(string(bMetaRaw))
|
||||
if err != nil {
|
||||
return errors.New("decode ncm meta failed: " + err.Error())
|
||||
}
|
||||
metaRaw := utils.PKCS7UnPadding(utils.DecryptAes128Ecb(cipherText, keyMeta))
|
||||
sepIdx := bytes.IndexRune(metaRaw, ':')
|
||||
if sepIdx == -1 {
|
||||
metaRaw := utils.PKCS7UnPadding(utils.DecryptAES128ECB(cipherText, keyMeta))
|
||||
sep := bytes.IndexByte(metaRaw, ':')
|
||||
if sep == -1 {
|
||||
return errors.New("invalid ncm meta file")
|
||||
}
|
||||
|
||||
d.metaType = string(metaRaw[:sepIdx])
|
||||
d.metaRaw = metaRaw[sepIdx+1:]
|
||||
d.metaType = string(metaRaw[:sep])
|
||||
d.metaRaw = metaRaw[sep+1:]
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) buildKeyBox() {
|
||||
box := make([]byte, 256)
|
||||
for i := 0; i < 256; i++ {
|
||||
box[i] = byte(i)
|
||||
func (d *Decoder) readCoverData() error {
|
||||
bCoverCRC := make([]byte, 4)
|
||||
if _, err := io.ReadFull(d.rd, bCoverCRC); err != nil {
|
||||
return fmt.Errorf("ncm read cover crc: %w", err)
|
||||
}
|
||||
|
||||
keyLen := len(d.key)
|
||||
var j byte
|
||||
for i := 0; i < 256; i++ {
|
||||
j = box[i] + j + d.key[i%keyLen]
|
||||
box[i], box[j] = box[j], box[i]
|
||||
bCoverLen := make([]byte, 4) //
|
||||
if _, err := io.ReadFull(d.rd, bCoverLen); err != nil {
|
||||
return fmt.Errorf("ncm read cover length: %w", err)
|
||||
}
|
||||
iCoverLen := binary.LittleEndian.Uint32(bCoverLen)
|
||||
|
||||
d.box = make([]byte, 256)
|
||||
var _i byte
|
||||
for i := 0; i < 256; i++ {
|
||||
_i = byte(i + 1)
|
||||
si := box[_i]
|
||||
sj := box[_i+si]
|
||||
d.box[i] = box[si+sj]
|
||||
coverBuf := make([]byte, iCoverLen)
|
||||
if _, err := io.ReadFull(d.rd, coverBuf); err != nil {
|
||||
return fmt.Errorf("ncm read cover data: %w", err)
|
||||
}
|
||||
d.cover = coverBuf
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) parseMeta() error {
|
||||
@ -146,59 +187,16 @@ func (d *Decoder) parseMeta() error {
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Decoder) readCoverData() error {
|
||||
if d.offsetCover == 0 || d.offsetCover+13 > d.fileLen {
|
||||
return errors.New("invalid cover file offset")
|
||||
func (d *Decoder) Read(buf []byte) (int, error) {
|
||||
n, err := d.rd.Read(buf)
|
||||
if n > 0 {
|
||||
d.cipher.Decrypt(buf[:n], d.offset)
|
||||
d.offset += n
|
||||
}
|
||||
|
||||
coverLenStart := d.offsetCover + 5 + 4
|
||||
bCoverLen := d.file[coverLenStart : coverLenStart+4]
|
||||
|
||||
iCoverLen := binary.LittleEndian.Uint32(bCoverLen)
|
||||
d.offsetAudio = coverLenStart + 4 + iCoverLen
|
||||
if iCoverLen == 0 {
|
||||
return errors.New("no any cover file found")
|
||||
}
|
||||
d.cover = d.file[coverLenStart+4 : 4+coverLenStart+iCoverLen]
|
||||
return nil
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (d *Decoder) readAudioData() error {
|
||||
if d.offsetAudio == 0 || d.offsetAudio > d.fileLen {
|
||||
return errors.New("invalid audio offset")
|
||||
}
|
||||
audioRaw := d.file[d.offsetAudio:]
|
||||
audioLen := len(audioRaw)
|
||||
d.audio = make([]byte, audioLen)
|
||||
for i := uint32(0); i < uint32(audioLen); i++ {
|
||||
d.audio[i] = d.box[i&0xff] ^ audioRaw[i]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode() error {
|
||||
if err := d.readKeyData(); err != nil {
|
||||
return err
|
||||
}
|
||||
d.buildKeyBox()
|
||||
|
||||
err := d.readMetaData()
|
||||
if err == nil {
|
||||
err = d.parseMeta()
|
||||
}
|
||||
if err != nil {
|
||||
logging.Log().Warn("parse ncm meta file failed", zap.Error(err))
|
||||
}
|
||||
|
||||
err = d.readCoverData()
|
||||
if err != nil {
|
||||
logging.Log().Warn("parse ncm cover file failed", zap.Error(err))
|
||||
}
|
||||
|
||||
return d.readAudioData()
|
||||
}
|
||||
|
||||
func (d Decoder) GetAudioExt() string {
|
||||
func (d *Decoder) GetAudioExt() string {
|
||||
if d.meta != nil {
|
||||
if format := d.meta.GetFormat(); format != "" {
|
||||
return "." + d.meta.GetFormat()
|
||||
@ -207,40 +205,34 @@ func (d Decoder) GetAudioExt() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (d Decoder) GetAudioData() []byte {
|
||||
return d.audio
|
||||
}
|
||||
|
||||
func (d Decoder) GetCoverImage() []byte {
|
||||
func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
|
||||
if d.cover != nil {
|
||||
return d.cover
|
||||
return d.cover, nil
|
||||
}
|
||||
{
|
||||
imgURL := d.meta.GetAlbumImageURL()
|
||||
if d.meta != nil && !strings.HasPrefix(imgURL, "http") {
|
||||
return nil
|
||||
}
|
||||
resp, err := http.Get(imgURL)
|
||||
if err != nil {
|
||||
logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
|
||||
return nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
logging.Log().Warn("download image failed", zap.String("http", resp.Status),
|
||||
zap.String("url", imgURL))
|
||||
return nil
|
||||
}
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
|
||||
return nil
|
||||
}
|
||||
return data
|
||||
imgURL := d.meta.GetAlbumImageURL()
|
||||
if d.meta != nil && !strings.HasPrefix(imgURL, "http") {
|
||||
return nil, nil // no cover image
|
||||
}
|
||||
|
||||
// fetch cover image
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, imgURL, nil)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("download image failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("download image failed: unexpected http status %s", resp.Status)
|
||||
}
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("download image failed: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (d Decoder) GetMeta() common.Meta {
|
||||
func (d *Decoder) GetMeta() common.Meta {
|
||||
return d.meta
|
||||
}
|
||||
|
||||
|
42
algo/ncm/ncm_cipher.go
Normal file
42
algo/ncm/ncm_cipher.go
Normal file
@ -0,0 +1,42 @@
|
||||
package ncm
|
||||
|
||||
type ncmCipher struct {
|
||||
key []byte
|
||||
box []byte
|
||||
}
|
||||
|
||||
func newNcmCipher(key []byte) *ncmCipher {
|
||||
return &ncmCipher{
|
||||
key: key,
|
||||
box: buildKeyBox(key),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ncmCipher) Decrypt(buf []byte, offset int) {
|
||||
for i := 0; i < len(buf); i++ {
|
||||
buf[i] ^= c.box[(i+offset)&0xff]
|
||||
}
|
||||
}
|
||||
|
||||
func buildKeyBox(key []byte) []byte {
|
||||
box := make([]byte, 256)
|
||||
for i := 0; i < 256; i++ {
|
||||
box[i] = byte(i)
|
||||
}
|
||||
|
||||
var j byte
|
||||
for i := 0; i < 256; i++ {
|
||||
j = box[i] + j + key[i%len(key)]
|
||||
box[i], box[j] = box[j], box[i]
|
||||
}
|
||||
|
||||
ret := make([]byte, 256)
|
||||
var _i byte
|
||||
for i := 0; i < 256; i++ {
|
||||
_i = byte(i + 1)
|
||||
si := box[_i]
|
||||
sj := box[_i+si]
|
||||
ret[i] = box[si+sj]
|
||||
}
|
||||
return ret
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package qmc
|
||||
|
||||
type streamCipher interface {
|
||||
Decrypt(buf []byte, offset int)
|
||||
}
|
@ -8,7 +8,7 @@ type mapCipher struct {
|
||||
size int
|
||||
}
|
||||
|
||||
func NewMapCipher(key []byte) (*mapCipher, error) {
|
||||
func newMapCipher(key []byte) (*mapCipher, error) {
|
||||
if len(key) == 0 {
|
||||
return nil, errors.New("qmc/cipher_map: invalid key size")
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ func Test_mapCipher_Decrypt(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("load testing data failed: %s", err)
|
||||
}
|
||||
c, err := NewMapCipher(key)
|
||||
c, err := newMapCipher(key)
|
||||
if err != nil {
|
||||
t.Errorf("init mapCipher failed: %s", err)
|
||||
return
|
||||
|
@ -12,9 +12,9 @@ type rc4Cipher struct {
|
||||
n int
|
||||
}
|
||||
|
||||
// NewRC4Cipher creates and returns a new rc4Cipher. The key argument should be the
|
||||
// newRC4Cipher creates and returns a new rc4Cipher. The key argument should be the
|
||||
// RC4 key, at least 1 byte and at most 256 bytes.
|
||||
func NewRC4Cipher(key []byte) (*rc4Cipher, error) {
|
||||
func newRC4Cipher(key []byte) (*rc4Cipher, error) {
|
||||
n := len(key)
|
||||
if n == 0 {
|
||||
return nil, errors.New("qmc/cipher_rc4: invalid key size")
|
||||
|
@ -37,7 +37,7 @@ func Test_rc4Cipher_Decrypt(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("load testing data failed: %s", err)
|
||||
}
|
||||
c, err := NewRC4Cipher(key)
|
||||
c, err := newRC4Cipher(key)
|
||||
if err != nil {
|
||||
t.Errorf("init rc4Cipher failed: %s", err)
|
||||
return
|
||||
@ -55,7 +55,7 @@ func BenchmarkRc4Cipher_Decrypt(b *testing.B) {
|
||||
if err != nil {
|
||||
b.Fatalf("load testing data failed: %s", err)
|
||||
}
|
||||
c, err := NewRC4Cipher(key)
|
||||
c, err := newRC4Cipher(key)
|
||||
if err != nil {
|
||||
b.Errorf("init rc4Cipher failed: %s", err)
|
||||
return
|
||||
@ -72,7 +72,7 @@ func Test_rc4Cipher_encFirstSegment(t *testing.T) {
|
||||
t.Fatalf("load testing data failed: %s", err)
|
||||
}
|
||||
t.Run("first-block(0~128)", func(t *testing.T) {
|
||||
c, err := NewRC4Cipher(key)
|
||||
c, err := newRC4Cipher(key)
|
||||
if err != nil {
|
||||
t.Errorf("init rc4Cipher failed: %s", err)
|
||||
return
|
||||
@ -91,7 +91,7 @@ func Test_rc4Cipher_encASegment(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Run("align-block(128~5120)", func(t *testing.T) {
|
||||
c, err := NewRC4Cipher(key)
|
||||
c, err := newRC4Cipher(key)
|
||||
if err != nil {
|
||||
t.Errorf("init rc4Cipher failed: %s", err)
|
||||
return
|
||||
@ -102,7 +102,7 @@ func Test_rc4Cipher_encASegment(t *testing.T) {
|
||||
}
|
||||
})
|
||||
t.Run("simple-block(5120~10240)", func(t *testing.T) {
|
||||
c, err := NewRC4Cipher(key)
|
||||
c, err := newRC4Cipher(key)
|
||||
if err != nil {
|
||||
t.Errorf("init rc4Cipher failed: %s", err)
|
||||
return
|
||||
|
@ -1,6 +1,6 @@
|
||||
package qmc
|
||||
|
||||
func NewStaticCipher() *staticCipher {
|
||||
func newStaticCipher() *staticCipher {
|
||||
return &defaultStaticCipher
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,10 @@
|
||||
package qmc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"golang.org/x/crypto/tea"
|
||||
@ -16,16 +18,30 @@ func simpleMakeKey(salt byte, length int) []byte {
|
||||
}
|
||||
return keyBuf
|
||||
}
|
||||
func DecryptKey(rawKey []byte) ([]byte, error) {
|
||||
|
||||
const rawKeyPrefixV2 = "QQMusic EncV2,Key:"
|
||||
|
||||
func deriveKey(rawKey []byte) ([]byte, error) {
|
||||
rawKeyDec := make([]byte, base64.StdEncoding.DecodedLen(len(rawKey)))
|
||||
n, err := base64.StdEncoding.Decode(rawKeyDec, rawKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if n < 16 {
|
||||
rawKeyDec = rawKeyDec[:n]
|
||||
|
||||
if bytes.HasPrefix(rawKeyDec, []byte(rawKeyPrefixV2)) {
|
||||
rawKeyDec, err = deriveKeyV2(bytes.TrimPrefix(rawKeyDec, []byte(rawKeyPrefixV2)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("deriveKeyV2 failed: %w", err)
|
||||
}
|
||||
}
|
||||
return deriveKeyV1(rawKeyDec)
|
||||
}
|
||||
|
||||
func deriveKeyV1(rawKeyDec []byte) ([]byte, error) {
|
||||
if len(rawKeyDec) < 16 {
|
||||
return nil, errors.New("key length is too short")
|
||||
}
|
||||
rawKeyDec = rawKeyDec[:n]
|
||||
|
||||
simpleKey := simpleMakeKey(106, 8)
|
||||
teaKey := make([]byte, 16)
|
||||
@ -40,6 +56,37 @@ func DecryptKey(rawKey []byte) ([]byte, error) {
|
||||
}
|
||||
return append(rawKeyDec[:8], rs...), nil
|
||||
}
|
||||
|
||||
var (
|
||||
deriveV2Key1 = []byte{
|
||||
0x33, 0x38, 0x36, 0x5A, 0x4A, 0x59, 0x21, 0x40,
|
||||
0x23, 0x2A, 0x24, 0x25, 0x5E, 0x26, 0x29, 0x28,
|
||||
}
|
||||
|
||||
deriveV2Key2 = []byte{
|
||||
0x2A, 0x2A, 0x23, 0x21, 0x28, 0x23, 0x24, 0x25,
|
||||
0x26, 0x5E, 0x61, 0x31, 0x63, 0x5A, 0x2C, 0x54,
|
||||
}
|
||||
)
|
||||
|
||||
func deriveKeyV2(raw []byte) ([]byte, error) {
|
||||
buf, err := decryptTencentTea(raw, deriveV2Key1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf, err = decryptTencentTea(buf, deriveV2Key2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
n, err := base64.StdEncoding.Decode(buf, buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf[:n], nil
|
||||
}
|
||||
|
||||
func decryptTencentTea(inBuf []byte, key []byte) ([]byte, error) {
|
||||
const saltLen = 2
|
||||
const zeroLen = 7
|
||||
@ -59,9 +106,7 @@ func decryptTencentTea(inBuf []byte, key []byte) ([]byte, error) {
|
||||
blk.Decrypt(destBuf, inBuf)
|
||||
padLen := int(destBuf[0] & 0x7)
|
||||
outLen := len(inBuf) - 1 - padLen - saltLen - zeroLen
|
||||
if padLen+saltLen != 8 {
|
||||
return nil, errors.New("invalid pad len")
|
||||
}
|
||||
|
||||
out := make([]byte, outLen)
|
||||
|
||||
ivPrev := make([]byte, 8)
|
||||
@ -108,6 +153,7 @@ func decryptTencentTea(inBuf []byte, key []byte) ([]byte, error) {
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func xor8Bytes(dst, a, b []byte) {
|
||||
for i := 0; i < 8; i++ {
|
||||
dst[i] = a[i] ^ b[i]
|
@ -43,13 +43,13 @@ func TestDecryptKey(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("load test data failed: %s", err)
|
||||
}
|
||||
got, err := DecryptKey(raw)
|
||||
got, err := deriveKey(raw)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("DecryptKey() error = %v, wantErr %v", err, tt.wantErr)
|
||||
t.Errorf("deriveKey() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("DecryptKey() got = %v..., want %v...",
|
||||
t.Errorf("deriveKey() got = %v..., want %v...",
|
||||
string(got[:32]), string(want[:32]))
|
||||
}
|
||||
})
|
210
algo/qmc/qmc.go
210
algo/qmc/qmc.go
@ -4,22 +4,23 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
)
|
||||
|
||||
type Decoder struct {
|
||||
r io.ReadSeeker
|
||||
fileExt string
|
||||
raw io.ReadSeeker
|
||||
audio io.Reader
|
||||
offset int
|
||||
audioLen int
|
||||
|
||||
audioLen int
|
||||
decodedKey []byte
|
||||
cipher streamCipher
|
||||
offset int
|
||||
cipher common.StreamDecoder
|
||||
|
||||
decodedKey []byte
|
||||
rawMetaExtra1 int
|
||||
rawMetaExtra2 int
|
||||
}
|
||||
@ -27,113 +28,113 @@ type Decoder struct {
|
||||
// Read implements io.Reader, offer the decrypted audio data.
|
||||
// Validate should call before Read to check if the file is valid.
|
||||
func (d *Decoder) Read(p []byte) (int, error) {
|
||||
n := len(p)
|
||||
if d.audioLen-d.offset <= 0 {
|
||||
return 0, io.EOF
|
||||
} else if d.audioLen-d.offset < n {
|
||||
n = d.audioLen - d.offset
|
||||
n, err := d.audio.Read(p)
|
||||
if n > 0 {
|
||||
d.cipher.Decrypt(p[:n], d.offset)
|
||||
d.offset += n
|
||||
}
|
||||
m, err := d.r.Read(p[:n])
|
||||
if m == 0 {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
d.cipher.Decrypt(p[:m], d.offset)
|
||||
d.offset += m
|
||||
return m, err
|
||||
return n, err
|
||||
}
|
||||
|
||||
func NewDecoder(r io.ReadSeeker) (*Decoder, error) {
|
||||
d := &Decoder{r: r}
|
||||
err := d.searchKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(d.decodedKey) > 300 {
|
||||
d.cipher, err = NewRC4Cipher(d.decodedKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if len(d.decodedKey) != 0 {
|
||||
d.cipher, err = NewMapCipher(d.decodedKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
d.cipher = NewStaticCipher()
|
||||
}
|
||||
|
||||
_, err = d.r.Seek(0, io.SeekStart)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return d, nil
|
||||
func NewDecoder(r io.ReadSeeker) common.Decoder {
|
||||
return &Decoder{raw: r}
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
buf := make([]byte, 16)
|
||||
if _, err := io.ReadFull(d.r, buf); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := d.r.Seek(0, io.SeekStart)
|
||||
// search & derive key
|
||||
err := d.searchKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.cipher.Decrypt(buf, 0)
|
||||
fileExt, ok := common.SniffAll(buf)
|
||||
if !ok {
|
||||
return errors.New("detect file type failed")
|
||||
// check cipher type and init decode cipher
|
||||
if len(d.decodedKey) > 300 {
|
||||
d.cipher, err = newRC4Cipher(d.decodedKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if len(d.decodedKey) != 0 {
|
||||
d.cipher, err = newMapCipher(d.decodedKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
d.cipher = newStaticCipher()
|
||||
}
|
||||
d.fileExt = fileExt
|
||||
|
||||
// test with first 16 bytes
|
||||
if err := d.validateDecode(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// reset position, limit to audio, prepare for Read
|
||||
if _, err := d.raw.Seek(0, io.SeekStart); err != nil {
|
||||
return err
|
||||
}
|
||||
d.audio = io.LimitReader(d.raw, int64(d.audioLen))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Decoder) GetFileExt() string {
|
||||
return d.fileExt
|
||||
func (d *Decoder) validateDecode() error {
|
||||
_, err := d.raw.Seek(0, io.SeekStart)
|
||||
if err != nil {
|
||||
return fmt.Errorf("qmc seek to start: %w", err)
|
||||
}
|
||||
|
||||
buf := make([]byte, 16)
|
||||
if _, err := io.ReadFull(d.raw, buf); err != nil {
|
||||
return fmt.Errorf("qmc read header: %w", err)
|
||||
}
|
||||
|
||||
d.cipher.Decrypt(buf, 0)
|
||||
_, ok := common.SniffAll(buf)
|
||||
if !ok {
|
||||
return errors.New("qmc: detect file type failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) searchKey() error {
|
||||
fileSizeM4, err := d.r.Seek(-4, io.SeekEnd)
|
||||
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
|
||||
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if string(buf) == "QTag" {
|
||||
if err := d.readRawMetaQTag(); err != nil {
|
||||
return err
|
||||
}
|
||||
return d.readRawMetaQTag()
|
||||
} else if string(buf) == "STag" {
|
||||
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
|
||||
} else {
|
||||
size := binary.LittleEndian.Uint32(buf)
|
||||
if size < 0x300 && size != 0 {
|
||||
if size <= 0xFFFF && size != 0 { // assume size is key len
|
||||
return d.readRawKey(int64(size))
|
||||
} else {
|
||||
// try to use default static cipher
|
||||
d.audioLen = int(fileSizeM4 + 4)
|
||||
return nil
|
||||
}
|
||||
// try to use default static cipher
|
||||
d.audioLen = int(fileSizeM4 + 4)
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
||||
audioLen, err := d.r.Seek(-(4 + rawKeyLen), io.SeekEnd)
|
||||
audioLen, err := d.raw.Seek(-(4 + rawKeyLen), io.SeekEnd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.audioLen = int(audioLen)
|
||||
|
||||
rawKeyData, err := io.ReadAll(io.LimitReader(d.r, rawKeyLen))
|
||||
rawKeyData, err := io.ReadAll(io.LimitReader(d.raw, rawKeyLen))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.decodedKey, err = DecryptKey(rawKeyData)
|
||||
// clean suffix NULs
|
||||
rawKeyData = bytes.TrimRight(rawKeyData, "\x00")
|
||||
|
||||
d.decodedKey, err = deriveKey(rawKeyData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -143,22 +144,22 @@ func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
||||
|
||||
func (d *Decoder) readRawMetaQTag() error {
|
||||
// get raw meta data len
|
||||
if _, err := d.r.Seek(-8, io.SeekEnd); err != nil {
|
||||
if _, err := d.raw.Seek(-8, io.SeekEnd); err != nil {
|
||||
return err
|
||||
}
|
||||
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
|
||||
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rawMetaLen := int64(binary.BigEndian.Uint32(buf))
|
||||
|
||||
// read raw meta data
|
||||
audioLen, err := d.r.Seek(-(8 + rawMetaLen), io.SeekEnd)
|
||||
audioLen, err := d.raw.Seek(-(8 + rawMetaLen), io.SeekEnd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.audioLen = int(audioLen)
|
||||
rawMetaData, err := io.ReadAll(io.LimitReader(d.r, rawMetaLen))
|
||||
rawMetaData, err := io.ReadAll(io.LimitReader(d.raw, rawMetaLen))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -168,7 +169,7 @@ func (d *Decoder) readRawMetaQTag() error {
|
||||
return errors.New("invalid raw meta data")
|
||||
}
|
||||
|
||||
d.decodedKey, err = DecryptKey([]byte(items[0]))
|
||||
d.decodedKey, err = deriveKey([]byte(items[0]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -207,57 +208,6 @@ func init() {
|
||||
"mflac", "mflac0", //QQ Music New Flac
|
||||
}
|
||||
for _, ext := range supportedExts {
|
||||
common.RegisterDecoder(ext, false, newCompactDecoder)
|
||||
common.RegisterDecoder(ext, false, NewDecoder)
|
||||
}
|
||||
}
|
||||
|
||||
type compactDecoder struct {
|
||||
decoder *Decoder
|
||||
createErr error
|
||||
buf *bytes.Buffer
|
||||
}
|
||||
|
||||
func newCompactDecoder(p []byte) common.Decoder {
|
||||
r := bytes.NewReader(p)
|
||||
d, err := NewDecoder(r)
|
||||
c := compactDecoder{
|
||||
decoder: d,
|
||||
createErr: err,
|
||||
}
|
||||
return &c
|
||||
}
|
||||
|
||||
func (c *compactDecoder) Validate() error {
|
||||
if c.createErr != nil {
|
||||
return c.createErr
|
||||
}
|
||||
return c.decoder.Validate()
|
||||
}
|
||||
|
||||
func (c *compactDecoder) Decode() error {
|
||||
if c.createErr != nil {
|
||||
return c.createErr
|
||||
}
|
||||
c.buf = bytes.NewBuffer(nil)
|
||||
_, err := io.Copy(c.buf, c.decoder)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *compactDecoder) GetCoverImage() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *compactDecoder) GetAudioData() []byte {
|
||||
return c.buf.Bytes()
|
||||
}
|
||||
|
||||
func (c *compactDecoder) GetAudioExt() string {
|
||||
if c.createErr != nil {
|
||||
return ""
|
||||
}
|
||||
return c.decoder.GetFileExt()
|
||||
}
|
||||
|
||||
func (c *compactDecoder) GetMeta() common.Meta {
|
||||
return nil
|
||||
}
|
||||
|
@ -3,77 +3,54 @@ package tm
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
)
|
||||
|
||||
var replaceHeader = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70}
|
||||
var magicHeader = []byte{0x51, 0x51, 0x4D, 0x55} //0x15, 0x1D, 0x1A, 0x21
|
||||
|
||||
type Decoder struct {
|
||||
file []byte
|
||||
audio []byte
|
||||
headerMatch bool
|
||||
audioExt string
|
||||
}
|
||||
|
||||
func (d *Decoder) GetCoverImage() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) GetAudioData() []byte {
|
||||
return d.audio
|
||||
}
|
||||
|
||||
func (d *Decoder) GetAudioExt() string {
|
||||
if d.audioExt != "" {
|
||||
return "." + d.audioExt
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (d *Decoder) GetMeta() common.Meta {
|
||||
return nil
|
||||
raw io.ReadSeeker
|
||||
offset int
|
||||
audio io.Reader
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
if len(d.file) < 8 {
|
||||
return errors.New("invalid file size")
|
||||
header := make([]byte, 8)
|
||||
if _, err := io.ReadFull(d.raw, header); err != nil {
|
||||
return fmt.Errorf("tm read header: %w", err)
|
||||
}
|
||||
if !bytes.Equal(magicHeader, d.file[:4]) {
|
||||
return errors.New("not a valid tm file")
|
||||
|
||||
if bytes.Equal(magicHeader, header[:len(magicHeader)]) { // replace m4a header
|
||||
d.audio = io.MultiReader(bytes.NewReader(replaceHeader), d.raw)
|
||||
return nil
|
||||
}
|
||||
d.headerMatch = true
|
||||
return nil
|
||||
|
||||
if _, ok := common.SniffAll(header); ok { // not encrypted
|
||||
d.audio = io.MultiReader(bytes.NewReader(header), d.raw)
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("tm: valid magic header")
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode() error {
|
||||
d.audio = d.file
|
||||
if d.headerMatch {
|
||||
for i := 0; i < 8; i++ {
|
||||
d.audio[i] = replaceHeader[i]
|
||||
}
|
||||
d.audioExt = "m4a"
|
||||
}
|
||||
return nil
|
||||
func (d *Decoder) Read(buf []byte) (int, error) {
|
||||
return d.audio.Read(buf)
|
||||
}
|
||||
|
||||
//goland:noinspection GoUnusedExportedFunction
|
||||
func NewDecoder(data []byte) common.Decoder {
|
||||
return &Decoder{file: data}
|
||||
}
|
||||
|
||||
func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
|
||||
return func(file []byte) common.Decoder {
|
||||
return &Decoder{file: file, audioExt: ext}
|
||||
}
|
||||
func NewTmDecoder(rd io.ReadSeeker) common.Decoder {
|
||||
return &Decoder{raw: rd}
|
||||
}
|
||||
|
||||
func init() {
|
||||
// QQ Music IOS M4a
|
||||
common.RegisterDecoder("tm2", false, DecoderFuncWithExt("m4a"))
|
||||
common.RegisterDecoder("tm6", false, DecoderFuncWithExt("m4a"))
|
||||
// QQ Music IOS Mp3
|
||||
common.RegisterDecoder("tm0", false, common.NewRawDecoder)
|
||||
common.RegisterDecoder("tm3", false, common.NewRawDecoder)
|
||||
// QQ Music IOS M4a (replace header)
|
||||
common.RegisterDecoder("tm2", false, NewTmDecoder)
|
||||
common.RegisterDecoder("tm6", false, NewTmDecoder)
|
||||
|
||||
// QQ Music IOS Mp3 (not encrypted)
|
||||
common.RegisterDecoder("tm0", false, NewTmDecoder)
|
||||
common.RegisterDecoder("tm3", false, NewTmDecoder)
|
||||
}
|
||||
|
@ -3,9 +3,10 @@ package xm
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
"github.com/unlock-music/cli/internal/logging"
|
||||
"go.uber.org/zap"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -17,26 +18,19 @@ var (
|
||||
" MP3": "mp3",
|
||||
" A4M": "m4a",
|
||||
}
|
||||
ErrFileSize = errors.New("xm invalid file size")
|
||||
ErrMagicHeader = errors.New("xm magic header not matched")
|
||||
)
|
||||
|
||||
type Decoder struct {
|
||||
file []byte
|
||||
headerLen uint32
|
||||
rd io.ReadSeeker
|
||||
offset int
|
||||
|
||||
cipher common.StreamDecoder
|
||||
outputExt string
|
||||
mask byte
|
||||
audio []byte
|
||||
}
|
||||
|
||||
func (d *Decoder) GetCoverImage() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) GetAudioData() []byte {
|
||||
return d.audio
|
||||
}
|
||||
|
||||
func (d *Decoder) GetAudioExt() string {
|
||||
if d.outputExt != "" {
|
||||
return "." + d.outputExt
|
||||
@ -45,62 +39,53 @@ func (d *Decoder) GetAudioExt() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (d *Decoder) GetMeta() common.Meta {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewDecoder(data []byte) common.Decoder {
|
||||
return &Decoder{file: data}
|
||||
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
||||
return &Decoder{rd: rd}
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
lenData := len(d.file)
|
||||
if lenData < 16 {
|
||||
return ErrFileSize
|
||||
header := make([]byte, 16) // xm header is fixed to 16 bytes
|
||||
|
||||
if _, err := io.ReadFull(d.rd, header); err != nil {
|
||||
return fmt.Errorf("xm read header: %w", err)
|
||||
}
|
||||
if !bytes.Equal(magicHeader, d.file[:4]) ||
|
||||
!bytes.Equal(magicHeader2, d.file[8:12]) {
|
||||
|
||||
// 0x00 - 0x03 and 0x08 - 0x0B: magic header
|
||||
if !bytes.Equal(magicHeader, header[:4]) || !bytes.Equal(magicHeader2, header[8:12]) {
|
||||
return ErrMagicHeader
|
||||
}
|
||||
|
||||
// 0x04 - 0x07: Audio File Type
|
||||
var ok bool
|
||||
d.outputExt, ok = typeMapping[string(d.file[4:8])]
|
||||
d.outputExt, ok = typeMapping[string(header[4:8])]
|
||||
if !ok {
|
||||
return errors.New("detect unknown xm file type: " + string(d.file[4:8]))
|
||||
return fmt.Errorf("xm detect unknown audio type: %s", string(header[4:8]))
|
||||
}
|
||||
|
||||
if d.file[14] != 0 {
|
||||
logging.Log().Warn("not a simple xm file", zap.Uint8("b[14]", d.file[14]))
|
||||
}
|
||||
d.headerLen = uint32(d.file[12]) | uint32(d.file[13])<<8 | uint32(d.file[14])<<16 // LittleEndian Unit24
|
||||
if d.headerLen+16 > uint32(lenData) {
|
||||
return ErrFileSize
|
||||
}
|
||||
// 0x0C - 0x0E, Encrypt Start At, LittleEndian Unit24
|
||||
encStartAt := uint32(header[12]) | uint32(header[13])<<8 | uint32(header[14])<<16
|
||||
|
||||
// 0x0F, XOR Mask
|
||||
d.cipher = newXmCipher(header[15], int(encStartAt))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode() error {
|
||||
d.mask = d.file[15]
|
||||
d.audio = d.file[16:]
|
||||
dataLen := uint32(len(d.audio))
|
||||
for i := d.headerLen; i < dataLen; i++ {
|
||||
d.audio[i] = ^(d.audio[i] - d.mask)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
|
||||
return func(file []byte) common.Decoder {
|
||||
return &Decoder{file: file, outputExt: ext}
|
||||
func (d *Decoder) Read(p []byte) (int, error) {
|
||||
n, err := d.rd.Read(p)
|
||||
if n > 0 {
|
||||
d.cipher.Decrypt(p[:n], d.offset)
|
||||
d.offset += n
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Xiami Wav/M4a/Mp3/Flac
|
||||
common.RegisterDecoder("xm", false, NewDecoder)
|
||||
// Xiami Typed Format
|
||||
common.RegisterDecoder("wav", false, DecoderFuncWithExt("wav"))
|
||||
common.RegisterDecoder("mp3", false, DecoderFuncWithExt("mp3"))
|
||||
common.RegisterDecoder("flac", false, DecoderFuncWithExt("flac"))
|
||||
common.RegisterDecoder("m4a", false, DecoderFuncWithExt("m4a"))
|
||||
common.RegisterDecoder("wav", false, NewDecoder)
|
||||
common.RegisterDecoder("mp3", false, NewDecoder)
|
||||
common.RegisterDecoder("flac", false, NewDecoder)
|
||||
common.RegisterDecoder("m4a", false, NewDecoder)
|
||||
}
|
||||
|
21
algo/xm/xm_cipher.go
Normal file
21
algo/xm/xm_cipher.go
Normal file
@ -0,0 +1,21 @@
|
||||
package xm
|
||||
|
||||
type xmCipher struct {
|
||||
mask byte
|
||||
encryptStartAt int
|
||||
}
|
||||
|
||||
func newXmCipher(mask byte, encryptStartAt int) *xmCipher {
|
||||
return &xmCipher{
|
||||
mask: mask,
|
||||
encryptStartAt: encryptStartAt,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *xmCipher) Decrypt(buf []byte, offset int) {
|
||||
for i := 0; i < len(buf); i++ {
|
||||
if offset+i >= c.encryptStartAt {
|
||||
buf[i] ^= c.mask
|
||||
}
|
||||
}
|
||||
}
|
@ -1,34 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
_ "github.com/unlock-music/cli/algo/kgm"
|
||||
_ "github.com/unlock-music/cli/algo/kwm"
|
||||
_ "github.com/unlock-music/cli/algo/ncm"
|
||||
_ "github.com/unlock-music/cli/algo/qmc"
|
||||
_ "github.com/unlock-music/cli/algo/tm"
|
||||
_ "github.com/unlock-music/cli/algo/xm"
|
||||
"github.com/unlock-music/cli/internal/logging"
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
_ "unlock-music.dev/cli/algo/kgm"
|
||||
_ "unlock-music.dev/cli/algo/kwm"
|
||||
_ "unlock-music.dev/cli/algo/ncm"
|
||||
_ "unlock-music.dev/cli/algo/qmc"
|
||||
_ "unlock-music.dev/cli/algo/tm"
|
||||
_ "unlock-music.dev/cli/algo/xm"
|
||||
"unlock-music.dev/cli/internal/logging"
|
||||
)
|
||||
|
||||
var AppVersion = "v0.0.6"
|
||||
|
||||
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger
|
||||
|
||||
func main() {
|
||||
module, ok := debug.ReadBuildInfo()
|
||||
if ok && module.Main.Version != "(devel)" {
|
||||
AppVersion = module.Main.Version
|
||||
}
|
||||
app := cli.App{
|
||||
Name: "Unlock Music CLI",
|
||||
HelpName: "um",
|
||||
Usage: "Unlock your encrypted music file https://github.com/unlock-music/cli",
|
||||
Usage: "Unlock your encrypted music file https://git.unlock-music.dev/um/cli",
|
||||
Version: fmt.Sprintf("%s (%s,%s/%s)", AppVersion, runtime.Version(), runtime.GOOS, runtime.GOARCH),
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{Name: "input", Aliases: []string{"i"}, Usage: "path to input file or dir", Required: false},
|
||||
@ -39,17 +49,17 @@ func main() {
|
||||
},
|
||||
|
||||
Action: appMain,
|
||||
Copyright: "Copyright (c) 2020 - 2021 Unlock Music https://github.com/unlock-music/cli/blob/master/LICENSE",
|
||||
Copyright: fmt.Sprintf("Copyright (c) 2020 - %d Unlock Music https://git.unlock-music.dev/um/cli/src/branch/master/LICENSE", time.Now().Year()),
|
||||
HideHelpCommand: true,
|
||||
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input",
|
||||
}
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
logging.Log().Fatal("run app failed", zap.Error(err))
|
||||
logger.Fatal("run app failed", zap.Error(err))
|
||||
}
|
||||
}
|
||||
func printSupportedExtensions() {
|
||||
exts := []string{}
|
||||
var exts []string
|
||||
for ext := range common.DecoderRegistry {
|
||||
exts = append(exts, ext)
|
||||
}
|
||||
@ -115,7 +125,7 @@ func appMain(c *cli.Context) (err error) {
|
||||
} else {
|
||||
allDec := common.GetDecoder(inputStat.Name(), skipNoop)
|
||||
if len(allDec) == 0 {
|
||||
logging.Log().Fatal("skipping while no suitable decoder")
|
||||
logger.Fatal("skipping while no suitable decoder")
|
||||
}
|
||||
return tryDecFile(input, output, allDec, removeSource)
|
||||
}
|
||||
@ -132,56 +142,64 @@ func dealDirectory(inputDir string, outputDir string, skipNoop bool, removeSourc
|
||||
}
|
||||
allDec := common.GetDecoder(item.Name(), skipNoop)
|
||||
if len(allDec) == 0 {
|
||||
logging.Log().Info("skipping while no suitable decoder", zap.String("file", item.Name()))
|
||||
logger.Info("skipping while no suitable decoder", zap.String("file", item.Name()))
|
||||
continue
|
||||
}
|
||||
|
||||
err := tryDecFile(filepath.Join(inputDir, item.Name()), outputDir, allDec, removeSource)
|
||||
if err != nil {
|
||||
logging.Log().Error("conversion failed", zap.String("source", item.Name()))
|
||||
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFunc, removeSource bool) error {
|
||||
file, err := os.ReadFile(inputFile)
|
||||
file, err := os.Open(inputFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var dec common.Decoder
|
||||
for _, decFunc := range allDec {
|
||||
dec = decFunc(file)
|
||||
if err := dec.Validate(); err == nil {
|
||||
break
|
||||
} else {
|
||||
logger.Warn("try decode failed", zap.Error(err))
|
||||
dec = nil
|
||||
}
|
||||
logging.Log().Warn("try decode failed", zap.Error(err))
|
||||
dec = nil
|
||||
}
|
||||
if dec == nil {
|
||||
return errors.New("no any decoder can resolve the file")
|
||||
}
|
||||
if err := dec.Decode(); err != nil {
|
||||
return errors.New("failed while decoding: " + err.Error())
|
||||
|
||||
header := bytes.NewBuffer(nil)
|
||||
_, err = io.CopyN(header, dec, 16)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read header failed: %w", err)
|
||||
}
|
||||
|
||||
outData := dec.GetAudioData()
|
||||
outExt := dec.GetAudioExt()
|
||||
if outExt == "" {
|
||||
if ext, ok := common.SniffAll(outData); ok {
|
||||
outExt = ext
|
||||
} else {
|
||||
outExt = ".mp3"
|
||||
}
|
||||
outExt := ".mp3"
|
||||
if ext, ok := common.SniffAll(header.Bytes()); ok {
|
||||
outExt = ext
|
||||
}
|
||||
filenameOnly := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
|
||||
|
||||
outPath := filepath.Join(outputDir, filenameOnly+outExt)
|
||||
err = os.WriteFile(outPath, outData, 0644)
|
||||
outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
if _, err := io.Copy(outFile, header); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(outFile, dec); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// if source file need to be removed
|
||||
if removeSource {
|
||||
@ -189,9 +207,9 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logging.Log().Info("successfully converted, and source file is removed", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
logger.Info("successfully converted, and source file is removed", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
} else {
|
||||
logging.Log().Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
logger.Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
16
go.mod
16
go.mod
@ -1,17 +1,17 @@
|
||||
module github.com/unlock-music/cli
|
||||
module unlock-music.dev/cli
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/ulikunitz/xz v0.5.10
|
||||
github.com/urfave/cli/v2 v2.3.0
|
||||
go.uber.org/zap v1.19.1
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
|
||||
github.com/urfave/cli/v2 v2.23.5
|
||||
go.uber.org/zap v1.23.0
|
||||
golang.org/x/crypto v0.3.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.7.0 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||
go.uber.org/atomic v1.10.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
)
|
||||
|
63
go.sum
63
go.sum
@ -1,9 +1,8 @@
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@ -14,60 +13,76 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
|
||||
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
||||
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
|
||||
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/urfave/cli/v2 v2.23.5 h1:xbrU7tAYviSpqeR3X4nEFWUdB/uDZ6DE+HxmRU7Xtyw=
|
||||
github.com/urfave/cli/v2 v2.23.5/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
|
||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723 h1:sHOAIxRGBp443oHZIPB+HsUGaksVCXVQENPxwTfQdH4=
|
||||
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
|
||||
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
||||
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
|
||||
go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
|
||||
go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI=
|
||||
go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI=
|
||||
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
|
||||
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
|
||||
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
|
||||
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
|
||||
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
|
||||
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -1,41 +0,0 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
// newDefaultProductionLog configures a custom log that is
|
||||
// intended for use by default if no other log is specified
|
||||
// in a config. It writes to stderr, uses the console encoder,
|
||||
// and enables INFO-level logs and higher.
|
||||
func newDefaultProductionLog() *zap.Logger {
|
||||
encCfg := zap.NewProductionEncoderConfig()
|
||||
// if interactive terminal, make output more human-readable by default
|
||||
encCfg.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
|
||||
encoder.AppendString(ts.Format("2006/01/02 15:04:05.000"))
|
||||
}
|
||||
encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
enc := zapcore.NewConsoleEncoder(encCfg)
|
||||
core := zapcore.NewCore(enc, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(zap.DebugLevel))
|
||||
return zap.New(core)
|
||||
|
||||
}
|
||||
|
||||
// Log returns the current default logger.
|
||||
func Log() *zap.Logger {
|
||||
defaultLoggerMu.RLock()
|
||||
defer defaultLoggerMu.RUnlock()
|
||||
return defaultLogger
|
||||
}
|
||||
|
||||
var (
|
||||
defaultLogger = newDefaultProductionLog()
|
||||
defaultLoggerMu sync.RWMutex
|
||||
)
|
13
internal/logging/zap.go
Normal file
13
internal/logging/zap.go
Normal file
@ -0,0 +1,13 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
func NewZapLogger() (*zap.Logger, error) {
|
||||
zapCfg := zap.NewDevelopmentConfig()
|
||||
zapCfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
zapCfg.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006/01/02 15:04:05.000")
|
||||
return zapCfg.Build()
|
||||
}
|
@ -8,7 +8,7 @@ func PKCS7UnPadding(encrypt []byte) []byte {
|
||||
return encrypt[:(length - unPadding)]
|
||||
}
|
||||
|
||||
func DecryptAes128Ecb(data, key []byte) []byte {
|
||||
func DecryptAES128ECB(data, key []byte) []byte {
|
||||
cipher, _ := aes.NewCipher(key)
|
||||
decrypted := make([]byte, len(data))
|
||||
size := 16
|
||||
|
Loading…
Reference in New Issue
Block a user