Compare commits

..

No commits in common. "14c9d49d463980aa023d4ceb26d015211cef5d17" and "9647ca15fdc1d5ebba39e6191c70b2ea7faa8380" have entirely different histories.

31 changed files with 752 additions and 793 deletions

View File

@ -1,17 +1,12 @@
package common package common
import (
"context"
"io"
)
type Decoder interface { type Decoder interface {
Validate() error Validate() error
io.Reader Decode() error
} GetCoverImage() []byte
GetAudioData() []byte
type CoverImageGetter interface { GetAudioExt() string
GetCoverImage(ctx context.Context) ([]byte, error) GetMeta() Meta
} }
type Meta interface { type Meta interface {
@ -19,7 +14,3 @@ type Meta interface {
GetTitle() string GetTitle() string
GetAlbum() string GetAlbum() string
} }
type StreamDecoder interface {
Decrypt(buf []byte, offset int)
}

View File

@ -1,12 +1,11 @@
package common package common
import ( import (
"io"
"path/filepath" "path/filepath"
"strings" "strings"
) )
type NewDecoderFunc func(rd io.ReadSeeker) Decoder type NewDecoderFunc func([]byte) Decoder
type decoderItem struct { type decoderItem struct {
noop bool noop bool

View File

@ -2,39 +2,46 @@ package common
import ( import (
"errors" "errors"
"fmt" "strings"
"io"
) )
type RawDecoder struct { type RawDecoder struct {
rd io.ReadSeeker file []byte
audioExt string audioExt string
} }
func NewRawDecoder(rd io.ReadSeeker) Decoder { func NewRawDecoder(file []byte) Decoder {
return &RawDecoder{rd: rd} return &RawDecoder{file: file}
} }
func (d *RawDecoder) Validate() error { func (d *RawDecoder) Validate() error {
header := make([]byte, 16) for ext, sniffer := range snifferRegistry {
if _, err := io.ReadFull(d.rd, header); err != nil { if sniffer(d.file) {
return fmt.Errorf("read file header failed: %v", err) 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 func (d RawDecoder) Decode() error {
d.audioExt, ok = SniffAll(header)
if !ok {
return errors.New("raw: sniff audio type failed")
}
return nil return nil
} }
func (d *RawDecoder) Read(p []byte) (n int, err error) { func (d RawDecoder) GetCoverImage() []byte {
return d.rd.Read(p) 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() { func init() {

View File

@ -1,57 +1,90 @@
package kgm package kgm
import ( import (
"fmt" "bytes"
"io" "encoding/binary"
"errors"
"github.com/unlock-music/cli/algo/common"
"github.com/unlock-music/cli/internal/logging"
)
"unlock-music.dev/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 {
header header file []byte
cipher common.StreamDecoder key []byte
isVpr bool
rd io.ReadSeeker audio []byte
offset int
} }
func NewDecoder(rd io.ReadSeeker) common.Decoder { func NewDecoder(file []byte) common.Decoder {
return &Decoder{rd: rd} 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 return nil
} }
func (d *Decoder) Read(buf []byte) (int, error) { func (d Decoder) GetAudioData() []byte {
n, err := d.rd.Read(buf) return d.audio
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])
dataEncrypted := d.file[headerLen:]
lenData := len(dataEncrypted)
initMask()
if fullMaskLen < lenData {
logging.Log().Warn("The file is too large and the processed audio is incomplete, " +
"please report to us about this file at https://github.com/unlock-music/cli/issues")
lenData = fullMaskLen
}
d.audio = make([]byte, lenData)
for i := 0; i < lenData; i++ {
med8 := dataEncrypted[i] ^ d.key[i%17] ^ maskV2PreDef[i%(16*17)] ^ maskV2[i>>4]
d.audio[i] = med8 ^ (med8&0xf)<<4
}
if d.isVpr {
for i := 0; i < lenData; i++ {
d.audio[i] ^= maskDiffVpr[i%17]
}
}
return nil
}
func init() { func init() {
// Kugou // Kugou
common.RegisterDecoder("kgm", false, NewDecoder) common.RegisterDecoder("kgm", false, NewDecoder)

BIN
algo/kgm/kgm.v2.mask Normal file

Binary file not shown.

View File

@ -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
}

View File

@ -1,55 +0,0 @@
package kgm
import (
"crypto/md5"
"fmt"
"unlock-music.dev/cli/algo/common"
)
// kgmCryptoV3 is kgm file crypto v3
type kgmCryptoV3 struct {
slotBox []byte
fileBox []byte
}
var kgmV3Slot2Key = map[uint32][]byte{
1: {0x6C, 0x2C, 0x2F, 0x27},
}
func newKgmCryptoV3(header *header) (common.StreamDecoder, error) {
c := &kgmCryptoV3{}
slotKey, ok := kgmV3Slot2Key[header.CryptoSlot]
if !ok {
return nil, fmt.Errorf("kgm3: unknown crypto slot %d", header.CryptoSlot)
}
c.slotBox = kugouMD5(slotKey)
c.fileBox = append(kugouMD5(header.CryptoKey), 0x6b)
return c, nil
}
func (d *kgmCryptoV3) Decrypt(b []byte, offset int) {
for i := 0; i < len(b); i++ {
b[i] ^= d.fileBox[(offset+i)%len(d.fileBox)]
b[i] ^= b[i] << 4
b[i] ^= d.slotBox[(offset+i)%len(d.slotBox)]
b[i] ^= xorCollapseUint32(uint32(offset + i))
}
}
func xorCollapseUint32(i uint32) byte {
return byte(i) ^ byte(i>>8) ^ byte(i>>16) ^ byte(i>>24)
}
func kugouMD5(b []byte) []byte {
digest := md5.Sum(b)
ret := make([]byte, 16)
for i := 0; i < md5.Size; i += 2 {
ret[i] = digest[14-i]
ret[i+1] = digest[14-i+1]
}
return ret
}

59
algo/kgm/mask.go Normal file
View File

@ -0,0 +1,59 @@
package kgm
import (
"bytes"
_ "embed"
"github.com/ulikunitz/xz"
"github.com/unlock-music/cli/internal/logging"
"go.uber.org/zap"
"io"
)
var maskDiffVpr = []byte{
0x25, 0xDF, 0xE8, 0xA6, 0x75, 0x1E, 0x75, 0x0E,
0x2F, 0x80, 0xF3, 0x2D, 0xB8, 0xB6, 0xE3, 0x11,
0x00}
var maskV2PreDef = []byte{
0xB8, 0xD5, 0x3D, 0xB2, 0xE9, 0xAF, 0x78, 0x8C, 0x83, 0x33, 0x71, 0x51, 0x76, 0xA0, 0xCD, 0x37,
0x2F, 0x3E, 0x35, 0x8D, 0xA9, 0xBE, 0x98, 0xB7, 0xE7, 0x8C, 0x22, 0xCE, 0x5A, 0x61, 0xDF, 0x68,
0x69, 0x89, 0xFE, 0xA5, 0xB6, 0xDE, 0xA9, 0x77, 0xFC, 0xC8, 0xBD, 0xBD, 0xE5, 0x6D, 0x3E, 0x5A,
0x36, 0xEF, 0x69, 0x4E, 0xBE, 0xE1, 0xE9, 0x66, 0x1C, 0xF3, 0xD9, 0x02, 0xB6, 0xF2, 0x12, 0x9B,
0x44, 0xD0, 0x6F, 0xB9, 0x35, 0x89, 0xB6, 0x46, 0x6D, 0x73, 0x82, 0x06, 0x69, 0xC1, 0xED, 0xD7,
0x85, 0xC2, 0x30, 0xDF, 0xA2, 0x62, 0xBE, 0x79, 0x2D, 0x62, 0x62, 0x3D, 0x0D, 0x7E, 0xBE, 0x48,
0x89, 0x23, 0x02, 0xA0, 0xE4, 0xD5, 0x75, 0x51, 0x32, 0x02, 0x53, 0xFD, 0x16, 0x3A, 0x21, 0x3B,
0x16, 0x0F, 0xC3, 0xB2, 0xBB, 0xB3, 0xE2, 0xBA, 0x3A, 0x3D, 0x13, 0xEC, 0xF6, 0x01, 0x45, 0x84,
0xA5, 0x70, 0x0F, 0x93, 0x49, 0x0C, 0x64, 0xCD, 0x31, 0xD5, 0xCC, 0x4C, 0x07, 0x01, 0x9E, 0x00,
0x1A, 0x23, 0x90, 0xBF, 0x88, 0x1E, 0x3B, 0xAB, 0xA6, 0x3E, 0xC4, 0x73, 0x47, 0x10, 0x7E, 0x3B,
0x5E, 0xBC, 0xE3, 0x00, 0x84, 0xFF, 0x09, 0xD4, 0xE0, 0x89, 0x0F, 0x5B, 0x58, 0x70, 0x4F, 0xFB,
0x65, 0xD8, 0x5C, 0x53, 0x1B, 0xD3, 0xC8, 0xC6, 0xBF, 0xEF, 0x98, 0xB0, 0x50, 0x4F, 0x0F, 0xEA,
0xE5, 0x83, 0x58, 0x8C, 0x28, 0x2C, 0x84, 0x67, 0xCD, 0xD0, 0x9E, 0x47, 0xDB, 0x27, 0x50, 0xCA,
0xF4, 0x63, 0x63, 0xE8, 0x97, 0x7F, 0x1B, 0x4B, 0x0C, 0xC2, 0xC1, 0x21, 0x4C, 0xCC, 0x58, 0xF5,
0x94, 0x52, 0xA3, 0xF3, 0xD3, 0xE0, 0x68, 0xF4, 0x00, 0x23, 0xF3, 0x5E, 0x0A, 0x7B, 0x93, 0xDD,
0xAB, 0x12, 0xB2, 0x13, 0xE8, 0x84, 0xD7, 0xA7, 0x9F, 0x0F, 0x32, 0x4C, 0x55, 0x1D, 0x04, 0x36,
0x52, 0xDC, 0x03, 0xF3, 0xF9, 0x4E, 0x42, 0xE9, 0x3D, 0x61, 0xEF, 0x7C, 0xB6, 0xB3, 0x93, 0x50,
}
//go:embed kgm.v2.mask
var maskV2Xz []byte
var maskV2 []byte
var fullMaskLen int
var initMaskOK = false
//todo: decompress mask on demand
func initMask() {
if initMaskOK {
return
}
maskReader, err := xz.NewReader(bytes.NewReader(maskV2Xz))
if err != nil {
logging.Log().Fatal("load kgm mask failed", zap.Error(err))
}
maskV2, err = io.ReadAll(maskReader)
if err != nil {
logging.Log().Fatal("load kgm mask failed", zap.Error(err))
}
fullMaskLen = len(maskV2) * 16
initMaskOK = true
}

View File

@ -2,73 +2,107 @@ package kwm
import ( import (
"bytes" "bytes"
"encoding/binary"
"errors" "errors"
"fmt" "github.com/unlock-music/cli/algo/common"
"io"
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
"unlock-music.dev/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" const keyPreDefined = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
type Decoder struct { type Decoder struct {
cipher common.StreamDecoder file []byte
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 NewDecoder(rd io.ReadSeeker) common.Decoder { func (d *Decoder) GetMeta() common.Meta {
return &Decoder{rd: rd} 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 { func (d *Decoder) Validate() error {
header := make([]byte, 0x400) // kwm header is fixed to 1024 bytes lenData := len(d.file)
_, err := io.ReadFull(d.rd, header) if lenData < 1024 {
if err != nil { return ErrKwFileSize
return fmt.Errorf("kwm read header: %w", err)
} }
if !bytes.Equal(magicHeader, d.file[:16]) {
// check magic header, 0x00 - 0x0F return ErrKwMagicHeader
if !bytes.Equal([]byte(magicHeader), header[:len(magicHeader)]) {
return errors.New("kwm magic header not matched")
} }
d.cipher = newKwmCipher(header[0x18:0x20]) // Crypto Key, 0x18 - 0x1F
d.bitrate, d.outputExt = parseBitrateAndType(header[0x30:0x38]) // Bitrate & File Extension, 0x30 - 0x38
return nil return nil
} }
func parseBitrateAndType(header []byte) (int, string) { func generateMask(key []byte) []byte {
tmp := strings.TrimRight(string(header), "\x00") keyInt := binary.LittleEndian.Uint64(key)
sep := strings.IndexFunc(tmp, func(r rune) bool { keyStr := strconv.FormatUint(keyInt, 10)
return !unicode.IsDigit(r) keyStrTrim := padOrTruncate(keyStr, 32)
}) mask := make([]byte, 32)
for i := 0; i < 32; i++ {
bitrate, _ := strconv.Atoi(tmp[:sep]) // just ignore the error mask[i] = keyPreDefined[i] ^ keyStrTrim[i]
outputExt := strings.ToLower(tmp[sep:]) }
return bitrate, outputExt return mask
} }
func (d *Decoder) Read(b []byte) (int, error) { func (d *Decoder) parseBitrateAndType() {
n, err := d.rd.Read(b) bitType := string(bytes.TrimRight(d.file[0x30:0x38], string(byte(0))))
if n > 0 { charPos := 0
d.cipher.Decrypt(b[:n], d.offset) for charPos = range bitType {
d.offset += n 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 { func padOrTruncate(raw string, length int) string {

View File

@ -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]
}
}

View File

@ -1,9 +1,8 @@
package ncm package ncm
import ( import (
"github.com/unlock-music/cli/algo/common"
"strings" "strings"
"unlock-music.dev/cli/algo/common"
) )
type RawMeta interface { type RawMeta interface {

View File

@ -2,44 +2,40 @@ package ncm
import ( import (
"bytes" "bytes"
"context"
"encoding/base64" "encoding/base64"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "github.com/unlock-music/cli/algo/common"
"io" "github.com/unlock-music/cli/internal/logging"
"github.com/unlock-music/cli/internal/utils"
"go.uber.org/zap"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
"unlock-music.dev/cli/algo/common"
"unlock-music.dev/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(rd io.ReadSeeker) common.Decoder { func NewDecoder(data []byte) common.Decoder {
return &Decoder{ return &Decoder{
rd: rd, file: data,
fileLen: uint32(len(data)),
} }
} }
type Decoder struct { type Decoder struct {
rd io.ReadSeeker file []byte
offset int fileLen uint32
cipher common.StreamDecoder
key []byte key []byte
box []byte box []byte
@ -50,128 +46,91 @@ 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 err := d.validateMagicHeader(); err != nil { if !bytes.Equal(magicHeader, d.file[:len(magicHeader)]) {
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() ([]byte, error) { func (d *Decoder) readKeyData() error {
bKeyLen := make([]byte, 4) // if d.offsetKey == 0 || d.offsetKey+4 > d.fileLen {
if _, err := io.ReadFull(d.rd, bKeyLen); err != nil { return errors.New("invalid cover file offset")
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] ^= 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 { func (d *Decoder) readMetaData() error {
bMetaLen := make([]byte, 4) // if d.offsetMeta == 0 || d.offsetMeta+4 > d.fileLen {
if _, err := io.ReadFull(d.rd, bMetaLen); err != nil { return errors.New("invalid meta file offset")
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 nil // no meta data return errors.New("no any meta file found")
} }
bMetaRaw := make([]byte, iMetaLen) // Why sub 22: Remove "163 key(Don't modify):"
if _, err := io.ReadFull(d.rd, bMetaRaw); err != nil { bKeyRaw := make([]byte, iMetaLen-22)
return fmt.Errorf("ncm read meta data: %w", err) for i := uint32(0); i < iMetaLen-22; i++ {
} 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(bMetaRaw)) cipherText, err := base64.StdEncoding.DecodeString(string(bKeyRaw))
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))
sep := bytes.IndexByte(metaRaw, ':') sepIdx := bytes.IndexRune(metaRaw, ':')
if sep == -1 { if sepIdx == -1 {
return errors.New("invalid ncm meta file") return errors.New("invalid ncm meta file")
} }
d.metaType = string(metaRaw[:sep]) d.metaType = string(metaRaw[:sepIdx])
d.metaRaw = metaRaw[sep+1:] d.metaRaw = metaRaw[sepIdx+1:]
return nil return nil
} }
func (d *Decoder) readCoverData() error { func (d *Decoder) buildKeyBox() {
bCoverCRC := make([]byte, 4) box := make([]byte, 256)
if _, err := io.ReadFull(d.rd, bCoverCRC); err != nil { for i := 0; i < 256; i++ {
return fmt.Errorf("ncm read cover crc: %w", err) box[i] = byte(i)
} }
bCoverLen := make([]byte, 4) // keyLen := len(d.key)
if _, err := io.ReadFull(d.rd, bCoverLen); err != nil { var j byte
return fmt.Errorf("ncm read cover length: %w", err) 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) d.box = make([]byte, 256)
if _, err := io.ReadFull(d.rd, coverBuf); err != nil { var _i byte
return fmt.Errorf("ncm read cover data: %w", err) 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 { func (d *Decoder) parseMeta() error {
@ -187,16 +146,59 @@ func (d *Decoder) parseMeta() error {
} }
} }
func (d *Decoder) Read(buf []byte) (int, error) { func (d *Decoder) readCoverData() error {
n, err := d.rd.Read(buf) if d.offsetCover == 0 || d.offsetCover+13 > d.fileLen {
if n > 0 { return errors.New("invalid cover file offset")
d.cipher.Decrypt(buf[:n], d.offset)
d.offset += n
}
return n, err
} }
func (d *Decoder) GetAudioExt() string { 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) 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()
@ -205,34 +207,40 @@ func (d *Decoder) GetAudioExt() string {
return "" return ""
} }
func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) { func (d Decoder) GetAudioData() []byte {
if d.cover != nil { return d.audio
return d.cover, nil
} }
func (d Decoder) GetCoverImage() []byte {
if d.cover != nil {
return d.cover
}
{
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, nil // no cover image return nil
} }
resp, err := http.Get(imgURL)
// fetch cover image
req, err := http.NewRequestWithContext(ctx, http.MethodGet, imgURL, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("download image failed: %w", err) logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
return nil
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("download image failed: unexpected http status %s", resp.Status) logging.Log().Warn("download image failed", zap.String("http", resp.Status),
zap.String("url", imgURL))
return nil
} }
data, err := io.ReadAll(resp.Body) data, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("download image failed: %w", err) logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
return nil
}
return data
} }
return data, nil
} }
func (d *Decoder) GetMeta() common.Meta { func (d Decoder) GetMeta() common.Meta {
return d.meta return d.meta
} }

View File

@ -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
View File

@ -0,0 +1,5 @@
package qmc
type streamCipher interface {
Decrypt(buf []byte, offset int)
}

View File

@ -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")
} }

View File

@ -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

View File

@ -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")

View File

@ -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

View File

@ -1,6 +1,6 @@
package qmc package qmc
func newStaticCipher() *staticCipher { func NewStaticCipher() *staticCipher {
return &defaultStaticCipher return &defaultStaticCipher
} }

View File

@ -1,10 +1,8 @@
package qmc package qmc
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt"
"math" "math"
"golang.org/x/crypto/tea" "golang.org/x/crypto/tea"
@ -18,30 +16,16 @@ func simpleMakeKey(salt byte, length int) []byte {
} }
return keyBuf return keyBuf
} }
func DecryptKey(rawKey []byte) ([]byte, error) {
const rawKeyPrefixV2 = "QQMusic EncV2,Key:"
func deriveKey(rawKey []byte) ([]byte, error) {
rawKeyDec := make([]byte, base64.StdEncoding.DecodedLen(len(rawKey))) 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 {
return nil, err return nil, err
} }
rawKeyDec = rawKeyDec[:n] if n < 16 {
if bytes.HasPrefix(rawKeyDec, []byte(rawKeyPrefixV2)) {
rawKeyDec, err = deriveKeyV2(bytes.TrimPrefix(rawKeyDec, []byte(rawKeyPrefixV2)))
if err != nil {
return nil, fmt.Errorf("deriveKeyV2 failed: %w", err)
}
}
return deriveKeyV1(rawKeyDec)
}
func deriveKeyV1(rawKeyDec []byte) ([]byte, error) {
if len(rawKeyDec) < 16 {
return nil, errors.New("key length is too short") return nil, errors.New("key length is too short")
} }
rawKeyDec = rawKeyDec[:n]
simpleKey := simpleMakeKey(106, 8) simpleKey := simpleMakeKey(106, 8)
teaKey := make([]byte, 16) teaKey := make([]byte, 16)
@ -56,37 +40,6 @@ func deriveKeyV1(rawKeyDec []byte) ([]byte, error) {
} }
return append(rawKeyDec[:8], rs...), nil return append(rawKeyDec[:8], rs...), nil
} }
var (
deriveV2Key1 = []byte{
0x33, 0x38, 0x36, 0x5A, 0x4A, 0x59, 0x21, 0x40,
0x23, 0x2A, 0x24, 0x25, 0x5E, 0x26, 0x29, 0x28,
}
deriveV2Key2 = []byte{
0x2A, 0x2A, 0x23, 0x21, 0x28, 0x23, 0x24, 0x25,
0x26, 0x5E, 0x61, 0x31, 0x63, 0x5A, 0x2C, 0x54,
}
)
func deriveKeyV2(raw []byte) ([]byte, error) {
buf, err := decryptTencentTea(raw, deriveV2Key1)
if err != nil {
return nil, err
}
buf, err = decryptTencentTea(buf, deriveV2Key2)
if err != nil {
return nil, err
}
n, err := base64.StdEncoding.Decode(buf, buf)
if err != nil {
return nil, err
}
return buf[:n], nil
}
func decryptTencentTea(inBuf []byte, key []byte) ([]byte, error) { func decryptTencentTea(inBuf []byte, key []byte) ([]byte, error) {
const saltLen = 2 const saltLen = 2
const zeroLen = 7 const zeroLen = 7
@ -106,7 +59,9 @@ func decryptTencentTea(inBuf []byte, key []byte) ([]byte, error) {
blk.Decrypt(destBuf, inBuf) blk.Decrypt(destBuf, inBuf)
padLen := int(destBuf[0] & 0x7) padLen := int(destBuf[0] & 0x7)
outLen := len(inBuf) - 1 - padLen - saltLen - zeroLen outLen := len(inBuf) - 1 - padLen - saltLen - zeroLen
if padLen+saltLen != 8 {
return nil, errors.New("invalid pad len")
}
out := make([]byte, outLen) out := make([]byte, outLen)
ivPrev := make([]byte, 8) ivPrev := make([]byte, 8)
@ -153,7 +108,6 @@ func decryptTencentTea(inBuf []byte, key []byte) ([]byte, error) {
return out, nil return out, nil
} }
func xor8Bytes(dst, a, b []byte) { func xor8Bytes(dst, a, b []byte) {
for i := 0; i < 8; i++ { for i := 0; i < 8; i++ {
dst[i] = a[i] ^ b[i] dst[i] = a[i] ^ b[i]

View File

@ -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 := deriveKey(raw) got, err := DecryptKey(raw)
if (err != nil) != tt.wantErr { 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 return
} }
if !reflect.DeepEqual(got, want) { 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])) string(got[:32]), string(want[:32]))
} }
}) })

View File

@ -4,23 +4,22 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt"
"io" "io"
"strconv" "strconv"
"strings" "strings"
"unlock-music.dev/cli/algo/common" "github.com/unlock-music/cli/algo/common"
) )
type Decoder struct { type Decoder struct {
raw io.ReadSeeker r io.ReadSeeker
audio io.Reader fileExt string
offset int
audioLen int audioLen int
cipher common.StreamDecoder
decodedKey []byte decodedKey []byte
cipher streamCipher
offset int
rawMetaExtra1 int rawMetaExtra1 int
rawMetaExtra2 int rawMetaExtra2 int
} }
@ -28,113 +27,113 @@ 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, err := d.audio.Read(p) n := len(p)
if n > 0 { if d.audioLen-d.offset <= 0 {
d.cipher.Decrypt(p[:n], d.offset) return 0, io.EOF
d.offset += n } 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
} }
func NewDecoder(r io.ReadSeeker) common.Decoder { d.cipher.Decrypt(p[:m], d.offset)
return &Decoder{raw: r} d.offset += m
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 {
// 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) buf := make([]byte, 16)
if _, err := io.ReadFull(d.raw, buf); err != nil { if _, err := io.ReadFull(d.r, buf); err != nil {
return fmt.Errorf("qmc read header: %w", err) return err
}
_, err := d.r.Seek(0, io.SeekStart)
if err != nil {
return err
} }
d.cipher.Decrypt(buf, 0) d.cipher.Decrypt(buf, 0)
_, ok := common.SniffAll(buf) fileExt, ok := common.SniffAll(buf)
if !ok { if !ok {
return errors.New("qmc: detect file type failed") return errors.New("detect file type failed")
} }
d.fileExt = fileExt
return nil return nil
} }
func (d Decoder) GetFileExt() string {
return d.fileExt
}
func (d *Decoder) searchKey() error { func (d *Decoder) searchKey() error {
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd) fileSizeM4, err := d.r.Seek(-4, io.SeekEnd)
if err != nil { if err != nil {
return err return err
} }
buf, err := io.ReadAll(io.LimitReader(d.raw, 4)) buf, err := io.ReadAll(io.LimitReader(d.r, 4))
if err != nil { if err != nil {
return err return err
} }
if string(buf) == "QTag" { if string(buf) == "QTag" {
return d.readRawMetaQTag() if err := d.readRawMetaQTag(); err != nil {
} else if string(buf) == "STag" { return err
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 <= 0xFFFF && size != 0 { // assume size is key len if size < 0x300 && size != 0 {
return d.readRawKey(int64(size)) return d.readRawKey(int64(size))
} } else {
// try to use default static cipher // try to use default static cipher
d.audioLen = int(fileSizeM4 + 4) d.audioLen = int(fileSizeM4 + 4)
return nil return nil
} }
} }
return nil
}
func (d *Decoder) readRawKey(rawKeyLen int64) 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 { if err != nil {
return err return err
} }
d.audioLen = int(audioLen) 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 { if err != nil {
return err return err
} }
// clean suffix NULs d.decodedKey, err = DecryptKey(rawKeyData)
rawKeyData = bytes.TrimRight(rawKeyData, "\x00")
d.decodedKey, err = deriveKey(rawKeyData)
if err != nil { if err != nil {
return err return err
} }
@ -144,22 +143,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.raw.Seek(-8, io.SeekEnd); err != nil { if _, err := d.r.Seek(-8, io.SeekEnd); err != nil {
return err return err
} }
buf, err := io.ReadAll(io.LimitReader(d.raw, 4)) buf, err := io.ReadAll(io.LimitReader(d.r, 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.raw.Seek(-(8 + rawMetaLen), io.SeekEnd) audioLen, err := d.r.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.raw, rawMetaLen)) rawMetaData, err := io.ReadAll(io.LimitReader(d.r, rawMetaLen))
if err != nil { if err != nil {
return err return err
} }
@ -169,7 +168,7 @@ func (d *Decoder) readRawMetaQTag() error {
return errors.New("invalid raw meta data") return errors.New("invalid raw meta data")
} }
d.decodedKey, err = deriveKey([]byte(items[0])) d.decodedKey, err = DecryptKey([]byte(items[0]))
if err != nil { if err != nil {
return err return err
} }
@ -208,6 +207,57 @@ 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, 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
}

View File

@ -3,54 +3,77 @@ package tm
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt" "github.com/unlock-music/cli/algo/common"
"io"
"unlock-music.dev/cli/algo/common"
) )
var replaceHeader = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70} 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 {
raw io.ReadSeeker file []byte
offset int audio []byte
audio io.Reader 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 { func (d *Decoder) Validate() error {
header := make([]byte, 8) if len(d.file) < 8 {
if _, err := io.ReadFull(d.raw, header); err != nil { return errors.New("invalid file size")
return fmt.Errorf("tm read header: %w", err)
} }
if !bytes.Equal(magicHeader, d.file[:4]) {
if bytes.Equal(magicHeader, header[:len(magicHeader)]) { // replace m4a header return errors.New("not a valid tm file")
d.audio = io.MultiReader(bytes.NewReader(replaceHeader), d.raw) }
d.headerMatch = true
return nil return nil
} }
if _, ok := common.SniffAll(header); ok { // not encrypted func (d *Decoder) Decode() error {
d.audio = io.MultiReader(bytes.NewReader(header), d.raw) d.audio = d.file
if d.headerMatch {
for i := 0; i < 8; i++ {
d.audio[i] = replaceHeader[i]
}
d.audioExt = "m4a"
}
return nil return nil
} }
return errors.New("tm: valid magic header") //goland:noinspection GoUnusedExportedFunction
func NewDecoder(data []byte) common.Decoder {
return &Decoder{file: data}
} }
func (d *Decoder) Read(buf []byte) (int, error) { func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
return d.audio.Read(buf) return func(file []byte) common.Decoder {
return &Decoder{file: file, audioExt: ext}
} }
func NewTmDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{raw: rd}
} }
func init() { func init() {
// QQ Music IOS M4a (replace header) // QQ Music IOS M4a
common.RegisterDecoder("tm2", false, NewTmDecoder) common.RegisterDecoder("tm2", false, DecoderFuncWithExt("m4a"))
common.RegisterDecoder("tm6", false, NewTmDecoder) 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)
} }

