Compare commits
10 Commits
21bd246d6c
...
6f1cdc5b4f
Author | SHA1 | Date | |
---|---|---|---|
6f1cdc5b4f | |||
8fd6763b29 | |||
2880c63bf7 | |||
67ff0c44cd | |||
4365628bff | |||
b275b552ed | |||
4ecaef1ff9 | |||
e2fc56ddb2 | |||
a2c55721cc | |||
e6ba9f0bfa |
@ -1,12 +1,17 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
type Decoder interface {
|
type Decoder interface {
|
||||||
Validate() error
|
Validate() error
|
||||||
Decode() error
|
io.Reader
|
||||||
GetCoverImage() []byte
|
}
|
||||||
GetAudioData() []byte
|
|
||||||
GetAudioExt() string
|
type CoverImageGetter interface {
|
||||||
GetMeta() Meta
|
GetCoverImage(ctx context.Context) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Meta interface {
|
type Meta interface {
|
||||||
@ -14,3 +19,7 @@ type Meta interface {
|
|||||||
GetTitle() string
|
GetTitle() string
|
||||||
GetAlbum() string
|
GetAlbum() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type StreamDecoder interface {
|
||||||
|
Decrypt(buf []byte, offset int)
|
||||||
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NewDecoderFunc func([]byte) Decoder
|
type NewDecoderFunc func(rd io.ReadSeeker) Decoder
|
||||||
|
|
||||||
type decoderItem struct {
|
type decoderItem struct {
|
||||||
noop bool
|
noop bool
|
||||||
|
@ -2,46 +2,39 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"fmt"
|
||||||
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RawDecoder struct {
|
type RawDecoder struct {
|
||||||
file []byte
|
rd io.ReadSeeker
|
||||||
|
|
||||||
audioExt string
|
audioExt string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRawDecoder(file []byte) Decoder {
|
func NewRawDecoder(rd io.ReadSeeker) Decoder {
|
||||||
return &RawDecoder{file: file}
|
return &RawDecoder{rd: rd}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *RawDecoder) Validate() error {
|
func (d *RawDecoder) Validate() error {
|
||||||
for ext, sniffer := range snifferRegistry {
|
header := make([]byte, 16)
|
||||||
if sniffer(d.file) {
|
if _, err := io.ReadFull(d.rd, header); err != nil {
|
||||||
d.audioExt = strings.ToLower(ext)
|
return fmt.Errorf("read file header failed: %v", err)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
if _, err := d.rd.Seek(0, io.SeekStart); err != nil {
|
||||||
return errors.New("audio doesn't recognized")
|
return fmt.Errorf("seek file failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d RawDecoder) GetCoverImage() []byte {
|
func (d *RawDecoder) Read(p []byte) (n int, err error) {
|
||||||
return nil
|
return d.rd.Read(p)
|
||||||
}
|
|
||||||
|
|
||||||
func (d RawDecoder) GetAudioData() []byte {
|
|
||||||
return d.file
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d RawDecoder) GetAudioExt() string {
|
|
||||||
return d.audioExt
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d RawDecoder) GetMeta() Meta {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -1,82 +1,57 @@
|
|||||||
package kgm
|
package kgm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"fmt"
|
||||||
"encoding/binary"
|
"io"
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/unlock-music/cli/algo/common"
|
"github.com/unlock-music/cli/algo/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
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")
|
|
||||||
)
|
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
file []byte
|
header header
|
||||||
key []byte
|
cipher common.StreamDecoder
|
||||||
isVpr bool
|
|
||||||
audio []byte
|
rd io.ReadSeeker
|
||||||
|
offset int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecoder(file []byte) common.Decoder {
|
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
||||||
return &Decoder{
|
return &Decoder{rd: rd}
|
||||||
file: file,
|
}
|
||||||
}
|
|
||||||
|
func (d *Decoder) Validate() (err error) {
|
||||||
|
if err := d.header.FromFile(d.rd); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// TODO; validate crypto version
|
||||||
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) GetCoverImage() []byte {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) GetAudioData() []byte {
|
func (d *Decoder) Read(buf []byte) (int, error) {
|
||||||
return d.audio
|
n, err := d.rd.Read(buf)
|
||||||
|
if n > 0 {
|
||||||
|
d.cipher.Decrypt(buf[:n], d.offset)
|
||||||
|
d.offset += n
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
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])
|
|
||||||
d.audio = d.file[headerLen:]
|
|
||||||
|
|
||||||
for i := 0; i < len(d.audio); i++ {
|
|
||||||
med8 := d.audio[i] ^ d.key[i%17] ^ maskV2PreDef[i%(16*17)] ^ getKgmMaskV2(i>>4)
|
|
||||||
d.audio[i] = med8 ^ (med8&0xf)<<4
|
|
||||||
}
|
|
||||||
if d.isVpr {
|
|
||||||
for i := 0; i < len(d.audio); i++ {
|
|
||||||
d.audio[i] ^= maskDiffVpr[i%17]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func init() {
|
func init() {
|
||||||
// Kugou
|
// Kugou
|
||||||
common.RegisterDecoder("kgm", false, NewDecoder)
|
common.RegisterDecoder("kgm", false, NewDecoder)
|
||||||
|
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"
|
||||||
|
|
||||||
|
"github.com/unlock-music/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,81 +0,0 @@
|
|||||||
package kgm
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "embed"
|
|
||||||
)
|
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
|
|
||||||
var kgmMaskTable1 = []byte{
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x01, 0x21, 0x01, 0x61, 0x01, 0x21, 0x01, 0xe1, 0x01, 0x21, 0x01, 0x61, 0x01, 0x21, 0x01,
|
|
||||||
0xd2, 0x23, 0x02, 0x02, 0x42, 0x42, 0x02, 0x02, 0xc2, 0xc2, 0x02, 0x02, 0x42, 0x42, 0x02, 0x02,
|
|
||||||
0xd3, 0xd3, 0x02, 0x03, 0x63, 0x43, 0x63, 0x03, 0xe3, 0xc3, 0xe3, 0x03, 0x63, 0x43, 0x63, 0x03,
|
|
||||||
0x94, 0xb4, 0x94, 0x65, 0x04, 0x04, 0x04, 0x04, 0x84, 0x84, 0x84, 0x84, 0x04, 0x04, 0x04, 0x04,
|
|
||||||
0x95, 0x95, 0x95, 0x95, 0x04, 0x05, 0x25, 0x05, 0xe5, 0x85, 0xa5, 0x85, 0xe5, 0x05, 0x25, 0x05,
|
|
||||||
0xd6, 0xb6, 0x96, 0xb6, 0xd6, 0x27, 0x06, 0x06, 0xc6, 0xc6, 0x86, 0x86, 0xc6, 0xc6, 0x06, 0x06,
|
|
||||||
0xd7, 0xd7, 0x97, 0x97, 0xd7, 0xd7, 0x06, 0x07, 0xe7, 0xc7, 0xe7, 0x87, 0xe7, 0xc7, 0xe7, 0x07,
|
|
||||||
0x18, 0x38, 0x18, 0x78, 0x18, 0x38, 0x18, 0xe9, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
|
||||||
0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x08, 0x09, 0x29, 0x09, 0x69, 0x09, 0x29, 0x09,
|
|
||||||
0xda, 0x3a, 0x1a, 0x3a, 0x5a, 0x3a, 0x1a, 0x3a, 0xda, 0x2b, 0x0a, 0x0a, 0x4a, 0x4a, 0x0a, 0x0a,
|
|
||||||
0xdb, 0xdb, 0x1b, 0x1b, 0x5b, 0x5b, 0x1b, 0x1b, 0xdb, 0xdb, 0x0a, 0x0b, 0x6b, 0x4b, 0x6b, 0x0b,
|
|
||||||
0x9c, 0xbc, 0x9c, 0x7c, 0x1c, 0x3c, 0x1c, 0x7c, 0x9c, 0xbc, 0x9c, 0x6d, 0x0c, 0x0c, 0x0c, 0x0c,
|
|
||||||
0x9d, 0x9d, 0x9d, 0x9d, 0x1d, 0x1d, 0x1d, 0x1d, 0x9d, 0x9d, 0x9d, 0x9d, 0x0c, 0x0d, 0x2d, 0x0d,
|
|
||||||
0xde, 0xbe, 0x9e, 0xbe, 0xde, 0x3e, 0x1e, 0x3e, 0xde, 0xbe, 0x9e, 0xbe, 0xde, 0x2f, 0x0e, 0x0e,
|
|
||||||
0xdf, 0xdf, 0x9f, 0x9f, 0xdf, 0xdf, 0x1f, 0x1f, 0xdf, 0xdf, 0x9f, 0x9f, 0xdf, 0xdf, 0x0e, 0x0f,
|
|
||||||
0x00, 0x20, 0x00, 0x60, 0x00, 0x20, 0x00, 0xe0, 0x00, 0x20, 0x00, 0x60, 0x00, 0x20, 0x00, 0xf1,
|
|
||||||
}
|
|
||||||
|
|
||||||
var kgmMaskTable2 = []byte{
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x01, 0x23, 0x01, 0x67, 0x01, 0x23, 0x01, 0xef, 0x01, 0x23, 0x01, 0x67, 0x01, 0x23, 0x01,
|
|
||||||
0xdf, 0x21, 0x02, 0x02, 0x46, 0x46, 0x02, 0x02, 0xce, 0xce, 0x02, 0x02, 0x46, 0x46, 0x02, 0x02,
|
|
||||||
0xde, 0xde, 0x02, 0x03, 0x65, 0x47, 0x65, 0x03, 0xed, 0xcf, 0xed, 0x03, 0x65, 0x47, 0x65, 0x03,
|
|
||||||
0x9d, 0xbf, 0x9d, 0x63, 0x04, 0x04, 0x04, 0x04, 0x8c, 0x8c, 0x8c, 0x8c, 0x04, 0x04, 0x04, 0x04,
|
|
||||||
0x9c, 0x9c, 0x9c, 0x9c, 0x04, 0x05, 0x27, 0x05, 0xeb, 0x8d, 0xaf, 0x8d, 0xeb, 0x05, 0x27, 0x05,
|
|
||||||
0xdb, 0xbd, 0x9f, 0xbd, 0xdb, 0x25, 0x06, 0x06, 0xca, 0xca, 0x8e, 0x8e, 0xca, 0xca, 0x06, 0x06,
|
|
||||||
0xda, 0xda, 0x9e, 0x9e, 0xda, 0xda, 0x06, 0x07, 0xe9, 0xcb, 0xe9, 0x8f, 0xe9, 0xcb, 0xe9, 0x07,
|
|
||||||
0x19, 0x3b, 0x19, 0x7f, 0x19, 0x3b, 0x19, 0xe7, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
|
||||||
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x08, 0x09, 0x2b, 0x09, 0x6f, 0x09, 0x2b, 0x09,
|
|
||||||
0xd7, 0x39, 0x1b, 0x39, 0x5f, 0x39, 0x1b, 0x39, 0xd7, 0x29, 0x0a, 0x0a, 0x4e, 0x4e, 0x0a, 0x0a,
|
|
||||||
0xd6, 0xd6, 0x1a, 0x1a, 0x5e, 0x5e, 0x1a, 0x1a, 0xd6, 0xd6, 0x0a, 0x0b, 0x6d, 0x4f, 0x6d, 0x0b,
|
|
||||||
0x95, 0xb7, 0x95, 0x7b, 0x1d, 0x3f, 0x1d, 0x7b, 0x95, 0xb7, 0x95, 0x6b, 0x0c, 0x0c, 0x0c, 0x0c,
|
|
||||||
0x94, 0x94, 0x94, 0x94, 0x1c, 0x1c, 0x1c, 0x1c, 0x94, 0x94, 0x94, 0x94, 0x0c, 0x0d, 0x2f, 0x0d,
|
|
||||||
0xd3, 0xb5, 0x97, 0xb5, 0xd3, 0x3d, 0x1f, 0x3d, 0xd3, 0xb5, 0x97, 0xb5, 0xd3, 0x2d, 0x0e, 0x0e,
|
|
||||||
0xd2, 0xd2, 0x96, 0x96, 0xd2, 0xd2, 0x1e, 0x1e, 0xd2, 0xd2, 0x96, 0x96, 0xd2, 0xd2, 0x0e, 0x0f,
|
|
||||||
0x00, 0x22, 0x00, 0x66, 0x00, 0x22, 0x00, 0xee, 0x00, 0x22, 0x00, 0x66, 0x00, 0x22, 0x00, 0xfe,
|
|
||||||
}
|
|
||||||
|
|
||||||
func getKgmMaskV2(offset int) byte {
|
|
||||||
ret := byte(0)
|
|
||||||
for offset >= 0x11 {
|
|
||||||
ret ^= kgmMaskTable1[offset%272]
|
|
||||||
offset = offset >> 4
|
|
||||||
ret ^= kgmMaskTable2[offset%272]
|
|
||||||
offset = offset >> 4
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
116
algo/kwm/kwm.go
116
algo/kwm/kwm.go
@ -2,109 +2,75 @@ package kwm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/unlock-music/cli/algo/common"
|
"fmt"
|
||||||
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
|
"github.com/unlock-music/cli/algo/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
const magicHeader = "yeelion-kuwo-tme"
|
||||||
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 keyPreDefined = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
|
const keyPreDefined = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
file []byte
|
cipher common.StreamDecoder
|
||||||
|
|
||||||
|
rd io.ReadSeeker
|
||||||
|
offset int
|
||||||
|
|
||||||
key []byte
|
|
||||||
outputExt string
|
outputExt string
|
||||||
bitrate int
|
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 {
|
func (d *Decoder) GetAudioExt() string {
|
||||||
return "." + d.outputExt
|
return "." + d.outputExt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) GetMeta() common.Meta {
|
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
||||||
return nil
|
return &Decoder{rd: rd}
|
||||||
}
|
|
||||||
|
|
||||||
func NewDecoder(data []byte) common.Decoder {
|
|
||||||
//todo: Notice the input data will be changed for now
|
|
||||||
return &Decoder{file: data}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Validate() error {
|
func (d *Decoder) Validate() error {
|
||||||
lenData := len(d.file)
|
header := make([]byte, 0x400) // kwm header is fixed to 1024 bytes
|
||||||
if lenData < 1024 {
|
_, err := io.ReadFull(d.rd, header)
|
||||||
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])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.bitrate = 0
|
return fmt.Errorf("kwm read header: %w", err)
|
||||||
}
|
|
||||||
d.outputExt = strings.ToLower(bitType[charPos:])
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Decode() error {
|
// check magic header, 0x00 - 0x0F
|
||||||
d.parseBitrateAndType()
|
if !bytes.Equal([]byte(magicHeader), header[:len(magicHeader)]) {
|
||||||
|
return errors.New("kwm magic header not matched")
|
||||||
d.mask = generateMask(d.file[0x18:0x20])
|
|
||||||
|
|
||||||
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]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseBitrateAndType(header []byte) (int, string) {
|
||||||
|
tmp := strings.TrimRight(string(header), "\x00")
|
||||||
|
sep := strings.IndexFunc(tmp, func(r rune) bool {
|
||||||
|
return !unicode.IsDigit(r)
|
||||||
|
})
|
||||||
|
|
||||||
|
bitrate, _ := strconv.Atoi(tmp[:sep]) // just ignore the error
|
||||||
|
outputExt := strings.ToLower(tmp[sep:])
|
||||||
|
return bitrate, outputExt
|
||||||
|
}
|
||||||
|
|
||||||
|
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 n, err
|
||||||
|
}
|
||||||
|
|
||||||
func padOrTruncate(raw string, length int) string {
|
func padOrTruncate(raw string, length int) string {
|
||||||
lenRaw := len(raw)
|
lenRaw := len(raw)
|
||||||
out := raw
|
out := raw
|
||||||
|
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]
|
||||||
|
}
|
||||||
|
}
|
270
algo/ncm/ncm.go
270
algo/ncm/ncm.go
@ -2,40 +2,44 @@ package ncm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/unlock-music/cli/algo/common"
|
"fmt"
|
||||||
"github.com/unlock-music/cli/internal/logging"
|
"io"
|
||||||
"github.com/unlock-music/cli/internal/utils"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/unlock-music/cli/algo/common"
|
||||||
|
"github.com/unlock-music/cli/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const magicHeader = "CTENFDAM"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
magicHeader = []byte{
|
|
||||||
0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D}
|
|
||||||
keyCore = []byte{
|
keyCore = []byte{
|
||||||
0x68, 0x7a, 0x48, 0x52, 0x41, 0x6d, 0x73, 0x6f,
|
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{
|
keyMeta = []byte{
|
||||||
0x23, 0x31, 0x34, 0x6C, 0x6A, 0x6B, 0x5F, 0x21,
|
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{
|
return &Decoder{
|
||||||
file: data,
|
rd: rd,
|
||||||
fileLen: uint32(len(data)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
file []byte
|
rd io.ReadSeeker
|
||||||
fileLen uint32
|
offset int
|
||||||
|
|
||||||
|
cipher common.StreamDecoder
|
||||||
|
|
||||||
key []byte
|
key []byte
|
||||||
box []byte
|
box []byte
|
||||||
@ -46,91 +50,128 @@ type Decoder struct {
|
|||||||
|
|
||||||
cover []byte
|
cover []byte
|
||||||
audio []byte
|
audio []byte
|
||||||
|
|
||||||
offsetKey uint32
|
|
||||||
offsetMeta uint32
|
|
||||||
offsetCover uint32
|
|
||||||
offsetAudio uint32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Validate() error {
|
func (d *Decoder) Validate() error {
|
||||||
if !bytes.Equal(magicHeader, d.file[:len(magicHeader)]) {
|
if err := d.validateMagicHeader(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
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) 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 errors.New("ncm magic header not match")
|
||||||
}
|
}
|
||||||
d.offsetKey = 8 + 2
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) readKeyData() error {
|
func (d *Decoder) readKeyData() ([]byte, error) {
|
||||||
if d.offsetKey == 0 || d.offsetKey+4 > d.fileLen {
|
bKeyLen := make([]byte, 4) //
|
||||||
return errors.New("invalid cover file offset")
|
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)
|
iKeyLen := binary.LittleEndian.Uint32(bKeyLen)
|
||||||
d.offsetMeta = d.offsetKey + 4 + iKeyLen
|
|
||||||
|
|
||||||
bKeyRaw := make([]byte, 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++ {
|
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 utils.PKCS7UnPadding(utils.DecryptAES128ECB(bKeyRaw, keyCore))[17:], nil
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) readMetaData() error {
|
func (d *Decoder) readMetaData() error {
|
||||||
if d.offsetMeta == 0 || d.offsetMeta+4 > d.fileLen {
|
bMetaLen := make([]byte, 4) //
|
||||||
return errors.New("invalid meta file offset")
|
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)
|
iMetaLen := binary.LittleEndian.Uint32(bMetaLen)
|
||||||
d.offsetCover = d.offsetMeta + 4 + iMetaLen
|
|
||||||
if iMetaLen == 0 {
|
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):"
|
bMetaRaw := make([]byte, iMetaLen)
|
||||||
bKeyRaw := make([]byte, iMetaLen-22)
|
if _, err := io.ReadFull(d.rd, bMetaRaw); err != nil {
|
||||||
for i := uint32(0); i < iMetaLen-22; i++ {
|
return fmt.Errorf("ncm read meta data: %w", err)
|
||||||
bKeyRaw[i] = d.file[d.offsetMeta+4+22+i] ^ 0x63
|
}
|
||||||
|
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 {
|
if err != nil {
|
||||||
return errors.New("decode ncm meta failed: " + err.Error())
|
return errors.New("decode ncm meta failed: " + err.Error())
|
||||||
}
|
}
|
||||||
metaRaw := utils.PKCS7UnPadding(utils.DecryptAes128Ecb(cipherText, keyMeta))
|
metaRaw := utils.PKCS7UnPadding(utils.DecryptAES128ECB(cipherText, keyMeta))
|
||||||
sepIdx := bytes.IndexRune(metaRaw, ':')
|
sep := bytes.IndexByte(metaRaw, ':')
|
||||||
if sepIdx == -1 {
|
if sep == -1 {
|
||||||
return errors.New("invalid ncm meta file")
|
return errors.New("invalid ncm meta file")
|
||||||
}
|
}
|
||||||
|
|
||||||
d.metaType = string(metaRaw[:sepIdx])
|
d.metaType = string(metaRaw[:sep])
|
||||||
d.metaRaw = metaRaw[sepIdx+1:]
|
d.metaRaw = metaRaw[sep+1:]
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) buildKeyBox() {
|
func (d *Decoder) readCoverData() error {
|
||||||
box := make([]byte, 256)
|
bCoverCRC := make([]byte, 4)
|
||||||
for i := 0; i < 256; i++ {
|
if _, err := io.ReadFull(d.rd, bCoverCRC); err != nil {
|
||||||
box[i] = byte(i)
|
return fmt.Errorf("ncm read cover crc: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
keyLen := len(d.key)
|
bCoverLen := make([]byte, 4) //
|
||||||
var j byte
|
if _, err := io.ReadFull(d.rd, bCoverLen); err != nil {
|
||||||
for i := 0; i < 256; i++ {
|
return fmt.Errorf("ncm read cover length: %w", err)
|
||||||
j = box[i] + j + d.key[i%keyLen]
|
|
||||||
box[i], box[j] = box[j], box[i]
|
|
||||||
}
|
}
|
||||||
|
iCoverLen := binary.LittleEndian.Uint32(bCoverLen)
|
||||||
|
|
||||||
d.box = make([]byte, 256)
|
coverBuf := make([]byte, iCoverLen)
|
||||||
var _i byte
|
if _, err := io.ReadFull(d.rd, coverBuf); err != nil {
|
||||||
for i := 0; i < 256; i++ {
|
return fmt.Errorf("ncm read cover data: %w", err)
|
||||||
_i = byte(i + 1)
|
|
||||||
si := box[_i]
|
|
||||||
sj := box[_i+si]
|
|
||||||
d.box[i] = box[si+sj]
|
|
||||||
}
|
}
|
||||||
|
d.cover = coverBuf
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) parseMeta() error {
|
func (d *Decoder) parseMeta() error {
|
||||||
@ -146,59 +187,16 @@ func (d *Decoder) parseMeta() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) readCoverData() error {
|
func (d *Decoder) Read(buf []byte) (int, error) {
|
||||||
if d.offsetCover == 0 || d.offsetCover+13 > d.fileLen {
|
n, err := d.rd.Read(buf)
|
||||||
return errors.New("invalid cover file offset")
|
if n > 0 {
|
||||||
|
d.cipher.Decrypt(buf[:n], d.offset)
|
||||||
|
d.offset += n
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
coverLenStart := d.offsetCover + 5 + 4
|
func (d *Decoder) GetAudioExt() string {
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
if d.meta != nil {
|
if d.meta != nil {
|
||||||
if format := d.meta.GetFormat(); format != "" {
|
if format := d.meta.GetFormat(); format != "" {
|
||||||
return "." + d.meta.GetFormat()
|
return "." + d.meta.GetFormat()
|
||||||
@ -207,40 +205,34 @@ func (d Decoder) GetAudioExt() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Decoder) GetAudioData() []byte {
|
func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
|
||||||
return d.audio
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d Decoder) GetCoverImage() []byte {
|
|
||||||
if d.cover != nil {
|
if d.cover != nil {
|
||||||
return d.cover
|
return d.cover, nil
|
||||||
}
|
}
|
||||||
{
|
|
||||||
imgURL := d.meta.GetAlbumImageURL()
|
imgURL := d.meta.GetAlbumImageURL()
|
||||||
if d.meta != nil && !strings.HasPrefix(imgURL, "http") {
|
if d.meta != nil && !strings.HasPrefix(imgURL, "http") {
|
||||||
return nil
|
return nil, nil // no cover image
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Decoder) GetMeta() common.Meta {
|
// 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 {
|
||||||
return d.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
|
size int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMapCipher(key []byte) (*mapCipher, error) {
|
func newMapCipher(key []byte) (*mapCipher, error) {
|
||||||
if len(key) == 0 {
|
if len(key) == 0 {
|
||||||
return nil, errors.New("qmc/cipher_map: invalid key size")
|
return nil, errors.New("qmc/cipher_map: invalid key size")
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ func Test_mapCipher_Decrypt(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("load testing data failed: %s", err)
|
t.Fatalf("load testing data failed: %s", err)
|
||||||
}
|
}
|
||||||
c, err := NewMapCipher(key)
|
c, err := newMapCipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("init mapCipher failed: %s", err)
|
t.Errorf("init mapCipher failed: %s", err)
|
||||||
return
|
return
|
||||||
|
@ -12,9 +12,9 @@ type rc4Cipher struct {
|
|||||||
n int
|
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.
|
// 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)
|
n := len(key)
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return nil, errors.New("qmc/cipher_rc4: invalid key size")
|
return nil, errors.New("qmc/cipher_rc4: invalid key size")
|
||||||
|
@ -37,7 +37,7 @@ func Test_rc4Cipher_Decrypt(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("load testing data failed: %s", err)
|
t.Fatalf("load testing data failed: %s", err)
|
||||||
}
|
}
|
||||||
c, err := NewRC4Cipher(key)
|
c, err := newRC4Cipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("init rc4Cipher failed: %s", err)
|
t.Errorf("init rc4Cipher failed: %s", err)
|
||||||
return
|
return
|
||||||
@ -55,7 +55,7 @@ func BenchmarkRc4Cipher_Decrypt(b *testing.B) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatalf("load testing data failed: %s", err)
|
b.Fatalf("load testing data failed: %s", err)
|
||||||
}
|
}
|
||||||
c, err := NewRC4Cipher(key)
|
c, err := newRC4Cipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Errorf("init rc4Cipher failed: %s", err)
|
b.Errorf("init rc4Cipher failed: %s", err)
|
||||||
return
|
return
|
||||||
@ -72,7 +72,7 @@ func Test_rc4Cipher_encFirstSegment(t *testing.T) {
|
|||||||
t.Fatalf("load testing data failed: %s", err)
|
t.Fatalf("load testing data failed: %s", err)
|
||||||
}
|
}
|
||||||
t.Run("first-block(0~128)", func(t *testing.T) {
|
t.Run("first-block(0~128)", func(t *testing.T) {
|
||||||
c, err := NewRC4Cipher(key)
|
c, err := newRC4Cipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("init rc4Cipher failed: %s", err)
|
t.Errorf("init rc4Cipher failed: %s", err)
|
||||||
return
|
return
|
||||||
@ -91,7 +91,7 @@ func Test_rc4Cipher_encASegment(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Run("align-block(128~5120)", func(t *testing.T) {
|
t.Run("align-block(128~5120)", func(t *testing.T) {
|
||||||
c, err := NewRC4Cipher(key)
|
c, err := newRC4Cipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("init rc4Cipher failed: %s", err)
|
t.Errorf("init rc4Cipher failed: %s", err)
|
||||||
return
|
return
|
||||||
@ -102,7 +102,7 @@ func Test_rc4Cipher_encASegment(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run("simple-block(5120~10240)", func(t *testing.T) {
|
t.Run("simple-block(5120~10240)", func(t *testing.T) {
|
||||||
c, err := NewRC4Cipher(key)
|
c, err := newRC4Cipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("init rc4Cipher failed: %s", err)
|
t.Errorf("init rc4Cipher failed: %s", err)
|
||||||
return
|
return
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package qmc
|
package qmc
|
||||||
|
|
||||||
func NewStaticCipher() *staticCipher {
|
func newStaticCipher() *staticCipher {
|
||||||
return &defaultStaticCipher
|
return &defaultStaticCipher
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ func simpleMakeKey(salt byte, length int) []byte {
|
|||||||
|
|
||||||
const rawKeyPrefixV2 = "QQMusic EncV2,Key:"
|
const rawKeyPrefixV2 = "QQMusic EncV2,Key:"
|
||||||
|
|
||||||
func DecryptKey(rawKey []byte) ([]byte, error) {
|
func deriveKey(rawKey []byte) ([]byte, error) {
|
||||||
rawKeyDec := make([]byte, base64.StdEncoding.DecodedLen(len(rawKey)))
|
rawKeyDec := make([]byte, base64.StdEncoding.DecodedLen(len(rawKey)))
|
||||||
n, err := base64.StdEncoding.Decode(rawKeyDec, rawKey)
|
n, err := base64.StdEncoding.Decode(rawKeyDec, rawKey)
|
||||||
if err != nil {
|
if err != nil {
|
@ -43,13 +43,13 @@ func TestDecryptKey(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("load test data failed: %s", err)
|
t.Fatalf("load test data failed: %s", err)
|
||||||
}
|
}
|
||||||
got, err := DecryptKey(raw)
|
got, err := deriveKey(raw)
|
||||||
if (err != nil) != tt.wantErr {
|
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
|
return
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(got, want) {
|
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]))
|
string(got[:32]), string(want[:32]))
|
||||||
}
|
}
|
||||||
})
|
})
|
191
algo/qmc/qmc.go
191
algo/qmc/qmc.go
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -12,14 +13,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
r io.ReadSeeker
|
raw io.ReadSeeker
|
||||||
fileExt string
|
audio io.Reader
|
||||||
|
|
||||||
audioLen int
|
|
||||||
decodedKey []byte
|
|
||||||
cipher streamCipher
|
|
||||||
offset int
|
offset int
|
||||||
|
audioLen int
|
||||||
|
|
||||||
|
cipher common.StreamDecoder
|
||||||
|
|
||||||
|
decodedKey []byte
|
||||||
rawMetaExtra1 int
|
rawMetaExtra1 int
|
||||||
rawMetaExtra2 int
|
rawMetaExtra2 int
|
||||||
}
|
}
|
||||||
@ -27,80 +28,79 @@ type Decoder struct {
|
|||||||
// Read implements io.Reader, offer the decrypted audio data.
|
// Read implements io.Reader, offer the decrypted audio data.
|
||||||
// Validate should call before Read to check if the file is valid.
|
// Validate should call before Read to check if the file is valid.
|
||||||
func (d *Decoder) Read(p []byte) (int, error) {
|
func (d *Decoder) Read(p []byte) (int, error) {
|
||||||
n := len(p)
|
n, err := d.audio.Read(p)
|
||||||
if d.audioLen-d.offset <= 0 {
|
if n > 0 {
|
||||||
return 0, io.EOF
|
d.cipher.Decrypt(p[:n], d.offset)
|
||||||
} else if d.audioLen-d.offset < n {
|
d.offset += n
|
||||||
n = d.audioLen - d.offset
|
|
||||||
}
|
}
|
||||||
m, err := d.r.Read(p[:n])
|
return n, err
|
||||||
if m == 0 {
|
|
||||||
return 0, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d.cipher.Decrypt(p[:m], d.offset)
|
func NewDecoder(r io.ReadSeeker) common.Decoder {
|
||||||
d.offset += m
|
return &Decoder{raw: r}
|
||||||
return m, 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 (d *Decoder) Validate() error {
|
func (d *Decoder) Validate() error {
|
||||||
buf := make([]byte, 16)
|
// search & derive key
|
||||||
if _, err := io.ReadFull(d.r, buf); err != nil {
|
err := d.searchKey()
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err := d.r.Seek(0, io.SeekStart)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
d.cipher.Decrypt(buf, 0)
|
// check cipher type and init decode cipher
|
||||||
fileExt, ok := common.SniffAll(buf)
|
if len(d.decodedKey) > 300 {
|
||||||
if !ok {
|
d.cipher, err = newRC4Cipher(d.decodedKey)
|
||||||
return errors.New("detect file type failed")
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
d.fileExt = fileExt
|
} else if len(d.decodedKey) != 0 {
|
||||||
|
d.cipher, err = newMapCipher(d.decodedKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
d.cipher = newStaticCipher()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) GetFileExt() string {
|
func (d *Decoder) validateDecode() error {
|
||||||
return d.fileExt
|
_, 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 {
|
func (d *Decoder) searchKey() error {
|
||||||
fileSizeM4, err := d.r.Seek(-4, io.SeekEnd)
|
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
|
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -110,7 +110,7 @@ func (d *Decoder) searchKey() error {
|
|||||||
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
|
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
|
||||||
} else {
|
} else {
|
||||||
size := binary.LittleEndian.Uint32(buf)
|
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))
|
return d.readRawKey(int64(size))
|
||||||
}
|
}
|
||||||
// try to use default static cipher
|
// try to use default static cipher
|
||||||
@ -120,13 +120,13 @@ func (d *Decoder) searchKey() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
d.audioLen = int(audioLen)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -134,7 +134,7 @@ func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
|||||||
// clean suffix NULs
|
// clean suffix NULs
|
||||||
rawKeyData = bytes.TrimRight(rawKeyData, "\x00")
|
rawKeyData = bytes.TrimRight(rawKeyData, "\x00")
|
||||||
|
|
||||||
d.decodedKey, err = DecryptKey(rawKeyData)
|
d.decodedKey, err = deriveKey(rawKeyData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -144,22 +144,22 @@ func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
|||||||
|
|
||||||
func (d *Decoder) readRawMetaQTag() error {
|
func (d *Decoder) readRawMetaQTag() error {
|
||||||
// get raw meta data len
|
// 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
|
return err
|
||||||
}
|
}
|
||||||
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
|
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
rawMetaLen := int64(binary.BigEndian.Uint32(buf))
|
rawMetaLen := int64(binary.BigEndian.Uint32(buf))
|
||||||
|
|
||||||
// read raw meta data
|
// 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
d.audioLen = int(audioLen)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -169,7 +169,7 @@ func (d *Decoder) readRawMetaQTag() error {
|
|||||||
return errors.New("invalid raw meta data")
|
return errors.New("invalid raw meta data")
|
||||||
}
|
}
|
||||||
|
|
||||||
d.decodedKey, err = DecryptKey([]byte(items[0]))
|
d.decodedKey, err = deriveKey([]byte(items[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -208,57 +208,6 @@ func init() {
|
|||||||
"mflac", "mflac0", //QQ Music New Flac
|
"mflac", "mflac0", //QQ Music New Flac
|
||||||
}
|
}
|
||||||
for _, ext := range supportedExts {
|
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,6 +3,9 @@ package tm
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/unlock-music/cli/algo/common"
|
"github.com/unlock-music/cli/algo/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -10,70 +13,44 @@ var replaceHeader = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70}
|
|||||||
var magicHeader = []byte{0x51, 0x51, 0x4D, 0x55} //0x15, 0x1D, 0x1A, 0x21
|
var magicHeader = []byte{0x51, 0x51, 0x4D, 0x55} //0x15, 0x1D, 0x1A, 0x21
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
file []byte
|
raw io.ReadSeeker
|
||||||
audio []byte
|
offset int
|
||||||
headerMatch bool
|
audio io.Reader
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Validate() error {
|
func (d *Decoder) Validate() error {
|
||||||
if len(d.file) < 8 {
|
header := make([]byte, 8)
|
||||||
return errors.New("invalid file size")
|
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)
|
||||||
d.headerMatch = true
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Decode() error {
|
if _, ok := common.SniffAll(header); ok { // not encrypted
|
||||||
d.audio = d.file
|
d.audio = io.MultiReader(bytes.NewReader(header), d.raw)
|
||||||
if d.headerMatch {
|
|
||||||
for i := 0; i < 8; i++ {
|
|
||||||
d.audio[i] = replaceHeader[i]
|
|
||||||
}
|
|
||||||
d.audioExt = "m4a"
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//goland:noinspection GoUnusedExportedFunction
|
return errors.New("tm: valid magic header")
|
||||||
func NewDecoder(data []byte) common.Decoder {
|
|
||||||
return &Decoder{file: data}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
|
func (d *Decoder) Read(buf []byte) (int, error) {
|
||||||
return func(file []byte) common.Decoder {
|
return d.audio.Read(buf)
|
||||||
return &Decoder{file: file, audioExt: ext}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewTmDecoder(rd io.ReadSeeker) common.Decoder {
|
||||||
|
return &Decoder{raw: rd}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// QQ Music IOS M4a
|
// QQ Music IOS M4a (replace header)
|
||||||
common.RegisterDecoder("tm2", false, DecoderFuncWithExt("m4a"))
|
common.RegisterDecoder("tm2", false, NewTmDecoder)
|
||||||
common.RegisterDecoder("tm6", false, DecoderFuncWithExt("m4a"))
|
common.RegisterDecoder("tm6", false, NewTmDecoder)
|
||||||
// QQ Music IOS Mp3
|
|
||||||
common.RegisterDecoder("tm0", false, common.NewRawDecoder)
|
|
||||||
common.RegisterDecoder("tm3", false, common.NewRawDecoder)
|
|
||||||
|
|
||||||
|
// QQ Music IOS Mp3 (not encrypted)
|
||||||
|
common.RegisterDecoder("tm0", false, NewTmDecoder)
|
||||||
|
common.RegisterDecoder("tm3", false, NewTmDecoder)
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,10 @@ package xm
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/unlock-music/cli/algo/common"
|
"github.com/unlock-music/cli/algo/common"
|
||||||
"github.com/unlock-music/cli/internal/logging"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -17,26 +18,19 @@ var (
|
|||||||
" MP3": "mp3",
|
" MP3": "mp3",
|
||||||
" A4M": "m4a",
|
" A4M": "m4a",
|
||||||
}
|
}
|
||||||
ErrFileSize = errors.New("xm invalid file size")
|
|
||||||
ErrMagicHeader = errors.New("xm magic header not matched")
|
ErrMagicHeader = errors.New("xm magic header not matched")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
file []byte
|
rd io.ReadSeeker
|
||||||
headerLen uint32
|
offset int
|
||||||
|
|
||||||
|
cipher common.StreamDecoder
|
||||||
outputExt string
|
outputExt string
|
||||||
mask byte
|
mask byte
|
||||||
audio []byte
|
audio []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) GetCoverImage() []byte {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetAudioData() []byte {
|
|
||||||
return d.audio
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetAudioExt() string {
|
func (d *Decoder) GetAudioExt() string {
|
||||||
if d.outputExt != "" {
|
if d.outputExt != "" {
|
||||||
return "." + d.outputExt
|
return "." + d.outputExt
|
||||||
@ -45,62 +39,53 @@ func (d *Decoder) GetAudioExt() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) GetMeta() common.Meta {
|
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
||||||
return nil
|
return &Decoder{rd: rd}
|
||||||
}
|
|
||||||
|
|
||||||
func NewDecoder(data []byte) common.Decoder {
|
|
||||||
return &Decoder{file: data}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Validate() error {
|
func (d *Decoder) Validate() error {
|
||||||
lenData := len(d.file)
|
header := make([]byte, 16) // xm header is fixed to 16 bytes
|
||||||
if lenData < 16 {
|
|
||||||
return ErrFileSize
|
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
|
return ErrMagicHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 0x04 - 0x07: Audio File Type
|
||||||
var ok bool
|
var ok bool
|
||||||
d.outputExt, ok = typeMapping[string(d.file[4:8])]
|
d.outputExt, ok = typeMapping[string(header[4:8])]
|
||||||
if !ok {
|
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 {
|
// 0x0C - 0x0E, Encrypt Start At, LittleEndian Unit24
|
||||||
logging.Log().Warn("not a simple xm file", zap.Uint8("b[14]", d.file[14]))
|
encStartAt := uint32(header[12]) | uint32(header[13])<<8 | uint32(header[14])<<16
|
||||||
}
|
|
||||||
d.headerLen = uint32(d.file[12]) | uint32(d.file[13])<<8 | uint32(d.file[14])<<16 // LittleEndian Unit24
|
// 0x0F, XOR Mask
|
||||||
if d.headerLen+16 > uint32(lenData) {
|
d.cipher = newXmCipher(header[15], int(encStartAt))
|
||||||
return ErrFileSize
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Decode() error {
|
func (d *Decoder) Read(p []byte) (int, error) {
|
||||||
d.mask = d.file[15]
|
n, err := d.rd.Read(p)
|
||||||
d.audio = d.file[16:]
|
if n > 0 {
|
||||||
dataLen := uint32(len(d.audio))
|
d.cipher.Decrypt(p[:n], d.offset)
|
||||||
for i := d.headerLen; i < dataLen; i++ {
|
d.offset += n
|
||||||
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}
|
|
||||||
}
|
}
|
||||||
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Xiami Wav/M4a/Mp3/Flac
|
// Xiami Wav/M4a/Mp3/Flac
|
||||||
common.RegisterDecoder("xm", false, NewDecoder)
|
common.RegisterDecoder("xm", false, NewDecoder)
|
||||||
// Xiami Typed Format
|
// Xiami Typed Format
|
||||||
common.RegisterDecoder("wav", false, DecoderFuncWithExt("wav"))
|
common.RegisterDecoder("wav", false, NewDecoder)
|
||||||
common.RegisterDecoder("mp3", false, DecoderFuncWithExt("mp3"))
|
common.RegisterDecoder("mp3", false, NewDecoder)
|
||||||
common.RegisterDecoder("flac", false, DecoderFuncWithExt("flac"))
|
common.RegisterDecoder("flac", false, NewDecoder)
|
||||||
common.RegisterDecoder("m4a", false, DecoderFuncWithExt("m4a"))
|
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,13 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"runtime/debug"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@ -24,11 +28,17 @@ import (
|
|||||||
|
|
||||||
var AppVersion = "v0.0.6"
|
var AppVersion = "v0.0.6"
|
||||||
|
|
||||||
|
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
module, ok := debug.ReadBuildInfo()
|
||||||
|
if ok && module.Main.Version != "(devel)" {
|
||||||
|
AppVersion = module.Main.Version
|
||||||
|
}
|
||||||
app := cli.App{
|
app := cli.App{
|
||||||
Name: "Unlock Music CLI",
|
Name: "Unlock Music CLI",
|
||||||
HelpName: "um",
|
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),
|
Version: fmt.Sprintf("%s (%s,%s/%s)", AppVersion, runtime.Version(), runtime.GOOS, runtime.GOARCH),
|
||||||
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},
|
||||||
@ -39,17 +49,17 @@ func main() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
Action: appMain,
|
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,
|
HideHelpCommand: true,
|
||||||
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input",
|
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input",
|
||||||
}
|
}
|
||||||
err := app.Run(os.Args)
|
err := app.Run(os.Args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Log().Fatal("run app failed", zap.Error(err))
|
logger.Fatal("run app failed", zap.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func printSupportedExtensions() {
|
func printSupportedExtensions() {
|
||||||
exts := []string{}
|
var exts []string
|
||||||
for ext := range common.DecoderRegistry {
|
for ext := range common.DecoderRegistry {
|
||||||
exts = append(exts, ext)
|
exts = append(exts, ext)
|
||||||
}
|
}
|
||||||
@ -115,7 +125,7 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
} else {
|
} else {
|
||||||
allDec := common.GetDecoder(inputStat.Name(), skipNoop)
|
allDec := common.GetDecoder(inputStat.Name(), skipNoop)
|
||||||
if len(allDec) == 0 {
|
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)
|
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)
|
allDec := common.GetDecoder(item.Name(), skipNoop)
|
||||||
if len(allDec) == 0 {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err := tryDecFile(filepath.Join(inputDir, item.Name()), outputDir, allDec, removeSource)
|
err := tryDecFile(filepath.Join(inputDir, item.Name()), outputDir, allDec, removeSource)
|
||||||
if err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFunc, removeSource bool) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
var dec common.Decoder
|
var dec common.Decoder
|
||||||
for _, decFunc := range allDec {
|
for _, decFunc := range allDec {
|
||||||
dec = decFunc(file)
|
dec = decFunc(file)
|
||||||
if err := dec.Validate(); err == nil {
|
if err := dec.Validate(); err == nil {
|
||||||
break
|
break
|
||||||
}
|
} else {
|
||||||
logging.Log().Warn("try decode failed", zap.Error(err))
|
logger.Warn("try decode failed", zap.Error(err))
|
||||||
dec = nil
|
dec = nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if dec == nil {
|
if dec == nil {
|
||||||
return errors.New("no any decoder can resolve the file")
|
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 := ".mp3"
|
||||||
outExt := dec.GetAudioExt()
|
if ext, ok := common.SniffAll(header.Bytes()); ok {
|
||||||
if outExt == "" {
|
|
||||||
if ext, ok := common.SniffAll(outData); ok {
|
|
||||||
outExt = ext
|
outExt = ext
|
||||||
} else {
|
|
||||||
outExt = ".mp3"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
filenameOnly := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
|
filenameOnly := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
|
||||||
|
|
||||||
outPath := filepath.Join(outputDir, filenameOnly+outExt)
|
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 {
|
if err != nil {
|
||||||
return err
|
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 source file need to be removed
|
||||||
if removeSource {
|
if removeSource {
|
||||||
@ -189,9 +207,9 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
} 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
|
return nil
|
||||||
|
14
go.mod
14
go.mod
@ -3,15 +3,15 @@ module github.com/unlock-music/cli
|
|||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ulikunitz/xz v0.5.10
|
github.com/urfave/cli/v2 v2.23.5
|
||||||
github.com/urfave/cli/v2 v2.3.0
|
go.uber.org/zap v1.23.0
|
||||||
go.uber.org/zap v1.19.1
|
golang.org/x/crypto v0.3.0
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
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
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
go.uber.org/atomic v1.9.0 // indirect
|
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||||
go.uber.org/multierr v1.7.0 // 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 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
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.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
|
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.1/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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
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/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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
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 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
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.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.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/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||||
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
|
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.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.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
|
||||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||||
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723 h1:sHOAIxRGBp443oHZIPB+HsUGaksVCXVQENPxwTfQdH4=
|
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
||||||
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
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.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||||
go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
|
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
|
||||||
go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
|
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
|
||||||
go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI=
|
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
|
||||||
go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI=
|
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-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-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/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.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-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-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-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-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-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-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-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-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-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-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-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-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.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.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-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-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.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.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-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-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/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 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/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-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.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)]
|
return encrypt[:(length - unPadding)]
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecryptAes128Ecb(data, key []byte) []byte {
|
func DecryptAES128ECB(data, key []byte) []byte {
|
||||||
cipher, _ := aes.NewCipher(key)
|
cipher, _ := aes.NewCipher(key)
|
||||||
decrypted := make([]byte, len(data))
|
decrypted := make([]byte, len(data))
|
||||||
size := 16
|
size := 16
|
||||||
|
Loading…
Reference in New Issue
Block a user