mirror of
https://git.um-react.app/um/cli.git
synced 2025-11-28 19:53:01 +00:00
Compare commits
178 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2190e0ff5 | ||
|
|
c104a0bc6e | ||
|
|
a163d40fb2 | ||
|
|
40f93b4dd2 | ||
|
|
8c1f40bfe1 | ||
|
|
aea3bd5714 | ||
|
|
e4bfefd0a6 | ||
|
|
b1aa3bacdf | ||
|
|
6ec434f6b1 | ||
|
|
a46428e07c | ||
|
|
57dd4b80b3 | ||
|
|
e06a21123f | ||
|
|
1b62887619 | ||
|
|
35dbfdc5d1 | ||
|
|
f6614973ee | ||
|
|
00fefc1297 | ||
|
|
4671143030 | ||
|
|
9b0455b0fd | ||
|
|
92ad51402e | ||
|
|
589e573b55 | ||
|
|
20b746bee5 | ||
|
|
0a94383ba3 | ||
|
|
369112af01 | ||
|
|
111952199f | ||
|
|
a217170077 | ||
|
|
1a943309fa | ||
|
|
c0649d1246 | ||
|
|
3344db1645 | ||
|
|
791f9c0621 | ||
|
|
896ace49fd | ||
|
|
17cde2a1a5 | ||
|
|
61fba401c7 | ||
|
|
a968be6063 | ||
|
|
000ef4ac13 | ||
|
|
05e5783336 | ||
|
|
006bad8c48 | ||
|
|
380ed78b6b | ||
|
|
208aceb1b5 | ||
|
|
28ca8cef60 | ||
|
|
2b0bd2985e | ||
|
|
72ace9fc62 | ||
|
|
074e4f874f | ||
|
|
2bfb5ffddf | ||
|
|
2c9de7c56c | ||
|
|
b374c11c86 | ||
|
|
6493b2c5fc | ||
|
|
f753b9c67d | ||
|
|
8829a3b3ba | ||
|
|
b2ef27761f | ||
|
|
7f7cb66fe5 | ||
|
|
77729cf653 | ||
|
|
b317b89ae9 | ||
|
|
89b629304e | ||
|
|
c0c3bda9ce | ||
|
|
fa8f7a1565 | ||
|
|
d4d5e5ddf4 | ||
|
|
57c1aa3e54 | ||
|
|
b0998d8c8a | ||
|
|
f819726f3e | ||
|
|
fbad7ec450 | ||
|
|
19bc6c466e | ||
|
|
b9e2a38f82 | ||
|
|
673ea8491f | ||
|
|
0d071a82be | ||
|
|
b8e6196248 | ||
|
|
1323fb9e1a | ||
|
|
36df203bdd | ||
|
|
2afc232eb1 | ||
|
|
2abdd47c9c | ||
|
|
8b59bc026d | ||
|
|
91855f8f5b | ||
|
|
7edd326b95 | ||
|
|
0b3ad0d97c | ||
|
|
c87204c78a | ||
|
|
3630fc0c78 | ||
|
|
b3f7cd33df | ||
|
|
c6fa777db1 | ||
|
|
cab705a130 | ||
|
|
f258e3e8f2 | ||
|
|
59f3759d48 | ||
|
|
db547002f9 | ||
|
|
05a1affa03 | ||
|
|
68e0c06246 | ||
|
|
e010d1b308 | ||
|
|
9b4e3b87ef | ||
|
|
1ccc507f61 | ||
|
|
119a8cf380 | ||
|
|
6179bcf0c7 | ||
|
|
c9ed772ce0 | ||
|
|
9c0acb794e | ||
|
|
bda32bc53e | ||
|
|
3f6566f7a9 | ||
|
|
1c5310ffae | ||
|
|
2941a9ac76 | ||
|
|
1835f9852a | ||
|
|
4f4d8db3d5 | ||
|
|
877f37f01e | ||
|
|
7b12f61a97 | ||
|
|
f6bc911125 | ||
|
|
9ddbd5f702 | ||
|
|
d480c9a2f8 | ||
|
|
907fe4143b | ||
|
|
4ad9a721f0 | ||
|
|
1bbc707dcc | ||
|
|
dfbb807e41 | ||
|
|
a928611a8d | ||
|
|
2754c14fa6 | ||
|
|
8858ff37ad | ||
|
|
d0d9836990 | ||
|
|
1a508fc2b6 | ||
|
|
0acf905d08 | ||
|
|
f3cbd2f3b4 | ||
|
|
fd6f830916 | ||
|
|
02e065aac4 | ||
|
|
8319df6ca3 | ||
|
|
3857ba91a6 | ||
|
|
9856f52070 | ||
|
|
112d9ab28e | ||
|
|
f60f0b3d07 | ||
|
|
3cf542c84c | ||
|
|
9494a535a9 | ||
|
|
138adbf846 | ||
|
|
c878bb8ca4 | ||
|
|
e9e63ce175 | ||
|
|
2c378d4d46 | ||
|
|
21eab14e6c | ||
|
|
0a0179c614 | ||
|
|
12be881d42 | ||
|
|
6f033af336 | ||
|
|
79d00b356f | ||
|
|
f6149c9109 | ||
|
|
3739638ddf | ||
|
|
423767ba63 | ||
|
|
a9c976f47d | ||
|
|
5fbcdb77d4 | ||
|
|
743c672c44 | ||
|
|
9241512f2d | ||
|
|
52e986e644 | ||
|
|
d2019b04ec | ||
|
|
ea3236e14b | ||
|
|
ad64a0f91d | ||
|
|
921f9b2ae6 | ||
|
|
cb948e74df | ||
|
|
7637a91f71 | ||
|
|
e7d360362e | ||
|
|
04320bd45a | ||
|
|
26b580a4b8 | ||
|
|
6c168ee536 | ||
|
|
62a38d5ab4 | ||
|
|
62548955dc | ||
|
|
3794ff3154 | ||
|
|
b22453215f | ||
|
|
81862b26c9 | ||
|
|
110a78433a | ||
|
|
d896925dff | ||
|
|
bd95fdb53b | ||
|
|
f6748d644d | ||
|
|
8e068b9c8d | ||
|
|
14c9d49d46 | ||
|
|
6f1cdc5b4f | ||
|
|
8fd6763b29 | ||
|
|
2880c63bf7 | ||
|
|
67ff0c44cd | ||
|
|
4365628bff | ||
|
|
b275b552ed | ||
|
|
4ecaef1ff9 | ||
|
|
e2fc56ddb2 | ||
|
|
a2c55721cc | ||
|
|
e6ba9f0bfa | ||
|
|
21bd246d6c | ||
|
|
f05ae61aff | ||
|
|
7b37e4dd3c | ||
|
|
9647ca15fd | ||
|
|
f27b73a64c | ||
|
|
2058504384 | ||
|
|
76df8e6c1a | ||
|
|
42d658625f | ||
|
|
def2925900 |
97
.gitea/workflows/build.yml
Normal file
97
.gitea/workflows/build.yml
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
name: Build
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
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"
|
||||||
|
- "arm64"
|
||||||
|
|
||||||
|
include:
|
||||||
|
- GOOS: windows
|
||||||
|
BIN_SUFFIX: ".exe"
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout codebase
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Set up Go 1.25
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: ^1.25
|
||||||
|
|
||||||
|
- 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: Checkout codebase
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
- name: Setup vars
|
||||||
|
id: vars
|
||||||
|
run: |
|
||||||
|
echo "::set-output name=git_tag::$(git describe --tags --always)"
|
||||||
|
- name: prepare archive
|
||||||
|
run: |
|
||||||
|
mkdir -p dist prepare
|
||||||
|
- name: Download artifacts
|
||||||
|
uses: christopherhx/gitea-download-artifact@v4
|
||||||
|
with:
|
||||||
|
path: prepare
|
||||||
|
pattern: um-*
|
||||||
|
- name: repack archive
|
||||||
|
run: |
|
||||||
|
apt-get update
|
||||||
|
apt-get install -y strip-nondeterminism
|
||||||
|
./misc/repack.sh "${{ steps.vars.outputs.git_tag }}"
|
||||||
|
- 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
|
|
||||||
|
|
||||||
11
.gitignore
vendored
11
.gitignore
vendored
@@ -1 +1,12 @@
|
|||||||
.idea
|
.idea
|
||||||
|
|
||||||
|
/dist
|
||||||
|
*.exe
|
||||||
|
|
||||||
|
/um
|
||||||
|
/um-*.tar.gz
|
||||||
|
/um-*.zip
|
||||||
|
/.vscode
|
||||||
|
|
||||||
|
/prepare
|
||||||
|
/dist
|
||||||
|
|||||||
75
CHANGELOG.md
Normal file
75
CHANGELOG.md
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [v0.2.18] - 2025-11-16
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- QMC2: Fix `musicex\0` tag parsing.
|
||||||
|
- MMKV: Improved tolerance for corrupted MMKV file parsing.
|
||||||
|
- Updated project dependencies.
|
||||||
|
|
||||||
|
## [v0.2.17] - 2025-09-09 ⚠️ **(Broken Release)**
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Update RegEx used to extract UDID in plist.
|
||||||
|
|
||||||
|
## [v0.2.16] - 2025-09-09 ⚠️ **(Broken Release)**
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Update RegEx used to extract UDID in plist.
|
||||||
|
|
||||||
|
## [v0.2.15] - 2025-09-09 ⚠️ **(Broken Release)**
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Support MMKV dump in QQMusic Mac 10.x (AppStore version).
|
||||||
|
|
||||||
|
## [v0.2.14] - 2025-09-08 ⚠️ **(Broken Release)**
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Support MMKV dump in QQMusic Mac 10.x.
|
||||||
|
|
||||||
|
## [v0.2.13] - 2025-09-06 ⚠️ **(Broken Release)**
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Updated project namespace and repository URLs to new url
|
||||||
|
- Upgraded Go version requirement to 1.25
|
||||||
|
- Restricted KGG database support to Windows platform only
|
||||||
|
- Enhanced MMKV key extraction logic with improved reliability
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Fixed NCM metadata parsing to properly handle mixed-type artist arrays
|
||||||
|
- Drop i386 targets in CI build
|
||||||
|
|
||||||
|
## [v0.2.12] - 2025-05-07
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- KGG (KGMv5) file format support
|
||||||
|
- Support for `.mflacm` file extension
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Updated default version identifier to "custom" for development builds
|
||||||
|
- Upgraded GoLang version
|
||||||
|
|
||||||
|
## [v0.2.11] - 2024-11-05
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Resolved relative path resolution issues on Windows platforms (#108)
|
||||||
|
- Improved cross-platform compatibility for file path handling
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Historical Versions
|
||||||
|
|
||||||
|
**Note**: This changelog was created starting from v0.2.11. For changes in earlier versions (v0.2.10 and below), please refer to the project's git commit history:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git log --oneline --before="2024-11-05"
|
||||||
|
```
|
||||||
|
|
||||||
|
Or view the complete commit history on the project repository for detailed information about features, fixes, and improvements in previous releases.
|
||||||
33
README.md
33
README.md
@@ -1,22 +1,35 @@
|
|||||||
# Unlock Music Project - CLI Edition
|
# Unlock Music Project - CLI Edition
|
||||||
|
|
||||||
Original: Web Edition https://github.com/ix64/unlock-music
|
Original: Web Edition https://git.um-react.app/um/web
|
||||||
|
|
||||||
- [Release Download](https://github.com/unlock-music/cli/releases/latest)
|
- [](https://git.um-react.app/um/cli/actions?workflow=build.yml)
|
||||||
|
- [Release Download](https://git.um-react.app/um/cli/releases/latest)
|
||||||
|
- [Latest Build](https://git.um-react.app/um/cli/actions)
|
||||||
|
|
||||||
|
> **WARNING**
|
||||||
|
> 在本站 fork 不会起到备份的作用,只会浪费服务器储存空间。如无必要请勿 fork 该仓库。
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- [x] All Algorithm Supported By `ix64/unlock-music`
|
- [x] All Algorithm Supported By `unlock-music/web`
|
||||||
- [ ] Complete Cover Image
|
- [x] Complete Metadata & Cover Image
|
||||||
- [ ] Parse Meta Data
|
|
||||||
- [ ] Complete Meta Data
|
|
||||||
|
|
||||||
## Hou to Build
|
## Release
|
||||||
|
|
||||||
- Requirements: **Golang 1.17**
|
[Latest release](https://git.um-react.app/um/cli/releases/latest).
|
||||||
|
|
||||||
1. Clone this repo `git clone https://github.com/unlock-music/cli && cd cli`
|
## Install from source
|
||||||
2. Build the executable `go build ./cmd/um`
|
|
||||||
|
- Requirements: **Golang 1.23.3**
|
||||||
|
|
||||||
|
1. run `go install git.um-react.app/um/cli/cmd/um@main`
|
||||||
|
|
||||||
|
### Build from repo source
|
||||||
|
|
||||||
|
1. Pull repo source.
|
||||||
|
2. Build with `go build ./cmd/um`.
|
||||||
|
|
||||||
|
It will produce `um` or `um.exe` (Windows).
|
||||||
|
|
||||||
## How to use
|
## How to use
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
package common
|
|
||||||
|
|
||||||
type Decoder interface {
|
|
||||||
Validate() error
|
|
||||||
Decode() error
|
|
||||||
GetCoverImage() []byte
|
|
||||||
GetAudioData() []byte
|
|
||||||
GetAudioExt() string
|
|
||||||
GetMeta() Meta
|
|
||||||
}
|
|
||||||
|
|
||||||
type Meta interface {
|
|
||||||
GetArtists() []string
|
|
||||||
GetTitle() string
|
|
||||||
GetAlbum() string
|
|
||||||
}
|
|
||||||
@@ -1,30 +1,67 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/internal/utils"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NewDecoderFunc func([]byte) Decoder
|
type QMCKeys map[string]string
|
||||||
|
|
||||||
type decoderItem struct {
|
type CryptoParams struct {
|
||||||
noop bool
|
// KuGou kgg database path
|
||||||
decoder NewDecoderFunc
|
KggDbPath string
|
||||||
|
|
||||||
|
// QMC Crypto config
|
||||||
|
QmcKeys QMCKeys
|
||||||
}
|
}
|
||||||
|
|
||||||
var DecoderRegistry = make(map[string][]decoderItem)
|
func (k QMCKeys) Get(key string) (string, bool) {
|
||||||
|
value, ok := k[utils.NormalizeUnicode(key)]
|
||||||
|
return value, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
type DecoderParams struct {
|
||||||
|
Reader io.ReadSeeker // required
|
||||||
|
Extension string // required, source extension, eg. ".mp3"
|
||||||
|
|
||||||
|
FilePath string // optional, source file path
|
||||||
|
|
||||||
|
Logger *zap.Logger // required
|
||||||
|
|
||||||
|
CryptoParams CryptoParams
|
||||||
|
}
|
||||||
|
type NewDecoderFunc func(p *DecoderParams) Decoder
|
||||||
|
|
||||||
|
type DecoderFactory struct {
|
||||||
|
noop bool
|
||||||
|
Suffix string
|
||||||
|
Create NewDecoderFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
var DecoderRegistry []DecoderFactory
|
||||||
|
|
||||||
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
|
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
|
||||||
DecoderRegistry[ext] = append(DecoderRegistry[ext],
|
DecoderRegistry = append(DecoderRegistry,
|
||||||
decoderItem{noop: noop, decoder: dispatchFunc})
|
DecoderFactory{noop: noop, Create: dispatchFunc, Suffix: "." + strings.TrimPrefix(ext, ".")})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDecoder(filename string, skipNoop bool) []DecoderFactory {
|
||||||
|
var result []DecoderFactory
|
||||||
|
// Some extensions contains multiple dots, e.g. ".kgm.flac", hence iterate
|
||||||
|
// all decoders for each extension.
|
||||||
|
name := strings.ToLower(filepath.Base(filename))
|
||||||
|
for _, dec := range DecoderRegistry {
|
||||||
|
if !strings.HasSuffix(name, dec.Suffix) {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
func GetDecoder(filename string, skipNoop bool) (rs []NewDecoderFunc) {
|
|
||||||
ext := strings.ToLower(strings.TrimLeft(filepath.Ext(filename), "."))
|
|
||||||
for _, dec := range DecoderRegistry[ext] {
|
|
||||||
if skipNoop && dec.noop {
|
if skipNoop && dec.noop {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
rs = append(rs, dec.decoder)
|
result = append(result, dec)
|
||||||
}
|
}
|
||||||
return
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
29
algo/common/interface.go
Normal file
29
algo/common/interface.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StreamDecoder interface {
|
||||||
|
Decrypt(buf []byte, offset int)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Decoder interface {
|
||||||
|
Validate() error
|
||||||
|
io.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
type CoverImageGetter interface {
|
||||||
|
GetCoverImage(ctx context.Context) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type AudioMeta interface {
|
||||||
|
GetArtists() []string
|
||||||
|
GetTitle() string
|
||||||
|
GetAlbum() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type AudioMetaGetter interface {
|
||||||
|
GetAudioMeta(ctx context.Context) (AudioMeta, error)
|
||||||
|
}
|
||||||
50
algo/common/meta.go
Normal file
50
algo/common/meta.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type filenameMeta struct {
|
||||||
|
artists []string
|
||||||
|
title string
|
||||||
|
album string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filenameMeta) GetArtists() []string {
|
||||||
|
return f.artists
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filenameMeta) GetTitle() string {
|
||||||
|
return f.title
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filenameMeta) GetAlbum() string {
|
||||||
|
return f.album
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseFilenameMeta(filename string) (meta AudioMeta) {
|
||||||
|
partName := strings.TrimSuffix(filename, path.Ext(filename))
|
||||||
|
items := strings.Split(partName, "-")
|
||||||
|
ret := &filenameMeta{}
|
||||||
|
|
||||||
|
switch len(items) {
|
||||||
|
case 0:
|
||||||
|
// no-op
|
||||||
|
case 1:
|
||||||
|
ret.title = strings.TrimSpace(items[0])
|
||||||
|
default:
|
||||||
|
ret.title = strings.TrimSpace(items[len(items)-1])
|
||||||
|
|
||||||
|
for _, v := range items[:len(items)-1] {
|
||||||
|
artists := strings.FieldsFunc(v, func(r rune) bool {
|
||||||
|
return r == ',' || r == '_'
|
||||||
|
})
|
||||||
|
for _, artist := range artists {
|
||||||
|
ret.artists = append(ret.artists, strings.TrimSpace(artist))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
38
algo/common/meta_test.go
Normal file
38
algo/common/meta_test.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseFilenameMeta(t *testing.T) {
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
wantMeta AudioMeta
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "test1",
|
||||||
|
wantMeta: &filenameMeta{title: "test1"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "周杰伦 - 晴天.flac",
|
||||||
|
wantMeta: &filenameMeta{artists: []string{"周杰伦"}, title: "晴天"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Alan Walker _ Iselin Solheim - Sing Me to Sleep.flac",
|
||||||
|
wantMeta: &filenameMeta{artists: []string{"Alan Walker", "Iselin Solheim"}, title: "Sing Me to Sleep"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Christopher,Madcon - Limousine.flac",
|
||||||
|
wantMeta: &filenameMeta{artists: []string{"Christopher", "Madcon"}, title: "Limousine"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if gotMeta := ParseFilenameMeta(tt.name); !reflect.DeepEqual(gotMeta, tt.wantMeta) {
|
||||||
|
t.Errorf("ParseFilenameMeta() = %v, want %v", gotMeta, tt.wantMeta)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,46 +2,41 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/internal/sniff"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RawDecoder struct {
|
type RawDecoder struct {
|
||||||
file []byte
|
rd io.ReadSeeker
|
||||||
|
|
||||||
audioExt string
|
audioExt string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRawDecoder(file []byte) Decoder {
|
func NewRawDecoder(p *DecoderParams) Decoder {
|
||||||
return &RawDecoder{file: file}
|
return &RawDecoder{rd: p.Reader}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *RawDecoder) Validate() error {
|
func (d *RawDecoder) Validate() error {
|
||||||
for ext, sniffer := range snifferRegistry {
|
header := make([]byte, 16)
|
||||||
if sniffer(d.file) {
|
if _, err := io.ReadFull(d.rd, header); err != nil {
|
||||||
d.audioExt = strings.ToLower(ext)
|
return fmt.Errorf("read file header failed: %v", err)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
if _, err := d.rd.Seek(0, io.SeekStart); err != nil {
|
||||||
return errors.New("audio doesn't recognized")
|
return fmt.Errorf("seek file failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d RawDecoder) Decode() error {
|
var ok bool
|
||||||
|
d.audioExt, ok = sniff.AudioExtension(header)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("raw: sniff audio type failed")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d RawDecoder) GetCoverImage() []byte {
|
func (d *RawDecoder) Read(p []byte) (n int, err error) {
|
||||||
return nil
|
return d.rd.Read(p)
|
||||||
}
|
|
||||||
|
|
||||||
func (d RawDecoder) GetAudioData() []byte {
|
|
||||||
return d.file
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d RawDecoder) GetAudioExt() string {
|
|
||||||
return d.audioExt
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d RawDecoder) GetMeta() Meta {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
package common
|
|
||||||
|
|
||||||
import "bytes"
|
|
||||||
|
|
||||||
type Sniffer func(header []byte) bool
|
|
||||||
|
|
||||||
var snifferRegistry = map[string]Sniffer{
|
|
||||||
".mp3": SnifferMP3,
|
|
||||||
".flac": SnifferFLAC,
|
|
||||||
".ogg": SnifferOGG,
|
|
||||||
".m4a": SnifferM4A,
|
|
||||||
".wav": SnifferWAV,
|
|
||||||
".wma": SnifferWMA,
|
|
||||||
".aac": SnifferAAC,
|
|
||||||
".dff": SnifferDFF,
|
|
||||||
}
|
|
||||||
|
|
||||||
func SniffAll(header []byte) (string, bool) {
|
|
||||||
for ext, sniffer := range snifferRegistry {
|
|
||||||
if sniffer(header) {
|
|
||||||
return ext, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
|
|
||||||
func SnifferM4A(header []byte) bool {
|
|
||||||
return len(header) >= 8 && bytes.Equal([]byte("ftyp"), header[4:8])
|
|
||||||
}
|
|
||||||
|
|
||||||
func SnifferOGG(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("OggS"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func SnifferFLAC(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("fLaC"))
|
|
||||||
}
|
|
||||||
func SnifferMP3(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("ID3"))
|
|
||||||
}
|
|
||||||
func SnifferWAV(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("RIFF"))
|
|
||||||
}
|
|
||||||
func SnifferWMA(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00\xaa\x00\x62\xce\x6c"))
|
|
||||||
}
|
|
||||||
func SnifferAAC(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte{0xFF, 0xF1})
|
|
||||||
}
|
|
||||||
|
|
||||||
// SnifferDFF sniff a DSDIFF format
|
|
||||||
// reference to: https://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf
|
|
||||||
func SnifferDFF(header []byte) bool {
|
|
||||||
return bytes.HasPrefix(header, []byte("FRM8"))
|
|
||||||
}
|
|
||||||
123
algo/kgm/kgm.go
123
algo/kgm/kgm.go
@@ -1,94 +1,75 @@
|
|||||||
package kgm
|
package kgm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"fmt"
|
||||||
"encoding/binary"
|
"io"
|
||||||
"errors"
|
|
||||||
"github.com/unlock-music/cli/algo/common"
|
|
||||||
"github.com/unlock-music/cli/internal/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
"git.um-react.app/um/cli/algo/common"
|
||||||
vprHeader = []byte{
|
|
||||||
0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43,
|
|
||||||
0x91, 0xAA, 0xBD, 0xD0, 0x7A, 0xF5, 0x36, 0x31}
|
|
||||||
kgmHeader = []byte{
|
|
||||||
0x7C, 0xD5, 0x32, 0xEB, 0x86, 0x02, 0x7F, 0x4B,
|
|
||||||
0xA8, 0xAF, 0xA6, 0x8E, 0x0F, 0xFF, 0x99, 0x14}
|
|
||||||
ErrKgmMagicHeader = errors.New("kgm/vpr magic header not matched")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
file []byte
|
rd io.ReadSeeker
|
||||||
key []byte
|
|
||||||
isVpr bool
|
cipher common.StreamDecoder
|
||||||
audio []byte
|
offset int
|
||||||
|
|
||||||
|
header header
|
||||||
|
|
||||||
|
KggDatabasePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecoder(file []byte) common.Decoder {
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
return &Decoder{
|
return &Decoder{rd: p.Reader, KggDatabasePath: p.CryptoParams.KggDbPath}
|
||||||
file: file,
|
}
|
||||||
}
|
|
||||||
|
// Validate checks if the file is a valid Kugou (.kgm, .vpr, .kgma) file.
|
||||||
|
// rd will be seeked to the beginning of the encrypted audio.
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepare for read
|
||||||
|
if _, err := d.rd.Seek(int64(d.header.AudioOffset), io.SeekStart); err != nil {
|
||||||
|
return fmt.Errorf("kgm seek to audio: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Decoder) GetCoverImage() []byte {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Decoder) GetAudioData() []byte {
|
func (d *Decoder) Read(buf []byte) (int, error) {
|
||||||
return d.audio
|
n, err := d.rd.Read(buf)
|
||||||
|
if n > 0 {
|
||||||
|
d.cipher.Decrypt(buf[:n], d.offset)
|
||||||
|
d.offset += n
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Decoder) GetAudioExt() string {
|
|
||||||
return "" // use sniffer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d Decoder) GetMeta() common.Meta {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) Validate() error {
|
|
||||||
if bytes.Equal(kgmHeader, d.file[:len(kgmHeader)]) {
|
|
||||||
d.isVpr = false
|
|
||||||
} else if bytes.Equal(vprHeader, d.file[:len(vprHeader)]) {
|
|
||||||
d.isVpr = true
|
|
||||||
} else {
|
|
||||||
return ErrKgmMagicHeader
|
|
||||||
}
|
|
||||||
|
|
||||||
d.key = d.file[0x1c:0x2c]
|
|
||||||
d.key = append(d.key, 0x00)
|
|
||||||
_ = d.file[0x2c:0x3c] //todo: key2
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) Decode() error {
|
|
||||||
headerLen := binary.LittleEndian.Uint32(d.file[0x10:0x14])
|
|
||||||
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("kgg", false, NewDecoder)
|
||||||
common.RegisterDecoder("kgm", false, NewDecoder)
|
common.RegisterDecoder("kgm", false, NewDecoder)
|
||||||
common.RegisterDecoder("kgma", false, NewDecoder)
|
common.RegisterDecoder("kgma", false, NewDecoder)
|
||||||
// Viper
|
// Viper
|
||||||
common.RegisterDecoder("vpr", false, NewDecoder)
|
common.RegisterDecoder("vpr", false, NewDecoder)
|
||||||
|
// Kugou Android
|
||||||
|
common.RegisterDecoder("kgm.flac", false, NewDecoder)
|
||||||
|
common.RegisterDecoder("vpr.flac", false, NewDecoder)
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
93
algo/kgm/kgm_header.go
Normal file
93
algo/kgm/kgm_header.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
AudioHash string // v5: audio hash
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.FromBytes(rd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *header) FromBytes(r io.ReadSeeker) error {
|
||||||
|
h.MagicHeader = make([]byte, 16)
|
||||||
|
_, err := r.Read(h.MagicHeader)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !bytes.Equal(kgmHeader, h.MagicHeader) && !bytes.Equal(vprHeader, h.MagicHeader) {
|
||||||
|
return ErrKgmMagicHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
55
algo/kgm/kgm_v3.go
Normal file
55
algo/kgm/kgm_v3.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package kgm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.um-react.app/um/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
|
||||||
|
}
|
||||||
30
algo/kgm/kgm_v5.go
Normal file
30
algo/kgm/kgm_v5.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package kgm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"git.um-react.app/um/cli/algo/kgm/pc_kugou_db"
|
||||||
|
"git.um-react.app/um/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))
|
||||||
|
}
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
package kgm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
_ "embed"
|
|
||||||
"github.com/ulikunitz/xz"
|
|
||||||
"github.com/unlock-music/cli/internal/logging"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
var maskDiffVpr = []byte{
|
|
||||||
0x25, 0xDF, 0xE8, 0xA6, 0x75, 0x1E, 0x75, 0x0E,
|
|
||||||
0x2F, 0x80, 0xF3, 0x2D, 0xB8, 0xB6, 0xE3, 0x11,
|
|
||||||
0x00}
|
|
||||||
|
|
||||||
var maskV2PreDef = []byte{
|
|
||||||
0xB8, 0xD5, 0x3D, 0xB2, 0xE9, 0xAF, 0x78, 0x8C, 0x83, 0x33, 0x71, 0x51, 0x76, 0xA0, 0xCD, 0x37,
|
|
||||||
0x2F, 0x3E, 0x35, 0x8D, 0xA9, 0xBE, 0x98, 0xB7, 0xE7, 0x8C, 0x22, 0xCE, 0x5A, 0x61, 0xDF, 0x68,
|
|
||||||
0x69, 0x89, 0xFE, 0xA5, 0xB6, 0xDE, 0xA9, 0x77, 0xFC, 0xC8, 0xBD, 0xBD, 0xE5, 0x6D, 0x3E, 0x5A,
|
|
||||||
0x36, 0xEF, 0x69, 0x4E, 0xBE, 0xE1, 0xE9, 0x66, 0x1C, 0xF3, 0xD9, 0x02, 0xB6, 0xF2, 0x12, 0x9B,
|
|
||||||
0x44, 0xD0, 0x6F, 0xB9, 0x35, 0x89, 0xB6, 0x46, 0x6D, 0x73, 0x82, 0x06, 0x69, 0xC1, 0xED, 0xD7,
|
|
||||||
0x85, 0xC2, 0x30, 0xDF, 0xA2, 0x62, 0xBE, 0x79, 0x2D, 0x62, 0x62, 0x3D, 0x0D, 0x7E, 0xBE, 0x48,
|
|
||||||
0x89, 0x23, 0x02, 0xA0, 0xE4, 0xD5, 0x75, 0x51, 0x32, 0x02, 0x53, 0xFD, 0x16, 0x3A, 0x21, 0x3B,
|
|
||||||
0x16, 0x0F, 0xC3, 0xB2, 0xBB, 0xB3, 0xE2, 0xBA, 0x3A, 0x3D, 0x13, 0xEC, 0xF6, 0x01, 0x45, 0x84,
|
|
||||||
0xA5, 0x70, 0x0F, 0x93, 0x49, 0x0C, 0x64, 0xCD, 0x31, 0xD5, 0xCC, 0x4C, 0x07, 0x01, 0x9E, 0x00,
|
|
||||||
0x1A, 0x23, 0x90, 0xBF, 0x88, 0x1E, 0x3B, 0xAB, 0xA6, 0x3E, 0xC4, 0x73, 0x47, 0x10, 0x7E, 0x3B,
|
|
||||||
0x5E, 0xBC, 0xE3, 0x00, 0x84, 0xFF, 0x09, 0xD4, 0xE0, 0x89, 0x0F, 0x5B, 0x58, 0x70, 0x4F, 0xFB,
|
|
||||||
0x65, 0xD8, 0x5C, 0x53, 0x1B, 0xD3, 0xC8, 0xC6, 0xBF, 0xEF, 0x98, 0xB0, 0x50, 0x4F, 0x0F, 0xEA,
|
|
||||||
0xE5, 0x83, 0x58, 0x8C, 0x28, 0x2C, 0x84, 0x67, 0xCD, 0xD0, 0x9E, 0x47, 0xDB, 0x27, 0x50, 0xCA,
|
|
||||||
0xF4, 0x63, 0x63, 0xE8, 0x97, 0x7F, 0x1B, 0x4B, 0x0C, 0xC2, 0xC1, 0x21, 0x4C, 0xCC, 0x58, 0xF5,
|
|
||||||
0x94, 0x52, 0xA3, 0xF3, 0xD3, 0xE0, 0x68, 0xF4, 0x00, 0x23, 0xF3, 0x5E, 0x0A, 0x7B, 0x93, 0xDD,
|
|
||||||
0xAB, 0x12, 0xB2, 0x13, 0xE8, 0x84, 0xD7, 0xA7, 0x9F, 0x0F, 0x32, 0x4C, 0x55, 0x1D, 0x04, 0x36,
|
|
||||||
0x52, 0xDC, 0x03, 0xF3, 0xF9, 0x4E, 0x42, 0xE9, 0x3D, 0x61, 0xEF, 0x7C, 0xB6, 0xB3, 0x93, 0x50,
|
|
||||||
}
|
|
||||||
|
|
||||||
//go:embed kgm.v2.mask
|
|
||||||
var maskV2Xz []byte
|
|
||||||
|
|
||||||
var maskV2 []byte
|
|
||||||
var fullMaskLen int
|
|
||||||
var initMaskOK = false
|
|
||||||
|
|
||||||
//todo: decompress mask on demand
|
|
||||||
func initMask() {
|
|
||||||
if initMaskOK {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
maskReader, err := xz.NewReader(bytes.NewReader(maskV2Xz))
|
|
||||||
if err != nil {
|
|
||||||
logging.Log().Fatal("load kgm mask failed", zap.Error(err))
|
|
||||||
}
|
|
||||||
maskV2, err = io.ReadAll(maskReader)
|
|
||||||
if err != nil {
|
|
||||||
logging.Log().Fatal("load kgm mask failed", zap.Error(err))
|
|
||||||
}
|
|
||||||
fullMaskLen = len(maskV2) * 16
|
|
||||||
initMaskOK = true
|
|
||||||
}
|
|
||||||
7
algo/kgm/pc_kugou_db/cipher_default.go
Normal file
7
algo/kgm/pc_kugou_db/cipher_default.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
//go:build !windows
|
||||||
|
|
||||||
|
package pc_kugou_db
|
||||||
|
|
||||||
|
func CachedDumpEKey(dbPath string) (map[string]string, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
238
algo/kgm/pc_kugou_db/cipher_windows.go
Normal file
238
algo/kgm/pc_kugou_db/cipher_windows.go
Normal file
@@ -0,0 +1,238 @@
|
|||||||
|
package pc_kugou_db
|
||||||
|
|
||||||
|
// ported from lib_um_crypto_rust:
|
||||||
|
// https://git.um-react.app/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 deriveIvSeed(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
|
||||||
|
}
|
||||||
|
|
||||||
|
// derivePageIv generates a 16-byte IV for database page.
|
||||||
|
func derivePageIv(page uint32) []byte {
|
||||||
|
iv := make([]byte, 0x10)
|
||||||
|
page = page + 1
|
||||||
|
for i := 0; i < 0x10; i += 4 {
|
||||||
|
page = deriveIvSeed(page)
|
||||||
|
binary.LittleEndian.PutUint32(iv[i:i+4], page)
|
||||||
|
}
|
||||||
|
digest := md5.Sum(iv)
|
||||||
|
return digest[:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// derivePageKey generates a 16-byte AES key for database page.
|
||||||
|
func derivePageKey(page uint32) []byte {
|
||||||
|
masterKey := make([]byte, len(DEFAULT_MASTER_KEY))
|
||||||
|
copy(masterKey, DEFAULT_MASTER_KEY)
|
||||||
|
binary.LittleEndian.PutUint32(masterKey[0x10:0x14], page)
|
||||||
|
digest := md5.Sum(masterKey)
|
||||||
|
return digest[:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// aes128cbcDecryptNoPadding decrypts the given buffer using AES-128-CBC with no padding.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// decryptPage decrypts a single database page using AES-128-CBC (no padding).
|
||||||
|
// page start from 1.
|
||||||
|
func decryptPage(buffer []byte, page uint32) error {
|
||||||
|
key := derivePageKey(page)
|
||||||
|
iv := derivePageIv(page)
|
||||||
|
|
||||||
|
return aes128cbcDecryptNoPadding(buffer, key, iv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decryptPage1(buffer []byte) error {
|
||||||
|
if err := validateFirstPageHeader(buffer); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backup expected header, swap cipher text
|
||||||
|
expectedHeader := make([]byte, 8)
|
||||||
|
copy(expectedHeader, buffer[0x10:0x18])
|
||||||
|
copy(buffer[0x10:0x18], buffer[0x08:0x10])
|
||||||
|
if err := decryptPage(buffer[0x10:], 1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate header
|
||||||
|
if !bytes.Equal(buffer[0x10:0x18], expectedHeader) {
|
||||||
|
return fmt.Errorf("decrypt page 1 failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore SQLite file header
|
||||||
|
copy(buffer[:0x10], SQLITE_HEADER)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateFirstPageHeader(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 decryptDatabase(buffer []byte) error {
|
||||||
|
dbSize := len(buffer)
|
||||||
|
|
||||||
|
// not encrypted
|
||||||
|
if bytes.Equal(buffer[:len(SQLITE_HEADER)], SQLITE_HEADER) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if dbSize%PAGE_SIZE != 0 || dbSize == 0 {
|
||||||
|
return fmt.Errorf("invalid database size: %d", dbSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := decryptPage1(buffer[:PAGE_SIZE]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
offset := PAGE_SIZE
|
||||||
|
lastPage := uint32(dbSize / PAGE_SIZE)
|
||||||
|
|
||||||
|
var pageNumber uint32
|
||||||
|
for pageNumber = 2; pageNumber <= lastPage; pageNumber++ {
|
||||||
|
if err := decryptPage(buffer[offset:offset+PAGE_SIZE], uint32(pageNumber)); 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 = decryptDatabase(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_windows_test.go
Normal file
22
algo/kgm/pc_kugou_db/cipher_windows_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 := derivePageKey(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 := derivePageIv(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)
|
||||||
|
}
|
||||||
|
}
|
||||||
121
algo/kwm/kwm.go
121
algo/kwm/kwm.go
@@ -2,109 +2,80 @@ package kwm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/unlock-music/cli/algo/common"
|
"fmt"
|
||||||
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
const magicHeader1 = "yeelion-kuwo-tme"
|
||||||
magicHeader = []byte{
|
const magicHeader2 = "yeelion-kuwo\x00\x00\x00\x00"
|
||||||
0x79, 0x65, 0x65, 0x6C, 0x69, 0x6F, 0x6E, 0x2D,
|
|
||||||
0x6B, 0x75, 0x77, 0x6F, 0x2D, 0x74, 0x6D, 0x65}
|
|
||||||
ErrKwFileSize = errors.New("kwm invalid file size")
|
|
||||||
ErrKwMagicHeader = errors.New("kwm magic header not matched")
|
|
||||||
)
|
|
||||||
|
|
||||||
const keyPreDefined = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
|
const keyPreDefined = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
file []byte
|
rd io.ReadSeeker
|
||||||
|
|
||||||
|
cipher common.StreamDecoder
|
||||||
|
offset int
|
||||||
|
|
||||||
key []byte
|
|
||||||
outputExt string
|
outputExt string
|
||||||
bitrate int
|
bitrate int
|
||||||
mask []byte
|
|
||||||
|
|
||||||
audio []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetCoverImage() []byte {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetAudioData() []byte {
|
|
||||||
return d.audio
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) GetAudioExt() string {
|
func (d *Decoder) GetAudioExt() string {
|
||||||
return "." + d.outputExt
|
return "." + d.outputExt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) GetMeta() common.Meta {
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
return nil
|
return &Decoder{rd: p.Reader}
|
||||||
}
|
|
||||||
|
|
||||||
func NewDecoder(data []byte) common.Decoder {
|
|
||||||
//todo: Notice the input data will be changed for now
|
|
||||||
return &Decoder{file: data}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate checks if the file is a valid Kuwo .kw file.
|
||||||
|
// rd will be seeked to the beginning of the encrypted audio.
|
||||||
func (d *Decoder) Validate() error {
|
func (d *Decoder) Validate() error {
|
||||||
lenData := len(d.file)
|
header := make([]byte, 0x400) // kwm header is fixed to 1024 bytes
|
||||||
if lenData < 1024 {
|
_, err := io.ReadFull(d.rd, header)
|
||||||
return ErrKwFileSize
|
|
||||||
}
|
|
||||||
if !bytes.Equal(magicHeader, d.file[:16]) {
|
|
||||||
return ErrKwMagicHeader
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateMask(key []byte) []byte {
|
|
||||||
keyInt := binary.LittleEndian.Uint64(key)
|
|
||||||
keyStr := strconv.FormatUint(keyInt, 10)
|
|
||||||
keyStrTrim := padOrTruncate(keyStr, 32)
|
|
||||||
mask := make([]byte, 32)
|
|
||||||
for i := 0; i < 32; i++ {
|
|
||||||
mask[i] = keyPreDefined[i] ^ keyStrTrim[i]
|
|
||||||
}
|
|
||||||
return mask
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) parseBitrateAndType() {
|
|
||||||
bitType := string(bytes.TrimRight(d.file[0x30:0x38], string(byte(0))))
|
|
||||||
charPos := 0
|
|
||||||
for charPos = range bitType {
|
|
||||||
if !unicode.IsNumber(rune(bitType[charPos])) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var err error
|
|
||||||
d.bitrate, err = strconv.Atoi(bitType[:charPos])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.bitrate = 0
|
return fmt.Errorf("kwm read header: %w", err)
|
||||||
}
|
|
||||||
d.outputExt = strings.ToLower(bitType[charPos:])
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Decode() error {
|
// check magic header, 0x00 - 0x0F
|
||||||
d.parseBitrateAndType()
|
magicHeader := header[:0x10]
|
||||||
|
if !bytes.Equal([]byte(magicHeader1), magicHeader) &&
|
||||||
d.mask = generateMask(d.file[0x18:0x20])
|
!bytes.Equal([]byte(magicHeader2), magicHeader) {
|
||||||
|
return errors.New("kwm magic header not matched")
|
||||||
d.audio = d.file[1024:]
|
|
||||||
dataLen := len(d.audio)
|
|
||||||
for i := 0; i < dataLen; i++ {
|
|
||||||
d.audio[i] ^= d.mask[i&0x1F] //equals: [i % 32]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
d.cipher = newKwmCipher(header[0x18:0x20]) // Crypto Key, 0x18 - 0x1F
|
||||||
|
d.bitrate, d.outputExt = parseBitrateAndType(header[0x30:0x38]) // Bitrate & File Extension, 0x30 - 0x38
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseBitrateAndType(header []byte) (int, string) {
|
||||||
|
tmp := strings.TrimRight(string(header), "\x00")
|
||||||
|
sep := strings.IndexFunc(tmp, func(r rune) bool {
|
||||||
|
return !unicode.IsDigit(r)
|
||||||
|
})
|
||||||
|
|
||||||
|
bitrate, _ := strconv.Atoi(tmp[:sep]) // just ignore the error
|
||||||
|
outputExt := strings.ToLower(tmp[sep:])
|
||||||
|
return bitrate, outputExt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) Read(b []byte) (int, error) {
|
||||||
|
n, err := d.rd.Read(b)
|
||||||
|
if n > 0 {
|
||||||
|
d.cipher.Decrypt(b[:n], d.offset)
|
||||||
|
d.offset += n
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
func padOrTruncate(raw string, length int) string {
|
func padOrTruncate(raw string, length int) string {
|
||||||
lenRaw := len(raw)
|
lenRaw := len(raw)
|
||||||
out := raw
|
out := raw
|
||||||
|
|||||||
31
algo/kwm/kwm_cipher.go
Normal file
31
algo/kwm/kwm_cipher.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package kwm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type kwmCipher struct {
|
||||||
|
mask []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func newKwmCipher(key []byte) *kwmCipher {
|
||||||
|
return &kwmCipher{mask: generateMask(key)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateMask(key []byte) []byte {
|
||||||
|
keyInt := binary.LittleEndian.Uint64(key)
|
||||||
|
keyStr := strconv.FormatUint(keyInt, 10)
|
||||||
|
keyStrTrim := padOrTruncate(keyStr, 32)
|
||||||
|
mask := make([]byte, 32)
|
||||||
|
for i := 0; i < 32; i++ {
|
||||||
|
mask[i] = keyPreDefined[i] ^ keyStrTrim[i]
|
||||||
|
}
|
||||||
|
return mask
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c kwmCipher) Decrypt(buf []byte, offset int) {
|
||||||
|
for i := range buf {
|
||||||
|
buf[i] ^= c.mask[(offset+i)&0x1F] // equivalent: [i % 32]
|
||||||
|
}
|
||||||
|
}
|
||||||
103
algo/ncm/meta.go
103
algo/ncm/meta.go
@@ -1,25 +1,32 @@
|
|||||||
package ncm
|
package ncm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/unlock-music/cli/algo/common"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RawMeta interface {
|
type ncmMeta interface {
|
||||||
common.Meta
|
common.AudioMeta
|
||||||
|
|
||||||
|
// GetFormat return the audio format, e.g. mp3, flac
|
||||||
GetFormat() string
|
GetFormat() string
|
||||||
|
|
||||||
|
// GetAlbumImageURL return the album image url
|
||||||
GetAlbumImageURL() string
|
GetAlbumImageURL() string
|
||||||
}
|
}
|
||||||
type RawMetaMusic struct {
|
|
||||||
|
type ncmMetaMusic struct {
|
||||||
|
logger *zap.Logger
|
||||||
|
|
||||||
Format string `json:"format"`
|
Format string `json:"format"`
|
||||||
MusicID int `json:"musicId"`
|
|
||||||
MusicName string `json:"musicName"`
|
MusicName string `json:"musicName"`
|
||||||
Artist [][]interface{} `json:"artist"`
|
Artist interface{} `json:"artist"`
|
||||||
Album string `json:"album"`
|
Album string `json:"album"`
|
||||||
AlbumID int `json:"albumId"`
|
|
||||||
AlbumPicDocID interface{} `json:"albumPicDocId"`
|
AlbumPicDocID interface{} `json:"albumPicDocId"`
|
||||||
AlbumPic string `json:"albumPic"`
|
AlbumPic string `json:"albumPic"`
|
||||||
MvID int `json:"mvId"`
|
|
||||||
Flag int `json:"flag"`
|
Flag int `json:"flag"`
|
||||||
Bitrate int `json:"bitrate"`
|
Bitrate int `json:"bitrate"`
|
||||||
Duration int `json:"duration"`
|
Duration int `json:"duration"`
|
||||||
@@ -27,37 +34,71 @@ type RawMetaMusic struct {
|
|||||||
TransNames []interface{} `json:"transNames"`
|
TransNames []interface{} `json:"transNames"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaMusic) GetAlbumImageURL() string {
|
func newNcmMetaMusic(logger *zap.Logger) *ncmMetaMusic {
|
||||||
return m.AlbumPic
|
ncm := new(ncmMetaMusic)
|
||||||
}
|
ncm.logger = logger.With(zap.String("module", "ncmMetaMusic"))
|
||||||
func (m RawMetaMusic) GetArtists() (artists []string) {
|
return ncm
|
||||||
for _, artist := range m.Artist {
|
|
||||||
for _, item := range artist {
|
|
||||||
name, ok := item.(string)
|
|
||||||
if ok {
|
|
||||||
artists = append(artists, name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaMusic) GetTitle() string {
|
func (m *ncmMetaMusic) GetAlbumImageURL() string {
|
||||||
|
return m.AlbumPic
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ncmMetaMusic) GetArtists() []string {
|
||||||
|
m.logger.Debug("ncm artists raw", zap.Any("artists", m.Artist))
|
||||||
|
var artists []string
|
||||||
|
switch v := m.Artist.(type) {
|
||||||
|
|
||||||
|
// Case 1: Handles the format [['artistA'], ['artistB']]
|
||||||
|
case [][]string:
|
||||||
|
for _, artistSlice := range v {
|
||||||
|
artists = append(artists, artistSlice...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 2: Handles the simple format "artistA"
|
||||||
|
// Ref: https://git.unlock-music.dev/um/cli/issues/78
|
||||||
|
case string:
|
||||||
|
artists = []string{v}
|
||||||
|
|
||||||
|
// Case 3: Handles the mixed-type format [['artistA', 12345], ['artistB', 67890]]
|
||||||
|
// This is the key fix for correctly parsing artist info from certain files.
|
||||||
|
case []interface{}:
|
||||||
|
for _, item := range v {
|
||||||
|
if innerSlice, ok := item.([]interface{}); ok {
|
||||||
|
if len(innerSlice) > 0 {
|
||||||
|
// Assume the first element is the artist's name.
|
||||||
|
if artistName, ok := innerSlice[0].(string); ok {
|
||||||
|
artists = append(artists, artistName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Log a warning if the artist type is unexpected and not handled.
|
||||||
|
m.logger.Warn("unexpected artist type", zap.Any("artists", m.Artist))
|
||||||
|
}
|
||||||
|
|
||||||
|
return artists
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ncmMetaMusic) GetTitle() string {
|
||||||
return m.MusicName
|
return m.MusicName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaMusic) GetAlbum() string {
|
func (m *ncmMetaMusic) GetAlbum() string {
|
||||||
return m.Album
|
return m.Album
|
||||||
}
|
}
|
||||||
func (m RawMetaMusic) GetFormat() string {
|
|
||||||
|
func (m *ncmMetaMusic) GetFormat() string {
|
||||||
return m.Format
|
return m.Format
|
||||||
}
|
}
|
||||||
|
|
||||||
//goland:noinspection SpellCheckingInspection
|
//goland:noinspection SpellCheckingInspection
|
||||||
type RawMetaDJ struct {
|
type ncmMetaDJ struct {
|
||||||
ProgramID int `json:"programId"`
|
ProgramID int `json:"programId"`
|
||||||
ProgramName string `json:"programName"`
|
ProgramName string `json:"programName"`
|
||||||
MainMusic RawMetaMusic `json:"mainMusic"`
|
MainMusic ncmMetaMusic `json:"mainMusic"`
|
||||||
DjID int `json:"djId"`
|
DjID int `json:"djId"`
|
||||||
DjName string `json:"djName"`
|
DjName string `json:"djName"`
|
||||||
DjAvatarURL string `json:"djAvatarUrl"`
|
DjAvatarURL string `json:"djAvatarUrl"`
|
||||||
@@ -79,32 +120,32 @@ type RawMetaDJ struct {
|
|||||||
RadioPurchaseCount int `json:"radioPurchaseCount"`
|
RadioPurchaseCount int `json:"radioPurchaseCount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaDJ) GetArtists() []string {
|
func (m *ncmMetaDJ) GetArtists() []string {
|
||||||
if m.DjName != "" {
|
if m.DjName != "" {
|
||||||
return []string{m.DjName}
|
return []string{m.DjName}
|
||||||
}
|
}
|
||||||
return m.MainMusic.GetArtists()
|
return m.MainMusic.GetArtists()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaDJ) GetTitle() string {
|
func (m *ncmMetaDJ) GetTitle() string {
|
||||||
if m.ProgramName != "" {
|
if m.ProgramName != "" {
|
||||||
return m.ProgramName
|
return m.ProgramName
|
||||||
}
|
}
|
||||||
return m.MainMusic.GetTitle()
|
return m.MainMusic.GetTitle()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaDJ) GetAlbum() string {
|
func (m *ncmMetaDJ) GetAlbum() string {
|
||||||
if m.Brand != "" {
|
if m.Brand != "" {
|
||||||
return m.Brand
|
return m.Brand
|
||||||
}
|
}
|
||||||
return m.MainMusic.GetAlbum()
|
return m.MainMusic.GetAlbum()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaDJ) GetFormat() string {
|
func (m *ncmMetaDJ) GetFormat() string {
|
||||||
return m.MainMusic.GetFormat()
|
return m.MainMusic.GetFormat()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaDJ) GetAlbumImageURL() string {
|
func (m *ncmMetaDJ) GetAlbumImageURL() string {
|
||||||
if strings.HasPrefix(m.MainMusic.GetAlbumImageURL(), "http") {
|
if strings.HasPrefix(m.MainMusic.GetAlbumImageURL(), "http") {
|
||||||
return m.MainMusic.GetAlbumImageURL()
|
return m.MainMusic.GetAlbumImageURL()
|
||||||
}
|
}
|
||||||
|
|||||||
288
algo/ncm/ncm.go
288
algo/ncm/ncm.go
@@ -2,203 +2,208 @@ package ncm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/unlock-music/cli/algo/common"
|
"fmt"
|
||||||
"github.com/unlock-music/cli/internal/logging"
|
"io"
|
||||||
"github.com/unlock-music/cli/internal/utils"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"git.um-react.app/um/cli/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const magicHeader = "CTENFDAM"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
magicHeader = []byte{
|
|
||||||
0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D}
|
|
||||||
keyCore = []byte{
|
keyCore = []byte{
|
||||||
0x68, 0x7a, 0x48, 0x52, 0x41, 0x6d, 0x73, 0x6f,
|
0x68, 0x7a, 0x48, 0x52, 0x41, 0x6d, 0x73, 0x6f,
|
||||||
0x35, 0x6b, 0x49, 0x6e, 0x62, 0x61, 0x78, 0x57}
|
0x35, 0x6b, 0x49, 0x6e, 0x62, 0x61, 0x78, 0x57,
|
||||||
|
}
|
||||||
keyMeta = []byte{
|
keyMeta = []byte{
|
||||||
0x23, 0x31, 0x34, 0x6C, 0x6A, 0x6B, 0x5F, 0x21,
|
0x23, 0x31, 0x34, 0x6C, 0x6A, 0x6B, 0x5F, 0x21,
|
||||||
0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28}
|
0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewDecoder(data []byte) common.Decoder {
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
return &Decoder{
|
return &Decoder{rd: p.Reader, logger: p.Logger.With(zap.String("module", "ncm"))}
|
||||||
file: data,
|
|
||||||
fileLen: uint32(len(data)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
file []byte
|
logger *zap.Logger
|
||||||
fileLen uint32
|
rd io.ReadSeeker // rd is the original file reader
|
||||||
|
|
||||||
key []byte
|
offset int
|
||||||
box []byte
|
cipher common.StreamDecoder
|
||||||
|
|
||||||
metaRaw []byte
|
metaRaw []byte
|
||||||
metaType string
|
metaType string
|
||||||
meta RawMeta
|
meta ncmMeta
|
||||||
|
|
||||||
cover []byte
|
cover []byte
|
||||||
audio []byte
|
|
||||||
|
|
||||||
offsetKey uint32
|
|
||||||
offsetMeta uint32
|
|
||||||
offsetCover uint32
|
|
||||||
offsetAudio uint32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate checks if the file is a valid Netease .ncm file.
|
||||||
|
// rd will be seeked to the beginning of the encrypted audio.
|
||||||
func (d *Decoder) Validate() error {
|
func (d *Decoder) Validate() error {
|
||||||
if !bytes.Equal(magicHeader, d.file[:len(magicHeader)]) {
|
if err := d.validateMagicHeader(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := d.rd.Seek(2, io.SeekCurrent); err != nil { // 2 bytes gap
|
||||||
|
return fmt.Errorf("ncm seek file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
keyData, err := d.readKeyData()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := d.readMetaData(); err != nil {
|
||||||
|
return fmt.Errorf("read meta date failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := d.rd.Seek(5, io.SeekCurrent); err != nil { // 5 bytes gap
|
||||||
|
return fmt.Errorf("ncm seek gap: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := d.readCoverData(); err != nil {
|
||||||
|
return fmt.Errorf("parse ncm cover file failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := d.parseMeta(); err != nil {
|
||||||
|
return fmt.Errorf("parse meta failed: %w (raw json=%s)", err, string(d.metaRaw))
|
||||||
|
}
|
||||||
|
|
||||||
|
d.cipher = newNcmCipher(keyData)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) validateMagicHeader() error {
|
||||||
|
header := make([]byte, len(magicHeader)) // 0x00 - 0x07
|
||||||
|
if _, err := d.rd.Read(header); err != nil {
|
||||||
|
return fmt.Errorf("ncm read magic header: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal([]byte(magicHeader), header) {
|
||||||
return errors.New("ncm magic header not match")
|
return errors.New("ncm magic header not match")
|
||||||
}
|
}
|
||||||
d.offsetKey = 8 + 2
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) readKeyData() error {
|
func (d *Decoder) readKeyData() ([]byte, error) {
|
||||||
if d.offsetKey == 0 || d.offsetKey+4 > d.fileLen {
|
bKeyLen := make([]byte, 4) //
|
||||||
return errors.New("invalid cover file offset")
|
if _, err := io.ReadFull(d.rd, bKeyLen); err != nil {
|
||||||
|
return nil, fmt.Errorf("ncm read key length: %w", err)
|
||||||
}
|
}
|
||||||
bKeyLen := d.file[d.offsetKey : d.offsetKey+4]
|
|
||||||
iKeyLen := binary.LittleEndian.Uint32(bKeyLen)
|
iKeyLen := binary.LittleEndian.Uint32(bKeyLen)
|
||||||
d.offsetMeta = d.offsetKey + 4 + iKeyLen
|
|
||||||
|
|
||||||
bKeyRaw := make([]byte, iKeyLen)
|
bKeyRaw := make([]byte, iKeyLen)
|
||||||
|
if _, err := io.ReadFull(d.rd, bKeyRaw); err != nil {
|
||||||
|
return nil, fmt.Errorf("ncm read key data: %w", err)
|
||||||
|
}
|
||||||
for i := uint32(0); i < iKeyLen; i++ {
|
for i := uint32(0); i < iKeyLen; i++ {
|
||||||
bKeyRaw[i] = d.file[i+4+d.offsetKey] ^ 0x64
|
bKeyRaw[i] ^= 0x64
|
||||||
}
|
}
|
||||||
|
|
||||||
d.key = utils.PKCS7UnPadding(utils.DecryptAes128Ecb(bKeyRaw, keyCore))[17:]
|
return utils.PKCS7UnPadding(utils.DecryptAES128ECB(bKeyRaw, keyCore))[17:], nil
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) readMetaData() error {
|
func (d *Decoder) readMetaData() error {
|
||||||
if d.offsetMeta == 0 || d.offsetMeta+4 > d.fileLen {
|
bMetaLen := make([]byte, 4) //
|
||||||
return errors.New("invalid meta file offset")
|
if _, err := io.ReadFull(d.rd, bMetaLen); err != nil {
|
||||||
|
return fmt.Errorf("ncm read key length: %w", err)
|
||||||
}
|
}
|
||||||
bMetaLen := d.file[d.offsetMeta : d.offsetMeta+4]
|
|
||||||
iMetaLen := binary.LittleEndian.Uint32(bMetaLen)
|
iMetaLen := binary.LittleEndian.Uint32(bMetaLen)
|
||||||
d.offsetCover = d.offsetMeta + 4 + iMetaLen
|
|
||||||
if iMetaLen == 0 {
|
if iMetaLen == 0 {
|
||||||
return errors.New("no any meta file found")
|
return nil // no meta data
|
||||||
}
|
}
|
||||||
|
|
||||||
// Why sub 22: Remove "163 key(Don't modify):"
|
bMetaRaw := make([]byte, iMetaLen)
|
||||||
bKeyRaw := make([]byte, iMetaLen-22)
|
if _, err := io.ReadFull(d.rd, bMetaRaw); err != nil {
|
||||||
for i := uint32(0); i < iMetaLen-22; i++ {
|
return fmt.Errorf("ncm read meta data: %w", err)
|
||||||
bKeyRaw[i] = d.file[d.offsetMeta+4+22+i] ^ 0x63
|
}
|
||||||
|
bMetaRaw = bMetaRaw[22:] // skip "163 key(Don't modify):"
|
||||||
|
for i := 0; i < len(bMetaRaw); i++ {
|
||||||
|
bMetaRaw[i] ^= 0x63
|
||||||
}
|
}
|
||||||
|
|
||||||
cipherText, err := base64.StdEncoding.DecodeString(string(bKeyRaw))
|
cipherText, err := base64.StdEncoding.DecodeString(string(bMetaRaw))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("decode ncm meta failed: " + err.Error())
|
return errors.New("decode ncm meta failed: " + err.Error())
|
||||||
}
|
}
|
||||||
metaRaw := utils.PKCS7UnPadding(utils.DecryptAes128Ecb(cipherText, keyMeta))
|
metaRaw := utils.PKCS7UnPadding(utils.DecryptAES128ECB(cipherText, keyMeta))
|
||||||
sepIdx := bytes.IndexRune(metaRaw, ':')
|
sep := bytes.IndexByte(metaRaw, ':')
|
||||||
if sepIdx == -1 {
|
if sep == -1 {
|
||||||
return errors.New("invalid ncm meta file")
|
return errors.New("invalid ncm meta file")
|
||||||
}
|
}
|
||||||
|
|
||||||
d.metaType = string(metaRaw[:sepIdx])
|
d.metaType = string(metaRaw[:sep])
|
||||||
d.metaRaw = metaRaw[sepIdx+1:]
|
d.metaRaw = metaRaw[sep+1:]
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) buildKeyBox() {
|
func (d *Decoder) readCoverData() error {
|
||||||
box := make([]byte, 256)
|
bCoverFrameLen := make([]byte, 4)
|
||||||
for i := 0; i < 256; i++ {
|
if _, err := io.ReadFull(d.rd, bCoverFrameLen); err != nil {
|
||||||
box[i] = byte(i)
|
return fmt.Errorf("ncm read cover length: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
keyLen := len(d.key)
|
coverFrameStartOffset, err := d.rd.Seek(0, io.SeekCurrent)
|
||||||
var j byte
|
if err != nil {
|
||||||
for i := 0; i < 256; i++ {
|
return fmt.Errorf("ncm fetch cover frame start offset: %w", err)
|
||||||
j = box[i] + j + d.key[i%keyLen]
|
|
||||||
box[i], box[j] = box[j], box[i]
|
|
||||||
}
|
}
|
||||||
|
coverFrameLen := binary.LittleEndian.Uint32(bCoverFrameLen)
|
||||||
|
|
||||||
d.box = make([]byte, 256)
|
bCoverLen := make([]byte, 4)
|
||||||
var _i byte
|
if _, err := io.ReadFull(d.rd, bCoverLen); err != nil {
|
||||||
for i := 0; i < 256; i++ {
|
return fmt.Errorf("ncm read cover length: %w", err)
|
||||||
_i = byte(i + 1)
|
|
||||||
si := box[_i]
|
|
||||||
sj := box[_i+si]
|
|
||||||
d.box[i] = box[si+sj]
|
|
||||||
}
|
}
|
||||||
|
iCoverLen := binary.LittleEndian.Uint32(bCoverLen)
|
||||||
|
|
||||||
|
coverBuf := make([]byte, iCoverLen)
|
||||||
|
if _, err := io.ReadFull(d.rd, coverBuf); err != nil {
|
||||||
|
return fmt.Errorf("ncm read cover data: %w", err)
|
||||||
|
}
|
||||||
|
d.cover = coverBuf
|
||||||
|
|
||||||
|
offsetAudioData := coverFrameStartOffset + int64(coverFrameLen) + 4
|
||||||
|
_, err = d.rd.Seek(offsetAudioData, io.SeekStart)
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) parseMeta() error {
|
func (d *Decoder) parseMeta() error {
|
||||||
switch d.metaType {
|
switch d.metaType {
|
||||||
case "music":
|
case "music":
|
||||||
d.meta = new(RawMetaMusic)
|
d.meta = newNcmMetaMusic(d.logger)
|
||||||
return json.Unmarshal(d.metaRaw, d.meta)
|
return json.Unmarshal(d.metaRaw, d.meta)
|
||||||
case "dj":
|
case "dj":
|
||||||
d.meta = new(RawMetaDJ)
|
d.meta = new(ncmMetaDJ)
|
||||||
return json.Unmarshal(d.metaRaw, d.meta)
|
return json.Unmarshal(d.metaRaw, d.meta)
|
||||||
default:
|
default:
|
||||||
return errors.New("unknown ncm meta type: " + d.metaType)
|
return errors.New("unknown ncm meta type: " + d.metaType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) readCoverData() error {
|
func (d *Decoder) Read(buf []byte) (int, error) {
|
||||||
if d.offsetCover == 0 || d.offsetCover+13 > d.fileLen {
|
n, err := d.rd.Read(buf)
|
||||||
return errors.New("invalid cover file offset")
|
if n > 0 {
|
||||||
|
d.cipher.Decrypt(buf[:n], d.offset)
|
||||||
|
d.offset += n
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
coverLenStart := d.offsetCover + 5 + 4
|
func (d *Decoder) GetAudioExt() string {
|
||||||
bCoverLen := d.file[coverLenStart : coverLenStart+4]
|
|
||||||
|
|
||||||
iCoverLen := binary.LittleEndian.Uint32(bCoverLen)
|
|
||||||
d.offsetAudio = coverLenStart + 4 + iCoverLen
|
|
||||||
if iCoverLen == 0 {
|
|
||||||
return errors.New("no any cover file found")
|
|
||||||
}
|
|
||||||
d.cover = d.file[coverLenStart+4 : 4+coverLenStart+iCoverLen]
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) readAudioData() error {
|
|
||||||
if d.offsetAudio == 0 || d.offsetAudio > d.fileLen {
|
|
||||||
return errors.New("invalid audio offset")
|
|
||||||
}
|
|
||||||
audioRaw := d.file[d.offsetAudio:]
|
|
||||||
audioLen := len(audioRaw)
|
|
||||||
d.audio = make([]byte, audioLen)
|
|
||||||
for i := uint32(0); i < uint32(audioLen); i++ {
|
|
||||||
d.audio[i] = d.box[i&0xff] ^ audioRaw[i]
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) Decode() error {
|
|
||||||
if err := d.readKeyData(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
d.buildKeyBox()
|
|
||||||
|
|
||||||
err := d.readMetaData()
|
|
||||||
if err == nil {
|
|
||||||
err = d.parseMeta()
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
logging.Log().Warn("parse ncm meta file failed", zap.Error(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
err = d.readCoverData()
|
|
||||||
if err != nil {
|
|
||||||
logging.Log().Warn("parse ncm cover file failed", zap.Error(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return d.readAudioData()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d Decoder) GetAudioExt() string {
|
|
||||||
if d.meta != nil {
|
if d.meta != nil {
|
||||||
if format := d.meta.GetFormat(); format != "" {
|
if format := d.meta.GetFormat(); format != "" {
|
||||||
return "." + d.meta.GetFormat()
|
return "." + d.meta.GetFormat()
|
||||||
@@ -207,41 +212,40 @@ func (d Decoder) GetAudioExt() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Decoder) GetAudioData() []byte {
|
func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
|
||||||
return d.audio
|
if d.cover != nil {
|
||||||
|
return d.cover, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Decoder) GetCoverImage() []byte {
|
if d.meta == nil {
|
||||||
if d.cover != nil {
|
return nil, errors.New("ncm meta not found")
|
||||||
return d.cover
|
|
||||||
}
|
}
|
||||||
{
|
|
||||||
imgURL := d.meta.GetAlbumImageURL()
|
imgURL := d.meta.GetAlbumImageURL()
|
||||||
if d.meta != nil && !strings.HasPrefix(imgURL, "http") {
|
if !strings.HasPrefix(imgURL, "http") {
|
||||||
return nil
|
return nil, nil // no cover image
|
||||||
}
|
}
|
||||||
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 {
|
||||||
logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
|
return nil, fmt.Errorf("ncm download image failed: %w", err)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
logging.Log().Warn("download image failed", zap.String("http", resp.Status),
|
return nil, fmt.Errorf("ncm download image failed: unexpected http status %s", resp.Status)
|
||||||
zap.String("url", imgURL))
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
data, err := ioutil.ReadAll(resp.Body)
|
d.cover, err = io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
|
return nil, fmt.Errorf("ncm download image failed: %w", err)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Decoder) GetMeta() common.Meta {
|
return d.cover, nil
|
||||||
return d.meta
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) GetAudioMeta(_ context.Context) (common.AudioMeta, error) {
|
||||||
|
return d.meta, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
42
algo/ncm/ncm_cipher.go
Normal file
42
algo/ncm/ncm_cipher.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package ncm
|
||||||
|
|
||||||
|
type ncmCipher struct {
|
||||||
|
key []byte
|
||||||
|
box []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func newNcmCipher(key []byte) *ncmCipher {
|
||||||
|
return &ncmCipher{
|
||||||
|
key: key,
|
||||||
|
box: buildKeyBox(key),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ncmCipher) Decrypt(buf []byte, offset int) {
|
||||||
|
for i := 0; i < len(buf); i++ {
|
||||||
|
buf[i] ^= c.box[(i+offset)&0xff]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildKeyBox(key []byte) []byte {
|
||||||
|
box := make([]byte, 256)
|
||||||
|
for i := 0; i < 256; i++ {
|
||||||
|
box[i] = byte(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
var j byte
|
||||||
|
for i := 0; i < 256; i++ {
|
||||||
|
j = box[i] + j + key[i%len(key)]
|
||||||
|
box[i], box[j] = box[j], box[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := make([]byte, 256)
|
||||||
|
var _i byte
|
||||||
|
for i := 0; i < 256; i++ {
|
||||||
|
_i = byte(i + 1)
|
||||||
|
si := box[_i]
|
||||||
|
sj := box[_i+si]
|
||||||
|
ret[i] = box[si+sj]
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package qmc
|
|
||||||
|
|
||||||
type streamCipher interface {
|
|
||||||
Decrypt(buf []byte, offset int)
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,7 @@ type mapCipher struct {
|
|||||||
size int
|
size int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMapCipher(key []byte) (*mapCipher, error) {
|
func newMapCipher(key []byte) (*mapCipher, error) {
|
||||||
if len(key) == 0 {
|
if len(key) == 0 {
|
||||||
return nil, errors.New("qmc/cipher_map: invalid key size")
|
return nil, errors.New("qmc/cipher_map: invalid key size")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ func Test_mapCipher_Decrypt(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("load testing data failed: %s", err)
|
t.Fatalf("load testing data failed: %s", err)
|
||||||
}
|
}
|
||||||
c, err := NewMapCipher(key)
|
c, err := newMapCipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("init mapCipher failed: %s", err)
|
t.Errorf("init mapCipher failed: %s", err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -9,20 +9,19 @@ type rc4Cipher struct {
|
|||||||
box []byte
|
box []byte
|
||||||
key []byte
|
key []byte
|
||||||
hash uint32
|
hash uint32
|
||||||
boxTmp []byte
|
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")
|
||||||
}
|
}
|
||||||
|
|
||||||
var c = rc4Cipher{key: key}
|
var c = rc4Cipher{key: key, n: n}
|
||||||
c.box = make([]byte, n)
|
c.box = make([]byte, n)
|
||||||
c.boxTmp = make([]byte, n)
|
|
||||||
|
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
c.box[i] = byte(i)
|
c.box[i] = byte(i)
|
||||||
@@ -39,7 +38,7 @@ func NewRC4Cipher(key []byte) (*rc4Cipher, error) {
|
|||||||
|
|
||||||
func (c *rc4Cipher) getHashBase() {
|
func (c *rc4Cipher) getHashBase() {
|
||||||
c.hash = 1
|
c.hash = 1
|
||||||
for i := 0; i < len(c.key); i++ {
|
for i := 0; i < c.n; i++ {
|
||||||
v := uint32(c.key[i])
|
v := uint32(c.key[i])
|
||||||
if v == 0 {
|
if v == 0 {
|
||||||
continue
|
continue
|
||||||
@@ -52,7 +51,10 @@ func (c *rc4Cipher) getHashBase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const rc4SegmentSize = 5120
|
const (
|
||||||
|
rc4SegmentSize = 5120
|
||||||
|
rc4FirstSegmentSize = 128
|
||||||
|
)
|
||||||
|
|
||||||
func (c *rc4Cipher) Decrypt(src []byte, offset int) {
|
func (c *rc4Cipher) Decrypt(src []byte, offset int) {
|
||||||
toProcess := len(src)
|
toProcess := len(src)
|
||||||
@@ -64,10 +66,10 @@ func (c *rc4Cipher) Decrypt(src []byte, offset int) {
|
|||||||
return toProcess == 0
|
return toProcess == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if offset < 128 {
|
if offset < rc4FirstSegmentSize {
|
||||||
blockSize := toProcess
|
blockSize := toProcess
|
||||||
if blockSize > 128-offset {
|
if blockSize > rc4FirstSegmentSize-offset {
|
||||||
blockSize = 128 - offset
|
blockSize = rc4FirstSegmentSize - offset
|
||||||
}
|
}
|
||||||
c.encFirstSegment(src[:blockSize], offset)
|
c.encFirstSegment(src[:blockSize], offset)
|
||||||
if markProcess(blockSize) {
|
if markProcess(blockSize) {
|
||||||
@@ -80,8 +82,7 @@ func (c *rc4Cipher) Decrypt(src []byte, offset int) {
|
|||||||
if blockSize > rc4SegmentSize-offset%rc4SegmentSize {
|
if blockSize > rc4SegmentSize-offset%rc4SegmentSize {
|
||||||
blockSize = rc4SegmentSize - offset%rc4SegmentSize
|
blockSize = rc4SegmentSize - offset%rc4SegmentSize
|
||||||
}
|
}
|
||||||
k := src[processed : processed+blockSize]
|
c.encASegment(src[processed:processed+blockSize], offset)
|
||||||
c.encASegment(k, offset)
|
|
||||||
if markProcess(blockSize) {
|
if markProcess(blockSize) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -96,38 +97,28 @@ func (c *rc4Cipher) Decrypt(src []byte, offset int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (c *rc4Cipher) encFirstSegment(buf []byte, offset int) {
|
func (c *rc4Cipher) encFirstSegment(buf []byte, offset int) {
|
||||||
n := len(c.box)
|
|
||||||
for i := 0; i < len(buf); i++ {
|
for i := 0; i < len(buf); i++ {
|
||||||
idx1 := offset + i
|
buf[i] ^= c.key[c.getSegmentSkip(offset+i)]
|
||||||
segmentID := int(c.key[idx1%n])
|
|
||||||
idx2 := int(float64(c.hash) / float64((idx1+1)*segmentID) * 100.0)
|
|
||||||
buf[i] ^= c.key[idx2%n]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *rc4Cipher) encASegment(buf []byte, offset int) {
|
func (c *rc4Cipher) encASegment(buf []byte, offset int) {
|
||||||
n := len(c.box)
|
box := make([]byte, c.n)
|
||||||
copy(c.boxTmp, c.box)
|
copy(box, c.box)
|
||||||
|
|
||||||
segmentID := (offset / rc4SegmentSize) & 0x1FF
|
|
||||||
|
|
||||||
if n <= segmentID {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
idx2 := int64(float64(c.hash) /
|
|
||||||
float64((offset/rc4SegmentSize+1)*int(c.key[segmentID])) *
|
|
||||||
100.0)
|
|
||||||
skipLen := int((idx2 & 0x1FF) + int64(offset%rc4SegmentSize))
|
|
||||||
|
|
||||||
j, k := 0, 0
|
j, k := 0, 0
|
||||||
|
|
||||||
|
skipLen := (offset % rc4SegmentSize) + c.getSegmentSkip(offset/rc4SegmentSize)
|
||||||
for i := -skipLen; i < len(buf); i++ {
|
for i := -skipLen; i < len(buf); i++ {
|
||||||
j = (j + 1) % n
|
j = (j + 1) % c.n
|
||||||
k = (int(c.boxTmp[j]) + k) % n
|
k = (int(box[j]) + k) % c.n
|
||||||
c.boxTmp[j], c.boxTmp[k] = c.boxTmp[k], c.boxTmp[j]
|
box[j], box[k] = box[k], box[j]
|
||||||
if i >= 0 {
|
if i >= 0 {
|
||||||
buf[i] ^= c.boxTmp[int(c.boxTmp[j])+int(c.boxTmp[k])%n]
|
buf[i] ^= box[(int(box[j])+int(box[k]))%c.n]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (c *rc4Cipher) getSegmentSkip(id int) int {
|
||||||
|
seed := int(c.key[id%c.n])
|
||||||
|
idx := int64(float64(c.hash) / float64((id+1)*seed) * 100.0)
|
||||||
|
return int(idx % int64(c.n))
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,16 +6,17 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadTestRC4CipherData() ([]byte, []byte, []byte, error) {
|
func loadTestRC4CipherData(name string) ([]byte, []byte, []byte, error) {
|
||||||
key, err := os.ReadFile("./testdata/mflac0_rc4_key.bin")
|
prefix := "./testdata/" + name
|
||||||
|
key, err := os.ReadFile(prefix + "_key.bin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
raw, err := os.ReadFile("./testdata/mflac0_rc4_raw.bin")
|
raw, err := os.ReadFile(prefix + "_raw.bin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
target, err := os.ReadFile("./testdata/mflac0_rc4_target.bin")
|
target, err := os.ReadFile(prefix + "_target.bin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -23,12 +24,20 @@ func loadTestRC4CipherData() ([]byte, []byte, []byte, error) {
|
|||||||
return key, raw, target, nil
|
return key, raw, target, nil
|
||||||
}
|
}
|
||||||
func Test_rc4Cipher_Decrypt(t *testing.T) {
|
func Test_rc4Cipher_Decrypt(t *testing.T) {
|
||||||
key, raw, target, err := loadTestRC4CipherData()
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"mflac0_rc4", false},
|
||||||
|
{"mflac_rc4", false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
key, raw, target, err := loadTestRC4CipherData(tt.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("load testing data failed: %s", err)
|
t.Fatalf("load testing data failed: %s", err)
|
||||||
}
|
}
|
||||||
t.Run("overall", 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
|
||||||
@@ -38,16 +47,32 @@ func Test_rc4Cipher_Decrypt(t *testing.T) {
|
|||||||
t.Error("overall")
|
t.Error("overall")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
func BenchmarkRc4Cipher_Decrypt(b *testing.B) {
|
||||||
|
key, raw, _, err := loadTestRC4CipherData("mflac0_rc4")
|
||||||
|
if err != nil {
|
||||||
|
b.Fatalf("load testing data failed: %s", err)
|
||||||
|
}
|
||||||
|
c, err := newRC4Cipher(key)
|
||||||
|
if err != nil {
|
||||||
|
b.Errorf("init rc4Cipher failed: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
c.Decrypt(raw, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Test_rc4Cipher_encFirstSegment(t *testing.T) {
|
func Test_rc4Cipher_encFirstSegment(t *testing.T) {
|
||||||
key, raw, target, err := loadTestRC4CipherData()
|
key, raw, target, err := loadTestRC4CipherData("mflac0_rc4")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
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
|
||||||
@@ -60,13 +85,13 @@ func Test_rc4Cipher_encFirstSegment(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_rc4Cipher_encASegment(t *testing.T) {
|
func Test_rc4Cipher_encASegment(t *testing.T) {
|
||||||
key, raw, target, err := loadTestRC4CipherData()
|
key, raw, target, err := loadTestRC4CipherData("mflac0_rc4")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("load testing data failed: %s", err)
|
t.Fatalf("load testing data failed: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
@@ -77,7 +102,7 @@ func Test_rc4Cipher_encASegment(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run("simple-block(5120~10240)", func(t *testing.T) {
|
t.Run("simple-block(5120~10240)", func(t *testing.T) {
|
||||||
c, err := NewRC4Cipher(key)
|
c, err := newRC4Cipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("init rc4Cipher failed: %s", err)
|
t.Errorf("init rc4Cipher failed: %s", err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package qmc
|
package qmc
|
||||||
|
|
||||||
func NewStaticCipher() *staticCipher {
|
func newStaticCipher() *staticCipher {
|
||||||
return &defaultStaticCipher
|
return &defaultStaticCipher
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
146
algo/qmc/client/base.go
Normal file
146
algo/qmc/client/base.go
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type QQMusic struct {
|
||||||
|
http *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) rpcDoRequest(ctx context.Context, reqBody any) ([]byte, error) {
|
||||||
|
reqBodyBuf, err := json.Marshal(reqBody)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcDoRequest] marshal request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
const endpointURL = "https://u.y.qq.com/cgi-bin/musicu.fcg"
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost,
|
||||||
|
endpointURL+fmt.Sprintf("?pcachetime=%d", time.Now().Unix()),
|
||||||
|
bytes.NewReader(reqBodyBuf),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcDoRequest] create request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Accept", "*/*")
|
||||||
|
req.Header.Set("Accept-Language", "zh-CN")
|
||||||
|
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)")
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
// req.Header.Set("Accept-Encoding", "gzip, deflate")
|
||||||
|
|
||||||
|
reqp, err := c.http.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcDoRequest] send request: %w", err)
|
||||||
|
}
|
||||||
|
defer reqp.Body.Close()
|
||||||
|
|
||||||
|
respBodyBuf, err := io.ReadAll(reqp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcDoRequest] read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return respBodyBuf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type rpcRequest struct {
|
||||||
|
Method string `json:"method"`
|
||||||
|
Module string `json:"module"`
|
||||||
|
Param any `json:"param"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type rpcResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Ts int64 `json:"ts"`
|
||||||
|
StartTs int64 `json:"start_ts"`
|
||||||
|
TraceID string `json:"traceid"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type rpcSubResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Data json.RawMessage `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) rpcCall(ctx context.Context,
|
||||||
|
protocol string, method string, module string,
|
||||||
|
param any,
|
||||||
|
) (json.RawMessage, error) {
|
||||||
|
reqBody := map[string]any{protocol: rpcRequest{
|
||||||
|
Method: method,
|
||||||
|
Module: module,
|
||||||
|
Param: param,
|
||||||
|
}}
|
||||||
|
|
||||||
|
respBodyBuf, err := c.rpcDoRequest(ctx, reqBody)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] do request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check rpc response status
|
||||||
|
respStatus := rpcResponse{}
|
||||||
|
if err := json.Unmarshal(respBodyBuf, &respStatus); err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
if respStatus.Code != 0 {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] rpc error: %d", respStatus.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse response data
|
||||||
|
var respBody map[string]json.RawMessage
|
||||||
|
if err := json.Unmarshal(respBodyBuf, &respBody); err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
subRespBuf, ok := respBody[protocol]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] sub-response not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
subResp := rpcSubResponse{}
|
||||||
|
if err := json.Unmarshal(subRespBuf, &subResp); err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] unmarshal sub-response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if subResp.Code != 0 {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] sub-response error: %d", subResp.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
return subResp.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) downloadFile(ctx context.Context, url string) ([]byte, error) {
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[downloadFile] init request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//req.Header.Set("Accept", "image/webp,image/*,*/*;q=0.8") // jpeg is preferred to embed in audio
|
||||||
|
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.5;q=0.4")
|
||||||
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.47.134 Safari/537.36 QBCore/3.53.47.400 QQBrowser/9.0.2524.400")
|
||||||
|
|
||||||
|
resp, err := c.http.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[downloadFile] send request: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("qmc[downloadFile] unexpected http status %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
return io.ReadAll(resp.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewQQMusicClient() *QQMusic {
|
||||||
|
return &QQMusic{
|
||||||
|
http: &http.Client{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
21
algo/qmc/client/cover.go
Normal file
21
algo/qmc/client/cover.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *QQMusic) AlbumCoverByID(ctx context.Context, albumID int) ([]byte, error) {
|
||||||
|
u := fmt.Sprintf("https://imgcache.qq.com/music/photo/album/%s/albumpic_%s_0.jpg",
|
||||||
|
strconv.Itoa(albumID%100),
|
||||||
|
strconv.Itoa(albumID),
|
||||||
|
)
|
||||||
|
return c.downloadFile(ctx, u)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) AlbumCoverByMediaID(ctx context.Context, mediaID string) ([]byte, error) {
|
||||||
|
// original: https://y.gtimg.cn/music/photo_new/T002M000%s.jpg
|
||||||
|
u := fmt.Sprintf("https://y.gtimg.cn/music/photo_new/T002R500x500M000%s.jpg", mediaID)
|
||||||
|
return c.downloadFile(ctx, u)
|
||||||
|
}
|
||||||
52
algo/qmc/client/search.go
Normal file
52
algo/qmc/client/search.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type searchParams struct {
|
||||||
|
Grp int `json:"grp"`
|
||||||
|
NumPerPage int `json:"num_per_page"`
|
||||||
|
PageNum int `json:"page_num"`
|
||||||
|
Query string `json:"query"`
|
||||||
|
RemotePlace string `json:"remoteplace"`
|
||||||
|
SearchType int `json:"search_type"`
|
||||||
|
//SearchID string `json:"searchid"` // todo: it seems generated randomly
|
||||||
|
}
|
||||||
|
|
||||||
|
type searchResponse struct {
|
||||||
|
Body struct {
|
||||||
|
Song struct {
|
||||||
|
List []*TrackInfo `json:"list"`
|
||||||
|
} `json:"song"`
|
||||||
|
} `json:"body"`
|
||||||
|
Code int `json:"code"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) Search(ctx context.Context, keyword string) ([]*TrackInfo, error) {
|
||||||
|
|
||||||
|
resp, err := c.rpcCall(ctx,
|
||||||
|
"music.search.SearchCgiService",
|
||||||
|
"DoSearchForQQMusicDesktop",
|
||||||
|
"music.search.SearchCgiService",
|
||||||
|
&searchParams{
|
||||||
|
SearchType: 0, Query: keyword,
|
||||||
|
PageNum: 1, NumPerPage: 40,
|
||||||
|
|
||||||
|
// static values
|
||||||
|
Grp: 1, RemotePlace: "sizer.newclient.song",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[Search] rpc call: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
respData := searchResponse{}
|
||||||
|
if err := json.Unmarshal(resp, &respData); err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[Search] unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return respData.Body.Song.List, nil
|
||||||
|
|
||||||
|
}
|
||||||
178
algo/qmc/client/track.go
Normal file
178
algo/qmc/client/track.go
Normal file
@@ -0,0 +1,178 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type getTrackInfoParams struct {
|
||||||
|
Ctx int `json:"ctx"`
|
||||||
|
Ids []int `json:"ids"`
|
||||||
|
Types []int `json:"types"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type getTrackInfoResponse struct {
|
||||||
|
Tracks []*TrackInfo `json:"tracks"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) GetTracksInfo(ctx context.Context, songIDs []int) ([]*TrackInfo, error) {
|
||||||
|
resp, err := c.rpcCall(ctx,
|
||||||
|
"Protocol_UpdateSongInfo",
|
||||||
|
"CgiGetTrackInfo",
|
||||||
|
"music.trackInfo.UniformRuleCtrl",
|
||||||
|
&getTrackInfoParams{Ctx: 0, Ids: songIDs, Types: []int{0}},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] rpc call: %w", err)
|
||||||
|
}
|
||||||
|
respData := getTrackInfoResponse{}
|
||||||
|
if err := json.Unmarshal(resp, &respData); err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return respData.Tracks, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) GetTrackInfo(ctx context.Context, songID int) (*TrackInfo, error) {
|
||||||
|
tracks, err := c.GetTracksInfo(ctx, []int{songID})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] get tracks info: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tracks) == 0 {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] track not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return tracks[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type TrackSinger struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Mid string `json:"mid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Type int `json:"type"`
|
||||||
|
Uin int `json:"uin"`
|
||||||
|
Pmid string `json:"pmid"`
|
||||||
|
}
|
||||||
|
type TrackAlbum struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Mid string `json:"mid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Subtitle string `json:"subtitle"`
|
||||||
|
Pmid string `json:"pmid"`
|
||||||
|
}
|
||||||
|
type TrackInfo struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Type int `json:"type"`
|
||||||
|
Mid string `json:"mid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Subtitle string `json:"subtitle"`
|
||||||
|
Singer []TrackSinger `json:"singer"`
|
||||||
|
Album TrackAlbum `json:"album"`
|
||||||
|
Mv struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Vid string `json:"vid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Vt int `json:"vt"`
|
||||||
|
} `json:"mv"`
|
||||||
|
Interval int `json:"interval"`
|
||||||
|
Isonly int `json:"isonly"`
|
||||||
|
Language int `json:"language"`
|
||||||
|
Genre int `json:"genre"`
|
||||||
|
IndexCd int `json:"index_cd"`
|
||||||
|
IndexAlbum int `json:"index_album"`
|
||||||
|
TimePublic string `json:"time_public"`
|
||||||
|
Status int `json:"status"`
|
||||||
|
Fnote int `json:"fnote"`
|
||||||
|
File struct {
|
||||||
|
MediaMid string `json:"media_mid"`
|
||||||
|
Size24Aac int `json:"size_24aac"`
|
||||||
|
Size48Aac int `json:"size_48aac"`
|
||||||
|
Size96Aac int `json:"size_96aac"`
|
||||||
|
Size192Ogg int `json:"size_192ogg"`
|
||||||
|
Size192Aac int `json:"size_192aac"`
|
||||||
|
Size128Mp3 int `json:"size_128mp3"`
|
||||||
|
Size320Mp3 int `json:"size_320mp3"`
|
||||||
|
SizeApe int `json:"size_ape"`
|
||||||
|
SizeFlac int `json:"size_flac"`
|
||||||
|
SizeDts int `json:"size_dts"`
|
||||||
|
SizeTry int `json:"size_try"`
|
||||||
|
TryBegin int `json:"try_begin"`
|
||||||
|
TryEnd int `json:"try_end"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
SizeHires int `json:"size_hires"`
|
||||||
|
HiresSample int `json:"hires_sample"`
|
||||||
|
HiresBitdepth int `json:"hires_bitdepth"`
|
||||||
|
B30S int `json:"b_30s"`
|
||||||
|
E30S int `json:"e_30s"`
|
||||||
|
Size96Ogg int `json:"size_96ogg"`
|
||||||
|
Size360Ra []interface{} `json:"size_360ra"`
|
||||||
|
SizeDolby int `json:"size_dolby"`
|
||||||
|
SizeNew []interface{} `json:"size_new"`
|
||||||
|
} `json:"file"`
|
||||||
|
Pay struct {
|
||||||
|
PayMonth int `json:"pay_month"`
|
||||||
|
PriceTrack int `json:"price_track"`
|
||||||
|
PriceAlbum int `json:"price_album"`
|
||||||
|
PayPlay int `json:"pay_play"`
|
||||||
|
PayDown int `json:"pay_down"`
|
||||||
|
PayStatus int `json:"pay_status"`
|
||||||
|
TimeFree int `json:"time_free"`
|
||||||
|
} `json:"pay"`
|
||||||
|
Action struct {
|
||||||
|
Switch int `json:"switch"`
|
||||||
|
Msgid int `json:"msgid"`
|
||||||
|
Alert int `json:"alert"`
|
||||||
|
Icons int `json:"icons"`
|
||||||
|
Msgshare int `json:"msgshare"`
|
||||||
|
Msgfav int `json:"msgfav"`
|
||||||
|
Msgdown int `json:"msgdown"`
|
||||||
|
Msgpay int `json:"msgpay"`
|
||||||
|
Switch2 int `json:"switch2"`
|
||||||
|
Icon2 int `json:"icon2"`
|
||||||
|
} `json:"action"`
|
||||||
|
Ksong struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Mid string `json:"mid"`
|
||||||
|
} `json:"ksong"`
|
||||||
|
Volume struct {
|
||||||
|
Gain float64 `json:"gain"`
|
||||||
|
Peak float64 `json:"peak"`
|
||||||
|
Lra float64 `json:"lra"`
|
||||||
|
} `json:"volume"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
Ppurl string `json:"ppurl"`
|
||||||
|
Bpm int `json:"bpm"`
|
||||||
|
Version int `json:"version"`
|
||||||
|
Trace string `json:"trace"`
|
||||||
|
DataType int `json:"data_type"`
|
||||||
|
ModifyStamp int `json:"modify_stamp"`
|
||||||
|
Aid int `json:"aid"`
|
||||||
|
Tid int `json:"tid"`
|
||||||
|
Ov int `json:"ov"`
|
||||||
|
Sa int `json:"sa"`
|
||||||
|
Es string `json:"es"`
|
||||||
|
Vs []string `json:"vs"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TrackInfo) GetArtists() []string {
|
||||||
|
return lo.Map(t.Singer, func(v TrackSinger, i int) string {
|
||||||
|
return v.Name
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TrackInfo) GetTitle() string {
|
||||||
|
return t.Title
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TrackInfo) GetAlbum() string {
|
||||||
|
return t.Album.Name
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
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"
|
||||||
@@ -16,16 +18,30 @@ 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
|
||||||
}
|
}
|
||||||
if n < 16 {
|
rawKeyDec = rawKeyDec[:n]
|
||||||
|
|
||||||
|
if bytes.HasPrefix(rawKeyDec, []byte(rawKeyPrefixV2)) {
|
||||||
|
rawKeyDec, err = deriveKeyV2(bytes.TrimPrefix(rawKeyDec, []byte(rawKeyPrefixV2)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("deriveKeyV2 failed: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return deriveKeyV1(rawKeyDec)
|
||||||
|
}
|
||||||
|
|
||||||
|
func deriveKeyV1(rawKeyDec []byte) ([]byte, error) {
|
||||||
|
if len(rawKeyDec) < 16 {
|
||||||
return nil, errors.New("key length is too short")
|
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)
|
||||||
@@ -40,6 +56,37 @@ func DecryptKey(rawKey []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
|
||||||
@@ -59,9 +106,7 @@ 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)
|
||||||
@@ -108,6 +153,7 @@ 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]
|
||||||
@@ -27,7 +27,6 @@ func loadDecryptKeyData(name string) ([]byte, []byte, error) {
|
|||||||
return keyRaw, keyDec, nil
|
return keyRaw, keyDec, nil
|
||||||
}
|
}
|
||||||
func TestDecryptKey(t *testing.T) {
|
func TestDecryptKey(t *testing.T) {
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
filename string
|
filename string
|
||||||
@@ -35,20 +34,22 @@ func TestDecryptKey(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{"mflac0_rc4(512)", "mflac0_rc4", false},
|
{"mflac0_rc4(512)", "mflac0_rc4", false},
|
||||||
{"mflac_map(256)", "mflac_map", false},
|
{"mflac_map(256)", "mflac_map", false},
|
||||||
|
{"mflac_rc4(256)", "mflac_rc4", false},
|
||||||
|
{"mgg_map(256)", "mgg_map", false},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
raw, want, err := loadDecryptKeyData(tt.filename)
|
raw, want, err := loadDecryptKeyData(tt.filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("load test data failed: %s", err)
|
t.Fatalf("load test data failed: %s", err)
|
||||||
}
|
}
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
got, err := deriveKey(raw)
|
||||||
got, err := DecryptKey(raw)
|
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("DecryptKey() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("deriveKey() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(got, want) {
|
if !reflect.DeepEqual(got, want) {
|
||||||
t.Errorf("DecryptKey() got = %v..., want %v...",
|
t.Errorf("deriveKey() got = %v..., want %v...",
|
||||||
string(got[:32]), string(want[:32]))
|
string(got[:32]), string(want[:32]))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
38
algo/qmc/key_mmkv_loader_darwin.go
Normal file
38
algo/qmc/key_mmkv_loader_darwin.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package qmc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"git.um-react.app/um/cli/algo/qmc/qmmac"
|
||||||
|
"git.um-react.app/um/cli/internal/mmkv"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadMMKVOrDefault(path string, key string, logger *zap.Logger) (result common.QMCKeys, err error) {
|
||||||
|
key1, err := qmmac.LoadMacKeysV8(logger)
|
||||||
|
if err != nil {
|
||||||
|
key1 = nil
|
||||||
|
logger.Warn("LoadMMKVOrDefault: could not read QQMusic v8.8.0 keys", zap.Error(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
key2, err := qmmac.LoadMacKeysV10(logger)
|
||||||
|
if err != nil {
|
||||||
|
key2 = nil
|
||||||
|
logger.Warn("LoadMMKVOrDefault: could not read QQMusic v10.x keys", zap.Error(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
userKeys := make(common.QMCKeys)
|
||||||
|
if path != "" {
|
||||||
|
logger.Info("Using user mmkv")
|
||||||
|
userKeys, err = mmkv.LoadFromPath(path, key, logger)
|
||||||
|
if err != nil {
|
||||||
|
userKeys = nil
|
||||||
|
logger.Warn("LoadMMKVOrDefault: could not read user keys", zap.Error(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allKeys := mmkv.Merge(key1, key2, userKeys)
|
||||||
|
|
||||||
|
logger.Debug("Keys loaded", zap.Any("keys", allKeys), zap.Int("len", len(allKeys)))
|
||||||
|
|
||||||
|
return allKeys, nil
|
||||||
|
}
|
||||||
13
algo/qmc/key_mmkv_loader_default.go
Normal file
13
algo/qmc/key_mmkv_loader_default.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
//go:build !darwin
|
||||||
|
|
||||||
|
package qmc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadMMKVOrDefault(path string, key string, logger *zap.Logger) (result common.QMCKeys, err error) {
|
||||||
|
// Stub: do nothing
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
292
algo/qmc/qmc.go
292
algo/qmc/qmc.go
@@ -4,161 +4,226 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/unlock-music/cli/algo/common"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"git.um-react.app/um/cli/internal/sniff"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
r io.ReadSeeker
|
raw io.ReadSeeker // raw is the original file reader
|
||||||
fileExt string
|
params *common.DecoderParams
|
||||||
|
|
||||||
audioLen int
|
audio io.Reader // audio is the encrypted audio data
|
||||||
decodedKey []byte
|
audioLen int // audioLen is the audio data length
|
||||||
cipher streamCipher
|
offset int // offset is the current audio read position
|
||||||
offset int
|
|
||||||
|
|
||||||
rawMetaExtra1 int
|
decodedKey []byte // decodedKey is the decoded key for cipher
|
||||||
|
cipher common.StreamDecoder
|
||||||
|
|
||||||
|
songID int
|
||||||
rawMetaExtra2 int
|
rawMetaExtra2 int
|
||||||
|
|
||||||
|
albumID int
|
||||||
|
albumMediaID string
|
||||||
|
|
||||||
|
// cache
|
||||||
|
meta common.AudioMeta
|
||||||
|
cover []byte
|
||||||
|
embeddedCover bool // embeddedCover is true if the cover is embedded in the file
|
||||||
|
probeBuf *bytes.Buffer // probeBuf is the buffer for sniffing metadata, TODO: consider pipe?
|
||||||
|
|
||||||
|
// provider
|
||||||
|
logger *zap.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read implements io.Reader, offer the decrypted audio data.
|
// Read implements io.Reader, offer the decrypted audio data.
|
||||||
// Validate should call before Read to check if the file is valid.
|
// Validate should call before Read to check if the file is valid.
|
||||||
func (d *Decoder) Read(p []byte) (int, error) {
|
func (d *Decoder) Read(p []byte) (int, error) {
|
||||||
n := len(p)
|
n, err := d.audio.Read(p)
|
||||||
if d.audioLen-d.offset <= 0 {
|
if n > 0 {
|
||||||
return 0, io.EOF
|
d.cipher.Decrypt(p[:n], d.offset)
|
||||||
} else if d.audioLen-d.offset < n {
|
d.offset += n
|
||||||
n = d.audioLen - d.offset
|
|
||||||
|
_, _ = d.probeBuf.Write(p[:n]) // bytes.Buffer.Write never return error
|
||||||
}
|
}
|
||||||
m, err := d.r.Read(p[:n])
|
return n, err
|
||||||
if m == 0 {
|
|
||||||
return 0, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d.cipher.Decrypt(p[:m], d.offset)
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
d.offset += m
|
return &Decoder{raw: p.Reader, params: p, logger: p.Logger}
|
||||||
return m, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecoder(r io.ReadSeeker) (*Decoder, error) {
|
func NewQmcCipherDecoder(key []byte) (common.StreamDecoder, error) {
|
||||||
d := &Decoder{r: r}
|
if len(key) > 300 {
|
||||||
err := d.searchKey()
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return NewQmcCipherDecoder(key)
|
||||||
if len(d.decodedKey) > 300 {
|
|
||||||
d.cipher, err = NewRC4Cipher(d.decodedKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else if len(d.decodedKey) != 0 {
|
|
||||||
d.cipher, err = NewMapCipher(d.decodedKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
d.cipher = NewStaticCipher()
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = d.r.Seek(0, io.SeekStart)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return d, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Validate() error {
|
func (d *Decoder) Validate() error {
|
||||||
buf := make([]byte, 16)
|
// search & derive key
|
||||||
if _, err := io.ReadFull(d.r, buf); err != nil {
|
err := d.searchKey()
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err := d.r.Seek(0, io.SeekStart)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check cipher type and init decode cipher
|
||||||
|
d.cipher, err = NewQmcCipherDecoder(d.decodedKey)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("qmc init cipher: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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))
|
||||||
|
|
||||||
|
// prepare for sniffing metadata
|
||||||
|
d.probeBuf = bytes.NewBuffer(make([]byte, 0, 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, 64)
|
||||||
|
if _, err := io.ReadFull(d.raw, buf); err != nil {
|
||||||
|
return fmt.Errorf("qmc read header: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
d.cipher.Decrypt(buf, 0)
|
d.cipher.Decrypt(buf, 0)
|
||||||
fileExt, ok := common.SniffAll(buf)
|
_, ok := sniff.AudioExtension(buf)
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("detect file type failed")
|
return errors.New("qmc: detect file type failed")
|
||||||
}
|
}
|
||||||
d.fileExt = fileExt
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Decoder) GetFileExt() string {
|
func (d *Decoder) searchKey() (err error) {
|
||||||
return d.fileExt
|
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fileSize := int(fileSizeM4) + 4
|
||||||
|
|
||||||
|
if key, ok := d.params.CryptoParams.QmcKeys.Get(d.params.FilePath); ok {
|
||||||
|
d.logger.Debug("QQMusic Mac Legacy file", zap.String("file", d.params.FilePath), zap.String("key", key))
|
||||||
|
d.decodedKey, err = deriveKey([]byte(key))
|
||||||
|
if err == nil {
|
||||||
|
d.audioLen = fileSize
|
||||||
|
} else {
|
||||||
|
d.decodedKey = nil
|
||||||
|
d.logger.Warn("could not derive key, skip", zap.Error(err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) searchKey() error {
|
suffixBuf := make([]byte, 4)
|
||||||
fileSizeM4, err := d.r.Seek(-4, io.SeekEnd)
|
if _, err := io.ReadFull(d.raw, suffixBuf); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch string(suffixBuf) {
|
||||||
|
case "QTag":
|
||||||
|
return d.readRawMetaQTag()
|
||||||
|
case "STag":
|
||||||
|
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
|
||||||
|
// speculative guess for "musicex\0"
|
||||||
|
case "cex\x00":
|
||||||
|
footer, err := NewMusicExTag(d.raw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
|
d.audioLen = fileSize - int(footer.TagSize)
|
||||||
if err != nil {
|
if key, ok := d.params.CryptoParams.QmcKeys.Get(footer.MediaFileName); ok {
|
||||||
return err
|
d.logger.Debug("searchKey: using key from MediaFileName", zap.String("MediaFileName", footer.MediaFileName), zap.String("key", key))
|
||||||
|
d.decodedKey, err = deriveKey([]byte(key))
|
||||||
|
} else if d.decodedKey == nil {
|
||||||
|
return errors.New("searchKey: no key found for musicex tag")
|
||||||
}
|
}
|
||||||
if string(buf) == "QTag" {
|
|
||||||
if err := d.readRawMetaQTag(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
|
default:
|
||||||
|
// if we already have a key from legacy mmkv, use it
|
||||||
|
if d.decodedKey != nil {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
size := binary.LittleEndian.Uint32(buf)
|
// try to use suffix as key length
|
||||||
if size < 0x300 && size != 0 {
|
size := binary.LittleEndian.Uint32(suffixBuf)
|
||||||
|
if size <= 0xFFFF && size != 0 { // assume size is key len
|
||||||
return d.readRawKey(int64(size))
|
return d.readRawKey(int64(size))
|
||||||
} else {
|
}
|
||||||
// try to use default static cipher
|
|
||||||
d.audioLen = int(fileSizeM4 + 4)
|
// try to use default static cipher,
|
||||||
|
// or the key read from the legacy mmkv
|
||||||
|
d.audioLen = fileSize
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
func (d *Decoder) readRawKey(rawKeyLen int64) error {
|
||||||
audioLen, err := d.r.Seek(-(4 + rawKeyLen), io.SeekEnd)
|
audioLen, err := d.raw.Seek(-(4 + rawKeyLen), io.SeekEnd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
d.audioLen = int(audioLen)
|
d.audioLen = int(audioLen)
|
||||||
|
|
||||||
rawKeyData, err := io.ReadAll(io.LimitReader(d.r, rawKeyLen))
|
rawKeyData, err := io.ReadAll(io.LimitReader(d.raw, rawKeyLen))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
d.decodedKey, err = DecryptKey(rawKeyData)
|
// clean suffix NULs
|
||||||
if err != nil {
|
rawKeyData = bytes.TrimRight(rawKeyData, "\x00")
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
d.decodedKey, err = deriveKey(rawKeyData)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) readRawMetaQTag() error {
|
func (d *Decoder) readRawMetaQTag() error {
|
||||||
// get raw meta data len
|
// get raw meta data len
|
||||||
if _, err := d.r.Seek(-8, io.SeekEnd); err != nil {
|
if _, err := d.raw.Seek(-8, io.SeekEnd); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buf, err := io.ReadAll(io.LimitReader(d.r, 4))
|
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
rawMetaLen := int64(binary.BigEndian.Uint32(buf))
|
rawMetaLen := int64(binary.BigEndian.Uint32(buf))
|
||||||
|
|
||||||
// read raw meta data
|
// read raw meta data
|
||||||
audioLen, err := d.r.Seek(-(8 + rawMetaLen), io.SeekEnd)
|
audioLen, err := d.raw.Seek(-(8 + rawMetaLen), io.SeekEnd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
d.audioLen = int(audioLen)
|
d.audioLen = int(audioLen)
|
||||||
rawMetaData, err := io.ReadAll(io.LimitReader(d.r, rawMetaLen))
|
rawMetaData, err := io.ReadAll(io.LimitReader(d.raw, rawMetaLen))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -168,12 +233,12 @@ func (d *Decoder) readRawMetaQTag() error {
|
|||||||
return errors.New("invalid raw meta data")
|
return errors.New("invalid raw meta data")
|
||||||
}
|
}
|
||||||
|
|
||||||
d.decodedKey, err = DecryptKey([]byte(items[0]))
|
d.decodedKey, err = deriveKey([]byte(items[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
d.rawMetaExtra1, err = strconv.Atoi(items[1])
|
d.songID, err = strconv.Atoi(items[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -194,8 +259,8 @@ func init() {
|
|||||||
"qmcogg", //QQ Music OGG
|
"qmcogg", //QQ Music OGG
|
||||||
|
|
||||||
"tkm", //QQ Music Accompaniment M4A
|
"tkm", //QQ Music Accompaniment M4A
|
||||||
"bkcmp3", //Moo Music Mp3
|
|
||||||
"bkcflac", //Moo Music Flac
|
"bkcmp3", "bkcm4a", "bkcflac", "bkcwav", "bkcape", "bkcogg", "bkcwma", //Moo Music
|
||||||
|
|
||||||
"666c6163", //QQ Music Weiyun Flac
|
"666c6163", //QQ Music Weiyun Flac
|
||||||
"6d7033", //QQ Music Weiyun Mp3
|
"6d7033", //QQ Music Weiyun Mp3
|
||||||
@@ -203,61 +268,20 @@ func init() {
|
|||||||
"6d3461", //QQ Music Weiyun M4a
|
"6d3461", //QQ Music Weiyun M4a
|
||||||
"776176", //QQ Music Weiyun Wav
|
"776176", //QQ Music Weiyun Wav
|
||||||
|
|
||||||
"mgg", "mgg1", //QQ Music New Ogg
|
"mmp4", // QQ Music MP4 Container, tipically used for Dolby EAC3 stream
|
||||||
"mflac", "mflac0", //QQ Music New Flac
|
|
||||||
}
|
}
|
||||||
for _, ext := range supportedExts {
|
for _, ext := range supportedExts {
|
||||||
common.RegisterDecoder(ext, false, newCompactDecoder)
|
common.RegisterDecoder(ext, false, NewDecoder)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type compactDecoder struct {
|
// New ogg/flac:
|
||||||
decoder *Decoder
|
extraExtsCanHaveSuffix := []string{"mgg", "mflac"}
|
||||||
createErr error
|
// Mac also adds some extra suffix to ext:
|
||||||
buf *bytes.Buffer
|
extraExtSuffix := []string{"0", "1", "a", "h", "l", "m"}
|
||||||
|
for _, ext := range extraExtsCanHaveSuffix {
|
||||||
|
common.RegisterDecoder(ext, false, NewDecoder)
|
||||||
|
for _, suffix := range extraExtSuffix {
|
||||||
|
common.RegisterDecoder(ext+suffix, false, NewDecoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
94
algo/qmc/qmc_footer_musicex.go
Normal file
94
algo/qmc/qmc_footer_musicex.go
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package qmc
|
||||||
|
|
||||||
|
import (
|
||||||
|
bytes "bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MusicExTagV1 struct {
|
||||||
|
SongID uint32 // Song ID
|
||||||
|
Unknown1 uint32 // unused & unknown
|
||||||
|
Unknown2 uint32 // unused & unknown
|
||||||
|
MediaID string // Media ID
|
||||||
|
MediaFileName string // real file name
|
||||||
|
Unknown3 uint32 // unused; uninitialized memory?
|
||||||
|
|
||||||
|
// 16 byte at the end of tag.
|
||||||
|
// TagSize should be respected when parsing.
|
||||||
|
TagSize uint32 // 19.57: fixed value: 0xC0
|
||||||
|
TagVersion uint32 // 19.57: fixed value: 0x01
|
||||||
|
TagMagic []byte // fixed value "musicex\0" (8 bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMusicExTag(f io.ReadSeeker) (*MusicExTagV1, error) {
|
||||||
|
_, err := f.Seek(-16, io.SeekEnd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("musicex seek error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer := make([]byte, 16)
|
||||||
|
bytesRead, err := f.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("get musicex error: %w", err)
|
||||||
|
}
|
||||||
|
if bytesRead != 16 {
|
||||||
|
return nil, fmt.Errorf("MusicExV1: read %d bytes (expected %d)", bytesRead, 16)
|
||||||
|
}
|
||||||
|
|
||||||
|
tag := &MusicExTagV1{
|
||||||
|
TagSize: binary.LittleEndian.Uint32(buffer[0x00:0x04]),
|
||||||
|
TagVersion: binary.LittleEndian.Uint32(buffer[0x04:0x08]),
|
||||||
|
TagMagic: buffer[0x08:],
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(tag.TagMagic, []byte("musicex\x00")) {
|
||||||
|
return nil, errors.New("MusicEx magic mismatch")
|
||||||
|
}
|
||||||
|
if tag.TagVersion != 1 {
|
||||||
|
return nil, fmt.Errorf("unsupported musicex tag version. expecting 1, got %d", tag.TagVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tag.TagSize < 0xC0 {
|
||||||
|
return nil, fmt.Errorf("unsupported musicex tag size. expecting at least 0xC0, got 0x%02x", tag.TagSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = make([]byte, tag.TagSize)
|
||||||
|
f.Seek(-int64(tag.TagSize), io.SeekEnd)
|
||||||
|
bytesRead, err = f.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("MusicExV1: Read error %w", err)
|
||||||
|
}
|
||||||
|
if uint32(bytesRead) != tag.TagSize {
|
||||||
|
return nil, fmt.Errorf("MusicExV1: read %d bytes (expected %d)", bytesRead, tag.TagSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
tag.SongID = binary.LittleEndian.Uint32(buffer[0x00:0x04])
|
||||||
|
tag.Unknown1 = binary.LittleEndian.Uint32(buffer[0x04:0x08])
|
||||||
|
tag.Unknown2 = binary.LittleEndian.Uint32(buffer[0x08:0x0C])
|
||||||
|
tag.MediaID = readUnicodeTagName(buffer[0x0C:], 30*2)
|
||||||
|
tag.MediaFileName = readUnicodeTagName(buffer[0x48:], 50*2)
|
||||||
|
tag.Unknown3 = binary.LittleEndian.Uint32(buffer[0xAC:0xB0])
|
||||||
|
return tag, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// readUnicodeTagName reads a buffer to maxLen.
|
||||||
|
// reconstruct text by skipping alternate char (ascii chars encoded in UTF-16-LE),
|
||||||
|
// until finding a zero or reaching maxLen.
|
||||||
|
func readUnicodeTagName(buffer []byte, maxLen int) string {
|
||||||
|
builder := strings.Builder{}
|
||||||
|
|
||||||
|
for i := 0; i < maxLen; i += 2 {
|
||||||
|
chr := buffer[i]
|
||||||
|
if chr != 0 {
|
||||||
|
builder.WriteByte(chr)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.String()
|
||||||
|
}
|
||||||
128
algo/qmc/qmc_meta.go
Normal file
128
algo/qmc/qmc_meta.go
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
package qmc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"git.um-react.app/um/cli/algo/qmc/client"
|
||||||
|
"git.um-react.app/um/cli/internal/ffmpeg"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (d *Decoder) GetAudioMeta(ctx context.Context) (common.AudioMeta, error) {
|
||||||
|
if d.meta != nil {
|
||||||
|
return d.meta, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.songID != 0 {
|
||||||
|
if err := d.getMetaBySongID(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return d.meta, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
embedMeta, err := ffmpeg.ProbeReader(ctx, d.probeBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[GetAudioMeta] probe reader: %w", err)
|
||||||
|
}
|
||||||
|
d.meta = embedMeta
|
||||||
|
d.embeddedCover = embedMeta.HasAttachedPic()
|
||||||
|
|
||||||
|
if !d.embeddedCover && embedMeta.HasMetadata() {
|
||||||
|
if err := d.searchMetaOnline(ctx, embedMeta); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return d.meta, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.meta, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) getMetaBySongID(ctx context.Context) error {
|
||||||
|
c := client.NewQQMusicClient() // todo: use global client
|
||||||
|
trackInfo, err := c.GetTrackInfo(ctx, d.songID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("qmc[GetAudioMeta] get track info: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.meta = trackInfo
|
||||||
|
d.albumID = trackInfo.Album.Id
|
||||||
|
if trackInfo.Album.Pmid == "" {
|
||||||
|
d.albumMediaID = trackInfo.Album.Pmid
|
||||||
|
} else {
|
||||||
|
d.albumMediaID = trackInfo.Album.Mid
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) searchMetaOnline(ctx context.Context, original common.AudioMeta) error {
|
||||||
|
c := client.NewQQMusicClient() // todo: use global client
|
||||||
|
keyword := lo.Compact(append(
|
||||||
|
[]string{original.GetTitle(), original.GetAlbum()},
|
||||||
|
original.GetArtists()...),
|
||||||
|
)
|
||||||
|
if len(keyword) == 0 {
|
||||||
|
return errors.New("qmc[searchMetaOnline] no keyword")
|
||||||
|
}
|
||||||
|
|
||||||
|
trackList, err := c.Search(ctx, strings.Join(keyword, " "))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("qmc[searchMetaOnline] search: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(trackList) == 0 {
|
||||||
|
return errors.New("qmc[searchMetaOnline] no result")
|
||||||
|
}
|
||||||
|
|
||||||
|
meta := trackList[0]
|
||||||
|
d.meta = meta
|
||||||
|
d.albumID = meta.Album.Id
|
||||||
|
if meta.Album.Pmid == "" {
|
||||||
|
d.albumMediaID = meta.Album.Pmid
|
||||||
|
} else {
|
||||||
|
d.albumMediaID = meta.Album.Mid
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
|
||||||
|
if d.cover != nil {
|
||||||
|
return d.cover, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.embeddedCover {
|
||||||
|
img, err := ffmpeg.ExtractAlbumArt(ctx, d.probeBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[GetCoverImage] extract album art: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.cover = img.Bytes()
|
||||||
|
|
||||||
|
return d.cover, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c := client.NewQQMusicClient() // todo: use global client
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if d.albumMediaID != "" {
|
||||||
|
d.cover, err = c.AlbumCoverByMediaID(ctx, d.albumMediaID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[GetCoverImage] get cover by media id: %w", err)
|
||||||
|
}
|
||||||
|
} else if d.albumID != 0 {
|
||||||
|
d.cover, err = c.AlbumCoverByID(ctx, d.albumID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[GetCoverImage] get cover by id: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("qmc[GetAudioMeta] album (or media) id is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.cover, nil
|
||||||
|
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
|
func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
|
||||||
@@ -29,12 +31,14 @@ func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
|
|||||||
func TestMflac0Decoder_Read(t *testing.T) {
|
func TestMflac0Decoder_Read(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
fileExt string
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{"mflac0_rc4", false},
|
{"mflac0_rc4", ".mflac0", false},
|
||||||
{"mflac_map", false},
|
{"mflac_rc4", ".mflac", false},
|
||||||
{"mgg_map", false},
|
{"mflac_map", ".mflac", false},
|
||||||
{"qmc0_static", false},
|
{"mgg_map", ".mgg", false},
|
||||||
|
{"qmc0_static", ".qmc0", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@@ -44,11 +48,14 @@ func TestMflac0Decoder_Read(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
d, err := NewDecoder(bytes.NewReader(raw))
|
d := NewDecoder(&common.DecoderParams{
|
||||||
if err != nil {
|
Reader: bytes.NewReader(raw),
|
||||||
t.Error(err)
|
Extension: tt.fileExt,
|
||||||
return
|
})
|
||||||
|
if err := d.Validate(); err != nil {
|
||||||
|
t.Errorf("validate file error = %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, len(target))
|
buf := make([]byte, len(target))
|
||||||
if _, err := io.ReadFull(d, buf); err != nil {
|
if _, err := io.ReadFull(d, buf); err != nil {
|
||||||
t.Errorf("read bytes from decoder error = %v", err)
|
t.Errorf("read bytes from decoder error = %v", err)
|
||||||
@@ -80,19 +87,15 @@ func TestMflac0Decoder_Validate(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
d, err := NewDecoder(bytes.NewReader(raw))
|
d := NewDecoder(&common.DecoderParams{
|
||||||
if err != nil {
|
Reader: bytes.NewReader(raw),
|
||||||
t.Error(err)
|
Extension: tt.fileExt,
|
||||||
return
|
})
|
||||||
}
|
|
||||||
|
|
||||||
if err := d.Validate(); err != nil {
|
if err := d.Validate(); err != nil {
|
||||||
t.Errorf("read bytes from decoder error = %v", err)
|
t.Errorf("read bytes from decoder error = %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if tt.fileExt != d.GetFileExt() {
|
|
||||||
t.Errorf("Decrypt() got = %v, want %v", d.GetFileExt(), tt.fileExt)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
163
algo/qmc/qmmac/v10_darwin.go
Normal file
163
algo/qmc/qmmac/v10_darwin.go
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
package qmmac
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"git.um-react.app/um/cli/internal/mmkv"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _RE_UDID_V10 = regexp.MustCompile(`_\x10\(([0-9a-f]{40})`)
|
||||||
|
|
||||||
|
type QQMusicMacV10 struct {
|
||||||
|
logger *zap.Logger
|
||||||
|
mmkv_dir string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *QQMusicMacV10) extractUdids(data []byte) ([]string, error) {
|
||||||
|
var result []string
|
||||||
|
for _, match := range _RE_UDID_V10.FindAllSubmatch(data, -1) {
|
||||||
|
udid := string(match[1])
|
||||||
|
q.logger.Debug("extractUdids: found udid", zap.String("udid", udid))
|
||||||
|
result = append(result, udid)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *QQMusicMacV10) caesar(text string, shift int) string {
|
||||||
|
var result strings.Builder
|
||||||
|
|
||||||
|
for _, char := range []byte(text) {
|
||||||
|
var transformed byte
|
||||||
|
if 'A' <= char && char <= 'Z' {
|
||||||
|
transformed = (char-'A'+byte(shift))%26 + 'A'
|
||||||
|
} else if 'a' <= char && char <= 'z' {
|
||||||
|
transformed = (char-'a'+byte(shift))%26 + 'a'
|
||||||
|
} else if '0' <= char && char <= '9' {
|
||||||
|
transformed = (char-'0'+byte(shift))%10 + '0'
|
||||||
|
} else {
|
||||||
|
transformed = char
|
||||||
|
}
|
||||||
|
result.WriteByte(transformed)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *QQMusicMacV10) mmkv(udid string, id int) (path string, key string, err error) {
|
||||||
|
str1 := q.caesar(udid, id+3)
|
||||||
|
int1, err := strconv.ParseInt(udid[5:7], 16, 32)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", fmt.Errorf("getMmkv: could not parse udid: %w", err)
|
||||||
|
}
|
||||||
|
int2 := 5 + (int(int1)+id)%4
|
||||||
|
name := str1[:int2]
|
||||||
|
path = filepath.Join(q.mmkv_dir, name)
|
||||||
|
|
||||||
|
int3 := id + 0xa546
|
||||||
|
str3 := fmt.Sprintf("%s%04x", udid, int3)
|
||||||
|
hash := md5.Sum([]byte(str3))
|
||||||
|
key = fmt.Sprintf("%x", hash)[:16]
|
||||||
|
|
||||||
|
return path, key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *QQMusicMacV10) loadByPList(plist_path string) ([]common.QMCKeys, error) {
|
||||||
|
logger := q.logger.With(zap.String("plist", plist_path))
|
||||||
|
logger.Debug("loadMacKeysV10: load key from plist")
|
||||||
|
if f, err := os.Stat(plist_path); err != nil || f.IsDir() {
|
||||||
|
logger.Debug("loadMacKeysV10: plist not found")
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
plist_data, err := os.ReadFile(plist_path)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("loadMacKeysV10: could not read plist", zap.Error(err))
|
||||||
|
return nil, fmt.Errorf("loadMacKeysV10: could not read plist: %w", err)
|
||||||
|
}
|
||||||
|
udids, err := q.extractUdids(plist_data)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("loadMacKeysV10: could not extract udid", zap.Error(err))
|
||||||
|
return nil, fmt.Errorf("loadMacKeysV10: could not extract udid: %w", err)
|
||||||
|
}
|
||||||
|
logger.Debug("loadMacKeysV10: read udid", zap.Strings("udids", udids))
|
||||||
|
|
||||||
|
var keysList []common.QMCKeys
|
||||||
|
for _, udid := range udids {
|
||||||
|
keys, err := q.loadByUDID(udid, logger)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("loadMacKeysV10: could not load by udid", zap.String("udid", udid), zap.Error(err))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
keysList = append(keysList, keys)
|
||||||
|
}
|
||||||
|
return keysList, nil
|
||||||
|
}
|
||||||
|
func (q *QQMusicMacV10) loadByUDID(udid string, logger *zap.Logger) (common.QMCKeys, error) {
|
||||||
|
mmkv_path, mmkv_key, err := q.mmkv(udid, 1)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("loadMacKeysV10: could not get mmkv name/key", zap.Error(err))
|
||||||
|
return nil, fmt.Errorf("loadMacKeysV10: could not get mmkv name/key: %w", err)
|
||||||
|
}
|
||||||
|
logger.Info("Using QQMusic 10.x mmkv", zap.String("mmkv", mmkv_path))
|
||||||
|
keys, err := mmkv.LoadFromPath(mmkv_path, mmkv_key, logger)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("loadMacKeysV10: could not load mmkv", zap.String("mmkv", mmkv_path), zap.Error(err))
|
||||||
|
return nil, fmt.Errorf("loadMacKeysV10: could not load mmkv: %w", err)
|
||||||
|
}
|
||||||
|
return keys, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadMacKeysV10(logger *zap.Logger) (common.QMCKeys, error) {
|
||||||
|
homeDir, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("Failed to get home dir")
|
||||||
|
return nil, fmt.Errorf("loadMacKeysV10: failed to get home: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MMKV dir is always inside the sandbox container
|
||||||
|
mmkv_dir := filepath.Join(
|
||||||
|
homeDir,
|
||||||
|
"Library/Containers/com.tencent.QQMusicMac/Data/",
|
||||||
|
"Library/Application Support/QQMusicMac/iData",
|
||||||
|
)
|
||||||
|
if f, err := os.Stat(mmkv_dir); err != nil || !f.IsDir() {
|
||||||
|
logger.Debug("loadMacKeysV10: mmkv dir not found", zap.String("mmkv_dir", mmkv_dir))
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// without sandbox
|
||||||
|
plist_without_sandbox := filepath.Join(
|
||||||
|
homeDir,
|
||||||
|
"Library/Preferences/com.tencent.QQMusicMac.plist",
|
||||||
|
)
|
||||||
|
|
||||||
|
// with sandbox (e.g. from App Store)
|
||||||
|
plist_sandboxed := filepath.Join(
|
||||||
|
homeDir,
|
||||||
|
"Library/Containers/com.tencent.QQMusicMac/Data/",
|
||||||
|
"Library/Preferences/com.tencent.QQMusicMac.plist",
|
||||||
|
)
|
||||||
|
|
||||||
|
q := QQMusicMacV10{
|
||||||
|
logger: logger,
|
||||||
|
mmkv_dir: mmkv_dir,
|
||||||
|
}
|
||||||
|
|
||||||
|
keys1, err := q.loadByPList(plist_without_sandbox)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
keys2, err := q.loadByPList(plist_sandboxed)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mmkv.Merge(append(keys1, keys2...)...), nil
|
||||||
|
}
|
||||||
30
algo/qmc/qmmac/v8_darwin.go
Normal file
30
algo/qmc/qmmac/v8_darwin.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package qmmac
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"git.um-react.app/um/cli/internal/mmkv"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadMacKeysV8(logger *zap.Logger) (keys common.QMCKeys, err error) {
|
||||||
|
homeDir, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("Failed to get home dir")
|
||||||
|
return nil, fmt.Errorf("loadMacKeysV8: failed to get home: %w", err)
|
||||||
|
}
|
||||||
|
p := filepath.Join(
|
||||||
|
homeDir,
|
||||||
|
"Library/Containers/com.tencent.QQMusicMac/Data",
|
||||||
|
"Library/Application Support/QQMusicMac/mmkv",
|
||||||
|
"MMKVStreamEncryptId",
|
||||||
|
)
|
||||||
|
if f, err := os.Stat(p); err == nil && !f.IsDir() {
|
||||||
|
logger.Info("Using QQMusic 8.x mmkv", zap.String("mmkv", p))
|
||||||
|
return mmkv.LoadFromPath(p, "", logger)
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
1
algo/qmc/testdata/mflac_rc4_key.bin
vendored
Normal file
1
algo/qmc/testdata/mflac_rc4_key.bin
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pUtyvqr0TgAvR95mNmY7DmNl386TsJNAEIz95CEcgIgJCcs28686O7llxD5E74ldn70xMtd5cG58TA5ILw09I8BOTf5EdHKd6wwPn689DUK13y3Req6H0P33my2miJ5bQ2AA22B8vp4V0NJ3hBqNtFf7cId48V6W51e1kwgu1xKKawxe9BByT92MFlqrFaKH32dB2zFgyd38l2P1outr4l2XLq48F9G17ptRz4W8Loxu28RvZgv0BzL26Ht9I2L5VCwMzzt7OeZ55iQs40Tr6k81QGraIUJj5zeBMgJRMTaSgi19hU5x5a08Qd662MbFhZZ0FjVvaDy1nbIDhrC62c1lX6wf70O45h4W42VxloBVeZ9Sef4V7cWrjrEjj3DJ5w2iu6Q9uoal2f4390kue42Um5HcDFWqv3m56k6O89bRV424PaRra1k9Cd2L56IN2zfBYqNo2WP5VC68G8w1hfflOY0O52h4WdcpoHSjZm4b35N7l47dT4dwEXj1U4J5
|
||||||
1
algo/qmc/testdata/mflac_rc4_key_raw.bin
vendored
Normal file
1
algo/qmc/testdata/mflac_rc4_key_raw.bin
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
cFV0eXZxcjAF/IXJ9qJT1u5C3S5AgY9BoVtIQNBKfxQMt5hH7BF36ndIJGV5L6qw5h4G0IOIOOewdHmMCNfKJftHM4nv3B0iRlSdqJKdL08wO3sV0v8eZk0OiYAlxgseGcBquQWYS/0b5Lj/Ioi2NfpOthAY9vUiRPnfH3+7/2AJGudHjj4Gg1KkpPW3mXIKbsk+Ou9fhrUqs873BCdsmI6qRmVNhOkLaUcbG6Zin3XU0WkgnnjebR43S8N4bw5BTphFvhy42QvspnD7Ewb1tVZQMQ2N1s38nBjukdfCB9R6aRwITOvg2U7Lr0RjLpbrIn6A6iVilpINjK4VptuKUTlpDXQwgCjoqeHQaHNCWgYpdjB69lXn8km/BfzK7QyDbh0VgTikwAHF9tvPhin3AIDRcU0xsaWYKURRfJelX3pSN495ADlhXdEKL/+l60hVnY7t6iCMxJL3lOtdGtdUYUGUCc76PB1fX+0HTWCcfcwvXTEdczr9J1h2yTeJNqFQ5pNy8vX7Ws8k7vDQVFkw4llZjPhb0kg9aDNePTNIKSGwy/7eofrcUQlC9DI+qqqwQ5abA/93fNsPq6XU3uwawnrbBsdz8DDdjJiEDI7abkPIDIfr/uR0YzgBxW90t5bt6xAtuW+VSYAM7kGxI3RZTl7JgOT60MLyIWkYASrRhRPMGks8zL10ED/4yGTEB1nt
|
||||||
BIN
algo/qmc/testdata/mflac_rc4_raw.bin
vendored
Normal file
BIN
algo/qmc/testdata/mflac_rc4_raw.bin
vendored
Normal file
Binary file not shown.
BIN
algo/qmc/testdata/mflac_rc4_suffix.bin
vendored
Normal file
BIN
algo/qmc/testdata/mflac_rc4_suffix.bin
vendored
Normal file
Binary file not shown.
BIN
algo/qmc/testdata/mflac_rc4_target.bin
vendored
Normal file
BIN
algo/qmc/testdata/mflac_rc4_target.bin
vendored
Normal file
Binary file not shown.
@@ -3,77 +3,56 @@ package tm
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/unlock-music/cli/algo/common"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"git.um-react.app/um/cli/internal/sniff"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 {
|
||||||
file []byte
|
raw io.ReadSeeker // raw is the original file reader
|
||||||
audio []byte
|
|
||||||
headerMatch bool
|
|
||||||
audioExt string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetCoverImage() []byte {
|
offset int
|
||||||
return nil
|
audio io.Reader // audio is the decrypted audio data
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetAudioData() []byte {
|
|
||||||
return d.audio
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetAudioExt() string {
|
|
||||||
if d.audioExt != "" {
|
|
||||||
return "." + d.audioExt
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetMeta() common.Meta {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Validate() error {
|
func (d *Decoder) Validate() error {
|
||||||
if len(d.file) < 8 {
|
header := make([]byte, 8)
|
||||||
return errors.New("invalid file size")
|
if _, err := io.ReadFull(d.raw, header); err != nil {
|
||||||
|
return fmt.Errorf("tm read header: %w", err)
|
||||||
}
|
}
|
||||||
if !bytes.Equal(magicHeader, d.file[:4]) {
|
|
||||||
return errors.New("not a valid tm file")
|
if bytes.Equal(magicHeader, header[:len(magicHeader)]) { // replace m4a header
|
||||||
}
|
d.audio = io.MultiReader(bytes.NewReader(replaceHeader), d.raw)
|
||||||
d.headerMatch = true
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Decode() error {
|
if _, ok := sniff.AudioExtension(header); ok { // not encrypted
|
||||||
d.audio = d.file
|
d.audio = io.MultiReader(bytes.NewReader(header), d.raw)
|
||||||
if d.headerMatch {
|
|
||||||
for i := 0; i < 8; i++ {
|
|
||||||
d.audio[i] = replaceHeader[i]
|
|
||||||
}
|
|
||||||
d.audioExt = "m4a"
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//goland:noinspection GoUnusedExportedFunction
|
return errors.New("tm: valid magic header")
|
||||||
func NewDecoder(data []byte) common.Decoder {
|
|
||||||
return &Decoder{file: data}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
|
func (d *Decoder) Read(buf []byte) (int, error) {
|
||||||
return func(file []byte) common.Decoder {
|
return d.audio.Read(buf)
|
||||||
return &Decoder{file: file, audioExt: ext}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewTmDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
|
return &Decoder{raw: p.Reader}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// QQ Music IOS M4a
|
// QQ Music IOS M4a (replace header)
|
||||||
common.RegisterDecoder("tm2", false, DecoderFuncWithExt("m4a"))
|
common.RegisterDecoder("tm2", false, NewTmDecoder)
|
||||||
common.RegisterDecoder("tm6", false, DecoderFuncWithExt("m4a"))
|
common.RegisterDecoder("tm6", false, NewTmDecoder)
|
||||||
// QQ Music IOS Mp3
|
|
||||||
common.RegisterDecoder("tm0", false, common.NewRawDecoder)
|
|
||||||
common.RegisterDecoder("tm3", false, common.NewRawDecoder)
|
|
||||||
|
|
||||||
|
// QQ Music IOS Mp3 (not encrypted)
|
||||||
|
common.RegisterDecoder("tm0", false, NewTmDecoder)
|
||||||
|
common.RegisterDecoder("tm3", false, NewTmDecoder)
|
||||||
}
|
}
|
||||||
|
|||||||
91
algo/xiami/xm.go
Normal file
91
algo/xiami/xm.go
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
package xiami
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
magicHeader = []byte{'i', 'f', 'm', 't'}
|
||||||
|
magicHeader2 = []byte{0xfe, 0xfe, 0xfe, 0xfe}
|
||||||
|
typeMapping = map[string]string{
|
||||||
|
" WAV": "wav",
|
||||||
|
"FLAC": "flac",
|
||||||
|
" MP3": "mp3",
|
||||||
|
" A4M": "m4a",
|
||||||
|
}
|
||||||
|
ErrMagicHeader = errors.New("xm magic header not matched")
|
||||||
|
)
|
||||||
|
|
||||||
|
type Decoder struct {
|
||||||
|
rd io.ReadSeeker // rd is the original file reader
|
||||||
|
offset int
|
||||||
|
|
||||||
|
cipher common.StreamDecoder
|
||||||
|
outputExt string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) GetAudioExt() string {
|
||||||
|
if d.outputExt != "" {
|
||||||
|
return "." + d.outputExt
|
||||||
|
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
|
return &Decoder{rd: p.Reader}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate checks if the file is a valid xiami .xm file.
|
||||||
|
// rd will set to the beginning of the encrypted audio data.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) Read(p []byte) (int, error) {
|
||||||
|
n, err := d.rd.Read(p)
|
||||||
|
if n > 0 {
|
||||||
|
d.cipher.Decrypt(p[:n], d.offset)
|
||||||
|
d.offset += n
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Xiami Wav/M4a/Mp3/Flac
|
||||||
|
common.RegisterDecoder("xm", false, NewDecoder)
|
||||||
|
// Xiami Typed Format
|
||||||
|
common.RegisterDecoder("wav", false, NewDecoder)
|
||||||
|
common.RegisterDecoder("mp3", false, NewDecoder)
|
||||||
|
common.RegisterDecoder("flac", false, NewDecoder)
|
||||||
|
common.RegisterDecoder("m4a", false, NewDecoder)
|
||||||
|
}
|
||||||
21
algo/xiami/xm_cipher.go
Normal file
21
algo/xiami/xm_cipher.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package xiami
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
algo/ximalaya/x2m_crypto.go
Normal file
34
algo/ximalaya/x2m_crypto.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package ximalaya
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"encoding/binary"
|
||||||
|
)
|
||||||
|
|
||||||
|
const x2mHeaderSize = 1024
|
||||||
|
|
||||||
|
var x2mKey = [...]byte{'x', 'm', 'l', 'y'}
|
||||||
|
var x2mScrambleTable = [x2mHeaderSize]uint16{}
|
||||||
|
|
||||||
|
//go:embed x2m_scramble_table.bin
|
||||||
|
var x2mScrambleTableBytes []byte
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if len(x2mScrambleTableBytes) != 2*x2mHeaderSize {
|
||||||
|
panic("invalid x2m scramble table")
|
||||||
|
}
|
||||||
|
for i := range x2mScrambleTable {
|
||||||
|
x2mScrambleTable[i] = binary.LittleEndian.Uint16(x2mScrambleTableBytes[i*2:])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// decryptX2MHeader decrypts the header of ximalaya .x2m file.
|
||||||
|
// make sure input src is 1024(x2mHeaderSize) bytes long.
|
||||||
|
func decryptX2MHeader(src []byte) []byte {
|
||||||
|
dst := make([]byte, len(src))
|
||||||
|
for dstIdx := range src {
|
||||||
|
srcIdx := x2mScrambleTable[dstIdx]
|
||||||
|
dst[dstIdx] = src[srcIdx] ^ x2mKey[dstIdx%len(x2mKey)]
|
||||||
|
}
|
||||||
|
return dst
|
||||||
|
}
|
||||||
BIN
algo/ximalaya/x2m_scramble_table.bin
Normal file
BIN
algo/ximalaya/x2m_scramble_table.bin
Normal file
Binary file not shown.
40
algo/ximalaya/x3m_crypto.go
Normal file
40
algo/ximalaya/x3m_crypto.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package ximalaya
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"encoding/binary"
|
||||||
|
)
|
||||||
|
|
||||||
|
var x3mKey = [...]byte{
|
||||||
|
'3', '9', '8', '9', 'd', '1', '1', '1',
|
||||||
|
'a', 'a', 'd', '5', '6', '1', '3', '9',
|
||||||
|
'4', '0', 'f', '4', 'f', 'c', '4', '4',
|
||||||
|
'b', '6', '3', '9', 'b', '2', '9', '2',
|
||||||
|
}
|
||||||
|
|
||||||
|
const x3mHeaderSize = 1024
|
||||||
|
|
||||||
|
var x3mScrambleTable = [x3mHeaderSize]uint16{}
|
||||||
|
|
||||||
|
//go:embed x3m_scramble_table.bin
|
||||||
|
var x3mScrambleTableBytes []byte
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if len(x3mScrambleTableBytes) != 2*x3mHeaderSize {
|
||||||
|
panic("invalid x3m scramble table")
|
||||||
|
}
|
||||||
|
for i := range x3mScrambleTable {
|
||||||
|
x3mScrambleTable[i] = binary.LittleEndian.Uint16(x3mScrambleTableBytes[i*2:])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// decryptX3MHeader decrypts the header of ximalaya .x3m file.
|
||||||
|
// make sure input src is 1024 (x3mHeaderSize) bytes long.
|
||||||
|
func decryptX3MHeader(src []byte) []byte {
|
||||||
|
dst := make([]byte, len(src))
|
||||||
|
for dstIdx := range src {
|
||||||
|
srcIdx := x3mScrambleTable[dstIdx]
|
||||||
|
dst[dstIdx] = src[srcIdx] ^ x3mKey[dstIdx%len(x3mKey)]
|
||||||
|
}
|
||||||
|
return dst
|
||||||
|
}
|
||||||
BIN
algo/ximalaya/x3m_scramble_table.bin
Normal file
BIN
algo/ximalaya/x3m_scramble_table.bin
Normal file
Binary file not shown.
57
algo/ximalaya/ximalaya.go
Normal file
57
algo/ximalaya/ximalaya.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package ximalaya
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"git.um-react.app/um/cli/internal/sniff"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Decoder struct {
|
||||||
|
rd io.ReadSeeker
|
||||||
|
offset int
|
||||||
|
|
||||||
|
audio io.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
|
return &Decoder{rd: p.Reader}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) Validate() error {
|
||||||
|
encryptedHeader := make([]byte, x2mHeaderSize)
|
||||||
|
if _, err := io.ReadFull(d.rd, encryptedHeader); err != nil {
|
||||||
|
return fmt.Errorf("ximalaya read header: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // try to decode with x2m
|
||||||
|
header := decryptX2MHeader(encryptedHeader)
|
||||||
|
if _, ok := sniff.AudioExtension(header); ok {
|
||||||
|
d.audio = io.MultiReader(bytes.NewReader(header), d.rd)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // try to decode with x3m
|
||||||
|
// not read file again, since x2m and x3m have the same header size
|
||||||
|
header := decryptX3MHeader(encryptedHeader)
|
||||||
|
if _, ok := sniff.AudioExtension(header); ok {
|
||||||
|
d.audio = io.MultiReader(bytes.NewReader(header), d.rd)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("ximalaya: unknown format")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) Read(p []byte) (n int, err error) {
|
||||||
|
return d.audio.Read(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
common.RegisterDecoder("x2m", false, NewDecoder)
|
||||||
|
common.RegisterDecoder("x3m", false, NewDecoder)
|
||||||
|
common.RegisterDecoder("xm", false, NewDecoder)
|
||||||
|
}
|
||||||
106
algo/xm/xm.go
106
algo/xm/xm.go
@@ -1,106 +0,0 @@
|
|||||||
package xm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"github.com/unlock-music/cli/algo/common"
|
|
||||||
"github.com/unlock-music/cli/internal/logging"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
magicHeader = []byte{'i', 'f', 'm', 't'}
|
|
||||||
magicHeader2 = []byte{0xfe, 0xfe, 0xfe, 0xfe}
|
|
||||||
typeMapping = map[string]string{
|
|
||||||
" WAV": "wav",
|
|
||||||
"FLAC": "flac",
|
|
||||||
" MP3": "mp3",
|
|
||||||
" A4M": "m4a",
|
|
||||||
}
|
|
||||||
ErrFileSize = errors.New("xm invalid file size")
|
|
||||||
ErrMagicHeader = errors.New("xm magic header not matched")
|
|
||||||
)
|
|
||||||
|
|
||||||
type Decoder struct {
|
|
||||||
file []byte
|
|
||||||
headerLen uint32
|
|
||||||
outputExt string
|
|
||||||
mask byte
|
|
||||||
audio []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetCoverImage() []byte {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetAudioData() []byte {
|
|
||||||
return d.audio
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetAudioExt() string {
|
|
||||||
if d.outputExt != "" {
|
|
||||||
return "." + d.outputExt
|
|
||||||
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) GetMeta() common.Meta {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDecoder(data []byte) common.Decoder {
|
|
||||||
return &Decoder{file: data}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) Validate() error {
|
|
||||||
lenData := len(d.file)
|
|
||||||
if lenData < 16 {
|
|
||||||
return ErrFileSize
|
|
||||||
}
|
|
||||||
if !bytes.Equal(magicHeader, d.file[:4]) ||
|
|
||||||
!bytes.Equal(magicHeader2, d.file[8:12]) {
|
|
||||||
return ErrMagicHeader
|
|
||||||
}
|
|
||||||
|
|
||||||
var ok bool
|
|
||||||
d.outputExt, ok = typeMapping[string(d.file[4:8])]
|
|
||||||
if !ok {
|
|
||||||
return errors.New("detect unknown xm file type: " + string(d.file[4:8]))
|
|
||||||
}
|
|
||||||
|
|
||||||
if d.file[14] != 0 {
|
|
||||||
logging.Log().Warn("not a simple xm file", zap.Uint8("b[14]", d.file[14]))
|
|
||||||
}
|
|
||||||
d.headerLen = uint32(d.file[12]) | uint32(d.file[13])<<8 | uint32(d.file[14])<<16 // LittleEndian Unit24
|
|
||||||
if d.headerLen+16 > uint32(lenData) {
|
|
||||||
return ErrFileSize
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Decoder) Decode() error {
|
|
||||||
d.mask = d.file[15]
|
|
||||||
d.audio = d.file[16:]
|
|
||||||
dataLen := uint32(len(d.audio))
|
|
||||||
for i := d.headerLen; i < dataLen; i++ {
|
|
||||||
d.audio[i] = ^(d.audio[i] - d.mask)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
|
|
||||||
return func(file []byte) common.Decoder {
|
|
||||||
return &Decoder{file: file, outputExt: ext}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
// Xiami Wav/M4a/Mp3/Flac
|
|
||||||
common.RegisterDecoder("xm", false, NewDecoder)
|
|
||||||
// Xiami Typed Format
|
|
||||||
common.RegisterDecoder("wav", false, DecoderFuncWithExt("wav"))
|
|
||||||
common.RegisterDecoder("mp3", false, DecoderFuncWithExt("mp3"))
|
|
||||||
common.RegisterDecoder("flac", false, DecoderFuncWithExt("flac"))
|
|
||||||
common.RegisterDecoder("m4a", false, DecoderFuncWithExt("m4a"))
|
|
||||||
}
|
|
||||||
422
cmd/um/main.go
422
cmd/um/main.go
@@ -1,63 +1,125 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"runtime/debug"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
_ "git.um-react.app/um/cli/algo/kgm"
|
||||||
|
_ "git.um-react.app/um/cli/algo/kwm"
|
||||||
|
_ "git.um-react.app/um/cli/algo/ncm"
|
||||||
|
"git.um-react.app/um/cli/algo/qmc"
|
||||||
|
_ "git.um-react.app/um/cli/algo/tm"
|
||||||
|
_ "git.um-react.app/um/cli/algo/xiami"
|
||||||
|
_ "git.um-react.app/um/cli/algo/ximalaya"
|
||||||
|
"git.um-react.app/um/cli/internal/ffmpeg"
|
||||||
|
"git.um-react.app/um/cli/internal/sniff"
|
||||||
|
"git.um-react.app/um/cli/internal/utils"
|
||||||
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
"github.com/unlock-music/cli/algo/common"
|
|
||||||
_ "github.com/unlock-music/cli/algo/kgm"
|
|
||||||
_ "github.com/unlock-music/cli/algo/kwm"
|
|
||||||
_ "github.com/unlock-music/cli/algo/ncm"
|
|
||||||
_ "github.com/unlock-music/cli/algo/qmc"
|
|
||||||
_ "github.com/unlock-music/cli/algo/tm"
|
|
||||||
_ "github.com/unlock-music/cli/algo/xm"
|
|
||||||
"github.com/unlock-music/cli/internal/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var AppVersion = "v0.0.6"
|
var AppVersion = "custom"
|
||||||
|
|
||||||
|
var logger = setupLogger(false) // TODO: inject logger to application, instead of using global logger
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
module, ok := debug.ReadBuildInfo()
|
||||||
|
if ok && module.Main.Version != "(devel)" {
|
||||||
|
AppVersion = module.Main.Version
|
||||||
|
}
|
||||||
app := cli.App{
|
app := cli.App{
|
||||||
Name: "Unlock Music CLI",
|
Name: "Unlock Music CLI",
|
||||||
HelpName: "um",
|
HelpName: "um",
|
||||||
Usage: "Unlock your encrypted music file https://github.com/unlock-music/cli",
|
Usage: "Unlock your encrypted music file https://git.um-react.app/um/cli",
|
||||||
Version: fmt.Sprintf("%s (%s,%s/%s)", AppVersion, runtime.Version(), runtime.GOOS, runtime.GOARCH),
|
Version: fmt.Sprintf("%s (%s,%s/%s)", AppVersion, runtime.Version(), runtime.GOOS, runtime.GOARCH),
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{Name: "input", Aliases: []string{"i"}, Usage: "path to input file or dir", Required: false},
|
&cli.StringFlag{Name: "input", Aliases: []string{"i"}, Usage: "path to input file or dir", Required: false},
|
||||||
&cli.StringFlag{Name: "output", Aliases: []string{"o"}, Usage: "path to output dir", Required: false},
|
&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 QQMusic mmkv path", Required: false},
|
||||||
|
&cli.StringFlag{Name: "qmc-mmkv-key", Aliases: []string{"key"}, Usage: "QQMusic 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: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true},
|
||||||
&cli.BoolFlag{Name: "supported-ext", Usage: "Show supported file extensions and exit", Required: false, Value: false},
|
&cli.BoolFlag{Name: "verbose", Aliases: []string{"V"}, Usage: "verbose logging", Required: false, Value: false},
|
||||||
|
&cli.BoolFlag{Name: "update-metadata", Usage: "update metadata & album art from network", Required: false, Value: false},
|
||||||
|
&cli.BoolFlag{Name: "overwrite", Usage: "overwrite output file without asking", Required: false, Value: false},
|
||||||
|
&cli.BoolFlag{Name: "watch", Usage: "watch the input dir and process new files", Required: false, Value: false},
|
||||||
|
|
||||||
|
&cli.BoolFlag{Name: "supported-ext", Usage: "show supported file extensions and exit", Required: false, Value: false},
|
||||||
},
|
},
|
||||||
|
|
||||||
Action: appMain,
|
Action: appMain,
|
||||||
Copyright: "Copyright (c) 2020 - 2021 Unlock Music https://github.com/unlock-music/cli/blob/master/LICENSE",
|
Copyright: fmt.Sprintf("Copyright (c) 2020 - %d Unlock Music https://git.um-react.app/um/cli/src/branch/main/LICENSE", time.Now().Year()),
|
||||||
HideHelpCommand: true,
|
HideHelpCommand: true,
|
||||||
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input",
|
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := app.Run(os.Args)
|
err := app.Run(os.Args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Log().Fatal("run app failed", zap.Error(err))
|
logger.Fatal("run app failed", zap.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printSupportedExtensions() {
|
func printSupportedExtensions() {
|
||||||
exts := []string{}
|
var exts []string
|
||||||
for ext := range common.DecoderRegistry {
|
extSet := make(map[string]int)
|
||||||
|
for _, factory := range common.DecoderRegistry {
|
||||||
|
ext := strings.TrimPrefix(factory.Suffix, ".")
|
||||||
|
if n, ok := extSet[ext]; ok {
|
||||||
|
extSet[ext] = n + 1
|
||||||
|
} else {
|
||||||
|
extSet[ext] = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for ext := range extSet {
|
||||||
exts = append(exts, ext)
|
exts = append(exts, ext)
|
||||||
}
|
}
|
||||||
sort.Strings(exts)
|
sort.Strings(exts)
|
||||||
for _, ext := range exts {
|
for _, ext := range exts {
|
||||||
fmt.Printf("%s: %d\n", ext, len(common.DecoderRegistry[ext]))
|
fmt.Printf("%s: %d\n", ext, extSet[ext])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupLogger(verbose bool) *zap.Logger {
|
||||||
|
logConfig := zap.NewProductionEncoderConfig()
|
||||||
|
logConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||||
|
logConfig.EncodeTime = zapcore.RFC3339TimeEncoder
|
||||||
|
enabler := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
|
||||||
|
if verbose {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return level >= zapcore.InfoLevel
|
||||||
|
})
|
||||||
|
|
||||||
|
return zap.New(zapcore.NewCore(
|
||||||
|
zapcore.NewConsoleEncoder(logConfig),
|
||||||
|
os.Stdout,
|
||||||
|
enabler,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
func appMain(c *cli.Context) (err error) {
|
func appMain(c *cli.Context) (err error) {
|
||||||
|
logger = setupLogger(c.Bool("verbose"))
|
||||||
|
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if c.Bool("supported-ext") {
|
if c.Bool("supported-ext") {
|
||||||
printSupportedExtensions()
|
printSupportedExtensions()
|
||||||
return nil
|
return nil
|
||||||
@@ -66,10 +128,7 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
if input == "" {
|
if input == "" {
|
||||||
switch c.Args().Len() {
|
switch c.Args().Len() {
|
||||||
case 0:
|
case 0:
|
||||||
input, err = os.Getwd()
|
input = cwd
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case 1:
|
case 1:
|
||||||
input = c.Args().Get(0)
|
input = c.Args().Get(0)
|
||||||
default:
|
default:
|
||||||
@@ -77,25 +136,34 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
input, absErr := filepath.Abs(input)
|
||||||
|
if absErr != nil {
|
||||||
|
return fmt.Errorf("get abs path failed: %w", absErr)
|
||||||
|
}
|
||||||
|
|
||||||
output := c.String("output")
|
output := c.String("output")
|
||||||
if output == "" {
|
|
||||||
var err error
|
|
||||||
output, err = os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if input == output {
|
|
||||||
return errors.New("input and output path are same")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
skipNoop := c.Bool("skip-noop")
|
|
||||||
|
|
||||||
inputStat, err := os.Stat(input)
|
inputStat, err := os.Stat(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var inputDir string
|
||||||
|
if inputStat.IsDir() {
|
||||||
|
inputDir = input
|
||||||
|
} else {
|
||||||
|
inputDir = filepath.Dir(input)
|
||||||
|
}
|
||||||
|
inputDir, absErr = filepath.Abs(inputDir)
|
||||||
|
if absErr != nil {
|
||||||
|
return fmt.Errorf("get abs path (inputDir) failed: %w", absErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if output == "" {
|
||||||
|
// Default to where the input dir is
|
||||||
|
output = inputDir
|
||||||
|
}
|
||||||
|
logger.Debug("resolve input/output path", zap.String("inputDir", inputDir), zap.String("input", input), zap.String("output", output))
|
||||||
|
|
||||||
outputStat, err := os.Stat(output)
|
outputStat, err := os.Stat(output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
@@ -108,79 +176,289 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
return errors.New("output should be a writable directory")
|
return errors.New("output should be a writable directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
if inputStat.IsDir() {
|
// QMC: Load keys
|
||||||
return dealDirectory(input, output, skipNoop)
|
qmcKeys, err := qmc.LoadMMKVOrDefault(c.String("qmc-mmkv"), c.String("qmc-mmkv-key"), logger)
|
||||||
} else {
|
if err != nil {
|
||||||
allDec := common.GetDecoder(inputStat.Name(), skipNoop)
|
return err
|
||||||
if len(allDec) == 0 {
|
|
||||||
logging.Log().Fatal("skipping while no suitable decoder")
|
|
||||||
}
|
}
|
||||||
return tryDecFile(input, output, allDec)
|
|
||||||
|
kggDbPath := c.String("kgg-db")
|
||||||
|
if kggDbPath == "" {
|
||||||
|
kggDbPath = filepath.Join(os.Getenv("APPDATA"), "Kugou8", "KGMusicV3.db")
|
||||||
|
}
|
||||||
|
|
||||||
|
proc := &processor{
|
||||||
|
logger: logger,
|
||||||
|
inputDir: inputDir,
|
||||||
|
outputDir: output,
|
||||||
|
skipNoopDecoder: c.Bool("skip-noop"),
|
||||||
|
removeSource: c.Bool("remove-source"),
|
||||||
|
updateMetadata: c.Bool("update-metadata"),
|
||||||
|
overwriteOutput: c.Bool("overwrite"),
|
||||||
|
|
||||||
|
crypto: common.CryptoParams{
|
||||||
|
// KuGou
|
||||||
|
KggDbPath: kggDbPath,
|
||||||
|
|
||||||
|
// QQMusic
|
||||||
|
QmcKeys: qmcKeys,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if inputStat.IsDir() {
|
||||||
|
watchDir := c.Bool("watch")
|
||||||
|
if !watchDir {
|
||||||
|
return proc.processDir(input)
|
||||||
|
} else {
|
||||||
|
return proc.watchDir(input)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return proc.processFile(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
func dealDirectory(inputDir string, outputDir string, skipNoop bool) error {
|
|
||||||
|
type processor struct {
|
||||||
|
logger *zap.Logger
|
||||||
|
inputDir string
|
||||||
|
outputDir string
|
||||||
|
|
||||||
|
skipNoopDecoder bool
|
||||||
|
removeSource bool
|
||||||
|
updateMetadata bool
|
||||||
|
overwriteOutput bool
|
||||||
|
|
||||||
|
crypto common.CryptoParams
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *processor) watchDir(inputDir string) error {
|
||||||
|
if err := p.processDir(inputDir); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
watcher, err := fsnotify.NewWatcher()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create watcher: %w", err)
|
||||||
|
}
|
||||||
|
defer watcher.Close()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case event, ok := <-watcher.Events:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.Has(fsnotify.Create) || event.Has(fsnotify.Write) {
|
||||||
|
// try open with exclusive mode, to avoid file is still writing
|
||||||
|
f, err := os.OpenFile(event.Name, os.O_RDONLY, os.ModeExclusive)
|
||||||
|
if err != nil {
|
||||||
|
logger.Debug("failed to open file exclusively", zap.String("path", event.Name), zap.Error(err))
|
||||||
|
time.Sleep(1 * time.Second) // wait for file writing complete
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_ = f.Close()
|
||||||
|
|
||||||
|
if err := p.processFile(event.Name); err != nil {
|
||||||
|
logger.Warn("failed to process file", zap.String("path", event.Name), zap.Error(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case err, ok := <-watcher.Errors:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.Error("file watcher got error", zap.Error(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = watcher.Add(inputDir)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to watch dir %s: %w", inputDir, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
signalCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||||
|
defer stop()
|
||||||
|
|
||||||
|
<-signalCtx.Done()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *processor) processDir(inputDir string) error {
|
||||||
items, err := os.ReadDir(inputDir)
|
items, err := os.ReadDir(inputDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var lastError error = nil
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
|
filePath := filepath.Join(inputDir, item.Name())
|
||||||
if item.IsDir() {
|
if item.IsDir() {
|
||||||
|
if err = p.processDir(filePath); err != nil {
|
||||||
|
lastError = err
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
allDec := common.GetDecoder(item.Name(), skipNoop)
|
|
||||||
|
if err := p.processFile(filePath); err != nil {
|
||||||
|
lastError = err
|
||||||
|
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if lastError != nil {
|
||||||
|
return fmt.Errorf("last error: %w", lastError)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *processor) processFile(filePath string) error {
|
||||||
|
p.logger.Debug("processFile", zap.String("file", filePath), zap.String("inputDir", p.inputDir))
|
||||||
|
|
||||||
|
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
|
||||||
if len(allDec) == 0 {
|
if len(allDec) == 0 {
|
||||||
logging.Log().Info("skipping while no suitable decoder", zap.String("file", item.Name()))
|
return errors.New("skipping while no suitable decoder")
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := tryDecFile(filepath.Join(inputDir, item.Name()), outputDir, allDec)
|
if err := p.process(filePath, allDec); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
logging.Log().Error("conversion failed", zap.String("source", item.Name()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if source file need to be removed
|
||||||
|
if p.removeSource {
|
||||||
|
err := os.RemoveAll(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logger.Info("source file removed after success conversion", zap.String("source", filePath))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFunc) error {
|
func (p *processor) findDecoder(decoders []common.DecoderFactory, params *common.DecoderParams) (*common.Decoder, *common.DecoderFactory, error) {
|
||||||
file, err := os.ReadFile(inputFile)
|
for _, factory := range decoders {
|
||||||
|
dec := factory.Create(params)
|
||||||
|
err := dec.Validate()
|
||||||
|
if err == nil {
|
||||||
|
return &dec, &factory, nil
|
||||||
|
}
|
||||||
|
logger.Warn("try decode failed", zap.Error(err))
|
||||||
|
}
|
||||||
|
return nil, nil, errors.New("no any decoder can resolve the file")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *processor) process(inputFile string, allDec []common.DecoderFactory) error {
|
||||||
|
file, err := os.Open(inputFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
logger := logger.With(zap.String("source", inputFile))
|
||||||
|
|
||||||
var dec common.Decoder
|
pDec, decoderFactory, err := p.findDecoder(allDec, &common.DecoderParams{
|
||||||
for _, decFunc := range allDec {
|
Reader: file,
|
||||||
dec = decFunc(file)
|
Extension: filepath.Ext(inputFile),
|
||||||
if err := dec.Validate(); err == nil {
|
FilePath: inputFile,
|
||||||
break
|
Logger: logger,
|
||||||
|
CryptoParams: p.crypto,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
logging.Log().Warn("try decode failed", zap.Error(err))
|
dec := *pDec
|
||||||
dec = nil
|
|
||||||
|
params := &ffmpeg.UpdateMetadataParams{}
|
||||||
|
|
||||||
|
header := bytes.NewBuffer(nil)
|
||||||
|
_, err = io.CopyN(header, dec, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("read header failed: %w", err)
|
||||||
}
|
}
|
||||||
if dec == nil {
|
audio := io.MultiReader(header, dec)
|
||||||
return errors.New("no any decoder can resolve the file")
|
params.AudioExt = sniff.AudioExtensionWithFallback(header.Bytes(), ".mp3")
|
||||||
|
|
||||||
|
if p.updateMetadata {
|
||||||
|
if audioMetaGetter, ok := dec.(common.AudioMetaGetter); ok {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// since ffmpeg doesn't support multiple input streams,
|
||||||
|
// we need to write the audio to a temp file.
|
||||||
|
// since qmc decoder doesn't support seeking & relying on ffmpeg probe, we need to read the whole file.
|
||||||
|
// TODO: support seeking or using pipe for qmc decoder.
|
||||||
|
params.Audio, err = utils.WriteTempFile(audio, params.AudioExt)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("updateAudioMeta write temp file: %w", err)
|
||||||
}
|
}
|
||||||
if err := dec.Decode(); err != nil {
|
defer os.Remove(params.Audio)
|
||||||
return errors.New("failed while decoding: " + err.Error())
|
|
||||||
|
params.Meta, err = audioMetaGetter.GetAudioMeta(ctx)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("get audio meta failed", zap.Error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
outData := dec.GetAudioData()
|
if params.Meta == nil { // reset audio meta if failed
|
||||||
outExt := dec.GetAudioExt()
|
audio, err = os.Open(params.Audio)
|
||||||
if outExt == "" {
|
if err != nil {
|
||||||
if ext, ok := common.SniffAll(outData); ok {
|
return fmt.Errorf("updateAudioMeta open temp file: %w", err)
|
||||||
outExt = ext
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.updateMetadata && params.Meta != nil {
|
||||||
|
if coverGetter, ok := dec.(common.CoverImageGetter); ok {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if cover, err := coverGetter.GetCoverImage(ctx); err != nil {
|
||||||
|
logger.Warn("get cover image failed", zap.Error(err))
|
||||||
|
} else if imgExt, ok := sniff.ImageExtension(cover); !ok {
|
||||||
|
logger.Warn("sniff cover image type failed", zap.Error(err))
|
||||||
} else {
|
} else {
|
||||||
outExt = ".mp3"
|
params.AlbumArtExt = imgExt
|
||||||
|
params.AlbumArt = cover
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
filenameOnly := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
|
|
||||||
|
|
||||||
outPath := filepath.Join(outputDir, filenameOnly+outExt)
|
inputRelDir, err := filepath.Rel(p.inputDir, filepath.Dir(inputFile))
|
||||||
err = os.WriteFile(outPath, outData, 0644)
|
if err != nil {
|
||||||
|
return fmt.Errorf("get relative dir failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
inFilename := strings.TrimSuffix(filepath.Base(inputFile), decoderFactory.Suffix)
|
||||||
|
outPath := filepath.Join(p.outputDir, inputRelDir, inFilename+params.AudioExt)
|
||||||
|
|
||||||
|
if !p.overwriteOutput {
|
||||||
|
_, err := os.Stat(outPath)
|
||||||
|
if err == nil {
|
||||||
|
logger.Warn("output file already exist, skip", zap.String("destination", outPath))
|
||||||
|
return nil
|
||||||
|
} else if !errors.Is(err, os.ErrNotExist) {
|
||||||
|
return fmt.Errorf("stat output file failed: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if params.Meta == nil {
|
||||||
|
outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logging.Log().Info("successfully converted",
|
defer outFile.Close()
|
||||||
zap.String("source", inputFile), zap.String("destination", outPath))
|
|
||||||
|
if _, err := io.Copy(outFile, audio); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if err := ffmpeg.UpdateMeta(ctx, outPath, params, logger); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
35
go.mod
35
go.mod
@@ -1,17 +1,34 @@
|
|||||||
module github.com/unlock-music/cli
|
module git.um-react.app/um/cli
|
||||||
|
|
||||||
go 1.17
|
go 1.25.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ulikunitz/xz v0.5.10
|
github.com/fsnotify/fsnotify v1.9.0
|
||||||
github.com/urfave/cli/v2 v2.3.0
|
github.com/go-flac/flacpicture v0.3.0
|
||||||
go.uber.org/zap v1.19.1
|
github.com/go-flac/flacvorbis v0.2.0
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
|
github.com/go-flac/go-flac v1.0.0
|
||||||
|
github.com/samber/lo v1.52.0
|
||||||
|
github.com/unlock-music/go-mmkv v0.1.3
|
||||||
|
github.com/urfave/cli/v2 v2.27.7
|
||||||
|
go.uber.org/zap v1.27.0
|
||||||
|
golang.org/x/crypto v0.44.0
|
||||||
|
golang.org/x/text v0.31.0
|
||||||
|
modernc.org/sqlite v1.40.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
|
github.com/cpuguy83/go-md2man/v2 v2.0.7 // 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 v1.0.0 // indirect
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
go.uber.org/atomic v1.9.0 // indirect
|
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 // indirect
|
||||||
go.uber.org/multierr v1.7.0 // indirect
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
|
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
||||||
|
golang.org/x/sys v0.38.0 // indirect
|
||||||
|
modernc.org/libc v1.66.10 // indirect
|
||||||
|
modernc.org/mathutil v1.7.1 // indirect
|
||||||
|
modernc.org/memory v1.11.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
148
go.sum
148
go.sum
@@ -1,73 +1,87 @@
|
|||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
|
||||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
|
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
|
||||||
github.com/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=
|
||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
|
||||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/go-flac/flacpicture v0.3.0 h1:LkmTxzFLIynwfhHiZsX0s8xcr3/u33MzvV89u+zOT8I=
|
||||||
|
github.com/go-flac/flacpicture v0.3.0/go.mod h1:DPbrzVYQ3fJcvSgLFp9HXIrEQEdfdk/+m0nQCzwodZI=
|
||||||
|
github.com/go-flac/flacvorbis v0.2.0 h1:KH0xjpkNTXFER4cszH4zeJxYcrHbUobz/RticWGOESs=
|
||||||
|
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/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||||
|
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||||
|
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 v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||||
|
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||||
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/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 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/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/unlock-music/go-mmkv v0.1.3 h1:BtO77wjmypCeaX3n8VRDX5ZWHlYdJ9xEfKGui9s4PM0=
|
||||||
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
|
github.com/unlock-music/go-mmkv v0.1.3/go.mod h1:u1fYHzYOO7et1E55Q86lTvjXf6p14CIELszAHI8T9zQ=
|
||||||
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU=
|
||||||
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
|
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
|
||||||
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
|
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 h1:FnBeRrxr7OU4VvAzt5X7s6266i6cSVkkFPS0TuXWbIg=
|
||||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
|
||||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||||
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723 h1:sHOAIxRGBp443oHZIPB+HsUGaksVCXVQENPxwTfQdH4=
|
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||||
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||||
go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
|
golang.org/x/crypto v0.44.0 h1:A97SsFvM3AIwEEmTBiaxPPTYpDC47w720rdiiUvgoAU=
|
||||||
go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
|
golang.org/x/crypto v0.44.0/go.mod h1:013i+Nw79BMiQiMsOPcVCB5ZIJbYkerPrGnOa00tvmc=
|
||||||
go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI=
|
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
|
||||||
go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI=
|
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
|
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
|
||||||
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=
|
||||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
modernc.org/cc/v4 v4.26.5 h1:xM3bX7Mve6G8K8b+T11ReenJOT+BmVqQj0FY5T4+5Y4=
|
||||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
modernc.org/cc/v4 v4.26.5/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
|
||||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
modernc.org/ccgo/v4 v4.28.1 h1:wPKYn5EC/mYTqBO373jKjvX2n+3+aK7+sICCv4Fjy1A=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
modernc.org/ccgo/v4 v4.28.1/go.mod h1:uD+4RnfrVgE6ec9NGguUNdhqzNIeeomeXf6CL0GTE5Q=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
modernc.org/fileutil v1.3.40 h1:ZGMswMNc9JOCrcrakF1HrvmergNLAmxOPjizirpfqBA=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
modernc.org/fileutil v1.3.40/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
modernc.org/libc v1.66.10 h1:yZkb3YeLx4oynyR+iUsXsybsX4Ubx7MQlSYEw4yj59A=
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
modernc.org/libc v1.66.10/go.mod h1:8vGSEwvoUoltr4dlywvHqjtAqHBaw0j1jI7iFBTAr2I=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||||
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
|
||||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
modernc.org/sqlite v1.40.0 h1:bNWEDlYhNPAUdUdBzjAvn8icAs/2gaKlj4vM+tQ6KdQ=
|
||||||
|
modernc.org/sqlite v1.40.0/go.mod h1:9fjQZ0mB1LLP0GYrp39oOJXx/I2sxEnZtzCmEQIKvGE=
|
||||||
|
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||||
|
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||||
|
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||||
|
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||||
|
|||||||
127
internal/ffmpeg/ffmpeg.go
Normal file
127
internal/ffmpeg/ffmpeg.go
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"git.um-react.app/um/cli/internal/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExtractAlbumArt(ctx context.Context, rd io.Reader) (*bytes.Buffer, error) {
|
||||||
|
cmd := exec.CommandContext(ctx, "ffmpeg",
|
||||||
|
"-i", "pipe:0", // input from stdin
|
||||||
|
"-an", // disable audio
|
||||||
|
"-codec:v", "copy", // copy video(image) codec
|
||||||
|
"-f", "image2", // use image2 muxer
|
||||||
|
"pipe:1", // output to stdout
|
||||||
|
)
|
||||||
|
|
||||||
|
cmd.Stdin = rd
|
||||||
|
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
|
||||||
|
cmd.Stdout, cmd.Stderr = stdout, stderr
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return nil, fmt.Errorf("ffmpeg run: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return stdout, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateMetadataParams struct {
|
||||||
|
Audio string // required
|
||||||
|
AudioExt string // required
|
||||||
|
|
||||||
|
Meta common.AudioMeta // required
|
||||||
|
|
||||||
|
AlbumArt []byte // optional
|
||||||
|
AlbumArtExt string // required if AlbumArt is not nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateMeta(ctx context.Context, outPath string, params *UpdateMetadataParams, logger *zap.Logger) error {
|
||||||
|
if params.AudioExt == ".flac" {
|
||||||
|
return updateMetaFlac(ctx, outPath, params, logger.With(zap.String("module", "updateMetaFlac")))
|
||||||
|
} else {
|
||||||
|
return updateMetaFFmpeg(ctx, outPath, params)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func updateMetaFFmpeg(ctx context.Context, outPath string, params *UpdateMetadataParams) error {
|
||||||
|
builder := newFFmpegBuilder()
|
||||||
|
|
||||||
|
out := newOutputBuilder(outPath) // output to file
|
||||||
|
builder.SetFlag("y") // overwrite output file
|
||||||
|
builder.AddOutput(out)
|
||||||
|
|
||||||
|
// input audio -> output audio
|
||||||
|
builder.AddInput(newInputBuilder(params.Audio)) // input 0: audio
|
||||||
|
out.AddOption("map", "0:a")
|
||||||
|
out.AddOption("codec:a", "copy")
|
||||||
|
|
||||||
|
// input cover -> output cover
|
||||||
|
if params.AlbumArt != nil &&
|
||||||
|
params.AudioExt != ".wav" /* wav doesn't support attached image */ {
|
||||||
|
|
||||||
|
// write cover to temp file
|
||||||
|
artPath, err := utils.WriteTempFile(bytes.NewReader(params.AlbumArt), params.AlbumArtExt)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("updateAudioMeta write temp file: %w", err)
|
||||||
|
}
|
||||||
|
defer os.Remove(artPath)
|
||||||
|
|
||||||
|
builder.AddInput(newInputBuilder(artPath)) // input 1: cover
|
||||||
|
out.AddOption("map", "1:v")
|
||||||
|
|
||||||
|
switch params.AudioExt {
|
||||||
|
case ".ogg": // ogg only supports theora codec
|
||||||
|
out.AddOption("codec:v", "libtheora")
|
||||||
|
case ".m4a": // .m4a(mp4) requires set codec, disposition, stream metadata
|
||||||
|
out.AddOption("codec:v", "mjpeg")
|
||||||
|
out.AddOption("disposition:v", "attached_pic")
|
||||||
|
out.AddMetadata("s:v", "title", "Album cover")
|
||||||
|
out.AddMetadata("s:v", "comment", "Cover (front)")
|
||||||
|
case ".mp3":
|
||||||
|
out.AddOption("codec:v", "mjpeg")
|
||||||
|
out.AddMetadata("s:v", "title", "Album cover")
|
||||||
|
out.AddMetadata("s:v", "comment", "Cover (front)")
|
||||||
|
default: // other formats use default behavior
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set file metadata
|
||||||
|
album := params.Meta.GetAlbum()
|
||||||
|
if album != "" {
|
||||||
|
out.AddMetadata("", "album", album)
|
||||||
|
}
|
||||||
|
|
||||||
|
title := params.Meta.GetTitle()
|
||||||
|
if album != "" {
|
||||||
|
out.AddMetadata("", "title", title)
|
||||||
|
}
|
||||||
|
|
||||||
|
artists := params.Meta.GetArtists()
|
||||||
|
if len(artists) != 0 {
|
||||||
|
// TODO: it seems that ffmpeg doesn't support multiple artists
|
||||||
|
out.AddMetadata("", "artist", strings.Join(artists, " / "))
|
||||||
|
}
|
||||||
|
|
||||||
|
if params.AudioExt == ".mp3" {
|
||||||
|
out.AddOption("write_id3v1", "true")
|
||||||
|
out.AddOption("id3v2_version", "3")
|
||||||
|
}
|
||||||
|
|
||||||
|
// execute ffmpeg
|
||||||
|
cmd := builder.Command(ctx)
|
||||||
|
|
||||||
|
if stdout, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
return fmt.Errorf("ffmpeg run: %w, %s", err, string(stdout))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
141
internal/ffmpeg/ffprobe.go
Normal file
141
internal/ffmpeg/ffprobe.go
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Result struct {
|
||||||
|
Format *Format `json:"format"`
|
||||||
|
Streams []*Stream `json:"streams"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Result) HasAttachedPic() bool {
|
||||||
|
return lo.ContainsBy(r.Streams, func(s *Stream) bool {
|
||||||
|
return s.CodecType == "video"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Result) getTagByKey(key string) string {
|
||||||
|
for k, v := range r.Format.Tags {
|
||||||
|
if key == strings.ToLower(k) {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, stream := range r.Streams { // try to find in streams
|
||||||
|
if stream.CodecType != "audio" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for k, v := range stream.Tags {
|
||||||
|
if key == strings.ToLower(k) {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
func (r *Result) GetTitle() string {
|
||||||
|
return r.getTagByKey("title")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Result) GetAlbum() string {
|
||||||
|
return r.getTagByKey("album")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Result) GetArtists() []string {
|
||||||
|
artists := strings.Split(r.getTagByKey("artist"), "/")
|
||||||
|
for i := range artists {
|
||||||
|
artists[i] = strings.TrimSpace(artists[i])
|
||||||
|
}
|
||||||
|
return artists
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Result) HasMetadata() bool {
|
||||||
|
return r.GetTitle() != "" || r.GetAlbum() != "" || len(r.GetArtists()) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type Format struct {
|
||||||
|
Filename string `json:"filename"`
|
||||||
|
NbStreams int `json:"nb_streams"`
|
||||||
|
NbPrograms int `json:"nb_programs"`
|
||||||
|
FormatName string `json:"format_name"`
|
||||||
|
FormatLongName string `json:"format_long_name"`
|
||||||
|
StartTime string `json:"start_time"`
|
||||||
|
Duration string `json:"duration"`
|
||||||
|
BitRate string `json:"bit_rate"`
|
||||||
|
ProbeScore int `json:"probe_score"`
|
||||||
|
Tags map[string]string `json:"tags"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Stream struct {
|
||||||
|
Index int `json:"index"`
|
||||||
|
CodecName string `json:"codec_name"`
|
||||||
|
CodecLongName string `json:"codec_long_name"`
|
||||||
|
CodecType string `json:"codec_type"`
|
||||||
|
CodecTagString string `json:"codec_tag_string"`
|
||||||
|
CodecTag string `json:"codec_tag"`
|
||||||
|
SampleFmt string `json:"sample_fmt"`
|
||||||
|
SampleRate string `json:"sample_rate"`
|
||||||
|
Channels int `json:"channels"`
|
||||||
|
ChannelLayout string `json:"channel_layout"`
|
||||||
|
BitsPerSample int `json:"bits_per_sample"`
|
||||||
|
RFrameRate string `json:"r_frame_rate"`
|
||||||
|
AvgFrameRate string `json:"avg_frame_rate"`
|
||||||
|
TimeBase string `json:"time_base"`
|
||||||
|
StartPts int `json:"start_pts"`
|
||||||
|
StartTime string `json:"start_time"`
|
||||||
|
BitRate string `json:"bit_rate"`
|
||||||
|
Disposition *ProbeDisposition `json:"disposition"`
|
||||||
|
Tags map[string]string `json:"tags"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProbeDisposition struct {
|
||||||
|
Default int `json:"default"`
|
||||||
|
Dub int `json:"dub"`
|
||||||
|
Original int `json:"original"`
|
||||||
|
Comment int `json:"comment"`
|
||||||
|
Lyrics int `json:"lyrics"`
|
||||||
|
Karaoke int `json:"karaoke"`
|
||||||
|
Forced int `json:"forced"`
|
||||||
|
HearingImpaired int `json:"hearing_impaired"`
|
||||||
|
VisualImpaired int `json:"visual_impaired"`
|
||||||
|
CleanEffects int `json:"clean_effects"`
|
||||||
|
AttachedPic int `json:"attached_pic"`
|
||||||
|
TimedThumbnails int `json:"timed_thumbnails"`
|
||||||
|
Captions int `json:"captions"`
|
||||||
|
Descriptions int `json:"descriptions"`
|
||||||
|
Metadata int `json:"metadata"`
|
||||||
|
Dependent int `json:"dependent"`
|
||||||
|
StillImage int `json:"still_image"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProbeReader(ctx context.Context, rd io.Reader) (*Result, error) {
|
||||||
|
cmd := exec.CommandContext(ctx, "ffprobe",
|
||||||
|
"-v", "quiet", // disable logging
|
||||||
|
"-print_format", "json", // use json format
|
||||||
|
"-show_format", "-show_streams", "-show_error", // retrieve format and streams
|
||||||
|
"pipe:0", // input from stdin
|
||||||
|
)
|
||||||
|
|
||||||
|
cmd.Stdin = rd
|
||||||
|
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
|
||||||
|
cmd.Stdout, cmd.Stderr = stdout, stderr
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := new(Result)
|
||||||
|
if err := json.Unmarshal(stdout.Bytes(), ret); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
95
internal/ffmpeg/meta_flac.go
Normal file
95
internal/ffmpeg/meta_flac.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"mime"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
|
|
||||||
|
"github.com/go-flac/flacpicture"
|
||||||
|
"github.com/go-flac/flacvorbis"
|
||||||
|
"github.com/go-flac/go-flac"
|
||||||
|
)
|
||||||
|
|
||||||
|
func updateMetaFlac(_ context.Context, outPath string, m *UpdateMetadataParams, logger *zap.Logger) error {
|
||||||
|
f, err := flac.ParseFile(m.Audio)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate comment block
|
||||||
|
comment := flacvorbis.MetaDataBlockVorbisComment{Vendor: "unlock-music.dev"}
|
||||||
|
|
||||||
|
// add metadata
|
||||||
|
title := m.Meta.GetTitle()
|
||||||
|
if title != "" {
|
||||||
|
_ = comment.Add(flacvorbis.FIELD_TITLE, title)
|
||||||
|
}
|
||||||
|
|
||||||
|
album := m.Meta.GetAlbum()
|
||||||
|
if album != "" {
|
||||||
|
_ = comment.Add(flacvorbis.FIELD_ALBUM, album)
|
||||||
|
}
|
||||||
|
|
||||||
|
artists := m.Meta.GetArtists()
|
||||||
|
for _, artist := range artists {
|
||||||
|
_ = comment.Add(flacvorbis.FIELD_ARTIST, artist)
|
||||||
|
}
|
||||||
|
|
||||||
|
existCommentIdx := slices.IndexFunc(f.Meta, func(b *flac.MetaDataBlock) bool {
|
||||||
|
return b.Type == flac.VorbisComment
|
||||||
|
})
|
||||||
|
if existCommentIdx >= 0 { // copy existing comment fields
|
||||||
|
exist, err := flacvorbis.ParseFromMetaDataBlock(*f.Meta[existCommentIdx])
|
||||||
|
if err != nil {
|
||||||
|
for _, s := range exist.Comments {
|
||||||
|
if strings.HasPrefix(s, flacvorbis.FIELD_TITLE+"=") && title != "" ||
|
||||||
|
strings.HasPrefix(s, flacvorbis.FIELD_ALBUM+"=") && album != "" ||
|
||||||
|
strings.HasPrefix(s, flacvorbis.FIELD_ARTIST+"=") && len(artists) != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
comment.Comments = append(comment.Comments, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add / replace flac comment
|
||||||
|
cmtBlock := comment.Marshal()
|
||||||
|
if existCommentIdx < 0 {
|
||||||
|
f.Meta = append(f.Meta, &cmtBlock)
|
||||||
|
} else {
|
||||||
|
f.Meta[existCommentIdx] = &cmtBlock
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.AlbumArt != nil {
|
||||||
|
coverMime := mime.TypeByExtension(m.AlbumArtExt)
|
||||||
|
logger.Debug("cover image mime detect", zap.String("mime", coverMime))
|
||||||
|
cover, err := flacpicture.NewFromImageData(
|
||||||
|
flacpicture.PictureTypeFrontCover,
|
||||||
|
"Front cover",
|
||||||
|
m.AlbumArt,
|
||||||
|
coverMime,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("failed to create flac cover", zap.Error(err))
|
||||||
|
} else {
|
||||||
|
coverBlock := cover.Marshal()
|
||||||
|
f.Meta = append(f.Meta, &coverBlock)
|
||||||
|
|
||||||
|
// add / replace flac cover
|
||||||
|
coverIdx := slices.IndexFunc(f.Meta, func(b *flac.MetaDataBlock) bool {
|
||||||
|
return b.Type == flac.Picture
|
||||||
|
})
|
||||||
|
if coverIdx < 0 {
|
||||||
|
f.Meta = append(f.Meta, &coverBlock)
|
||||||
|
} else {
|
||||||
|
f.Meta[coverIdx] = &coverBlock
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.Save(outPath)
|
||||||
|
}
|
||||||
131
internal/ffmpeg/options.go
Normal file
131
internal/ffmpeg/options.go
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ffmpegBuilder struct {
|
||||||
|
binary string // ffmpeg binary path
|
||||||
|
options map[string]string // global options
|
||||||
|
inputs []*inputBuilder // input options
|
||||||
|
outputs []*outputBuilder // output options
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFFmpegBuilder() *ffmpegBuilder {
|
||||||
|
return &ffmpegBuilder{
|
||||||
|
binary: "ffmpeg",
|
||||||
|
options: make(map[string]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) AddInput(src *inputBuilder) {
|
||||||
|
b.inputs = append(b.inputs, src)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) AddOutput(dst *outputBuilder) {
|
||||||
|
b.outputs = append(b.outputs, dst)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) SetBinary(bin string) {
|
||||||
|
b.binary = bin
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) SetFlag(flag string) {
|
||||||
|
b.options[flag] = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) SetOption(name, value string) {
|
||||||
|
b.options[name] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) Args() (args []string) {
|
||||||
|
for name, val := range b.options {
|
||||||
|
args = append(args, "-"+name)
|
||||||
|
if val != "" {
|
||||||
|
args = append(args, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, input := range b.inputs {
|
||||||
|
args = append(args, input.Args()...)
|
||||||
|
}
|
||||||
|
for _, output := range b.outputs {
|
||||||
|
args = append(args, output.Args()...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) Command(ctx context.Context) *exec.Cmd {
|
||||||
|
bin := "ffmpeg"
|
||||||
|
if b.binary != "" {
|
||||||
|
bin = b.binary
|
||||||
|
}
|
||||||
|
|
||||||
|
return exec.CommandContext(ctx, bin, b.Args()...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// inputBuilder is the builder for ffmpeg input options
|
||||||
|
type inputBuilder struct {
|
||||||
|
path string
|
||||||
|
options map[string][]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newInputBuilder(path string) *inputBuilder {
|
||||||
|
return &inputBuilder{
|
||||||
|
path: path,
|
||||||
|
options: make(map[string][]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *inputBuilder) AddOption(name, value string) {
|
||||||
|
b.options[name] = append(b.options[name], value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *inputBuilder) Args() (args []string) {
|
||||||
|
for name, values := range b.options {
|
||||||
|
for _, val := range values {
|
||||||
|
args = append(args, "-"+name, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return append(args, "-i", b.path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// outputBuilder is the builder for ffmpeg output options
|
||||||
|
type outputBuilder struct {
|
||||||
|
path string
|
||||||
|
options map[string][]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newOutputBuilder(path string) *outputBuilder {
|
||||||
|
return &outputBuilder{
|
||||||
|
path: path,
|
||||||
|
options: make(map[string][]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *outputBuilder) AddOption(name, value string) {
|
||||||
|
b.options[name] = append(b.options[name], value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *outputBuilder) Args() (args []string) {
|
||||||
|
for name, values := range b.options {
|
||||||
|
for _, val := range values {
|
||||||
|
args = append(args, "-"+name, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return append(args, b.path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddMetadata is the shortcut for adding "metadata" option
|
||||||
|
func (b *outputBuilder) AddMetadata(stream, key, value string) {
|
||||||
|
optVal := strings.TrimSpace(key) + "=" + strings.TrimSpace(value)
|
||||||
|
|
||||||
|
if stream != "" {
|
||||||
|
b.AddOption("metadata:"+stream, optVal)
|
||||||
|
} else {
|
||||||
|
b.AddOption("metadata", optVal)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package logging
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
import (
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"go.uber.org/zap/zapcore"
|
|
||||||
)
|
|
||||||
|
|
||||||
// newDefaultProductionLog configures a custom log that is
|
|
||||||
// intended for use by default if no other log is specified
|
|
||||||
// in a config. It writes to stderr, uses the console encoder,
|
|
||||||
// and enables INFO-level logs and higher.
|
|
||||||
func newDefaultProductionLog() *zap.Logger {
|
|
||||||
encCfg := zap.NewProductionEncoderConfig()
|
|
||||||
// if interactive terminal, make output more human-readable by default
|
|
||||||
encCfg.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
|
|
||||||
encoder.AppendString(ts.Format("2006/01/02 15:04:05.000"))
|
|
||||||
}
|
|
||||||
encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
|
||||||
enc := zapcore.NewConsoleEncoder(encCfg)
|
|
||||||
core := zapcore.NewCore(enc, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(zap.DebugLevel))
|
|
||||||
return zap.New(core)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Log returns the current default logger.
|
|
||||||
func Log() *zap.Logger {
|
|
||||||
defaultLoggerMu.RLock()
|
|
||||||
defer defaultLoggerMu.RUnlock()
|
|
||||||
return defaultLogger
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
defaultLogger = newDefaultProductionLog()
|
|
||||||
defaultLoggerMu sync.RWMutex
|
|
||||||
)
|
|
||||||
14
internal/logging/zap.go
Normal file
14
internal/logging/zap.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewZapLogger() (*zap.Logger, error) {
|
||||||
|
zapCfg := zap.NewDevelopmentConfig()
|
||||||
|
zapCfg.DisableStacktrace = true
|
||||||
|
zapCfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||||
|
zapCfg.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006/01/02 15:04:05.000")
|
||||||
|
return zapCfg.Build()
|
||||||
|
}
|
||||||
71
internal/mmkv/mmkv.go
Normal file
71
internal/mmkv/mmkv.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package mmkv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"git.um-react.app/um/cli/algo/common"
|
||||||
|
"git.um-react.app/um/cli/internal/utils"
|
||||||
|
go_mmkv "github.com/unlock-music/go-mmkv"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Merge(keys ...common.QMCKeys) common.QMCKeys {
|
||||||
|
result := make(common.QMCKeys)
|
||||||
|
for _, k := range keys {
|
||||||
|
for key, value := range k {
|
||||||
|
result[utils.NormalizeUnicode(key)] = utils.NormalizeUnicode(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadFromPath(path string, key string, logger *zap.Logger) (result common.QMCKeys, err error) {
|
||||||
|
mmkv_path := path
|
||||||
|
mmkv_crc := path + ".crc"
|
||||||
|
|
||||||
|
mr, err := os.Open(mmkv_path)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("LoadMMKV: Could not open mmkv file", zap.Error(err))
|
||||||
|
return nil, fmt.Errorf("LoadMMKV: open error: %w", err)
|
||||||
|
}
|
||||||
|
defer mr.Close()
|
||||||
|
|
||||||
|
cr, err := os.Open(mmkv_crc)
|
||||||
|
if err != nil {
|
||||||
|
// crc is optional
|
||||||
|
logger.Warn("LoadMMKV: Failed to open crc file, assuming no encryption", zap.Error(err))
|
||||||
|
key = ""
|
||||||
|
} else {
|
||||||
|
defer cr.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
var password []byte = nil
|
||||||
|
if key != "" {
|
||||||
|
password = make([]byte, 16)
|
||||||
|
copy(password, []byte(key))
|
||||||
|
}
|
||||||
|
mmkv, err := go_mmkv.NewMMKVReader(mr, password, cr)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("LoadMMKV: failed to create reader", zap.Error(err))
|
||||||
|
return nil, fmt.Errorf("LoadMMKV: NewMMKVReader error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result = make(common.QMCKeys)
|
||||||
|
for !mmkv.IsEOF() {
|
||||||
|
key, err := mmkv.ReadKey()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("LoadMMKV: read key error", zap.Error(err))
|
||||||
|
return nil, fmt.Errorf("LoadMMKV: read key error: %w", err)
|
||||||
|
}
|
||||||
|
value, err := mmkv.ReadStringValue()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("LoadMMKV: read value error", zap.Error(err))
|
||||||
|
return nil, fmt.Errorf("LoadMMKV: read value error: %w", err)
|
||||||
|
}
|
||||||
|
logger.Debug("LoadMMKV: read", zap.String("key", key), zap.String("value", value))
|
||||||
|
result[utils.NormalizeUnicode(key)] = utils.NormalizeUnicode(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
105
internal/sniff/audio.go
Normal file
105
internal/sniff/audio.go
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package sniff
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Sniffer interface {
|
||||||
|
Sniff(header []byte) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var audioExtensions = map[string]Sniffer{
|
||||||
|
// ref: https://mimesniff.spec.whatwg.org
|
||||||
|
".mp3": prefixSniffer("ID3"), // todo: check mp3 without ID3v2 tag
|
||||||
|
".ogg": prefixSniffer("OggS"),
|
||||||
|
".wav": prefixSniffer("RIFF"),
|
||||||
|
|
||||||
|
// ref: https://www.loc.gov/preservation/digital/formats/fdd/fdd000027.shtml
|
||||||
|
".wma": prefixSniffer{
|
||||||
|
0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66, 0xcf, 0x11,
|
||||||
|
0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c,
|
||||||
|
},
|
||||||
|
|
||||||
|
// ref: https://www.garykessler.net/library/file_sigs.html
|
||||||
|
".m4a": m4aSniffer{}, // MPEG-4 container, Apple Lossless Audio Codec
|
||||||
|
".mp4": &mpeg4Sniffer{}, // MPEG-4 container, other fallback
|
||||||
|
|
||||||
|
".flac": prefixSniffer("fLaC"), // ref: https://xiph.org/flac/format.html
|
||||||
|
".dff": prefixSniffer("FRM8"), // DSDIFF, ref: https://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// AudioExtension sniffs the known audio types, and returns the file extension.
|
||||||
|
// header is recommended to at least 16 bytes.
|
||||||
|
func AudioExtension(header []byte) (string, bool) {
|
||||||
|
for ext, sniffer := range audioExtensions {
|
||||||
|
if sniffer.Sniff(header) {
|
||||||
|
return ext, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
// AudioExtensionWithFallback is equivalent to AudioExtension, but returns fallback
|
||||||
|
// most likely to use .mp3 as fallback, because mp3 files may not have ID3v2 tag.
|
||||||
|
func AudioExtensionWithFallback(header []byte, fallback string) string {
|
||||||
|
ext, ok := AudioExtension(header)
|
||||||
|
if !ok {
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
return ext
|
||||||
|
}
|
||||||
|
|
||||||
|
type prefixSniffer []byte
|
||||||
|
|
||||||
|
func (s prefixSniffer) Sniff(header []byte) bool {
|
||||||
|
return bytes.HasPrefix(header, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
type m4aSniffer struct{}
|
||||||
|
|
||||||
|
func (m4aSniffer) Sniff(header []byte) bool {
|
||||||
|
box := readMpeg4FtypBox(header)
|
||||||
|
if box == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return box.majorBrand == "M4A " || slices.Contains(box.compatibleBrands, "M4A ")
|
||||||
|
}
|
||||||
|
|
||||||
|
type mpeg4Sniffer struct{}
|
||||||
|
|
||||||
|
func (s *mpeg4Sniffer) Sniff(header []byte) bool {
|
||||||
|
return readMpeg4FtypBox(header) != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type mpeg4FtpyBox struct {
|
||||||
|
majorBrand string
|
||||||
|
minorVersion uint32
|
||||||
|
compatibleBrands []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func readMpeg4FtypBox(header []byte) *mpeg4FtpyBox {
|
||||||
|
if (len(header) < 8) || !bytes.Equal([]byte("ftyp"), header[4:8]) {
|
||||||
|
return nil // not a valid ftyp box
|
||||||
|
}
|
||||||
|
|
||||||
|
size := binary.BigEndian.Uint32(header[0:4]) // size
|
||||||
|
if size < 16 || size%4 != 0 {
|
||||||
|
return nil // invalid ftyp box
|
||||||
|
}
|
||||||
|
|
||||||
|
box := mpeg4FtpyBox{
|
||||||
|
majorBrand: string(header[8:12]),
|
||||||
|
minorVersion: binary.BigEndian.Uint32(header[12:16]),
|
||||||
|
}
|
||||||
|
|
||||||
|
// compatible brands
|
||||||
|
for i := 16; i < int(size) && i+4 < len(header); i += 4 {
|
||||||
|
box.compatibleBrands = append(box.compatibleBrands, string(header[i:i+4]))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &box
|
||||||
|
}
|
||||||
30
internal/sniff/image.go
Normal file
30
internal/sniff/image.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package sniff
|
||||||
|
|
||||||
|
// ref: https://mimesniff.spec.whatwg.org
|
||||||
|
var imageMIMEs = map[string]Sniffer{
|
||||||
|
"image/jpeg": prefixSniffer{0xFF, 0xD8, 0xFF},
|
||||||
|
"image/png": prefixSniffer{0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n'},
|
||||||
|
"image/bmp": prefixSniffer("BM"),
|
||||||
|
"image/webp": prefixSniffer("RIFF"),
|
||||||
|
"image/gif": prefixSniffer("GIF8"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImageMIME sniffs the well-known image types, and returns its MIME.
|
||||||
|
func ImageMIME(header []byte) (string, bool) {
|
||||||
|
for ext, sniffer := range imageMIMEs {
|
||||||
|
if sniffer.Sniff(header) {
|
||||||
|
return ext, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImageExtension is equivalent to ImageMIME, but returns file extension
|
||||||
|
func ImageExtension(header []byte) (string, bool) {
|
||||||
|
ext, ok := ImageMIME(header)
|
||||||
|
if !ok {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
// todo: use mime.ExtensionsByType
|
||||||
|
return "." + ext[6:], true // "image/" is 6 bytes
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
24
internal/utils/temp.go
Normal file
24
internal/utils/temp.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func WriteTempFile(rd io.Reader, ext string) (string, error) {
|
||||||
|
audioFile, err := os.CreateTemp("", "*"+ext)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("ffmpeg create temp file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(audioFile, rd); err != nil {
|
||||||
|
return "", fmt.Errorf("ffmpeg write temp file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := audioFile.Close(); err != nil {
|
||||||
|
return "", fmt.Errorf("ffmpeg close temp file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return audioFile.Name(), nil
|
||||||
|
}
|
||||||
10
internal/utils/unicode.go
Normal file
10
internal/utils/unicode.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import "golang.org/x/text/unicode/norm"
|
||||||
|
|
||||||
|
// normalizeUnicode normalizes unicode string to NFC.
|
||||||
|
// since macOS may change some characters in the file name.
|
||||||
|
// e.g. "ぜ"(e3 81 9c) -> "ぜ"(e3 81 9b e3 82 99)
|
||||||
|
func NormalizeUnicode(str string) string {
|
||||||
|
return norm.NFC.String(str)
|
||||||
|
}
|
||||||
32
misc/release.sh
Executable file
32
misc/release.sh
Executable file
@@ -0,0 +1,32 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
PLATFORMS=(
|
||||||
|
"linux/amd64"
|
||||||
|
"linux/arm64"
|
||||||
|
"darwin/amd64"
|
||||||
|
"darwin/arm64"
|
||||||
|
"windows/amd64"
|
||||||
|
"windows/386"
|
||||||
|
)
|
||||||
|
|
||||||
|
DEST_DIR=${DEST_DIR:-"dist"}
|
||||||
|
|
||||||
|
for PLATFORM in "${PLATFORMS[@]}"; do
|
||||||
|
GOOS=${PLATFORM%/*}
|
||||||
|
GOARCH=${PLATFORM#*/}
|
||||||
|
echo "Building for $GOOS/$GOARCH"
|
||||||
|
|
||||||
|
FILENAME="um-$GOOS-$GOARCH"
|
||||||
|
if [ "$GOOS" = "windows" ]; then
|
||||||
|
FILENAME="$FILENAME.exe"
|
||||||
|
fi
|
||||||
|
|
||||||
|
GOOS=$GOOS GOARCH=$GOARCH go build -v \
|
||||||
|
-o "${DEST_DIR}/${FILENAME}" \
|
||||||
|
-ldflags "-s -w -X main.AppVersion=$(git describe --tags --always --dirty)" \
|
||||||
|
./cmd/um
|
||||||
|
done
|
||||||
|
|
||||||
|
cd "$DEST_DIR"
|
||||||
|
sha256sum um-* > sha256sums.txt
|
||||||
51
misc/repack.sh
Executable file
51
misc/repack.sh
Executable file
@@ -0,0 +1,51 @@
|
|||||||
|
#!/bin/bash -e
|
||||||
|
# see .gitea/workflows/build.yml
|
||||||
|
|
||||||
|
APP_VERSION="${1:-$(git describe --tags --always)}"
|
||||||
|
|
||||||
|
pack() {
|
||||||
|
local is_windows=0
|
||||||
|
local suffix=""
|
||||||
|
if [[ "$1" == *.exe ]]; then
|
||||||
|
suffix=".exe"
|
||||||
|
is_windows=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local exe_dir="$(dirname "$1")"
|
||||||
|
local archive_name="$(basename "$1" ".exe")-${APP_VERSION}"
|
||||||
|
local exe_name="um${suffix}"
|
||||||
|
|
||||||
|
echo "archiving ${exe_name}..."
|
||||||
|
|
||||||
|
mv "$1" "${exe_name}"
|
||||||
|
if [[ "$is_windows" == 1 ]]; then
|
||||||
|
zip -Xqj9 "dist/${archive_name}.zip" "${exe_name}" README.md LICENSE
|
||||||
|
else
|
||||||
|
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 -c \
|
||||||
|
"${exe_name}" README.md LICENSE |
|
||||||
|
gzip -9 >"dist/${archive_name}.tar.gz"
|
||||||
|
fi
|
||||||
|
rm -rf "$exe_dir" "${exe_name}"
|
||||||
|
}
|
||||||
|
|
||||||
|
for exe in prepare/*/um*; do
|
||||||
|
pack "$exe"
|
||||||
|
done
|
||||||
|
|
||||||
|
pushd dist
|
||||||
|
|
||||||
|
if command -v strip-nondeterminism >/dev/null 2>&1; then
|
||||||
|
echo 'strip archives...'
|
||||||
|
strip-nondeterminism *.zip *.tar.gz
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo 'Creating checksum...'
|
||||||
|
sha256sum *.zip *.tar.gz >sha256sum.txt
|
||||||
|
ls -alh *.zip *.tar.gz sha256sum.txt
|
||||||
|
popd
|
||||||
Reference in New Issue
Block a user