View File

@ -3,10 +3,9 @@ package xm
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt" "github.com/unlock-music/cli/algo/common"
"io" "github.com/unlock-music/cli/internal/logging"
"go.uber.org/zap"
"unlock-music.dev/cli/algo/common"
) )
var ( var (
@ -18,19 +17,26 @@ 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 {
rd io.ReadSeeker file []byte
offset int headerLen uint32
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
@ -39,53 +45,62 @@ func (d *Decoder) GetAudioExt() string {
return "" return ""
} }
func NewDecoder(rd io.ReadSeeker) common.Decoder { func (d *Decoder) GetMeta() common.Meta {
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))
return nil return nil
} }
func (d *Decoder) Read(p []byte) (int, error) { func NewDecoder(data []byte) common.Decoder {
n, err := d.rd.Read(p) return &Decoder{file: data}
if n > 0 { }
d.cipher.Decrypt(p[:n], d.offset)
d.offset += n 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() { 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, NewDecoder) common.RegisterDecoder("wav", false, DecoderFuncWithExt("wav"))
common.RegisterDecoder("mp3", false, NewDecoder) common.RegisterDecoder("mp3", false, DecoderFuncWithExt("mp3"))
common.RegisterDecoder("flac", false, NewDecoder) common.RegisterDecoder("flac", false, DecoderFuncWithExt("flac"))
common.RegisterDecoder("m4a", false, NewDecoder) common.RegisterDecoder("m4a", false, DecoderFuncWithExt("m4a"))
} }

