Compare commits
No commits in common. "6f1cdc5b4f5eda4b0bbf5047551f59feb2ddd809" and "21bd246d6c30781b08f3d56af424b6586133e5e4" have entirely different histories.
6f1cdc5b4f
...
21bd246d6c
@ -1,17 +1,12 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Decoder interface {
|
||||
Validate() error
|
||||
io.Reader
|
||||
}
|
||||
|
||||
type CoverImageGetter interface {
|
||||
GetCoverImage(ctx context.Context) ([]byte, error)
|
||||
Decode() error
|
||||
GetCoverImage() []byte
|
||||
GetAudioData() []byte
|
||||
GetAudioExt() string
|
||||
GetMeta() Meta
|
||||
}
|
||||
|
||||
type Meta interface {
|
||||
@ -19,7 +14,3 @@ type Meta interface {
|
||||
GetTitle() string
|
||||
GetAlbum() string
|
||||
}
|
||||
|
||||
type StreamDecoder interface {
|
||||
Decrypt(buf []byte, offset int)
|
||||
}
|
||||
|
@ -1,12 +1,11 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"io"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type NewDecoderFunc func(rd io.ReadSeeker) Decoder
|
||||
type NewDecoderFunc func([]byte) Decoder
|
||||
|
||||
type decoderItem struct {
|
||||
noop bool
|
||||
|
@ -2,39 +2,46 @@ package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RawDecoder struct {
|
||||
rd io.ReadSeeker
|
||||
|
||||
file []byte
|
||||
audioExt string
|
||||
}
|
||||
|
||||
func NewRawDecoder(rd io.ReadSeeker) Decoder {
|
||||
return &RawDecoder{rd: rd}
|
||||
func NewRawDecoder(file []byte) Decoder {
|
||||
return &RawDecoder{file: file}
|
||||
}
|
||||
|
||||
func (d *RawDecoder) Validate() error {
|
||||
header := make([]byte, 16)
|
||||
if _, err := io.ReadFull(d.rd, header); err != nil {
|
||||
return fmt.Errorf("read file header failed: %v", err)
|
||||
for ext, sniffer := range snifferRegistry {
|
||||
if sniffer(d.file) {
|
||||
d.audioExt = strings.ToLower(ext)
|
||||
return nil
|
||||
}
|
||||
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")
|
||||
}
|
||||
|
||||
var ok bool
|
||||
d.audioExt, ok = SniffAll(header)
|
||||
if !ok {
|
||||
return errors.New("raw: sniff audio type failed")
|
||||
}
|
||||
func (d RawDecoder) Decode() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *RawDecoder) Read(p []byte) (n int, err error) {
|
||||
return d.rd.Read(p)
|
||||
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 init() {
|
||||
|
@ -1,57 +1,82 @@
|
||||
package kgm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
|
||||
"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 {
|
||||
header header
|
||||
cipher common.StreamDecoder
|
||||
|
||||
rd io.ReadSeeker
|
||||
offset int
|
||||
file []byte
|
||||
key []byte
|
||||
isVpr bool
|
||||
audio []byte
|
||||
}
|
||||
|
||||
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
||||
return &Decoder{rd: rd}
|
||||
func NewDecoder(file []byte) common.Decoder {
|
||||
return &Decoder{
|
||||
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
|
||||
}
|
||||
|
||||
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 (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])
|
||||
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() {
|
||||
// Kugou
|
||||
common.RegisterDecoder("kgm", false, NewDecoder)
|
||||
|
@ -1,64 +0,0 @@
|
||||
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
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
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
|
||||
}
|
81
algo/kgm/mask.go
Normal file
81
algo/kgm/mask.go
Normal file
@ -0,0 +1,81 @@
|
||||
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
|
||||
}
|
108
algo/kwm/kwm.go
108
algo/kwm/kwm.go
@ -2,73 +2,107 @@ package kwm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
)
|
||||
|
||||
const magicHeader = "yeelion-kuwo-tme"
|
||||
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 keyPreDefined = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
|
||||
|
||||
type Decoder struct {
|
||||
cipher common.StreamDecoder
|
||||
|
||||
rd io.ReadSeeker
|
||||
offset int
|
||||
file []byte
|
||||
|
||||
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 NewDecoder(rd io.ReadSeeker) common.Decoder {
|
||||
return &Decoder{rd: rd}
|
||||
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 (d *Decoder) Validate() error {
|
||||
header := make([]byte, 0x400) // kwm header is fixed to 1024 bytes
|
||||
_, err := io.ReadFull(d.rd, header)
|
||||
if err != nil {
|
||||
return fmt.Errorf("kwm read header: %w", err)
|
||||
lenData := len(d.file)
|
||||
if lenData < 1024 {
|
||||
return ErrKwFileSize
|
||||
}
|
||||
|
||||
// check magic header, 0x00 - 0x0F
|
||||
if !bytes.Equal([]byte(magicHeader), header[:len(magicHeader)]) {
|
||||
return errors.New("kwm magic header not matched")
|
||||
if !bytes.Equal(magicHeader, d.file[:16]) {
|
||||
return ErrKwMagicHeader
|
||||
}
|
||||
|
||||
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 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 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) Read(b []byte) (int, error) {
|
||||
n, err := d.rd.Read(b)
|
||||
if n > 0 {
|
||||
d.cipher.Decrypt(b[:n], d.offset)
|
||||
d.offset += n
|
||||
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
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
var err error
|
||||
d.bitrate, err = strconv.Atoi(bitType[:charPos])
|
||||
if err != nil {
|
||||
d.bitrate = 0
|
||||
}
|
||||
d.outputExt = strings.ToLower(bitType[charPos:])
|
||||
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode() error {
|
||||
d.parseBitrateAndType()
|
||||
|
||||
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]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func padOrTruncate(raw string, length int) string {
|
||||
|
@ -1,31 +0,0 @@
|
||||
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]
|
||||
}
|
||||
}
|
276
algo/ncm/ncm.go
276
algo/ncm/ncm.go
@ -2,44 +2,40 @@ package ncm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"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"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
"github.com/unlock-music/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(rd io.ReadSeeker) common.Decoder {
|
||||
func NewDecoder(data []byte) common.Decoder {
|
||||
return &Decoder{
|
||||
rd: rd,
|
||||
file: data,
|
||||
fileLen: uint32(len(data)),
|
||||
}
|
||||
}
|
||||
|
||||
type Decoder struct {
|
||||
rd io.ReadSeeker
|
||||
offset int
|
||||
|
||||
cipher common.StreamDecoder
|
||||
file []byte
|
||||
fileLen uint32
|
||||
|
||||
key []byte
|
||||
box []byte
|
||||
@ -50,128 +46,91 @@ type Decoder struct {
|
||||
|
||||
cover []byte
|
||||
audio []byte
|
||||
|
||||
offsetKey uint32
|
||||
offsetMeta uint32
|
||||
offsetCover uint32
|
||||
offsetAudio uint32
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
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) {
|
||||
if !bytes.Equal(magicHeader, d.file[:len(magicHeader)]) {
|
||||
return errors.New("ncm magic header not match")
|
||||
}
|
||||
|
||||
d.offsetKey = 8 + 2
|
||||
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)
|
||||
func (d *Decoder) readKeyData() error {
|
||||
if d.offsetKey == 0 || d.offsetKey+4 > d.fileLen {
|
||||
return errors.New("invalid cover file offset")
|
||||
}
|
||||
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] ^= 0x64
|
||||
bKeyRaw[i] = d.file[i+4+d.offsetKey] ^ 0x64
|
||||
}
|
||||
|
||||
return utils.PKCS7UnPadding(utils.DecryptAES128ECB(bKeyRaw, keyCore))[17:], nil
|
||||
d.key = utils.PKCS7UnPadding(utils.DecryptAes128Ecb(bKeyRaw, keyCore))[17:]
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) readMetaData() error {
|
||||
bMetaLen := make([]byte, 4) //
|
||||
if _, err := io.ReadFull(d.rd, bMetaLen); err != nil {
|
||||
return fmt.Errorf("ncm read key length: %w", err)
|
||||
if d.offsetMeta == 0 || d.offsetMeta+4 > d.fileLen {
|
||||
return errors.New("invalid meta file offset")
|
||||
}
|
||||
bMetaLen := d.file[d.offsetMeta : d.offsetMeta+4]
|
||||
iMetaLen := binary.LittleEndian.Uint32(bMetaLen)
|
||||
|
||||
d.offsetCover = d.offsetMeta + 4 + iMetaLen
|
||||
if iMetaLen == 0 {
|
||||
return nil // no meta data
|
||||
return errors.New("no any meta file found")
|
||||
}
|
||||
|
||||
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
|
||||
// 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
|
||||
}
|
||||
|
||||
cipherText, err := base64.StdEncoding.DecodeString(string(bMetaRaw))
|
||||
cipherText, err := base64.StdEncoding.DecodeString(string(bKeyRaw))
|
||||
if err != nil {
|
||||
return errors.New("decode ncm meta failed: " + err.Error())
|
||||
}
|
||||
metaRaw := utils.PKCS7UnPadding(utils.DecryptAES128ECB(cipherText, keyMeta))
|
||||
sep := bytes.IndexByte(metaRaw, ':')
|
||||
if sep == -1 {
|
||||
metaRaw := utils.PKCS7UnPadding(utils.DecryptAes128Ecb(cipherText, keyMeta))
|
||||
sepIdx := bytes.IndexRune(metaRaw, ':')
|
||||
if sepIdx == -1 {
|
||||
return errors.New("invalid ncm meta file")
|
||||
}
|
||||
|
||||
d.metaType = string(metaRaw[:sep])
|
||||
d.metaRaw = metaRaw[sep+1:]
|
||||
|
||||
d.metaType = string(metaRaw[:sepIdx])
|
||||
d.metaRaw = metaRaw[sepIdx+1:]
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
func (d *Decoder) buildKeyBox() {
|
||||
box := make([]byte, 256)
|
||||
for i := 0; i < 256; i++ {
|
||||
box[i] = byte(i)
|
||||
}
|
||||
|
||||
bCoverLen := make([]byte, 4) //
|
||||
if _, err := io.ReadFull(d.rd, bCoverLen); err != nil {
|
||||
return fmt.Errorf("ncm read cover length: %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]
|
||||
}
|
||||
iCoverLen := binary.LittleEndian.Uint32(bCoverLen)
|
||||
|
||||
coverBuf := make([]byte, iCoverLen)
|
||||
if _, err := io.ReadFull(d.rd, coverBuf); err != nil {
|
||||
return fmt.Errorf("ncm read cover data: %w", err)
|
||||
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]
|
||||
}
|
||||
d.cover = coverBuf
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) parseMeta() error {
|
||||
@ -187,16 +146,59 @@ func (d *Decoder) parseMeta() error {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
func (d *Decoder) readCoverData() error {
|
||||
if d.offsetCover == 0 || d.offsetCover+13 > d.fileLen {
|
||||
return errors.New("invalid cover file offset")
|
||||
}
|
||||
return n, err
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (d *Decoder) GetAudioExt() string {
|
||||
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 format := d.meta.GetFormat(); format != "" {
|
||||
return "." + d.meta.GetFormat()
|
||||
@ -205,34 +207,40 @@ func (d *Decoder) GetAudioExt() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
|
||||
if d.cover != nil {
|
||||
return d.cover, nil
|
||||
}
|
||||
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) GetAudioData() []byte {
|
||||
return d.audio
|
||||
}
|
||||
|
||||
func (d *Decoder) GetMeta() common.Meta {
|
||||
func (d Decoder) GetCoverImage() []byte {
|
||||
if d.cover != nil {
|
||||
return d.cover
|
||||
}
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func (d Decoder) GetMeta() common.Meta {
|
||||
return d.meta
|
||||
}
|
||||
|
||||
|
@ -1,42 +0,0 @@
|
||||
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
|
||||
}
|
5
algo/qmc/cipher.go
Normal file
5
algo/qmc/cipher.go
Normal file
@ -0,0 +1,5 @@
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ func simpleMakeKey(salt byte, length int) []byte {
|
||||
|
||||
const rawKeyPrefixV2 = "QQMusic EncV2,Key:"
|
||||
|
||||
func deriveKey(rawKey []byte) ([]byte, error) {
|
||||
func DecryptKey(rawKey []byte) ([]byte, error) {
|
||||
rawKeyDec := make([]byte, base64.StdEncoding.DecodedLen(len(rawKey)))
|
||||
n, err := base64.StdEncoding.Decode(rawKeyDec, rawKey)
|
||||
if err != nil {
|
@ -43,13 +43,13 @@ func TestDecryptKey(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("load test data failed: %s", err)
|
||||
}
|
||||
got, err := deriveKey(raw)
|
||||
got, err := DecryptKey(raw)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("deriveKey() error = %v, wantErr %v", err, tt.wantErr)
|
||||
t.Errorf("DecryptKey() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("deriveKey() got = %v..., want %v...",
|
||||
t.Errorf("DecryptKey() got = %v..., want %v...",
|
||||
string(got[:32]), string(want[:32]))
|
||||
}
|
||||
})
|
193
algo/qmc/qmc.go
193
algo/qmc/qmc.go
@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -13,14 +12,14 @@ import (
|
||||
)
|
||||
|
||||
type Decoder struct {
|
||||
raw io.ReadSeeker
|
||||
audio io.Reader
|
||||
offset int
|
||||
r io.ReadSeeker
|
||||
fileExt string
|
||||
|
||||
audioLen int
|
||||
|
||||
cipher common.StreamDecoder
|
||||
|
||||
decodedKey []byte
|
||||
cipher streamCipher
|
||||
offset int
|
||||
|
||||
rawMetaExtra1 int
|
||||
rawMetaExtra2 int
|
||||
}
|
||||
@ -28,79 +27,80 @@ 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, err := d.audio.Read(p)
|
||||
if n > 0 {
|
||||
d.cipher.Decrypt(p[:n], d.offset)
|
||||
d.offset += n
|
||||
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
|
||||
}
|
||||
return n, err
|
||||
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
|
||||
}
|
||||
|
||||
func NewDecoder(r io.ReadSeeker) common.Decoder {
|
||||
return &Decoder{raw: r}
|
||||
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 {
|
||||
// search & derive key
|
||||
err := d.searchKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
// 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) 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)
|
||||
if _, err := io.ReadFull(d.r, buf); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := d.r.Seek(0, io.SeekStart)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.cipher.Decrypt(buf, 0)
|
||||
_, ok := common.SniffAll(buf)
|
||||
fileExt, ok := common.SniffAll(buf)
|
||||
if !ok {
|
||||
return errors.New("qmc: detect file type failed")
|
||||
return errors.New("detect file type failed")
|
||||
}
|
||||
d.fileExt = fileExt
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) GetFileExt() string {
|
||||
return d.fileExt
|
||||
}
|
||||
|
||||
func (d *Decoder) searchKey() error {
|
||||
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd)
|
||||
fileSizeM4, err := d.r.Seek(-4, io.SeekEnd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
|
||||
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -110,7 +110,7 @@ func (d *Decoder) searchKey() error {
|
||||
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
|
||||
} else {
|
||||
size := binary.LittleEndian.Uint32(buf)
|
||||
if size <= 0xFFFF && size != 0 { // assume size is key len
|
||||
if size < 0x300 && size != 0 {
|
||||
return d.readRawKey(int64(size))
|
||||
}
|
||||
// try to use default static cipher
|
||||
@ -120,13 +120,13 @@ func (d *Decoder) searchKey() error {
|
||||
}
|
||||
|
||||
func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
||||
audioLen, err := d.raw.Seek(-(4 + rawKeyLen), io.SeekEnd)
|
||||
audioLen, err := d.r.Seek(-(4 + rawKeyLen), io.SeekEnd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.audioLen = int(audioLen)
|
||||
|
||||
rawKeyData, err := io.ReadAll(io.LimitReader(d.raw, rawKeyLen))
|
||||
rawKeyData, err := io.ReadAll(io.LimitReader(d.r, rawKeyLen))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -134,7 +134,7 @@ func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
||||
// clean suffix NULs
|
||||
rawKeyData = bytes.TrimRight(rawKeyData, "\x00")
|
||||
|
||||
d.decodedKey, err = deriveKey(rawKeyData)
|
||||
d.decodedKey, err = DecryptKey(rawKeyData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -144,22 +144,22 @@ func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
||||
|
||||
func (d *Decoder) readRawMetaQTag() error {
|
||||
// get raw meta data len
|
||||
if _, err := d.raw.Seek(-8, io.SeekEnd); err != nil {
|
||||
if _, err := d.r.Seek(-8, io.SeekEnd); err != nil {
|
||||
return err
|
||||
}
|
||||
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
|
||||
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rawMetaLen := int64(binary.BigEndian.Uint32(buf))
|
||||
|
||||
// read raw meta data
|
||||
audioLen, err := d.raw.Seek(-(8 + rawMetaLen), io.SeekEnd)
|
||||
audioLen, err := d.r.Seek(-(8 + rawMetaLen), io.SeekEnd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.audioLen = int(audioLen)
|
||||
rawMetaData, err := io.ReadAll(io.LimitReader(d.raw, rawMetaLen))
|
||||
rawMetaData, err := io.ReadAll(io.LimitReader(d.r, rawMetaLen))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -169,7 +169,7 @@ func (d *Decoder) readRawMetaQTag() error {
|
||||
return errors.New("invalid raw meta data")
|
||||
}
|
||||
|
||||
d.decodedKey, err = deriveKey([]byte(items[0]))
|
||||
d.decodedKey, err = DecryptKey([]byte(items[0]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -208,6 +208,57 @@ func init() {
|
||||
"mflac", "mflac0", //QQ Music New Flac
|
||||
}
|
||||
for _, ext := range supportedExts {
|
||||
common.RegisterDecoder(ext, false, NewDecoder)
|
||||
common.RegisterDecoder(ext, false, newCompactDecoder)
|
||||
}
|
||||
}
|
||||
|
||||
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,9 +3,6 @@ package tm
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
)
|
||||
|
||||
@ -13,44 +10,70 @@ 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 {
|
||||
raw io.ReadSeeker
|
||||
offset int
|
||||
audio io.Reader
|
||||
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
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
header := make([]byte, 8)
|
||||
if _, err := io.ReadFull(d.raw, header); err != nil {
|
||||
return fmt.Errorf("tm read header: %w", err)
|
||||
if len(d.file) < 8 {
|
||||
return errors.New("invalid file size")
|
||||
}
|
||||
|
||||
if bytes.Equal(magicHeader, header[:len(magicHeader)]) { // replace m4a header
|
||||
d.audio = io.MultiReader(bytes.NewReader(replaceHeader), d.raw)
|
||||
if !bytes.Equal(magicHeader, d.file[:4]) {
|
||||
return errors.New("not a valid tm file")
|
||||
}
|
||||
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) Read(buf []byte) (int, error) {
|
||||
return d.audio.Read(buf)
|
||||
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 NewTmDecoder(rd io.ReadSeeker) common.Decoder {
|
||||
return &Decoder{raw: rd}
|
||||
//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 init() {
|
||||
// QQ Music IOS M4a (replace header)
|
||||
common.RegisterDecoder("tm2", false, NewTmDecoder)
|
||||
common.RegisterDecoder("tm6", false, NewTmDecoder)
|
||||
// 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 Mp3 (not encrypted)
|
||||
common.RegisterDecoder("tm0", false, NewTmDecoder)
|
||||
common.RegisterDecoder("tm3", false, NewTmDecoder)
|
||||
}
|
||||
|
107
algo/xm/xm.go
107
algo/xm/xm.go
@ -3,10 +3,9 @@ package xm
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/unlock-music/cli/algo/common"
|
||||
"github.com/unlock-music/cli/internal/logging"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -18,19 +17,26 @@ var (
|
||||
" MP3": "mp3",
|
||||
" A4M": "m4a",
|
||||
}
|
||||
ErrFileSize = errors.New("xm invalid file size")
|
||||
ErrMagicHeader = errors.New("xm magic header not matched")
|
||||
)
|
||||
|
||||
type Decoder struct {
|
||||
rd io.ReadSeeker
|
||||
offset int
|
||||
|
||||
cipher common.StreamDecoder
|
||||
file []byte
|
||||
headerLen uint32
|
||||
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
|
||||
@ -39,53 +45,62 @@ func (d *Decoder) GetAudioExt() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
||||
return &Decoder{rd: rd}
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
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)
|
||||
}
|
||||
|
||||
// 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(header[4:8])]
|
||||
if !ok {
|
||||
return fmt.Errorf("xm detect unknown audio type: %s", string(header[4:8]))
|
||||
}
|
||||
|
||||
// 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))
|
||||
|
||||
func (d *Decoder) GetMeta() common.Meta {
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
func NewDecoder(data []byte) common.Decoder {
|
||||
return &Decoder{file: data}
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
lenData := len(d.file)
|
||||
if lenData < 16 {
|
||||
return ErrFileSize
|
||||
}
|
||||
if !bytes.Equal(magicHeader, d.file[:4]) ||
|
||||
!bytes.Equal(magicHeader2, d.file[8:12]) {
|
||||
return ErrMagicHeader
|
||||
}
|
||||
|
||||
var ok bool
|
||||
d.outputExt, ok = typeMapping[string(d.file[4:8])]
|
||||
if !ok {
|
||||
return errors.New("detect unknown xm file type: " + string(d.file[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
|
||||
}
|
||||
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}
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Xiami Wav/M4a/Mp3/Flac
|
||||
common.RegisterDecoder("xm", false, NewDecoder)
|
||||
// Xiami Typed Format
|
||||
common.RegisterDecoder("wav", false, NewDecoder)
|
||||
common.RegisterDecoder("mp3", false, NewDecoder)
|
||||
common.RegisterDecoder("flac", false, NewDecoder)
|
||||
common.RegisterDecoder("m4a", false, NewDecoder)
|
||||
common.RegisterDecoder("wav", false, DecoderFuncWithExt("wav"))
|
||||
common.RegisterDecoder("mp3", false, DecoderFuncWithExt("mp3"))
|
||||
common.RegisterDecoder("flac", false, DecoderFuncWithExt("flac"))
|
||||
common.RegisterDecoder("m4a", false, DecoderFuncWithExt("m4a"))
|
||||
}
|
||||
|
@ -1,21 +0,0 @@
|
||||
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,17 +1,13 @@
|
||||
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"
|
||||
@ -28,17 +24,11 @@ import (
|
||||
|
||||
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://git.unlock-music.dev/um/cli",
|
||||
Usage: "Unlock your encrypted music file https://github.com/unlock-music/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},
|
||||
@ -49,17 +39,17 @@ func main() {
|
||||
},
|
||||
|
||||
Action: appMain,
|
||||
Copyright: fmt.Sprintf("Copyright (c) 2020 - %d Unlock Music https://git.unlock-music.dev/um/cli/src/branch/master/LICENSE", time.Now().Year()),
|
||||
Copyright: "Copyright (c) 2020 - 2021 Unlock Music https://github.com/unlock-music/cli/blob/master/LICENSE",
|
||||
HideHelpCommand: true,
|
||||
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input",
|
||||
}
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
logger.Fatal("run app failed", zap.Error(err))
|
||||
logging.Log().Fatal("run app failed", zap.Error(err))
|
||||
}
|
||||
}
|
||||
func printSupportedExtensions() {
|
||||
var exts []string
|
||||
exts := []string{}
|
||||
for ext := range common.DecoderRegistry {
|
||||
exts = append(exts, ext)
|
||||
}
|
||||
@ -125,7 +115,7 @@ func appMain(c *cli.Context) (err error) {
|
||||
} else {
|
||||
allDec := common.GetDecoder(inputStat.Name(), skipNoop)
|
||||
if len(allDec) == 0 {
|
||||
logger.Fatal("skipping while no suitable decoder")
|
||||
logging.Log().Fatal("skipping while no suitable decoder")
|
||||
}
|
||||
return tryDecFile(input, output, allDec, removeSource)
|
||||
}
|
||||
@ -142,64 +132,56 @@ func dealDirectory(inputDir string, outputDir string, skipNoop bool, removeSourc
|
||||
}
|
||||
allDec := common.GetDecoder(item.Name(), skipNoop)
|
||||
if len(allDec) == 0 {
|
||||
logger.Info("skipping while no suitable decoder", zap.String("file", item.Name()))
|
||||
logging.Log().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 {
|
||||
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
|
||||
logging.Log().Error("conversion failed", zap.String("source", item.Name()))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFunc, removeSource bool) error {
|
||||
file, err := os.Open(inputFile)
|
||||
file, err := os.ReadFile(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")
|
||||
}
|
||||
|
||||
header := bytes.NewBuffer(nil)
|
||||
_, err = io.CopyN(header, dec, 16)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read header failed: %w", err)
|
||||
if err := dec.Decode(); err != nil {
|
||||
return errors.New("failed while decoding: " + err.Error())
|
||||
}
|
||||
|
||||
outExt := ".mp3"
|
||||
if ext, ok := common.SniffAll(header.Bytes()); ok {
|
||||
outData := dec.GetAudioData()
|
||||
outExt := dec.GetAudioExt()
|
||||
if outExt == "" {
|
||||
if ext, ok := common.SniffAll(outData); ok {
|
||||
outExt = ext
|
||||
} else {
|
||||
outExt = ".mp3"
|
||||
}
|
||||
}
|
||||
filenameOnly := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
|
||||
|
||||
outPath := filepath.Join(outputDir, filenameOnly+outExt)
|
||||
outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||
err = os.WriteFile(outPath, outData, 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 {
|
||||
@ -207,9 +189,9 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("successfully converted, and source file is removed", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
logging.Log().Info("successfully converted, and source file is removed", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
} else {
|
||||
logger.Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
logging.Log().Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
14
go.mod
14
go.mod
@ -3,15 +3,15 @@ module github.com/unlock-music/cli
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/urfave/cli/v2 v2.23.5
|
||||
go.uber.org/zap v1.23.0
|
||||
golang.org/x/crypto v0.3.0
|
||||
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
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.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
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.7.0 // indirect
|
||||
)
|
||||
|
63
go.sum
63
go.sum
@ -1,8 +1,9 @@
|
||||
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
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.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
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/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=
|
||||
@ -13,76 +14,60 @@ 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/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/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/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.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/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/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
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=
|
||||
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=
|
||||
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=
|
||||
|
41
internal/logging/logging.go
Normal file
41
internal/logging/logging.go
Normal file
@ -0,0 +1,41 @@
|
||||
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
|
||||
)
|
@ -1,13 +0,0 @@
|
||||
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