mirror of
https://git.unlock-music.dev/um/cli.git
synced 2025-05-23 00:26:19 +08:00
Merge pull request 'kgg 支持' (#122) from feat/kgg into main
Reviewed-on: https://git.unlock-music.dev/um/cli/pulls/122
This commit is contained in:
commit
a968be6063
108
.gitea/workflows/build.yml
Normal file
108
.gitea/workflows/build.yml
Normal file
@ -0,0 +1,108 @@
|
||||
name: Build
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- "**/*.go"
|
||||
- "go.mod"
|
||||
- "go.sum"
|
||||
- ".gitea/workflows/*.yml"
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
types: [ opened, synchronize, reopened ]
|
||||
paths:
|
||||
- "**/*.go"
|
||||
- "go.mod"
|
||||
- "go.sum"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
GOOS:
|
||||
- linux
|
||||
- windows
|
||||
- darwin
|
||||
GOARCH:
|
||||
- "amd64"
|
||||
- "386"
|
||||
- "arm64"
|
||||
|
||||
exclude:
|
||||
- GOOS: darwin
|
||||
GOARCH: "386"
|
||||
|
||||
include:
|
||||
- GOOS: windows
|
||||
BIN_SUFFIX: ".exe"
|
||||
|
||||
steps:
|
||||
- name: Checkout codebase
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go 1.23
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ^1.23
|
||||
|
||||
- name: Setup vars
|
||||
id: vars
|
||||
run: |
|
||||
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
|
||||
echo "::set-output name=git_tag::$(git describe --tags --always)"
|
||||
|
||||
- name: Test
|
||||
run: go test -v ./...
|
||||
|
||||
- name: Build
|
||||
env:
|
||||
GOOS: ${{ matrix.GOOS }}
|
||||
GOARCH: ${{ matrix.GOARCH }}
|
||||
CGO_ENABLED: 0
|
||||
run: go build -v -trimpath -ldflags="-w -s -X main.AppVersion=${{ steps.vars.outputs.git_tag }}" -o um-${{ matrix.GOOS }}-${{ matrix.GOARCH }}${{ matrix.BIN_SUFFIX }} ./cmd/um
|
||||
|
||||
- name: Publish artifact
|
||||
uses: christopherhx/gitea-upload-artifact@v4
|
||||
with:
|
||||
name: um-${{ matrix.GOOS }}-${{ matrix.GOARCH }}
|
||||
path: ./um-${{ matrix.GOOS }}-${{ matrix.GOARCH }}${{ matrix.BIN_SUFFIX }}
|
||||
|
||||
archive:
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
steps:
|
||||
- name: prepare archive
|
||||
run: |
|
||||
mkdir -p dist prepare
|
||||
- name: Download artifacts
|
||||
uses: christopherhx/gitea-download-artifact@v4
|
||||
with:
|
||||
path: prepare
|
||||
pattern: um-*
|
||||
- name: prepare archive
|
||||
run: |
|
||||
for exe in prepare/*/um-*.exe; do
|
||||
zip -9 -j "dist/$(basename "$exe" .exe).zip" "$exe"
|
||||
rm -f "$exe"
|
||||
done
|
||||
for exe in prepare/*/um-*; do
|
||||
tar \
|
||||
--sort=name --format=posix \
|
||||
--pax-option=exthdr.name=%d/PaxHeaders/%f \
|
||||
--pax-option=delete=atime,delete=ctime \
|
||||
--clamp-mtime --mtime='1970-01-01T00:00:00Z' \
|
||||
--numeric-owner --owner=0 --group=0 \
|
||||
--mode=0755 \
|
||||
-cv \
|
||||
-C "$(dirname "$exe")" \
|
||||
"$(basename "$exe")" \
|
||||
| gzip -9 > "dist/$(basename "$exe").tar.gz"
|
||||
rm -f "$exe"
|
||||
done
|
||||
- name: Publish all-in-one archive
|
||||
uses: christopherhx/gitea-upload-artifact@v4
|
||||
with:
|
||||
name: dist-all
|
||||
path: dist
|
67
.github/workflows/build.yml
vendored
67
.github/workflows/build.yml
vendored
@ -1,67 +0,0 @@
|
||||
name: Build
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- "**/*.go"
|
||||
- "go.mod"
|
||||
- "go.sum"
|
||||
- ".github/workflows/*.yml"
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
types: [ opened, synchronize, reopened ]
|
||||
paths:
|
||||
- "**/*.go"
|
||||
- "go.mod"
|
||||
- "go.sum"
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- target: "linux/amd64"
|
||||
GOOS: "linux"
|
||||
GOARCH: "amd64"
|
||||
BIN_SUFFIX: ""
|
||||
- target: "windows/amd64"
|
||||
GOOS: "windows"
|
||||
GOARCH: "amd64"
|
||||
BIN_SUFFIX: ".exe"
|
||||
- target: "darwin/amd64"
|
||||
GOOS: "darwin"
|
||||
GOARCH: "amd64"
|
||||
BIN_SUFFIX: ""
|
||||
|
||||
steps:
|
||||
- name: Checkout codebase
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go 1.x
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: ^1.17
|
||||
|
||||
- name: Setup vars
|
||||
id: vars
|
||||
run: |
|
||||
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
|
||||
echo "::set-output name=git_tag::$(git describe --tags --always)"
|
||||
|
||||
- name: Test
|
||||
run: go test -v ./...
|
||||
|
||||
- name: Build
|
||||
env:
|
||||
GOOS: ${{ matrix.GOOS }}
|
||||
GOARCH: ${{ matrix.GOARCH }}
|
||||
CGO_ENABLED: 0
|
||||
run: go build -v -trimpath -ldflags="-w -s -X main.AppVersion=${{ steps.vars.outputs.git_tag }}" -o um-${{ matrix.GOOS }}-${{ matrix.GOARCH }}${{ matrix.BIN_SUFFIX }} ./cmd/um
|
||||
|
||||
- name: Publish artifact
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: um-${{ matrix.GOOS }}-${{ matrix.GOARCH }}
|
||||
path: ./um-${{ matrix.GOOS }}-${{ matrix.GOARCH }}${{ matrix.BIN_SUFFIX }}
|
84
.github/workflows/release.yml
vendored
84
.github/workflows/release.yml
vendored
@ -1,84 +0,0 @@
|
||||
name: Create Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
|
||||
jobs:
|
||||
create_release:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Get current time
|
||||
id: date
|
||||
run: echo "::set-output name=date::$(date +'%Y/%m/%d')"
|
||||
|
||||
- name: Create release
|
||||
id: release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: ${{ github.ref }}
|
||||
release_name: "Build ${{ steps.date.outputs.date }}"
|
||||
draft: true
|
||||
outputs:
|
||||
upload_url: "${{ steps.release.outputs.upload_url }}"
|
||||
build:
|
||||
needs:
|
||||
- create_release
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- target: "linux/amd64"
|
||||
GOOS: "linux"
|
||||
GOARCH: "amd64"
|
||||
BIN_SUFFIX: ""
|
||||
- target: "windows/amd64"
|
||||
GOOS: "windows"
|
||||
GOARCH: "amd64"
|
||||
BIN_SUFFIX: ".exe"
|
||||
- target: "windows/386"
|
||||
GOOS: "windows"
|
||||
GOARCH: "386"
|
||||
BIN_SUFFIX: ".exe"
|
||||
- target: "darwin/amd64"
|
||||
GOOS: "darwin"
|
||||
GOARCH: "amd64"
|
||||
BIN_SUFFIX: ""
|
||||
|
||||
steps:
|
||||
- name: Checkout codebase
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Setup vars
|
||||
id: vars
|
||||
run: |
|
||||
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
|
||||
echo "::set-output name=git_tag::$(git describe --tags --always)"
|
||||
|
||||
- name: Set up Go 1.x
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: ^1.17
|
||||
|
||||
- name: Build
|
||||
env:
|
||||
GOOS: ${{ matrix.GOOS }}
|
||||
GOARCH: ${{ matrix.GOARCH }}
|
||||
CGO_ENABLED: 0
|
||||
run: go build -trimpath -v -ldflags="-w -s -X main.AppVersion=${{ steps.vars.outputs.git_tag }}" -o um ./cmd/um
|
||||
|
||||
- name: Upload release assets
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ needs.create_release.outputs.upload_url }}
|
||||
asset_path: um
|
||||
asset_name: um-${{ matrix.GOOS }}-${{ matrix.GOARCH }}${{ matrix.BIN_SUFFIX }}
|
||||
asset_content_type: application/octet-stream
|
||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -3,5 +3,7 @@
|
||||
/dist
|
||||
*.exe
|
||||
|
||||
/um
|
||||
/um-*.tar.gz
|
||||
/um-*.zip
|
||||
/.vscode
|
||||
|
@ -15,6 +15,9 @@ type DecoderParams struct {
|
||||
FilePath string // optional, source file path
|
||||
|
||||
Logger *zap.Logger // required
|
||||
|
||||
// KuGou
|
||||
KggDatabasePath string
|
||||
}
|
||||
type NewDecoderFunc func(p *DecoderParams) Decoder
|
||||
|
||||
|
@ -14,10 +14,12 @@ type Decoder struct {
|
||||
offset int
|
||||
|
||||
header header
|
||||
|
||||
KggDatabasePath string
|
||||
}
|
||||
|
||||
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||
return &Decoder{rd: p.Reader}
|
||||
return &Decoder{rd: p.Reader, KggDatabasePath: p.KggDatabasePath}
|
||||
}
|
||||
|
||||
// Validate checks if the file is a valid Kugou (.kgm, .vpr, .kgma) file.
|
||||
@ -34,6 +36,11 @@ func (d *Decoder) Validate() (err error) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("kgm init crypto v3: %w", err)
|
||||
}
|
||||
case 5:
|
||||
d.cipher, err = newKgmCryptoV5(&d.header, d.KggDatabasePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("kgm init crypto v5: %w", err)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("kgm: unsupported crypto version %d", d.header.CryptoVersion)
|
||||
}
|
||||
@ -57,6 +64,7 @@ func (d *Decoder) Read(buf []byte) (int, error) {
|
||||
|
||||
func init() {
|
||||
// Kugou
|
||||
common.RegisterDecoder("kgg", false, NewDecoder)
|
||||
common.RegisterDecoder("kgm", false, NewDecoder)
|
||||
common.RegisterDecoder("kgma", false, NewDecoder)
|
||||
// Viper
|
||||
|
@ -29,6 +29,8 @@ type header struct {
|
||||
CryptoSlot uint32 // 0x18-0x1b: crypto key slot
|
||||
CryptoTestData []byte // 0x1c-0x2b: crypto test data
|
||||
CryptoKey []byte // 0x2c-0x3b: crypto key
|
||||
|
||||
AudioHash string // v5: audio hash
|
||||
}
|
||||
|
||||
func (h *header) FromFile(rd io.ReadSeeker) error {
|
||||
@ -36,29 +38,56 @@ func (h *header) FromFile(rd io.ReadSeeker) error {
|
||||
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(rd)
|
||||
}
|
||||
|
||||
return h.FromBytes(buf)
|
||||
func (h *header) FromBytes(r io.ReadSeeker) error {
|
||||
h.MagicHeader = make([]byte, 16)
|
||||
_, err := r.Read(h.MagicHeader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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]
|
||||
err = binary.Read(r, binary.LittleEndian, &h.AudioOffset)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = binary.Read(r, binary.LittleEndian, &h.CryptoVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = binary.Read(r, binary.LittleEndian, &h.CryptoSlot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.CryptoTestData = make([]byte, 0x10)
|
||||
_, err = r.Read(h.CryptoTestData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.CryptoKey = make([]byte, 0x10)
|
||||
_, err = r.Read(h.CryptoKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if h.CryptoVersion == 5 {
|
||||
r.Seek(0x08, io.SeekCurrent)
|
||||
var audioHashLen uint32 = 0
|
||||
err = binary.Read(r, binary.LittleEndian, &audioHashLen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
audioHashBuffer := make([]byte, audioHashLen)
|
||||
_, err = r.Read(audioHashBuffer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.AudioHash = string(audioHashBuffer)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
30
algo/kgm/kgm_v5.go
Normal file
30
algo/kgm/kgm_v5.go
Normal file
@ -0,0 +1,30 @@
|
||||
package kgm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
"unlock-music.dev/cli/algo/kgm/pc_kugou_db"
|
||||
"unlock-music.dev/cli/algo/qmc"
|
||||
)
|
||||
|
||||
func newKgmCryptoV5(header *header, kggDatabasePath string) (common.StreamDecoder, error) {
|
||||
if header.AudioHash == "" {
|
||||
return nil, fmt.Errorf("kgm v5: missing audio hash")
|
||||
}
|
||||
|
||||
if kggDatabasePath == "" {
|
||||
return nil, fmt.Errorf("kgm v5: missing kgg database path")
|
||||
}
|
||||
|
||||
m, err := pc_kugou_db.CachedDumpEKey(kggDatabasePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kgm v5: decrypt kgg database: %w", err)
|
||||
}
|
||||
ekey, ok := m[header.AudioHash]
|
||||
if !ok || ekey == "" {
|
||||
return nil, fmt.Errorf("kgm v5: ekey missing from db (audio_hash=%s)", header.AudioHash)
|
||||
}
|
||||
|
||||
return qmc.NewQmcCipherDecoderFromEKey([]byte(ekey))
|
||||
}
|
238
algo/kgm/pc_kugou_db/cipher.go
Normal file
238
algo/kgm/pc_kugou_db/cipher.go
Normal file
@ -0,0 +1,238 @@
|
||||
package pc_kugou_db
|
||||
|
||||
// ported from lib_um_crypto_rust:
|
||||
// https://git.unlock-music.dev/um/lib_um_crypto_rust/src/tag/v0.1.10/um_crypto/kgm/src/pc_db_decrypt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/md5"
|
||||
"database/sql"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
const PAGE_SIZE = 0x400
|
||||
|
||||
var SQLITE_HEADER = []byte("SQLite format 3\x00")
|
||||
var DEFAULT_MASTER_KEY = []byte{
|
||||
// master key (0x10 bytes)
|
||||
0x1D, 0x61, 0x31, 0x45, 0xB2, 0x47, 0xBF, 0x7F, 0x3D, 0x18, 0x96, 0x72, 0x14, 0x4F, 0xE4, 0xBF,
|
||||
0x00, 0x00, 0x00, 0x00, // page number (le)
|
||||
0x73, 0x41, 0x6C, 0x54, // fixed value
|
||||
}
|
||||
|
||||
func next_page_iv(seed uint32) uint32 {
|
||||
var left uint32 = seed * 0x9EF4
|
||||
var right uint32 = seed / 0xce26 * 0x7FFFFF07
|
||||
var value uint32 = left - right
|
||||
if value&0x8000_0000 == 0 {
|
||||
return value
|
||||
}
|
||||
return value + 0x7FFF_FF07
|
||||
}
|
||||
|
||||
func derive_page_aes_key(seed uint32) []byte {
|
||||
master_key := make([]byte, len(DEFAULT_MASTER_KEY))
|
||||
copy(master_key, DEFAULT_MASTER_KEY)
|
||||
binary.LittleEndian.PutUint32(master_key[0x10:0x14], seed)
|
||||
digest := md5.Sum(master_key)
|
||||
return digest[:]
|
||||
}
|
||||
|
||||
func derive_page_aes_iv(seed uint32) []byte {
|
||||
iv := make([]byte, 0x10)
|
||||
seed = seed + 1
|
||||
for i := 0; i < 0x10; i += 4 {
|
||||
seed = next_page_iv(seed)
|
||||
binary.LittleEndian.PutUint32(iv[i:i+4], seed)
|
||||
}
|
||||
digest := md5.Sum(iv)
|
||||
return digest[:]
|
||||
}
|
||||
|
||||
func aes128cbcDecryptNoPadding(buffer, key, iv []byte) error {
|
||||
if len(key) != 16 {
|
||||
return fmt.Errorf("invalid key size: %d (must be 16 bytes for AES-128)", len(key))
|
||||
}
|
||||
if len(iv) != aes.BlockSize {
|
||||
return fmt.Errorf("invalid IV size: %d (must be %d bytes)", len(iv), aes.BlockSize)
|
||||
}
|
||||
if len(buffer)%aes.BlockSize != 0 {
|
||||
return fmt.Errorf("ciphertext length must be a multiple of %d bytes", aes.BlockSize)
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(buffer, buffer)
|
||||
return nil
|
||||
}
|
||||
|
||||
func decrypt_db_page(buffer []byte, page_number uint32) error {
|
||||
key := derive_page_aes_key(page_number)
|
||||
iv := derive_page_aes_iv(page_number)
|
||||
|
||||
return aes128cbcDecryptNoPadding(buffer, key, iv)
|
||||
}
|
||||
|
||||
func decrypt_page_1(page []byte) error {
|
||||
if err := validate_page_1_header(page); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Backup expected hdr value
|
||||
|
||||
expected_hdr_value := make([]byte, 8)
|
||||
copy(expected_hdr_value, page[0x10:0x18])
|
||||
|
||||
// Copy encrypted hdr over
|
||||
hdr := page[:0x10]
|
||||
copy(page[0x10:0x18], hdr[0x08:0x10])
|
||||
|
||||
if err := decrypt_db_page(page[0x10:], 1); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Validate header
|
||||
if !bytes.Equal(page[0x10:0x18], expected_hdr_value[:8]) {
|
||||
return fmt.Errorf("decrypt page 1 failed")
|
||||
}
|
||||
|
||||
// Apply SQLite header
|
||||
copy(hdr, SQLITE_HEADER)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validate_page_1_header(header []byte) error {
|
||||
o10 := binary.LittleEndian.Uint32(header[0x10:0x14])
|
||||
o14 := binary.LittleEndian.Uint32(header[0x14:0x18])
|
||||
|
||||
v6 := ((o10 & 0xff) << 8) | ((o10 & 0xff00) << 16)
|
||||
ok := o14 == 0x20204000 && (v6-0x200) <= 0xFE00 && ((v6-1)&v6) == 0
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid page 1 header")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func decryptPcDatabase(buffer []byte) error {
|
||||
db_size := len(buffer)
|
||||
|
||||
// not encrypted
|
||||
if bytes.Equal(buffer[:len(SQLITE_HEADER)], SQLITE_HEADER) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if db_size%PAGE_SIZE != 0 || db_size == 0 {
|
||||
return fmt.Errorf("invalid database size: %d", db_size)
|
||||
}
|
||||
|
||||
last_page := db_size / PAGE_SIZE
|
||||
|
||||
// page 1 is the header
|
||||
if err := decrypt_page_1(buffer[:PAGE_SIZE]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
offset := PAGE_SIZE
|
||||
for page_no := 2; page_no <= last_page; page_no++ {
|
||||
if err := decrypt_db_page(buffer[offset:offset+PAGE_SIZE], uint32(page_no)); err != nil {
|
||||
return err
|
||||
}
|
||||
offset += PAGE_SIZE
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func extractKeyMapping(buffer []byte) (map[string]string, error) {
|
||||
// Create an in-memory SQLite database
|
||||
db, err := sql.Open("sqlite", "file::memory:?mode=memory&cache=shared")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
conn, err := db.Conn(context.Background())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = func() error {
|
||||
defer conn.Close()
|
||||
return conn.Raw(func(driverConn any) error {
|
||||
type serializer interface {
|
||||
Serialize() ([]byte, error)
|
||||
Deserialize([]byte) error
|
||||
}
|
||||
return driverConn.(serializer).Deserialize(buffer)
|
||||
})
|
||||
}()
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to import db: %w", err)
|
||||
}
|
||||
|
||||
conn, err = db.Conn(context.Background())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, err := conn.QueryContext(context.Background(), `
|
||||
select EncryptionKeyId, EncryptionKey from ShareFileItems
|
||||
where EncryptionKey != '' and EncryptionKey is not null
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
m := make(map[string]string)
|
||||
for rows.Next() {
|
||||
var keyId, key string
|
||||
if err := rows.Scan(&keyId, &key); err != nil {
|
||||
continue
|
||||
}
|
||||
m[keyId] = key
|
||||
}
|
||||
|
||||
return m, err
|
||||
}
|
||||
|
||||
var kugouPcDatabaseDumpLock = &sync.Mutex{}
|
||||
var kugouPcDatabaseDump = make(map[string]map[string]string)
|
||||
|
||||
func CachedDumpEKey(dbPath string) (map[string]string, error) {
|
||||
dump, exist := kugouPcDatabaseDump[dbPath]
|
||||
if !exist {
|
||||
kugouPcDatabaseDumpLock.Lock()
|
||||
defer kugouPcDatabaseDumpLock.Unlock()
|
||||
|
||||
if dump, exist = kugouPcDatabaseDump[dbPath]; !exist {
|
||||
buffer, err := os.ReadFile(dbPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = decryptPcDatabase(buffer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dump, err = extractKeyMapping(buffer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
kugouPcDatabaseDump[dbPath] = dump
|
||||
}
|
||||
}
|
||||
|
||||
return dump, nil
|
||||
}
|
22
algo/kgm/pc_kugou_db/cipher_test.go
Normal file
22
algo/kgm/pc_kugou_db/cipher_test.go
Normal file
@ -0,0 +1,22 @@
|
||||
package pc_kugou_db
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDerivePageAESKey_Page0(t *testing.T) {
|
||||
expectedKey := []byte{0x19, 0x62, 0xc0, 0x5f, 0xa2, 0xeb, 0xbe, 0x24, 0x28, 0xff, 0x52, 0x2b, 0x9e, 0x03, 0xea, 0xd4}
|
||||
pageKey := derive_page_aes_key(0)
|
||||
if !reflect.DeepEqual(expectedKey, pageKey) {
|
||||
t.Errorf("Derived AES key for page 0 does not match expected value: got %v, want %v", pageKey, expectedKey)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDerivePageAESIv_Page0(t *testing.T) {
|
||||
expectedIv := []byte{0x05, 0x5a, 0x67, 0x35, 0x93, 0x89, 0x2d, 0xdf, 0x3a, 0xb3, 0xb3, 0xc6, 0x21, 0xc3, 0x48, 0x02}
|
||||
pageKey := derive_page_aes_iv(0)
|
||||
if !reflect.DeepEqual(expectedIv, pageKey) {
|
||||
t.Errorf("Derived AES iv for page 0 does not match expected value: got %v, want %v", pageKey, expectedIv)
|
||||
}
|
||||
}
|
@ -5,12 +5,13 @@ import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
"unlock-music.dev/cli/internal/sniff"
|
||||
)
|
||||
@ -59,6 +60,23 @@ func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||
return &Decoder{raw: p.Reader, params: p, logger: p.Logger}
|
||||
}
|
||||
|
||||
func NewQmcCipherDecoder(key []byte) (common.StreamDecoder, error) {
|
||||
if len(key) > 300 {
|
||||
return newRC4Cipher(key)
|
||||
} else if len(key) != 0 {
|
||||
return newMapCipher(key)
|
||||
}
|
||||
return newStaticCipher(), nil
|
||||
}
|
||||
|
||||
func NewQmcCipherDecoderFromEKey(ekey []byte) (common.StreamDecoder, error) {
|
||||
key, err := deriveKey(ekey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewQmcCipherDecoder(key)
|
||||
}
|
||||
|
||||
func (d *Decoder) Validate() error {
|
||||
// search & derive key
|
||||
err := d.searchKey()
|
||||
@ -67,18 +85,9 @@ func (d *Decoder) Validate() error {
|
||||
}
|
||||
|
||||
// check cipher type and init decode cipher
|
||||
if len(d.decodedKey) > 300 {
|
||||
d.cipher, err = newRC4Cipher(d.decodedKey)
|
||||
d.cipher, err = NewQmcCipherDecoder(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()
|
||||
return fmt.Errorf("qmc init cipher: %w", err)
|
||||
}
|
||||
|
||||
// test with first 16 bytes
|
||||
@ -185,13 +194,9 @@ func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
||||
rawKeyData = bytes.TrimRight(rawKeyData, "\x00")
|
||||
|
||||
d.decodedKey, err = deriveKey(rawKeyData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) readRawMetaQTag() error {
|
||||
// get raw meta data len
|
||||
if _, err := d.raw.Seek(-8, io.SeekEnd); err != nil {
|
||||
|
@ -5,10 +5,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/urfave/cli/v2"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
@ -18,6 +14,11 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/urfave/cli/v2"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"unlock-music.dev/cli/algo/common"
|
||||
_ "unlock-music.dev/cli/algo/kgm"
|
||||
_ "unlock-music.dev/cli/algo/kwm"
|
||||
@ -50,6 +51,7 @@ func main() {
|
||||
&cli.StringFlag{Name: "output", Aliases: []string{"o"}, Usage: "path to output dir", Required: false},
|
||||
&cli.StringFlag{Name: "qmc-mmkv", Aliases: []string{"db"}, Usage: "path to qmc mmkv (.crc file also required)", Required: false},
|
||||
&cli.StringFlag{Name: "qmc-mmkv-key", Aliases: []string{"key"}, Usage: "mmkv password (16 ascii chars)", Required: false},
|
||||
&cli.StringFlag{Name: "kgg-db", Usage: "path to kgg db (win32 kugou v11)", Required: false},
|
||||
&cli.BoolFlag{Name: "remove-source", Aliases: []string{"rs"}, Usage: "remove source file", Required: false, Value: false},
|
||||
&cli.BoolFlag{Name: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true},
|
||||
&cli.BoolFlag{Name: "verbose", Aliases: []string{"V"}, Usage: "verbose logging", Required: false, Value: false},
|
||||
@ -183,10 +185,16 @@ func appMain(c *cli.Context) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
kggDbPath := c.String("kgg-db")
|
||||
if kggDbPath == "" {
|
||||
kggDbPath = filepath.Join(os.Getenv("APPDATA"), "Kugou8", "KGMusicV3.db")
|
||||
}
|
||||
|
||||
proc := &processor{
|
||||
logger: logger,
|
||||
inputDir: inputDir,
|
||||
outputDir: output,
|
||||
kggDbPath: kggDbPath,
|
||||
skipNoopDecoder: c.Bool("skip-noop"),
|
||||
removeSource: c.Bool("remove-source"),
|
||||
updateMetadata: c.Bool("update-metadata"),
|
||||
@ -211,6 +219,8 @@ type processor struct {
|
||||
inputDir string
|
||||
outputDir string
|
||||
|
||||
kggDbPath string
|
||||
|
||||
skipNoopDecoder bool
|
||||
removeSource bool
|
||||
updateMetadata bool
|
||||
@ -346,6 +356,7 @@ func (p *processor) process(inputFile string, allDec []common.DecoderFactory) er
|
||||
Extension: filepath.Ext(inputFile),
|
||||
FilePath: inputFile,
|
||||
Logger: logger,
|
||||
KggDatabasePath: p.kggDbPath,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
13
go.mod
13
go.mod
@ -11,16 +11,25 @@ require (
|
||||
github.com/urfave/cli/v2 v2.27.5
|
||||
go.uber.org/zap v1.27.0
|
||||
golang.org/x/crypto v0.29.0
|
||||
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
|
||||
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394
|
||||
golang.org/x/text v0.20.0
|
||||
unlock-music.dev/mmkv v0.1.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/sys v0.27.0 // indirect
|
||||
golang.org/x/sys v0.31.0 // indirect
|
||||
google.golang.org/protobuf v1.35.2 // indirect
|
||||
modernc.org/libc v1.62.1 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.9.1 // indirect
|
||||
modernc.org/sqlite v1.37.0 // indirect
|
||||
)
|
||||
|
23
go.sum
23
go.sum
@ -4,6 +4,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
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/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
|
||||
@ -14,8 +16,16 @@ github.com/go-flac/flacvorbis v0.2.0 h1:KH0xjpkNTXFER4cszH4zeJxYcrHbUobz/RticWGO
|
||||
github.com/go-flac/flacvorbis v0.2.0/go.mod h1:uIysHOtuU7OLGoCRG92bvnkg7QEqHx19qKRV6K1pBrI=
|
||||
github.com/go-flac/go-flac v1.0.0 h1:6qI9XOVLcO50xpzm3nXvO31BgDgHhnr/p/rER/K/doY=
|
||||
github.com/go-flac/go-flac v1.0.0/go.mod h1:WnZhcpmq4u1UdZMNn9LYSoASpWOCMOoxXxcWEHSzkW8=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
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/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
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/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
|
||||
@ -44,10 +54,15 @@ golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
|
||||
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
|
||||
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
|
||||
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
|
||||
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE7+F/fNFDSXLVYkE/Iw=
|
||||
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
|
||||
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
|
||||
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
|
||||
@ -56,5 +71,13 @@ google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8i
|
||||
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
modernc.org/libc v1.62.1 h1:s0+fv5E3FymN8eJVmnk0llBe6rOxCu/DEU+XygRbS8s=
|
||||
modernc.org/libc v1.62.1/go.mod h1:iXhATfJQLjG3NWy56a6WVU73lWOcdYVxsvwCgoPljuo=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.9.1 h1:V/Z1solwAVmMW1yttq3nDdZPJqV1rM05Ccq6KMSZ34g=
|
||||
modernc.org/memory v1.9.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/sqlite v1.37.0 h1:s1TMe7T3Q3ovQiK2Ouz4Jwh7dw4ZDqbebSDTlSJdfjI=
|
||||
modernc.org/sqlite v1.37.0/go.mod h1:5YiWv+YviqGMuGw4V+PNplcyaJ5v+vQd7TQOgkACoJM=
|
||||
unlock-music.dev/mmkv v0.1.0 h1:hgUHo0gJVoiKZ6bOcFOw2LHFqNiefIe+jb5o0OyL720=
|
||||
unlock-music.dev/mmkv v0.1.0/go.mod h1:qr34SM3x8xRxyUfGzefH/rSi+DUXkQZcSfXY/yfuTeo=
|
||||
|
Loading…
x
Reference in New Issue
Block a user