View File

@ -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
}
}
}

View File

@ -1,44 +1,34 @@
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"
"unlock-music.dev/cli/algo/common" "github.com/unlock-music/cli/algo/common"
_ "unlock-music.dev/cli/algo/kgm" _ "github.com/unlock-music/cli/algo/kgm"
_ "unlock-music.dev/cli/algo/kwm" _ "github.com/unlock-music/cli/algo/kwm"
_ "unlock-music.dev/cli/algo/ncm" _ "github.com/unlock-music/cli/algo/ncm"
_ "unlock-music.dev/cli/algo/qmc" _ "github.com/unlock-music/cli/algo/qmc"
_ "unlock-music.dev/cli/algo/tm" _ "github.com/unlock-music/cli/algo/tm"
_ "unlock-music.dev/cli/algo/xm" _ "github.com/unlock-music/cli/algo/xm"
"unlock-music.dev/cli/internal/logging" "github.com/unlock-music/cli/internal/logging"
) )
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://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), 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},
@ -49,17 +39,17 @@ func main() {
}, },
Action: appMain, 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, 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 {
logger.Fatal("run app failed", zap.Error(err)) logging.Log().Fatal("run app failed", zap.Error(err))
} }
} }
func printSupportedExtensions() { func printSupportedExtensions() {
var exts []string exts := []string{}
for ext := range common.DecoderRegistry { for ext := range common.DecoderRegistry {
exts = append(exts, ext) exts = append(exts, ext)
} }
@ -125,7 +115,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 {
logger.Fatal("skipping while no suitable decoder") logging.Log().Fatal("skipping while no suitable decoder")
} }
return tryDecFile(input, output, allDec, removeSource) 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) allDec := common.GetDecoder(item.Name(), skipNoop)
if len(allDec) == 0 { 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 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 {
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err)) logging.Log().Error("conversion failed", zap.String("source", item.Name()))
} }
} }
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.Open(inputFile) file, err := os.ReadFile(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 {
logger.Warn("try decode failed", zap.Error(err))
dec = nil
} }
logging.Log().Warn("try decode failed", zap.Error(err))
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 {
header := bytes.NewBuffer(nil) return errors.New("failed while decoding: " + err.Error())
_, err = io.CopyN(header, dec, 16)
if err != nil {
return fmt.Errorf("read header failed: %w", err)
} }
outExt := ".mp3" outData := dec.GetAudioData()
if ext, ok := common.SniffAll(header.Bytes()); ok { outExt := dec.GetAudioExt()
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)
outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) err = os.WriteFile(outPath, outData, 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 {
@ -207,9 +189,9 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu
if err != nil { if err != nil {
return err 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 { } 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 return nil

16
go.mod
View File

@ -1,17 +1,17 @@
module unlock-music.dev/cli module github.com/unlock-music/cli
go 1.17 go 1.17
require ( require (
github.com/urfave/cli/v2 v2.23.5 github.com/ulikunitz/xz v0.5.10
go.uber.org/zap v1.23.0 github.com/urfave/cli/v2 v2.3.0
golang.org/x/crypto v0.3.0 go.uber.org/zap v1.19.1
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
) )
require ( 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/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.uber.org/atomic v1.9.0 // indirect
go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.7.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
) )

63
go.sum
View File

@ -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 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.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= 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/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 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.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=
@ -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/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/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.23.5 h1:xbrU7tAYviSpqeR3X4nEFWUdB/uDZ6DE+HxmRU7Xtyw= github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
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.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/goleak v1.1.11-0.20210813005559-691160354723 h1:sHOAIxRGBp443oHZIPB+HsUGaksVCXVQENPxwTfQdH4=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= 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.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8= go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY= go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY= 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-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=

View 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
)

View File

@ -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()
}

View File

@ -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