Compare commits

..

No commits in common. "main" and "v0.0.5" have entirely different histories.
main ... v0.0.5

87 changed files with 1237 additions and 4383 deletions

View File

@ -1,148 +0,0 @@
// generate .drone.yaml, run:
// drone jsonnet --format --stream
local CreateRelease() = {
name: 'create release',
image: 'plugins/gitea-release',
settings: {
api_key: { from_secret: 'GITEA_API_KEY' },
base_url: 'https://git.unlock-music.dev',
files: [
'um-*.tar.gz',
'um-*.zip',
],
checksum: 'sha256',
draft: true,
title: '${DRONE_TAG}',
},
};
local StepGoBuild(GOOS, GOARCH) = {
local windows = GOOS == 'windows',
local archiveExt = if windows then 'zip' else 'tar.gz',
local filepath = 'dist/um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
local archive = if windows then [
// Ensure zip is installed
'command -v zip >/dev/null || (apt update && apt install -y zip)',
'zip -9 -j -r "%s" $DIST_DIR' % filepath,
] else [
'tar -c -C $DIST_DIR um | gzip -9 > "%s"' % filepath,
],
name: 'go build %s/%s' % [GOOS, GOARCH],
image: 'golang:1.23',
environment: {
GOOS: GOOS,
GOARCH: GOARCH,
GOPROXY: 'https://goproxy.io,direct',
},
commands: [
'DIST_DIR=$(mktemp -d)',
'go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags --always)" -o $DIST_DIR ./cmd/um',
'mkdir -p dist',
] + archive,
};
local StepUploadArtifact(GOOS, GOARCH) = {
local windows = GOOS == 'windows',
local archiveExt = if windows then 'zip' else 'tar.gz',
local filename = 'um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
local filepath = 'dist/%s' % filename,
local pkgname = '${DRONE_REPO_NAME}-build',
name: 'upload artifact',
image: 'golang:1.23', // reuse golang:1.19 for curl
environment: {
DRONE_GITEA_SERVER: 'https://git.unlock-music.dev',
GITEA_API_KEY: { from_secret: 'GITEA_API_KEY' },
},
commands: [
'curl --fail --include --user "um-release-bot:$GITEA_API_KEY" ' +
'--upload-file "%s" ' % filepath +
'"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/%s/${DRONE_BUILD_NUMBER}/%s"' % [pkgname, filename],
'sha256sum %s' % filepath,
'echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/%s/${DRONE_BUILD_NUMBER}' % pkgname,
],
};
local PipelineBuild(GOOS, GOARCH, RUN_TEST) = {
name: 'build %s/%s' % [GOOS, GOARCH],
kind: 'pipeline',
type: 'docker',
steps: [
{
name: 'fetch tags',
image: 'alpine/git',
commands: ['git fetch --tags'],
},
] +
(
if RUN_TEST then [{
name: 'go test',
image: 'golang:1.23',
environment: {
GOPROXY: 'https://goproxy.io,direct',
},
commands: ['go test -v ./...'],
}] else []
)
+
[
StepGoBuild(GOOS, GOARCH),
StepUploadArtifact(GOOS, GOARCH),
],
trigger: {
event: ['push', 'pull_request'],
},
};
local PipelineRelease() = {
name: 'release',
kind: 'pipeline',
type: 'docker',
steps: [
{
name: 'fetch tags',
image: 'alpine/git',
commands: ['git fetch --tags'],
},
{
name: 'go test',
image: 'golang:1.23',
environment: {
GOPROXY: 'https://goproxy.io,direct',
},
commands: ['go test -v ./...'],
},
StepGoBuild('linux', 'amd64'),
StepGoBuild('linux', 'arm64'),
StepGoBuild('linux', '386'),
StepGoBuild('windows', 'amd64'),
StepGoBuild('windows', 'arm64'),
StepGoBuild('windows', '386'),
StepGoBuild('darwin', 'amd64'),
StepGoBuild('darwin', 'arm64'),
{
name: 'prepare root',
image: 'golang:1.23',
commands: [
'mv dist/*.tar.gz dist/*.zip ./',
],
},
CreateRelease(),
],
trigger: {
event: ['tag'],
},
};
[
PipelineBuild('linux', 'amd64', true),
PipelineBuild('windows', 'amd64', false),
PipelineBuild('darwin', 'amd64', false),
PipelineRelease(),
]

View File

@ -1,257 +0,0 @@
---
kind: pipeline
name: build linux/amd64
steps:
- commands:
- git fetch --tags
image: alpine/git
name: fetch tags
- commands:
- go test -v ./...
environment:
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go test
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-amd64-$(git describe --tags
--always).tar.gz"
environment:
GOARCH: amd64
GOOS: linux
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go build linux/amd64
- commands:
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-linux-amd64-$(git
describe --tags --always).tar.gz" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-linux-amd64-$(git
describe --tags --always).tar.gz"
- sha256sum dist/um-linux-amd64-$(git describe --tags --always).tar.gz
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
environment:
DRONE_GITEA_SERVER: https://git.unlock-music.dev
GITEA_API_KEY:
from_secret: GITEA_API_KEY
image: golang:1.23
name: upload artifact
trigger:
event:
- push
- pull_request
type: docker
---
kind: pipeline
name: build windows/amd64
steps:
- commands:
- git fetch --tags
image: alpine/git
name: fetch tags
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- command -v zip >/dev/null || (apt update && apt install -y zip)
- zip -9 -j -r "dist/um-windows-amd64-$(git describe --tags --always).zip" $DIST_DIR
environment:
GOARCH: amd64
GOOS: windows
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go build windows/amd64
- commands:
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-windows-amd64-$(git
describe --tags --always).zip" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-windows-amd64-$(git
describe --tags --always).zip"
- sha256sum dist/um-windows-amd64-$(git describe --tags --always).zip
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
environment:
DRONE_GITEA_SERVER: https://git.unlock-music.dev
GITEA_API_KEY:
from_secret: GITEA_API_KEY
image: golang:1.23
name: upload artifact
trigger:
event:
- push
- pull_request
type: docker
---
kind: pipeline
name: build darwin/amd64
steps:
- commands:
- git fetch --tags
image: alpine/git
name: fetch tags
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-amd64-$(git describe --tags
--always).tar.gz"
environment:
GOARCH: amd64
GOOS: darwin
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go build darwin/amd64
- commands:
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-darwin-amd64-$(git
describe --tags --always).tar.gz" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-darwin-amd64-$(git
describe --tags --always).tar.gz"
- sha256sum dist/um-darwin-amd64-$(git describe --tags --always).tar.gz
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
environment:
DRONE_GITEA_SERVER: https://git.unlock-music.dev
GITEA_API_KEY:
from_secret: GITEA_API_KEY
image: golang:1.23
name: upload artifact
trigger:
event:
- push
- pull_request
type: docker
---
kind: pipeline
name: release
steps:
- commands:
- git fetch --tags
image: alpine/git
name: fetch tags
- commands:
- go test -v ./...
environment:
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go test
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-amd64-$(git describe --tags
--always).tar.gz"
environment:
GOARCH: amd64
GOOS: linux
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go build linux/amd64
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-arm64-$(git describe --tags
--always).tar.gz"
environment:
GOARCH: arm64
GOOS: linux
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go build linux/arm64
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-386-$(git describe --tags --always).tar.gz"
environment:
GOARCH: "386"
GOOS: linux
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go build linux/386
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- command -v zip >/dev/null || (apt update && apt install -y zip)
- zip -9 -j -r "dist/um-windows-amd64-$(git describe --tags --always).zip" $DIST_DIR
environment:
GOARCH: amd64
GOOS: windows
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go build windows/amd64
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- command -v zip >/dev/null || (apt update && apt install -y zip)
- zip -9 -j -r "dist/um-windows-arm64-$(git describe --tags --always).zip" $DIST_DIR
environment:
GOARCH: arm64
GOOS: windows
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go build windows/arm64
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- command -v zip >/dev/null || (apt update && apt install -y zip)
- zip -9 -j -r "dist/um-windows-386-$(git describe --tags --always).zip" $DIST_DIR
environment:
GOARCH: "386"
GOOS: windows
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go build windows/386
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-amd64-$(git describe --tags
--always).tar.gz"
environment:
GOARCH: amd64
GOOS: darwin
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go build darwin/amd64
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-arm64-$(git describe --tags
--always).tar.gz"
environment:
GOARCH: arm64
GOOS: darwin
GOPROXY: https://goproxy.io,direct
image: golang:1.23
name: go build darwin/arm64
- commands:
- mv dist/*.tar.gz dist/*.zip ./
image: golang:1.23
name: prepare root
- image: plugins/gitea-release
name: create release
settings:
api_key:
from_secret: GITEA_API_KEY
base_url: https://git.unlock-music.dev
checksum: sha256
draft: true
files:
- um-*.tar.gz
- um-*.zip
title: ${DRONE_TAG}
trigger:
event:
- tag
type: docker

View File

@ -1,6 +1,7 @@
name: Build name: Build
on: on:
push: push:
branches: [ master ]
paths: paths:
- "**/*.go" - "**/*.go"
- "go.mod" - "go.mod"
@ -16,23 +17,17 @@ on:
jobs: jobs:
test: test:
runs-on: ubuntu-latest runs-on: ${{ matrix.os }}
strategy: strategy:
matrix: matrix:
os: [ windows-latest, ubuntu-latest, macos-latest ]
include: include:
- target: "linux/amd64" - os: ubuntu-latest
GOOS: "linux"
GOARCH: "amd64"
BIN_SUFFIX: "" BIN_SUFFIX: ""
- target: "windows/amd64" - os: macos-latest
GOOS: "windows" BIN_SUFFIX: ""
GOARCH: "amd64" - os: windows-latest
BIN_SUFFIX: ".exe" BIN_SUFFIX: ".exe"
- target: "darwin/amd64"
GOOS: "darwin"
GOARCH: "amd64"
BIN_SUFFIX: ""
steps: steps:
- name: Checkout codebase - name: Checkout codebase
uses: actions/checkout@v2 uses: actions/checkout@v2
@ -42,7 +37,7 @@ jobs:
- name: Set up Go 1.x - name: Set up Go 1.x
uses: actions/setup-go@v2 uses: actions/setup-go@v2
with: with:
go-version: ^1.17 go-version: ^1.16
- name: Setup vars - name: Setup vars
id: vars id: vars
@ -50,18 +45,13 @@ jobs:
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)" echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
echo "::set-output name=git_tag::$(git describe --tags --always)" echo "::set-output name=git_tag::$(git describe --tags --always)"
- name: Test
run: go test -v ./...
- name: Build - name: Build
env: env:
GOOS: ${{ matrix.GOOS }}
GOARCH: ${{ matrix.GOARCH }}
CGO_ENABLED: 0 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 run: go build -trimpath -ldflags="-w -s -X main.AppVersion=${{ steps.vars.outputs.git_tag }}" -v -o um-${{ runner.os }}${{ matrix.BIN_SUFFIX }} ./cmd/um
- name: Publish artifact - name: Publish artifact
uses: actions/upload-artifact@v2 uses: actions/upload-artifact@v2
with: with:
name: um-${{ matrix.GOOS }}-${{ matrix.GOARCH }} name: um-${{ runner.os }}${{ matrix.BIN_SUFFIX }}
path: ./um-${{ matrix.GOOS }}-${{ matrix.GOARCH }}${{ matrix.BIN_SUFFIX }} path: ./um-${{ runner.os }}${{ matrix.BIN_SUFFIX }}

View File

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

6
.gitignore vendored
View File

@ -1,7 +1 @@
.idea .idea
/dist
*.exe
/um-*.tar.gz
/um-*.zip

View File

@ -1,48 +1,25 @@
# Unlock Music Project - CLI Edition # Unlock Music Project - CLI Edition
Original: Web Edition https://git.unlock-music.dev/um/web Original: Web Edition https://github.com/ix64/unlock-music
- [![Build Status](https://ci.unlock-music.dev/api/badges/um/cli/status.svg)](https://ci.unlock-music.dev/um/cli) - [Release Download](https://github.com/unlock-music/cli/releases/latest)
- [Release Download](https://git.unlock-music.dev/um/cli/releases/latest)
- [Latest Build](https://git.unlock-music.dev/um/-/packages/generic/cli-build/)
> **WARNING**
> 在本站 fork 不会起到备份的作用,只会浪费服务器储存空间。如无必要请勿 fork 该仓库。
## Features ## Features
- [x] All Algorithm Supported By `unlock-music/web` - [x] All Algorithm Supported By `ix64/unlock-music`
- [x] Complete Metadata & Cover Image - [ ] Complete Cover Image
- [ ] Parse Meta Data
- [ ] Complete Meta Data
## Release ## Hou to Build
[Latest release](https://git.unlock-music.dev/um/cli/releases/latest). - Requirements: **Golang 1.16**
## Install from source 1. Clone this repo `git clone https://github.com/unlock-music/cli && cd cli`
2. Build the executable `go build ./cmd/um`
- Requirements: **Golang 1.23.3**
1. run `go install unlock-music.dev/cli/cmd/um@master`
### 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
- Drag the encrypted file to `um.exe` (Tested on Windows) - Drag the encrypted file to `um.exe` (Tested on Windows)
- Run: `./um [-o <output dir>] [-i] <input dir/file>` - Run: `./um [-o <output dir>] [-i] <input dir/file>`
- Use `./um -h` to show help menu - Use `./um -h` to show help menu
## Update CI pipeline
1. Install [Drone CI binary](https://docs.drone.io/cli/install/)
2. Edit `.drone.jsonnet`
3. Update drone CI pipeline:
```sh
drone jsonnet --format --stream
```

16
algo/common/common.go Normal file
View File

@ -0,0 +1,16 @@
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
}

View File

@ -1,49 +1,12 @@
package common package common
import ( type NewDecoderFunc func([]byte) Decoder
"io"
"path/filepath"
"strings"
"go.uber.org/zap" var decoderRegistry = make(map[string][]NewDecoderFunc)
)
type DecoderParams struct { func RegisterDecoder(ext string, dispatchFunc NewDecoderFunc) {
Reader io.ReadSeeker // required decoderRegistry[ext] = append(decoderRegistry[ext], dispatchFunc)
Extension string // required, source extension, eg. ".mp3"
FilePath string // optional, source file path
Logger *zap.Logger // required
} }
type NewDecoderFunc func(p *DecoderParams) Decoder func GetDecoder(ext string) []NewDecoderFunc {
return decoderRegistry[ext]
type DecoderFactory struct {
noop bool
Suffix string
Create NewDecoderFunc
}
var DecoderRegistry []DecoderFactory
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
DecoderRegistry = append(DecoderRegistry,
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
}
if skipNoop && dec.noop {
continue
}
result = append(result, dec)
}
return result
} }

View File

@ -1,29 +0,0 @@
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)
}

View File

@ -1,50 +0,0 @@
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
}

View File

@ -1,38 +0,0 @@
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)
}
})
}
}

View File

@ -1,50 +1,52 @@
package common package common
import ( import "errors"
"errors"
"fmt"
"io"
"unlock-music.dev/cli/internal/sniff"
)
type RawDecoder struct { type RawDecoder struct {
rd io.ReadSeeker file []byte
audioExt string audioExt string
} }
func NewRawDecoder(p *DecoderParams) Decoder { func NewRawDecoder(file []byte) Decoder {
return &RawDecoder{rd: p.Reader} return &RawDecoder{file: file}
} }
func (d *RawDecoder) Validate() error { func (d *RawDecoder) Validate() error {
header := make([]byte, 16) for ext, sniffer := range snifferRegistry {
if _, err := io.ReadFull(d.rd, header); err != nil { if sniffer(d.file) {
return fmt.Errorf("read file header failed: %v", err) d.audioExt = ext
return nil
} }
if _, err := d.rd.Seek(0, io.SeekStart); err != nil { }
return fmt.Errorf("seek file failed: %v", err) return errors.New("audio doesn't recognized")
} }
var ok bool func (d RawDecoder) Decode() error {
d.audioExt, ok = sniff.AudioExtension(header)
if !ok {
return errors.New("raw: sniff audio type failed")
}
return nil return nil
} }
func (d *RawDecoder) Read(p []byte) (n int, err error) { func (d RawDecoder) GetCoverImage() []byte {
return d.rd.Read(p) return nil
}
func (d RawDecoder) GetAudioData() []byte {
return d.file
}
func (d RawDecoder) GetAudioExt() string {
return d.audioExt
}
func (d RawDecoder) GetMeta() Meta {
return nil
} }
func init() { func init() {
RegisterDecoder("mp3", true, NewRawDecoder) RegisterDecoder("mp3", NewRawDecoder)
RegisterDecoder("flac", true, NewRawDecoder) RegisterDecoder("flac", NewRawDecoder)
RegisterDecoder("ogg", true, NewRawDecoder) RegisterDecoder("ogg", NewRawDecoder)
RegisterDecoder("m4a", true, NewRawDecoder) RegisterDecoder("m4a", NewRawDecoder)
RegisterDecoder("wav", true, NewRawDecoder) RegisterDecoder("wav", NewRawDecoder)
RegisterDecoder("wma", true, NewRawDecoder) RegisterDecoder("wma", NewRawDecoder)
RegisterDecoder("aac", true, NewRawDecoder) RegisterDecoder("aac", NewRawDecoder)
} }

48
algo/common/sniff.go Normal file
View File

@ -0,0 +1,48 @@
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,
}
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})
}

View File

@ -1,67 +1,98 @@
package kgm package kgm
import ( import (
"fmt" "bytes"
"io" "encoding/binary"
"errors"
"github.com/unlock-music/cli/algo/common"
"github.com/unlock-music/cli/internal/logging"
)
"unlock-music.dev/cli/algo/common" var (
vprHeader = []byte{
0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43,
0x91, 0xAA, 0xBD, 0xD0, 0x7A, 0xF5, 0x36, 0x31}
kgmHeader = []byte{
0x7C, 0xD5, 0x32, 0xEB, 0x86, 0x02, 0x7F, 0x4B,
0xA8, 0xAF, 0xA6, 0x8E, 0x0F, 0xFF, 0x99, 0x14}
ErrKgmMagicHeader = errors.New("kgm/vpr magic header not matched")
) )
type Decoder struct { type Decoder struct {
rd io.ReadSeeker file []byte
key []byte
cipher common.StreamDecoder isVpr bool
offset int audio []byte
header header
} }
func NewDecoder(p *common.DecoderParams) common.Decoder { func NewDecoder(file []byte) common.Decoder {
return &Decoder{rd: p.Reader} return &Decoder{
} 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)
}
default:
return fmt.Errorf("kgm: unsupported crypto version %d", d.header.CryptoVersion)
}
// prepare for read
if _, err := d.rd.Seek(int64(d.header.AudioOffset), io.SeekStart); err != nil {
return fmt.Errorf("kgm seek to audio: %w", err)
} }
func (d Decoder) GetCoverImage() []byte {
return nil return nil
} }
func (d *Decoder) Read(buf []byte) (int, error) { func (d Decoder) GetAudioData() []byte {
n, err := d.rd.Read(buf) return d.audio
if n > 0 {
d.cipher.Decrypt(buf[:n], d.offset)
d.offset += n
}
return n, err
} }
func (d Decoder) GetAudioExt() string {
return "" // use sniffer
}
func (d Decoder) GetMeta() common.Meta {
return nil
}
func (d *Decoder) Validate() error {
if bytes.Equal(kgmHeader, d.file[:len(kgmHeader)]) {
d.isVpr = false
} else if bytes.Equal(vprHeader, d.file[:len(vprHeader)]) {
d.isVpr = true
} else {
return ErrKgmMagicHeader
}
d.key = d.file[0x1c:0x2c]
d.key = append(d.key, 0x00)
_ = d.file[0x2c:0x3c] //todo: key2
return nil
}
func (d *Decoder) Decode() error {
headerLen := binary.LittleEndian.Uint32(d.file[0x10:0x14])
dataEncrypted := d.file[headerLen:]
lenData := len(dataEncrypted)
initMask()
if fullMaskLen < lenData {
logging.Log().Warn("The file is too large and the processed audio is incomplete, " +
"please report to us about this file at https://github.com/unlock-music/cli/issues")
lenData = fullMaskLen
}
d.audio = make([]byte, lenData)
for i := 0; i < lenData; i++ {
med8 := d.key[i%17] ^ dataEncrypted[i]
med8 ^= (med8 & 0xf) << 4
msk8 := maskV2PreDef[i%272] ^ maskV2[i>>4]
msk8 ^= (msk8 & 0xf) << 4
d.audio[i] = med8 ^ msk8
}
if d.isVpr {
for i := 0; i < lenData; i++ {
d.audio[i] ^= maskDiffVpr[i%17]
}
}
return nil
}
func init() { func init() {
// Kugou // Kugou
common.RegisterDecoder("kgm", false, NewDecoder) common.RegisterDecoder("kgm", NewDecoder)
common.RegisterDecoder("kgma", false, NewDecoder) common.RegisterDecoder("kgma", NewDecoder)
// Viper // Viper
common.RegisterDecoder("vpr", false, NewDecoder) common.RegisterDecoder("vpr", NewDecoder)
// Kugou Android
common.RegisterDecoder("kgm.flac", false, NewDecoder)
common.RegisterDecoder("vpr.flac", false, NewDecoder)
} }

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

Binary file not shown.

View File

@ -1,64 +0,0 @@
package kgm
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
)
var (
vprHeader = []byte{
0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43,
0x91, 0xAA, 0xBD, 0xD0, 0x7A, 0xF5, 0x36, 0x31,
}
kgmHeader = []byte{
0x7C, 0xD5, 0x32, 0xEB, 0x86, 0x02, 0x7F, 0x4B,
0xA8, 0xAF, 0xA6, 0x8E, 0x0F, 0xFF, 0x99, 0x14,
}
ErrKgmMagicHeader = errors.New("kgm magic header not matched")
)
// header is the header of a KGM file.
type header struct {
MagicHeader []byte // 0x00-0x0f: magic header
AudioOffset uint32 // 0x10-0x13: offset of audio data
CryptoVersion uint32 // 0x14-0x17: crypto version
CryptoSlot uint32 // 0x18-0x1b: crypto key slot
CryptoTestData []byte // 0x1c-0x2b: crypto test data
CryptoKey []byte // 0x2c-0x3b: crypto key
}
func (h *header) FromFile(rd io.ReadSeeker) error {
if _, err := rd.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("kgm seek start: %w", err)
}
buf := make([]byte, 0x3c)
if _, err := io.ReadFull(rd, buf); err != nil {
return fmt.Errorf("kgm read header: %w", err)
}
return h.FromBytes(buf)
}
func (h *header) FromBytes(buf []byte) error {
if len(buf) < 0x3c {
return errors.New("invalid kgm header length")
}
h.MagicHeader = buf[:0x10]
if !bytes.Equal(kgmHeader, h.MagicHeader) && !bytes.Equal(vprHeader, h.MagicHeader) {
return ErrKgmMagicHeader
}
h.AudioOffset = binary.LittleEndian.Uint32(buf[0x10:0x14])
h.CryptoVersion = binary.LittleEndian.Uint32(buf[0x14:0x18])
h.CryptoSlot = binary.LittleEndian.Uint32(buf[0x18:0x1c])
h.CryptoTestData = buf[0x1c:0x2c]
h.CryptoKey = buf[0x2c:0x3c]
return nil
}

View File

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

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

@ -0,0 +1,59 @@
package kgm
import (
"bytes"
_ "embed"
"github.com/ulikunitz/xz"
"github.com/unlock-music/cli/internal/logging"
"go.uber.org/zap"
"io/ioutil"
)
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 = ioutil.ReadAll(maskReader)
if err != nil {
logging.Log().Fatal("load kgm mask failed", zap.Error(err))
}
fullMaskLen = len(maskV2) * 16
initMaskOK = true
}

View File

@ -2,78 +2,107 @@ package kwm
import ( import (
"bytes" "bytes"
"encoding/binary"
"errors" "errors"
"fmt" "github.com/unlock-music/cli/algo/common"
"io"
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
"unlock-music.dev/cli/algo/common"
) )
const magicHeader1 = "yeelion-kuwo-tme" var (
const magicHeader2 = "yeelion-kuwo\x00\x00\x00\x00" magicHeader = []byte{
0x79, 0x65, 0x65, 0x6C, 0x69, 0x6F, 0x6E, 0x2D,
0x6B, 0x75, 0x77, 0x6F, 0x2D, 0x74, 0x6D, 0x65}
ErrKwFileSize = errors.New("kwm invalid file size")
ErrKwMagicHeader = errors.New("kwm magic header not matched")
)
const keyPreDefined = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk" const keyPreDefined = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
type Decoder struct { type Decoder struct {
rd io.ReadSeeker file []byte
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 NewDecoder(p *common.DecoderParams) common.Decoder { func (d *Decoder) GetMeta() common.Meta {
return &Decoder{rd: p.Reader} return nil
}
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 {
header := make([]byte, 0x400) // kwm header is fixed to 1024 bytes lenData := len(d.file)
_, err := io.ReadFull(d.rd, header) if lenData < 1024 {
if err != nil { return ErrKwFileSize
return fmt.Errorf("kwm read header: %w", err)
} }
if !bytes.Equal(magicHeader, d.file[:16]) {
// check magic header, 0x00 - 0x0F return ErrKwMagicHeader
magicHeader := header[:0x10]
if !bytes.Equal([]byte(magicHeader1), magicHeader) &&
!bytes.Equal([]byte(magicHeader2), magicHeader) {
return errors.New("kwm magic header not matched")
} }
d.cipher = newKwmCipher(header[0x18:0x20]) // Crypto Key, 0x18 - 0x1F
d.bitrate, d.outputExt = parseBitrateAndType(header[0x30:0x38]) // Bitrate & File Extension, 0x30 - 0x38
return nil return nil
} }
func parseBitrateAndType(header []byte) (int, string) { func generateMask(key []byte) []byte {
tmp := strings.TrimRight(string(header), "\x00") keyInt := binary.LittleEndian.Uint64(key)
sep := strings.IndexFunc(tmp, func(r rune) bool { keyStr := strconv.FormatUint(keyInt, 10)
return !unicode.IsDigit(r) keyStrTrim := padOrTruncate(keyStr, 32)
}) mask := make([]byte, 32)
for i := 0; i < 32; i++ {
bitrate, _ := strconv.Atoi(tmp[:sep]) // just ignore the error mask[i] = keyPreDefined[i] ^ keyStrTrim[i]
outputExt := strings.ToLower(tmp[sep:]) }
return bitrate, outputExt return mask
} }
func (d *Decoder) Read(b []byte) (int, error) { func (d *Decoder) parseBitrateAndType() {
n, err := d.rd.Read(b) bitType := string(bytes.TrimRight(d.file[0x30:0x38], string(byte(0))))
if n > 0 { charPos := 0
d.cipher.Decrypt(b[:n], d.offset) for charPos = range bitType {
d.offset += n if !unicode.IsNumber(rune(bitType[charPos])) {
break
} }
return n, err }
var err error
d.bitrate, err = strconv.Atoi(bitType[:charPos])
if err != nil {
d.bitrate = 0
}
d.outputExt = strings.ToLower(bitType[charPos:])
}
func (d *Decoder) Decode() error {
d.parseBitrateAndType()
d.mask = generateMask(d.file[0x18:0x20])
d.audio = d.file[1024:]
dataLen := len(d.audio)
for i := 0; i < dataLen; i++ {
d.audio[i] ^= d.mask[i&0x1F] //equals: [i % 32]
}
return nil
} }
func padOrTruncate(raw string, length int) string { func padOrTruncate(raw string, length int) string {
@ -95,6 +124,6 @@ func padOrTruncate(raw string, length int) string {
func init() { func init() {
// Kuwo Mp3/Flac // Kuwo Mp3/Flac
common.RegisterDecoder("kwm", false, NewDecoder) common.RegisterDecoder("kwm", NewDecoder)
common.RegisterDecoder("kwm", false, common.NewRawDecoder) common.RegisterDecoder("kwm", common.NewRawDecoder)
} }

View File

@ -1,31 +0,0 @@
package kwm
import (
"encoding/binary"
"strconv"
)
type kwmCipher struct {
mask []byte
}
func newKwmCipher(key []byte) *kwmCipher {
return &kwmCipher{mask: generateMask(key)}
}
func generateMask(key []byte) []byte {
keyInt := binary.LittleEndian.Uint64(key)
keyStr := strconv.FormatUint(keyInt, 10)
keyStrTrim := padOrTruncate(keyStr, 32)
mask := make([]byte, 32)
for i := 0; i < 32; i++ {
mask[i] = keyPreDefined[i] ^ keyStrTrim[i]
}
return mask
}
func (c kwmCipher) Decrypt(buf []byte, offset int) {
for i := range buf {
buf[i] ^= c.mask[(offset+i)&0x1F] // equivalent: [i % 32]
}
}

View File

@ -1,31 +1,25 @@
package ncm package ncm
import ( import (
"go.uber.org/zap" "github.com/unlock-music/cli/algo/common"
"strings" "strings"
"unlock-music.dev/cli/algo/common"
) )
type ncmMeta interface { type RawMeta interface {
common.AudioMeta common.Meta
// 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"`
@ -33,53 +27,37 @@ type ncmMetaMusic struct {
TransNames []interface{} `json:"transNames"` TransNames []interface{} `json:"transNames"`
} }
func newNcmMetaMusic(logger *zap.Logger) *ncmMetaMusic { func (m RawMetaMusic) GetAlbumImageURL() string {
ncm := new(ncmMetaMusic)
ncm.logger = logger.With(zap.String("module", "ncmMetaMusic"))
return ncm
}
func (m *ncmMetaMusic) GetAlbumImageURL() string {
return m.AlbumPic return m.AlbumPic
} }
func (m RawMetaMusic) GetArtists() (artists []string) {
func (m *ncmMetaMusic) GetArtists() []string { for _, artist := range m.Artist {
m.logger.Debug("ncm artists", zap.Any("artists", m.Artist)) for _, item := range artist {
name, ok := item.(string)
var artists []string = nil if ok {
if jsonArtists, ok := m.Artist.([][]string); ok {
for _, artist := range jsonArtists {
for _, name := range artist {
artists = append(artists, name) artists = append(artists, name)
} }
} }
} else if artist, ok := m.Artist.(string); ok {
// #78: artist is a string type.
// https://git.unlock-music.dev/um/cli/issues/78
artists = []string{artist}
} else {
m.logger.Warn("unexpected artist type", zap.Any("artists", m.Artist))
} }
return artists return
} }
func (m *ncmMetaMusic) GetTitle() string { func (m RawMetaMusic) GetTitle() string {
return m.MusicName return m.MusicName
} }
func (m *ncmMetaMusic) GetAlbum() string { func (m RawMetaMusic) 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 ncmMetaDJ struct { type RawMetaDJ struct {
ProgramID int `json:"programId"` ProgramID int `json:"programId"`
ProgramName string `json:"programName"` ProgramName string `json:"programName"`
MainMusic ncmMetaMusic `json:"mainMusic"` MainMusic RawMetaMusic `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"`
@ -101,32 +79,32 @@ type ncmMetaDJ struct {
RadioPurchaseCount int `json:"radioPurchaseCount"` RadioPurchaseCount int `json:"radioPurchaseCount"`
} }
func (m *ncmMetaDJ) GetArtists() []string { func (m RawMetaDJ) 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 *ncmMetaDJ) GetTitle() string { func (m RawMetaDJ) GetTitle() string {
if m.ProgramName != "" { if m.ProgramName != "" {
return m.ProgramName return m.ProgramName
} }
return m.MainMusic.GetTitle() return m.MainMusic.GetTitle()
} }
func (m *ncmMetaDJ) GetAlbum() string { func (m RawMetaDJ) GetAlbum() string {
if m.Brand != "" { if m.Brand != "" {
return m.Brand return m.Brand
} }
return m.MainMusic.GetAlbum() return m.MainMusic.GetAlbum()
} }
func (m *ncmMetaDJ) GetFormat() string { func (m RawMetaDJ) GetFormat() string {
return m.MainMusic.GetFormat() return m.MainMusic.GetFormat()
} }
func (m *ncmMetaDJ) GetAlbumImageURL() string { func (m RawMetaDJ) GetAlbumImageURL() string {
if strings.HasPrefix(m.MainMusic.GetAlbumImageURL(), "http") { if strings.HasPrefix(m.MainMusic.GetAlbumImageURL(), "http") {
return m.MainMusic.GetAlbumImageURL() return m.MainMusic.GetAlbumImageURL()
} }

View File

@ -2,207 +2,203 @@ package ncm
import ( import (
"bytes" "bytes"
"context"
"encoding/base64" "encoding/base64"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "github.com/unlock-music/cli/algo/common"
"github.com/unlock-music/cli/internal/logging"
"github.com/unlock-music/cli/internal/utils"
"go.uber.org/zap" "go.uber.org/zap"
"io" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
"unlock-music.dev/cli/algo/common"
"unlock-music.dev/cli/internal/utils"
) )
const magicHeader = "CTENFDAM"
var ( var (
magicHeader = []byte{
0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D}
keyCore = []byte{ keyCore = []byte{
0x68, 0x7a, 0x48, 0x52, 0x41, 0x6d, 0x73, 0x6f, 0x68, 0x7a, 0x48, 0x52, 0x41, 0x6d, 0x73, 0x6f,
0x35, 0x6b, 0x49, 0x6e, 0x62, 0x61, 0x78, 0x57, 0x35, 0x6b, 0x49, 0x6e, 0x62, 0x61, 0x78, 0x57}
}
keyMeta = []byte{ keyMeta = []byte{
0x23, 0x31, 0x34, 0x6C, 0x6A, 0x6B, 0x5F, 0x21, 0x23, 0x31, 0x34, 0x6C, 0x6A, 0x6B, 0x5F, 0x21,
0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28, 0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28}
}
) )
func NewDecoder(p *common.DecoderParams) common.Decoder { func NewDecoder(data []byte) common.Decoder {
return &Decoder{rd: p.Reader, logger: p.Logger.With(zap.String("module", "ncm"))} return &Decoder{
file: data,
fileLen: uint32(len(data)),
}
} }
type Decoder struct { type Decoder struct {
logger *zap.Logger file []byte
rd io.ReadSeeker // rd is the original file reader fileLen uint32
offset int key []byte
cipher common.StreamDecoder box []byte
metaRaw []byte metaRaw []byte
metaType string metaType string
meta ncmMeta meta RawMeta
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 err := d.validateMagicHeader(); err != nil { if !bytes.Equal(magicHeader, d.file[:len(magicHeader)]) {
return err
}
if _, err := d.rd.Seek(2, io.SeekCurrent); err != nil { // 2 bytes gap
return fmt.Errorf("ncm seek file: %w", err)
}
keyData, err := d.readKeyData()
if err != nil {
return err
}
if err := d.readMetaData(); err != nil {
return fmt.Errorf("read meta date failed: %w", err)
}
if _, err := d.rd.Seek(5, io.SeekCurrent); err != nil { // 5 bytes gap
return fmt.Errorf("ncm seek gap: %w", err)
}
if err := d.readCoverData(); err != nil {
return fmt.Errorf("parse ncm cover file failed: %w", err)
}
if err := d.parseMeta(); err != nil {
return fmt.Errorf("parse meta failed: %w (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() ([]byte, error) { func (d *Decoder) readKeyData() error {
bKeyLen := make([]byte, 4) // if d.offsetKey == 0 || d.offsetKey+4 > d.fileLen {
if _, err := io.ReadFull(d.rd, bKeyLen); err != nil { return errors.New("invalid cover file offset")
return nil, fmt.Errorf("ncm read key length: %w", err)
} }
bKeyLen := d.file[d.offsetKey : d.offsetKey+4]
iKeyLen := binary.LittleEndian.Uint32(bKeyLen) iKeyLen := binary.LittleEndian.Uint32(bKeyLen)
d.offsetMeta = d.offsetKey + 4 + iKeyLen
bKeyRaw := make([]byte, iKeyLen) bKeyRaw := make([]byte, iKeyLen)
if _, err := io.ReadFull(d.rd, bKeyRaw); err != nil {
return nil, fmt.Errorf("ncm read key data: %w", err)
}
for i := uint32(0); i < iKeyLen; i++ { for i := uint32(0); i < iKeyLen; i++ {
bKeyRaw[i] ^= 0x64 bKeyRaw[i] = d.file[i+4+d.offsetKey] ^ 0x64
} }
return utils.PKCS7UnPadding(utils.DecryptAES128ECB(bKeyRaw, keyCore))[17:], nil d.key = utils.PKCS7UnPadding(utils.DecryptAes128Ecb(bKeyRaw, keyCore))[17:]
return nil
} }
func (d *Decoder) readMetaData() error { func (d *Decoder) readMetaData() error {
bMetaLen := make([]byte, 4) // if d.offsetMeta == 0 || d.offsetMeta+4 > d.fileLen {
if _, err := io.ReadFull(d.rd, bMetaLen); err != nil { return errors.New("invalid meta file offset")
return fmt.Errorf("ncm read key length: %w", err)
} }
bMetaLen := d.file[d.offsetMeta : d.offsetMeta+4]
iMetaLen := binary.LittleEndian.Uint32(bMetaLen) iMetaLen := binary.LittleEndian.Uint32(bMetaLen)
d.offsetCover = d.offsetMeta + 4 + iMetaLen
if iMetaLen == 0 { if iMetaLen == 0 {
return nil // no meta data return errors.New("no any meta file found")
} }
bMetaRaw := make([]byte, iMetaLen) // Why sub 22: Remove "163 key(Don't modify):"
if _, err := io.ReadFull(d.rd, bMetaRaw); err != nil { bKeyRaw := make([]byte, iMetaLen-22)
return fmt.Errorf("ncm read meta data: %w", err) for i := uint32(0); i < iMetaLen-22; i++ {
} bKeyRaw[i] = d.file[d.offsetMeta+4+22+i] ^ 0x63
bMetaRaw = bMetaRaw[22:] // skip "163 key(Don't modify):"
for i := 0; i < len(bMetaRaw); i++ {
bMetaRaw[i] ^= 0x63
} }
cipherText, err := base64.StdEncoding.DecodeString(string(bMetaRaw)) cipherText, err := base64.StdEncoding.DecodeString(string(bKeyRaw))
if err != nil { if err != nil {
return errors.New("decode ncm meta failed: " + err.Error()) return errors.New("decode ncm meta failed: " + err.Error())
} }
metaRaw := utils.PKCS7UnPadding(utils.DecryptAES128ECB(cipherText, keyMeta)) metaRaw := utils.PKCS7UnPadding(utils.DecryptAes128Ecb(cipherText, keyMeta))
sep := bytes.IndexByte(metaRaw, ':') sepIdx := bytes.IndexRune(metaRaw, ':')
if sep == -1 { if sepIdx == -1 {
return errors.New("invalid ncm meta file") return errors.New("invalid ncm meta file")
} }
d.metaType = string(metaRaw[:sep]) d.metaType = string(metaRaw[:sepIdx])
d.metaRaw = metaRaw[sep+1:] d.metaRaw = metaRaw[sepIdx+1:]
return nil return nil
} }
func (d *Decoder) readCoverData() error { func (d *Decoder) buildKeyBox() {
bCoverFrameLen := make([]byte, 4) box := make([]byte, 256)
if _, err := io.ReadFull(d.rd, bCoverFrameLen); err != nil { for i := 0; i < 256; i++ {
return fmt.Errorf("ncm read cover length: %w", err) box[i] = byte(i)
} }
coverFrameStartOffset, err := d.rd.Seek(0, io.SeekCurrent) keyLen := len(d.key)
if err != nil { var j byte
return fmt.Errorf("ncm fetch cover frame start offset: %w", err) for i := 0; i < 256; i++ {
j = box[i] + j + d.key[i%keyLen]
box[i], box[j] = box[j], box[i]
} }
coverFrameLen := binary.LittleEndian.Uint32(bCoverFrameLen)
bCoverLen := make([]byte, 4) d.box = make([]byte, 256)
if _, err := io.ReadFull(d.rd, bCoverLen); err != nil { var _i byte
return fmt.Errorf("ncm read cover length: %w", err) for i := 0; i < 256; i++ {
_i = byte(i + 1)
si := box[_i]
sj := box[_i+si]
d.box[i] = box[si+sj]
} }
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 = newNcmMetaMusic(d.logger) d.meta = new(RawMetaMusic)
return json.Unmarshal(d.metaRaw, d.meta) return json.Unmarshal(d.metaRaw, d.meta)
case "dj": case "dj":
d.meta = new(ncmMetaDJ) d.meta = new(RawMetaDJ)
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) Read(buf []byte) (int, error) { func (d *Decoder) readCoverData() error {
n, err := d.rd.Read(buf) if d.offsetCover == 0 || d.offsetCover+13 > d.fileLen {
if n > 0 { return errors.New("invalid cover file offset")
d.cipher.Decrypt(buf[:n], d.offset)
d.offset += n
}
return n, err
} }
func (d *Decoder) GetAudioExt() string { coverLenStart := d.offsetCover + 5 + 4
bCoverLen := d.file[coverLenStart : coverLenStart+4]
iCoverLen := binary.LittleEndian.Uint32(bCoverLen)
d.offsetAudio = coverLenStart + 4 + iCoverLen
if iCoverLen == 0 {
return errors.New("no any cover file found")
}
d.cover = d.file[coverLenStart+4 : 4+coverLenStart+iCoverLen]
return nil
}
func (d *Decoder) readAudioData() error {
if d.offsetAudio == 0 || d.offsetAudio > d.fileLen {
return errors.New("invalid audio offset")
}
audioRaw := d.file[d.offsetAudio:]
audioLen := len(audioRaw)
d.audio = make([]byte, audioLen)
for i := uint32(0); i < uint32(audioLen); i++ {
d.audio[i] = d.box[i&0xff] ^ audioRaw[i]
}
return nil
}
func (d *Decoder) Decode() error {
if err := d.readKeyData(); err != nil {
return err
}
d.buildKeyBox()
err := d.readMetaData()
if err == nil {
err = d.parseMeta()
}
if err != nil {
logging.Log().Warn("parse ncm meta file failed", zap.Error(err))
}
err = d.readCoverData()
if err != nil {
logging.Log().Warn("parse ncm cover file failed", zap.Error(err))
}
return d.readAudioData()
}
func (d Decoder) GetAudioExt() string {
if d.meta != nil { if d.meta != nil {
if format := d.meta.GetFormat(); format != "" { if format := d.meta.GetFormat(); format != "" {
return "." + d.meta.GetFormat() return "." + d.meta.GetFormat()
@ -211,43 +207,44 @@ func (d *Decoder) GetAudioExt() string {
return "" return ""
} }
func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) { func (d Decoder) GetAudioData() []byte {
return d.audio
}
func (d Decoder) GetCoverImage() []byte {
if d.cover != nil { if d.cover != nil {
return d.cover, nil return d.cover
}
if d.meta == nil {
return nil, errors.New("ncm meta not found")
} }
{
imgURL := d.meta.GetAlbumImageURL() imgURL := d.meta.GetAlbumImageURL()
if !strings.HasPrefix(imgURL, "http") { if d.meta != nil && !strings.HasPrefix(imgURL, "http") {
return nil, nil // no cover image return nil
} }
resp, err := http.Get(imgURL)
// fetch cover image
req, err := http.NewRequestWithContext(ctx, http.MethodGet, imgURL, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("ncm download image failed: %w", err) logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
return nil
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("ncm download image failed: unexpected http status %s", resp.Status) logging.Log().Warn("download image failed", zap.String("http", resp.Status),
zap.String("url", imgURL))
return nil
} }
d.cover, err = io.ReadAll(resp.Body) data, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("ncm download image failed: %w", err) logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
return nil
}
return data
}
} }
return d.cover, nil func (d Decoder) GetMeta() common.Meta {
} return d.meta
func (d *Decoder) GetAudioMeta(_ context.Context) (common.AudioMeta, error) {
return d.meta, nil
} }
func init() { func init() {
// Netease Mp3/Flac // Netease Mp3/Flac
common.RegisterDecoder("ncm", false, NewDecoder) common.RegisterDecoder("ncm", NewDecoder)
} }

View File

@ -1,42 +0,0 @@
package ncm
type ncmCipher struct {
key []byte
box []byte
}
func newNcmCipher(key []byte) *ncmCipher {
return &ncmCipher{
key: key,
box: buildKeyBox(key),
}
}
func (c *ncmCipher) Decrypt(buf []byte, offset int) {
for i := 0; i < len(buf); i++ {
buf[i] ^= c.box[(i+offset)&0xff]
}
}
func buildKeyBox(key []byte) []byte {
box := make([]byte, 256)
for i := 0; i < 256; i++ {
box[i] = byte(i)
}
var j byte
for i := 0; i < 256; i++ {
j = box[i] + j + key[i%len(key)]
box[i], box[j] = box[j], box[i]
}
ret := make([]byte, 256)
var _i byte
for i := 0; i < 256; i++ {
_i = byte(i + 1)
si := box[_i]
sj := box[_i+si]
ret[i] = box[si+sj]
}
return ret
}

View File

@ -1,39 +0,0 @@
package qmc
import "errors"
type mapCipher struct {
key []byte
box []byte
size int
}
func newMapCipher(key []byte) (*mapCipher, error) {
if len(key) == 0 {
return nil, errors.New("qmc/cipher_map: invalid key size")
}
c := &mapCipher{key: key, size: len(key)}
c.box = make([]byte, c.size)
return c, nil
}
func (c *mapCipher) getMask(offset int) byte {
if offset > 0x7FFF {
offset %= 0x7FFF
}
idx := (offset*offset + 71214) % c.size
return c.rotate(c.key[idx], byte(idx)&0x7)
}
func (c *mapCipher) rotate(value byte, bits byte) byte {
rotate := (bits + 4) % 8
left := value << rotate
right := value >> rotate
return left | right
}
func (c *mapCipher) Decrypt(buf []byte, offset int) {
for i := 0; i < len(buf); i++ {
buf[i] ^= c.getMask(offset + i)
}
}

View File

@ -1,53 +0,0 @@
package qmc
import (
"fmt"
"os"
"reflect"
"testing"
)
func loadTestDataMapCipher(name string) ([]byte, []byte, []byte, error) {
key, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key.bin", name))
if err != nil {
return nil, nil, nil, err
}
raw, err := os.ReadFile(fmt.Sprintf("./testdata/%s_raw.bin", name))
if err != nil {
return nil, nil, nil, err
}
target, err := os.ReadFile(fmt.Sprintf("./testdata/%s_target.bin", name))
if err != nil {
return nil, nil, nil, err
}
return key, raw, target, nil
}
func Test_mapCipher_Decrypt(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
{"mflac_map", false},
{"mgg_map", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key, raw, target, err := loadTestDataMapCipher(tt.name)
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
c, err := newMapCipher(key)
if err != nil {
t.Errorf("init mapCipher failed: %s", err)
return
}
c.Decrypt(raw, 0)
if !reflect.DeepEqual(raw, target) {
t.Error("overall")
}
})
}
}

View File

@ -1,124 +0,0 @@
package qmc
import (
"errors"
)
// A rc4Cipher is an instance of RC4 using a particular key.
type rc4Cipher struct {
box []byte
key []byte
hash uint32
n int
}
// newRC4Cipher creates and returns a new rc4Cipher. The key argument should be the
// RC4 key, at least 1 byte and at most 256 bytes.
func newRC4Cipher(key []byte) (*rc4Cipher, error) {
n := len(key)
if n == 0 {
return nil, errors.New("qmc/cipher_rc4: invalid key size")
}
var c = rc4Cipher{key: key, n: n}
c.box = make([]byte, n)
for i := 0; i < n; i++ {
c.box[i] = byte(i)
}
var j = 0
for i := 0; i < n; i++ {
j = (j + int(c.box[i]) + int(key[i%n])) % n
c.box[i], c.box[j] = c.box[j], c.box[i]
}
c.getHashBase()
return &c, nil
}
func (c *rc4Cipher) getHashBase() {
c.hash = 1
for i := 0; i < c.n; i++ {
v := uint32(c.key[i])
if v == 0 {
continue
}
nextHash := c.hash * v
if nextHash == 0 || nextHash <= c.hash {
break
}
c.hash = nextHash
}
}
const (
rc4SegmentSize = 5120
rc4FirstSegmentSize = 128
)
func (c *rc4Cipher) Decrypt(src []byte, offset int) {
toProcess := len(src)
processed := 0
markProcess := func(p int) (finished bool) {
offset += p
toProcess -= p
processed += p
return toProcess == 0
}
if offset < rc4FirstSegmentSize {
blockSize := toProcess
if blockSize > rc4FirstSegmentSize-offset {
blockSize = rc4FirstSegmentSize - offset
}
c.encFirstSegment(src[:blockSize], offset)
if markProcess(blockSize) {
return
}
}
if offset%rc4SegmentSize != 0 {
blockSize := toProcess
if blockSize > rc4SegmentSize-offset%rc4SegmentSize {
blockSize = rc4SegmentSize - offset%rc4SegmentSize
}
c.encASegment(src[processed:processed+blockSize], offset)
if markProcess(blockSize) {
return
}
}
for toProcess > rc4SegmentSize {
c.encASegment(src[processed:processed+rc4SegmentSize], offset)
markProcess(rc4SegmentSize)
}
if toProcess > 0 {
c.encASegment(src[processed:], offset)
}
}
func (c *rc4Cipher) encFirstSegment(buf []byte, offset int) {
for i := 0; i < len(buf); i++ {
buf[i] ^= c.key[c.getSegmentSkip(offset+i)]
}
}
func (c *rc4Cipher) encASegment(buf []byte, offset int) {
box := make([]byte, c.n)
copy(box, c.box)
j, k := 0, 0
skipLen := (offset % rc4SegmentSize) + c.getSegmentSkip(offset/rc4SegmentSize)
for i := -skipLen; i < len(buf); i++ {
j = (j + 1) % c.n
k = (int(box[j]) + k) % c.n
box[j], box[k] = box[k], box[j]
if i >= 0 {
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))
}

View File

@ -1,115 +0,0 @@
package qmc
import (
"os"
"reflect"
"testing"
)
func loadTestRC4CipherData(name string) ([]byte, []byte, []byte, error) {
prefix := "./testdata/" + name
key, err := os.ReadFile(prefix + "_key.bin")
if err != nil {
return nil, nil, nil, err
}
raw, err := os.ReadFile(prefix + "_raw.bin")
if err != nil {
return nil, nil, nil, err
}
target, err := os.ReadFile(prefix + "_target.bin")
if err != nil {
return nil, nil, nil, err
}
return key, raw, target, nil
}
func Test_rc4Cipher_Decrypt(t *testing.T) {
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 {
t.Fatalf("load testing data failed: %s", err)
}
c, err := newRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw, 0)
if !reflect.DeepEqual(raw, target) {
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) {
key, raw, target, err := loadTestRC4CipherData("mflac0_rc4")
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
t.Run("first-block(0~128)", func(t *testing.T) {
c, err := newRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw[:128], 0)
if !reflect.DeepEqual(raw[:128], target[:128]) {
t.Error("first-block(0~128)")
}
})
}
func Test_rc4Cipher_encASegment(t *testing.T) {
key, raw, target, err := loadTestRC4CipherData("mflac0_rc4")
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
t.Run("align-block(128~5120)", func(t *testing.T) {
c, err := newRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw[128:5120], 128)
if !reflect.DeepEqual(raw[128:5120], target[128:5120]) {
t.Error("align-block(128~5120)")
}
})
t.Run("simple-block(5120~10240)", func(t *testing.T) {
c, err := newRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw[5120:10240], 5120)
if !reflect.DeepEqual(raw[5120:10240], target[5120:10240]) {
t.Error("align-block(128~5120)")
}
})
}

View File

@ -1,57 +0,0 @@
package qmc
func newStaticCipher() *staticCipher {
return &defaultStaticCipher
}
var defaultStaticCipher = staticCipher{}
type staticCipher struct{}
func (c *staticCipher) Decrypt(buf []byte, offset int) {
for i := 0; i < len(buf); i++ {
buf[i] ^= c.getMask(offset + i)
}
}
func (c *staticCipher) getMask(offset int) byte {
if offset > 0x7FFF {
offset %= 0x7FFF
}
idx := (offset*offset + 27) & 0xff
return staticCipherBox[idx]
}
var staticCipherBox = [...]byte{
0x77, 0x48, 0x32, 0x73, 0xDE, 0xF2, 0xC0, 0xC8, //0x00
0x95, 0xEC, 0x30, 0xB2, 0x51, 0xC3, 0xE1, 0xA0, //0x08
0x9E, 0xE6, 0x9D, 0xCF, 0xFA, 0x7F, 0x14, 0xD1, //0x10
0xCE, 0xB8, 0xDC, 0xC3, 0x4A, 0x67, 0x93, 0xD6, //0x18
0x28, 0xC2, 0x91, 0x70, 0xCA, 0x8D, 0xA2, 0xA4, //0x20
0xF0, 0x08, 0x61, 0x90, 0x7E, 0x6F, 0xA2, 0xE0, //0x28
0xEB, 0xAE, 0x3E, 0xB6, 0x67, 0xC7, 0x92, 0xF4, //0x30
0x91, 0xB5, 0xF6, 0x6C, 0x5E, 0x84, 0x40, 0xF7, //0x38
0xF3, 0x1B, 0x02, 0x7F, 0xD5, 0xAB, 0x41, 0x89, //0x40
0x28, 0xF4, 0x25, 0xCC, 0x52, 0x11, 0xAD, 0x43, //0x48
0x68, 0xA6, 0x41, 0x8B, 0x84, 0xB5, 0xFF, 0x2C, //0x50
0x92, 0x4A, 0x26, 0xD8, 0x47, 0x6A, 0x7C, 0x95, //0x58
0x61, 0xCC, 0xE6, 0xCB, 0xBB, 0x3F, 0x47, 0x58, //0x60
0x89, 0x75, 0xC3, 0x75, 0xA1, 0xD9, 0xAF, 0xCC, //0x68
0x08, 0x73, 0x17, 0xDC, 0xAA, 0x9A, 0xA2, 0x16, //0x70
0x41, 0xD8, 0xA2, 0x06, 0xC6, 0x8B, 0xFC, 0x66, //0x78
0x34, 0x9F, 0xCF, 0x18, 0x23, 0xA0, 0x0A, 0x74, //0x80
0xE7, 0x2B, 0x27, 0x70, 0x92, 0xE9, 0xAF, 0x37, //0x88
0xE6, 0x8C, 0xA7, 0xBC, 0x62, 0x65, 0x9C, 0xC2, //0x90
0x08, 0xC9, 0x88, 0xB3, 0xF3, 0x43, 0xAC, 0x74, //0x98
0x2C, 0x0F, 0xD4, 0xAF, 0xA1, 0xC3, 0x01, 0x64, //0xA0
0x95, 0x4E, 0x48, 0x9F, 0xF4, 0x35, 0x78, 0x95, //0xA8
0x7A, 0x39, 0xD6, 0x6A, 0xA0, 0x6D, 0x40, 0xE8, //0xB0
0x4F, 0xA8, 0xEF, 0x11, 0x1D, 0xF3, 0x1B, 0x3F, //0xB8
0x3F, 0x07, 0xDD, 0x6F, 0x5B, 0x19, 0x30, 0x19, //0xC0
0xFB, 0xEF, 0x0E, 0x37, 0xF0, 0x0E, 0xCD, 0x16, //0xC8
0x49, 0xFE, 0x53, 0x47, 0x13, 0x1A, 0xBD, 0xA4, //0xD0
0xF1, 0x40, 0x19, 0x60, 0x0E, 0xED, 0x68, 0x09, //0xD8
0x06, 0x5F, 0x4D, 0xCF, 0x3D, 0x1A, 0xFE, 0x20, //0xE0
0x77, 0xE4, 0xD9, 0xDA, 0xF9, 0xA4, 0x2B, 0x76, //0xE8
0x1C, 0x71, 0xDB, 0x00, 0xBC, 0xFD, 0x0C, 0x6C, //0xF0
0xA5, 0x47, 0xF7, 0xF6, 0x00, 0x79, 0x4A, 0x11, //0xF8
}

View File

@ -1,146 +0,0 @@
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,
},
}
}

View File

@ -1,21 +0,0 @@
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)
}

View File

@ -1,52 +0,0 @@
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
}

View File

@ -1,178 +0,0 @@
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
}

70
algo/qmc/consts.go Normal file
View File

@ -0,0 +1,70 @@
package qmc
var oggPublicHeader1 = []byte{
0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x1e, 0x01, 0x76, 0x6f, 0x72,
0x62, 0x69, 0x73, 0x00, 0x00, 0x00, 0x00, 0x02, 0x44, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xee, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x01, 0x4f, 0x67, 0x67, 0x53, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff}
var oggPublicHeader2 = []byte{
0x03, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73, 0x2c, 0x00, 0x00, 0x00, 0x58, 0x69, 0x70, 0x68, 0x2e,
0x4f, 0x72, 0x67, 0x20, 0x6c, 0x69, 0x62, 0x56, 0x6f, 0x72, 0x62, 0x69, 0x73, 0x20, 0x49, 0x20,
0x32, 0x30, 0x31, 0x35, 0x30, 0x31, 0x30, 0x35, 0x20, 0x28, 0xe2, 0x9b, 0x84, 0xe2, 0x9b, 0x84,
0xe2, 0x9b, 0x84, 0xe2, 0x9b, 0x84, 0x29, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x54,
0x49, 0x54, 0x4c, 0x45, 0x3d}
var oggPublicConfidence1 = []uint{
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 0,
0, 0, 9, 9, 9, 9, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 6, 3, 3, 3, 3, 6, 6, 6, 6,
3, 3, 3, 3, 6, 6, 6, 6, 6, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 0, 0, 0, 0, 9, 9, 9, 9,
0, 0, 0, 0}
var oggPublicConfidence2 = []uint{
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 0, 1, 3, 3, 0, 1, 3, 3, 3,
3, 3, 3, 3, 3}
var (
defaultKey256Mask44 = []byte{
0xde, 0x51, 0xfa, 0xc3, 0x4a, 0xd6, 0xca, 0x90,
0x7e, 0x67, 0x5e, 0xf7, 0xd5, 0x52, 0x84, 0xd8,
0x47, 0x95, 0xbb, 0xa1, 0xaa, 0xc6, 0x66, 0x23,
0x92, 0x62, 0xf3, 0x74, 0xa1, 0x9f, 0xf4, 0xa0,
0x1d, 0x3f, 0x5b, 0xf0, 0x13, 0x0e, 0x09, 0x3d,
0xf9, 0xbc, 0x00, 0x11}
)
var key256MappingAll [][]int //[idx256][idx128]idx44
var key256Mapping128to44 map[int]int
func init() {
{ // init all mapping
key256MappingAll = make([][]int, 256)
for i := 0; i < 128; i++ {
realIdx := (i*i + 27) % 256
if key256MappingAll[realIdx] == nil {
key256MappingAll[realIdx] = []int{i}
} else {
key256MappingAll[realIdx] = append(key256MappingAll[realIdx], i)
}
}
}
{ // init
key256Mapping128to44 = make(map[int]int, 128)
idx44 := 0
for _, all128 := range key256MappingAll {
if all128 != nil {
for _, _i128 := range all128 {
key256Mapping128to44[_i128] = idx44
}
idx44++
}
}
}
}

View File

@ -1,161 +0,0 @@
package qmc
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"math"
"golang.org/x/crypto/tea"
)
func simpleMakeKey(salt byte, length int) []byte {
keyBuf := make([]byte, length)
for i := 0; i < length; i++ {
tmp := math.Tan(float64(salt) + float64(i)*0.1)
keyBuf[i] = byte(math.Abs(tmp) * 100.0)
}
return keyBuf
}
const rawKeyPrefixV2 = "QQMusic EncV2,Key:"
func deriveKey(rawKey []byte) ([]byte, error) {
rawKeyDec := make([]byte, base64.StdEncoding.DecodedLen(len(rawKey)))
n, err := base64.StdEncoding.Decode(rawKeyDec, rawKey)
if err != nil {
return nil, err
}
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")
}
simpleKey := simpleMakeKey(106, 8)
teaKey := make([]byte, 16)
for i := 0; i < 8; i++ {
teaKey[i<<1] = simpleKey[i]
teaKey[i<<1+1] = rawKeyDec[i]
}
rs, err := decryptTencentTea(rawKeyDec[8:], teaKey)
if err != nil {
return nil, err
}
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) {
const saltLen = 2
const zeroLen = 7
if len(inBuf)%8 != 0 {
return nil, errors.New("inBuf size not a multiple of the block size")
}
if len(inBuf) < 16 {
return nil, errors.New("inBuf size too small")
}
blk, err := tea.NewCipherWithRounds(key, 32)
if err != nil {
return nil, err
}
destBuf := make([]byte, 8)
blk.Decrypt(destBuf, inBuf)
padLen := int(destBuf[0] & 0x7)
outLen := len(inBuf) - 1 - padLen - saltLen - zeroLen
out := make([]byte, outLen)
ivPrev := make([]byte, 8)
ivCur := inBuf[:8]
inBufPos := 8
destIdx := 1 + padLen
cryptBlock := func() {
ivPrev = ivCur
ivCur = inBuf[inBufPos : inBufPos+8]
xor8Bytes(destBuf, destBuf, inBuf[inBufPos:inBufPos+8])
blk.Decrypt(destBuf, destBuf)
inBufPos += 8
destIdx = 0
}
for i := 1; i <= saltLen; {
if destIdx < 8 {
destIdx++
i++
} else if destIdx == 8 {
cryptBlock()
}
}
outPos := 0
for outPos < outLen {
if destIdx < 8 {
out[outPos] = destBuf[destIdx] ^ ivPrev[destIdx]
destIdx++
outPos++
} else if destIdx == 8 {
cryptBlock()
}
}
for i := 1; i <= zeroLen; i++ {
if destBuf[destIdx] != ivPrev[destIdx] {
return nil, errors.New("zero check failed")
}
}
return out, nil
}
func xor8Bytes(dst, a, b []byte) {
for i := 0; i < 8; i++ {
dst[i] = a[i] ^ b[i]
}
}

View File

@ -1,57 +0,0 @@
package qmc
import (
"fmt"
"os"
"reflect"
"testing"
)
func TestSimpleMakeKey(t *testing.T) {
expect := []byte{0x69, 0x56, 0x46, 0x38, 0x2b, 0x20, 0x15, 0x0b}
t.Run("106,8", func(t *testing.T) {
if got := simpleMakeKey(106, 8); !reflect.DeepEqual(got, expect) {
t.Errorf("simpleMakeKey() = %v, want %v", got, expect)
}
})
}
func loadDecryptKeyData(name string) ([]byte, []byte, error) {
keyRaw, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key_raw.bin", name))
if err != nil {
return nil, nil, err
}
keyDec, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key.bin", name))
if err != nil {
return nil, nil, err
}
return keyRaw, keyDec, nil
}
func TestDecryptKey(t *testing.T) {
tests := []struct {
name string
filename string
wantErr bool
}{
{"mflac0_rc4(512)", "mflac0_rc4", false},
{"mflac_map(256)", "mflac_map", false},
{"mflac_rc4(256)", "mflac_rc4", false},
{"mgg_map(256)", "mgg_map", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
raw, want, err := loadDecryptKeyData(tt.filename)
if err != nil {
t.Fatalf("load test data failed: %s", err)
}
got, err := deriveKey(raw)
if (err != nil) != tt.wantErr {
t.Errorf("deriveKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, want) {
t.Errorf("deriveKey() got = %v..., want %v...",
string(got[:32]), string(want[:32]))
}
})
}
}

View File

@ -1,158 +0,0 @@
package qmc
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/samber/lo"
"go.uber.org/zap"
"golang.org/x/exp/slices"
"golang.org/x/text/unicode/norm"
"unlock-music.dev/mmkv"
)
var streamKeyVault mmkv.Vault
// TODO: move to factory
func readKeyFromMMKV(file string, logger *zap.Logger) ([]byte, error) {
if file == "" {
return nil, errors.New("file path is required while reading key from mmkv")
}
//goland:noinspection GoBoolExpressions
if runtime.GOOS != "darwin" {
return nil, errors.New("mmkv vault not supported on this platform")
}
if streamKeyVault == nil {
mmkvDir, err := getRelativeMMKVDir(file)
if err != nil {
mmkvDir, err = getDefaultMMKVDir()
if err != nil {
return nil, fmt.Errorf("mmkv key valut not found: %w", err)
}
}
mgr, err := mmkv.NewManager(mmkvDir)
if err != nil {
return nil, fmt.Errorf("init mmkv manager: %w", err)
}
streamKeyVault, err = mgr.OpenVault("MMKVStreamEncryptId")
if err != nil {
return nil, fmt.Errorf("open mmkv vault: %w", err)
}
logger.Debug("mmkv vault opened", zap.Strings("keys", streamKeyVault.Keys()))
}
_, partName := filepath.Split(file)
partName = normalizeUnicode(partName)
buf, err := streamKeyVault.GetBytes(file)
if buf == nil {
filePaths := streamKeyVault.Keys()
fileNames := lo.Map(filePaths, func(filePath string, _ int) string {
_, name := filepath.Split(filePath)
return normalizeUnicode(name)
})
for _, key := range fileNames { // fallback: match filename only
if key != partName {
continue
}
idx := slices.Index(fileNames, key)
buf, err = streamKeyVault.GetBytes(filePaths[idx])
if err != nil {
logger.Warn("read key from mmkv", zap.String("key", filePaths[idx]), zap.Error(err))
}
}
}
if len(buf) == 0 {
return nil, errors.New("key not found in mmkv vault")
}
return deriveKey(buf)
}
func OpenMMKV(mmkvPath string, key string, logger *zap.Logger) error {
filePath, fileName := filepath.Split(mmkvPath)
mgr, err := mmkv.NewManager(filepath.Dir(filePath))
if err != nil {
return fmt.Errorf("init mmkv manager: %w", err)
}
// If `vaultKey` is empty, the key is ignored.
streamKeyVault, err = mgr.OpenVaultCrypto(fileName, key)
if err != nil {
return fmt.Errorf("open mmkv vault: %w", err)
}
logger.Debug("mmkv vault opened", zap.Strings("keys", streamKeyVault.Keys()))
return nil
}
// /
func readKeyFromMMKVCustom(mid string) ([]byte, error) {
if streamKeyVault == nil {
return nil, fmt.Errorf("mmkv vault not loaded")
}
// get ekey from mmkv vault
eKey, err := streamKeyVault.GetBytes(mid)
if err != nil {
return nil, fmt.Errorf("get eKey error: %w", err)
}
return deriveKey(eKey)
}
// / getRelativeMMKVDir get mmkv dir relative to file (legacy QQMusic for macOS behaviour)
func getRelativeMMKVDir(file string) (string, error) {
mmkvDir := filepath.Join(filepath.Dir(file), "../mmkv")
if _, err := os.Stat(mmkvDir); err != nil {
return "", fmt.Errorf("stat default mmkv dir: %w", err)
}
keyFile := filepath.Join(mmkvDir, "MMKVStreamEncryptId")
if _, err := os.Stat(keyFile); err != nil {
return "", fmt.Errorf("stat default mmkv file: %w", err)
}
return mmkvDir, nil
}
func getDefaultMMKVDir() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("get user home dir: %w", err)
}
mmkvDir := filepath.Join(
homeDir,
"Library/Containers/com.tencent.QQMusicMac/Data",
"Library/Application Support/QQMusicMac/mmkv",
)
if _, err := os.Stat(mmkvDir); err != nil {
return "", fmt.Errorf("stat default mmkv dir: %w", err)
}
keyFile := filepath.Join(mmkvDir, "MMKVStreamEncryptId")
if _, err := os.Stat(keyFile); err != nil {
return "", fmt.Errorf("stat default mmkv file: %w", err)
}
return mmkvDir, nil
}
// 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)
}

212
algo/qmc/mask_key256.go Normal file
View File

@ -0,0 +1,212 @@
package qmc
import (
"bytes"
"errors"
"github.com/unlock-music/cli/algo/common"
"github.com/unlock-music/cli/internal/logging"
"go.uber.org/zap"
)
var (
ErrFailToMatchMask = errors.New("can not match at least one key")
ErrTestDataLength = errors.New("invalid length of test file")
ErrMaskLength128 = errors.New("incorrect mask length 128")
ErrMaskLength44 = errors.New("incorrect mask length 44")
ErrMaskDecode = errors.New("decode mask-128 to mask-58 failed")
ErrDetectFlacMask = errors.New("can not detect mflac mask")
ErrDetectMggMask = errors.New("can not detect mgg mask")
)
type Key256Mask struct {
matrix []byte // Mask 128
}
func NewKey256FromMask128(mask128 []byte) (*Key256Mask, error) {
if len(mask128) != 128 {
return nil, ErrMaskLength128
}
q := &Key256Mask{matrix: mask128}
return q, nil
}
func NewKey256FromMask44(mask44 []byte) (*Key256Mask, error) {
mask128, err := convertKey256Mask44to128(mask44)
if err != nil {
return nil, err
}
q := &Key256Mask{matrix: mask128}
return q, nil
}
func (q *Key256Mask) getMatrix44() (mask44 []byte, err error) {
if len(q.matrix) != 128 {
return nil, ErrMaskLength128
}
matrix44 := make([]byte, 44)
idx44 := 0
for _, it256 := range key256MappingAll {
if it256 != nil {
it256Len := len(it256)
for i := 1; i < it256Len; i++ {
if q.matrix[it256[0]] != q.matrix[it256[i]] {
return nil, ErrMaskDecode
}
}
q.matrix[idx44] = q.matrix[it256[0]]
idx44++
}
}
return matrix44, nil
}
func (q *Key256Mask) Decrypt(data []byte) []byte {
dst := make([]byte, len(data))
index := -1
maskIdx := -1
for cur := 0; cur < len(data); cur++ {
index++
maskIdx++
if index == 0x8000 || (index > 0x8000 && (index+1)%0x8000 == 0) {
index++
maskIdx++
}
if maskIdx >= 128 {
maskIdx -= 128
}
dst[cur] = data[cur] ^ q.matrix[maskIdx]
}
return dst
}
func convertKey256Mask44to128(mask44 []byte) ([]byte, error) {
if len(mask44) != 44 {
return nil, ErrMaskLength44
}
mask128 := make([]byte, 128)
idx44 := 0
for _, it256 := range key256MappingAll {
if it256 != nil {
for _, idx128 := range it256 {
mask128[idx128] = mask44[idx44]
}
idx44++
}
}
return mask128, nil
}
func getDefaultMask() *Key256Mask {
y, _ := NewKey256FromMask44(defaultKey256Mask44)
return y
}
func detectMflac256Mask(input []byte) (*Key256Mask, error) {
var q *Key256Mask
var rtErr = ErrDetectFlacMask
lenData := len(input)
lenTest := 0x8000
if lenData < 0x8000 {
lenTest = lenData
}
for blockIdx := 0; blockIdx < lenTest; blockIdx += 128 {
var err error
q, err = NewKey256FromMask128(input[blockIdx : blockIdx+128])
if err != nil {
continue
}
if common.SnifferFLAC(q.Decrypt(input[:4])) {
rtErr = nil
break
}
}
return q, rtErr
}
func detectMgg256Mask(input []byte) (*Key256Mask, error) {
if len(input) < 0x100 {
return nil, ErrTestDataLength
}
matrixConf := make([]map[uint8]uint, 44) //meaning: [idx58][value]confidence
for i := uint(0); i < 44; i++ {
matrixConf[i] = make(map[uint8]uint)
}
page2size := input[0x54] ^ input[0xC] ^ oggPublicHeader1[0xC]
spHeader, spConf := generateOggFullHeader(int(page2size))
lenTest := len(spHeader)
for idx128 := 0; idx128 < lenTest; idx128++ {
confidence := spConf[idx128]
if confidence > 0 {
mask := input[idx128] ^ spHeader[idx128]
idx44 := key256Mapping128to44[idx128&0x7f] // equals: [idx128 % 128]
if _, ok2 := matrixConf[idx44][mask]; ok2 {
matrixConf[idx44][mask] += confidence
} else {
matrixConf[idx44][mask] = confidence
}
}
}
matrix := make([]uint8, 44)
var err error
for i := uint(0); i < 44; i++ {
matrix[i], err = decideMgg256MaskItemConf(matrixConf[i])
if err != nil {
return nil, err
}
}
q, err := NewKey256FromMask44(matrix)
if err != nil {
return nil, err
}
if common.SnifferOGG(q.Decrypt(input[:4])) {
return q, nil
}
return nil, ErrDetectMggMask
}
func generateOggFullHeader(pageSize int) ([]byte, []uint) {
spec := make([]byte, pageSize+1)
spec[0], spec[1], spec[pageSize] = uint8(pageSize), 0xFF, 0xFF
for i := 2; i < pageSize; i++ {
spec[i] = 0xFF
}
specConf := make([]uint, pageSize+1)
specConf[0], specConf[1], specConf[pageSize] = 6, 0, 0
for i := 2; i < pageSize; i++ {
specConf[i] = 4
}
allConf := append(oggPublicConfidence1, specConf...)
allConf = append(allConf, oggPublicConfidence2...)
allHeader := bytes.Join(
[][]byte{oggPublicHeader1, spec, oggPublicHeader2},
[]byte{},
)
return allHeader, allConf
}
func decideMgg256MaskItemConf(confidence map[uint8]uint) (uint8, error) {
lenConf := len(confidence)
if lenConf == 0 {
return 0xff, ErrFailToMatchMask
} else if lenConf > 1 {
logging.Log().Warn("there are 2 potential value for the mask", zap.Any("confidence", confidence))
}
result := uint8(0)
conf := uint(0)
for idx, item := range confidence {
if item > conf {
result = idx
conf = item
}
}
return result, nil
}

View File

@ -1,273 +1,128 @@
package qmc package qmc
import ( import (
"bytes" "encoding/base64"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "github.com/unlock-music/cli/algo/common"
"go.uber.org/zap" )
"io"
"runtime"
"strconv"
"strings"
"unlock-music.dev/cli/algo/common" var (
"unlock-music.dev/cli/internal/sniff" ErrQmcFileLength = errors.New("invalid qmc file length")
ErrQmcKeyDecodeFailed = errors.New("base64 decode qmc key failed")
ErrQmcKeyLength = errors.New("unexpected decoded qmc key length")
) )
type Decoder struct { type Decoder struct {
raw io.ReadSeeker // raw is the original file reader file []byte
params *common.DecoderParams maskDetector func(encodedData []byte) (*Key256Mask, error)
mask *Key256Mask
audio io.Reader // audio is the encrypted audio data audioExt string
audioLen int // audioLen is the audio data length key []byte
offset int // offset is the current audio read position audio []byte
decodedKey []byte // decodedKey is the decoded key for cipher
cipher common.StreamDecoder
songID 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. func NewMflac256Decoder(data []byte) common.Decoder {
// Validate should call before Read to check if the file is valid. return &Decoder{file: data, maskDetector: detectMflac256Mask, audioExt: "flac"}
func (d *Decoder) Read(p []byte) (int, error) {
n, err := d.audio.Read(p)
if n > 0 {
d.cipher.Decrypt(p[:n], d.offset)
d.offset += n
_, _ = d.probeBuf.Write(p[:n]) // bytes.Buffer.Write never return error
}
return n, err
} }
func NewDecoder(p *common.DecoderParams) common.Decoder { func NewMgg256Decoder(data []byte) common.Decoder {
return &Decoder{raw: p.Reader, params: p, logger: p.Logger} return &Decoder{file: data, maskDetector: detectMgg256Mask, audioExt: "ogg"}
} }
func (d *Decoder) Validate() error { func (d *Decoder) Validate() error {
// search & derive key if nil != d.mask {
err := d.searchKey() return nil
}
if nil != d.maskDetector {
if err := d.validateKey(); err != nil {
return err
}
var err error
d.mask, err = d.maskDetector(d.file)
return err
}
return errors.New("no mask or mask detector found")
}
func (d *Decoder) validateKey() error {
lenData := len(d.file)
if lenData < 4 {
return ErrQmcFileLength
}
keyLen := binary.LittleEndian.Uint32(d.file[lenData-4:])
if lenData < int(keyLen+4) {
return ErrQmcFileLength
}
var err error
d.key, err = base64.StdEncoding.DecodeString(
string(d.file[lenData-4-int(keyLen) : lenData-4]))
if err != nil { if err != nil {
return err return ErrQmcKeyDecodeFailed
} }
// check cipher type and init decode cipher if len(d.key) != 272 {
if len(d.decodedKey) > 300 { return ErrQmcKeyLength
d.cipher, err = newRC4Cipher(d.decodedKey)
if err != nil {
return err
} }
} else if len(d.decodedKey) != 0 { d.file = d.file[:lenData-4-int(keyLen)]
d.cipher, err = newMapCipher(d.decodedKey) return nil
if err != nil {
return err
}
} else {
d.cipher = newStaticCipher()
} }
// test with first 16 bytes func (d *Decoder) Decode() error {
if err := d.validateDecode(); err != nil { d.audio = d.mask.Decrypt(d.file)
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 return nil
} }
func (d *Decoder) validateDecode() error { func (d Decoder) GetCoverImage() []byte {
_, 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)
_, ok := sniff.AudioExtension(buf)
if !ok {
return errors.New("qmc: detect file type failed")
}
return nil return nil
} }
func (d *Decoder) searchKey() (err error) { func (d Decoder) GetAudioData() []byte {
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd) return d.audio
if err != nil {
return err
}
fileSize := int(fileSizeM4) + 4
//goland:noinspection GoBoolExpressions
if runtime.GOOS == "darwin" && !strings.HasPrefix(d.params.Extension, ".qmc") {
d.decodedKey, err = readKeyFromMMKV(d.params.FilePath, d.logger)
if err == nil {
d.audioLen = fileSize
return
}
d.logger.Warn("read key from mmkv failed", zap.Error(err))
} }
suffixBuf := make([]byte, 4) func (d Decoder) GetAudioExt() string {
if _, err := io.ReadFull(d.raw, suffixBuf); err != nil { if d.audioExt != "" {
return err return "." + d.audioExt
}
return ""
} }
switch string(suffixBuf) { func (d Decoder) GetMeta() common.Meta {
case "QTag":
return d.readRawMetaQTag()
case "STag":
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
case "cex\x00":
footer, err := NewMusicExTag(d.raw)
if err != nil {
return err
}
d.audioLen = fileSize - int(footer.TagSize)
d.decodedKey, err = readKeyFromMMKVCustom(footer.MediaFileName)
if err != nil {
return err
}
return nil
default:
size := binary.LittleEndian.Uint32(suffixBuf)
if size <= 0xFFFF && size != 0 { // assume size is key len
return d.readRawKey(int64(size))
}
// try to use default static cipher
d.audioLen = fileSize
return nil return nil
} }
func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
return func(file []byte) common.Decoder {
return &Decoder{file: file, audioExt: ext, mask: getDefaultMask()}
} }
func (d *Decoder) readRawKey(rawKeyLen int64) error {
audioLen, err := d.raw.Seek(-(4 + rawKeyLen), io.SeekEnd)
if err != nil {
return err
}
d.audioLen = int(audioLen)
rawKeyData, err := io.ReadAll(io.LimitReader(d.raw, rawKeyLen))
if err != nil {
return err
}
// clean suffix NULs
rawKeyData = bytes.TrimRight(rawKeyData, "\x00")
d.decodedKey, err = deriveKey(rawKeyData)
if err != nil {
return err
}
return nil
}
func (d *Decoder) readRawMetaQTag() error {
// get raw meta data len
if _, err := d.raw.Seek(-8, io.SeekEnd); err != nil {
return err
}
buf, err := io.ReadAll(io.LimitReader(d.raw, 4))
if err != nil {
return err
}
rawMetaLen := int64(binary.BigEndian.Uint32(buf))
// read raw meta data
audioLen, err := d.raw.Seek(-(8 + rawMetaLen), io.SeekEnd)
if err != nil {
return err
}
d.audioLen = int(audioLen)
rawMetaData, err := io.ReadAll(io.LimitReader(d.raw, rawMetaLen))
if err != nil {
return err
}
items := strings.Split(string(rawMetaData), ",")
if len(items) != 3 {
return errors.New("invalid raw meta data")
}
d.decodedKey, err = deriveKey([]byte(items[0]))
if err != nil {
return err
}
d.songID, err = strconv.Atoi(items[1])
if err != nil {
return err
}
d.rawMetaExtra2, err = strconv.Atoi(items[2])
if err != nil {
return err
}
return nil
} }
//goland:noinspection SpellCheckingInspection //goland:noinspection SpellCheckingInspection
func init() { func init() {
supportedExts := []string{ common.RegisterDecoder("qmc0", DecoderFuncWithExt("mp3")) //QQ Music Mp3
"qmc0", "qmc3", //QQ Music MP3 common.RegisterDecoder("qmc3", DecoderFuncWithExt("mp3")) //QQ Music Mp3
"qmc2", "qmc4", "qmc6", "qmc8", //QQ Music M4A
"qmcflac", //QQ Music FLAC
"qmcogg", //QQ Music OGG
"tkm", //QQ Music Accompaniment M4A common.RegisterDecoder("qmc2", DecoderFuncWithExt("m4a")) //QQ Music M4A
common.RegisterDecoder("qmc4", DecoderFuncWithExt("m4a")) //QQ Music M4A
common.RegisterDecoder("qmc6", DecoderFuncWithExt("m4a")) //QQ Music M4A
common.RegisterDecoder("qmc8", DecoderFuncWithExt("m4a")) //QQ Music M4A
"bkcmp3", "bkcm4a", "bkcflac", "bkcwav", "bkcape", "bkcogg", "bkcwma", //Moo Music common.RegisterDecoder("qmcflac", DecoderFuncWithExt("flac")) //QQ Music Flac
common.RegisterDecoder("qmcogg", DecoderFuncWithExt("ogg")) //QQ Music Ogg
common.RegisterDecoder("tkm", DecoderFuncWithExt("m4a")) //QQ Music Accompaniment M4a
"666c6163", //QQ Music Weiyun Flac common.RegisterDecoder("bkcmp3", DecoderFuncWithExt("mp3")) //Moo Music Mp3
"6d7033", //QQ Music Weiyun Mp3 common.RegisterDecoder("bkcflac", DecoderFuncWithExt("flac")) //Moo Music Flac
"6f6767", //QQ Music Weiyun Ogg
"6d3461", //QQ Music Weiyun M4a
"776176", //QQ Music Weiyun Wav
"mmp4", // QQ Music MP4 Container, tipically used for Dolby EAC3 stream common.RegisterDecoder("666c6163", DecoderFuncWithExt("flac")) //QQ Music Weiyun Flac
} common.RegisterDecoder("6d7033", DecoderFuncWithExt("mp3")) //QQ Music Weiyun Mp3
for _, ext := range supportedExts { common.RegisterDecoder("6f6767", DecoderFuncWithExt("ogg")) //QQ Music Weiyun Ogg
common.RegisterDecoder(ext, false, NewDecoder) common.RegisterDecoder("6d3461", DecoderFuncWithExt("m4a")) //QQ Music Weiyun M4a
} common.RegisterDecoder("776176", DecoderFuncWithExt("wav")) //QQ Music Weiyun Wav
// New ogg/flac: common.RegisterDecoder("mgg", NewMgg256Decoder) //QQ Music New Ogg
extraExtsCanHaveSuffix := []string{"mgg", "mflac"} common.RegisterDecoder("mflac", NewMflac256Decoder) //QQ Music New Flac
// Mac also adds some extra suffix to ext:
extraExtSuffix := []string{"0", "1", "a", "h", "l"}
for _, ext := range extraExtsCanHaveSuffix {
common.RegisterDecoder(ext, false, NewDecoder)
for _, suffix := range extraExtSuffix {
common.RegisterDecoder(ext+suffix, false, NewDecoder)
}
}
} }

View File

@ -1,93 +0,0 @@
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[0x04:0x0C],
}
if !bytes.Equal(tag.TagMagic, []byte("musicex\x00")) {
return nil, errors.New("MusicEx magic mismatch")
}
if tag.TagVersion != 1 {
return nil, errors.New(fmt.Sprintf("unsupported musicex tag version. expecting 1, got %d", tag.TagVersion))
}
if tag.TagSize < 0xC0 {
return nil, errors.New(fmt.Sprintf("unsupported musicex tag size. expecting at least 0xC0, got 0x%02x", tag.TagSize))
}
buffer = make([]byte, tag.TagSize)
bytesRead, err = f.Read(buffer)
if err != nil {
return nil, 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()
}

View File

@ -1,128 +0,0 @@
package qmc
import (
"context"
"errors"
"fmt"
"strings"
"github.com/samber/lo"
"unlock-music.dev/cli/algo/common"
"unlock-music.dev/cli/algo/qmc/client"
"unlock-music.dev/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.WithoutEmpty(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
}

View File

@ -1,101 +0,0 @@
package qmc
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"testing"
"unlock-music.dev/cli/algo/common"
)
func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
encBody, err := os.ReadFile(fmt.Sprintf("./testdata/%s_raw.bin", filename))
if err != nil {
return nil, nil, err
}
encSuffix, err := os.ReadFile(fmt.Sprintf("./testdata/%s_suffix.bin", filename))
if err != nil {
return nil, nil, err
}
target, err := os.ReadFile(fmt.Sprintf("./testdata/%s_target.bin", filename))
if err != nil {
return nil, nil, err
}
return bytes.Join([][]byte{encBody, encSuffix}, nil), target, nil
}
func TestMflac0Decoder_Read(t *testing.T) {
tests := []struct {
name string
fileExt string
wantErr bool
}{
{"mflac0_rc4", ".mflac0", false},
{"mflac_rc4", ".mflac", false},
{"mflac_map", ".mflac", false},
{"mgg_map", ".mgg", false},
{"qmc0_static", ".qmc0", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
raw, target, err := loadTestDataQmcDecoder(tt.name)
if err != nil {
t.Fatal(err)
}
d := NewDecoder(&common.DecoderParams{
Reader: bytes.NewReader(raw),
Extension: tt.fileExt,
})
if err := d.Validate(); err != nil {
t.Errorf("validate file error = %v", err)
}
buf := make([]byte, len(target))
if _, err := io.ReadFull(d, buf); err != nil {
t.Errorf("read bytes from decoder error = %v", err)
return
}
if !reflect.DeepEqual(buf, target) {
t.Errorf("Decrypt() got = %v, want %v", buf[:32], target[:32])
}
})
}
}
func TestMflac0Decoder_Validate(t *testing.T) {
tests := []struct {
name string
fileExt string
wantErr bool
}{
{"mflac0_rc4", ".flac", false},
{"mflac_map", ".flac", false},
{"mgg_map", ".ogg", false},
{"qmc0_static", ".mp3", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
raw, _, err := loadTestDataQmcDecoder(tt.name)
if err != nil {
t.Fatal(err)
}
d := NewDecoder(&common.DecoderParams{
Reader: bytes.NewReader(raw),
Extension: tt.fileExt,
})
if err := d.Validate(); err != nil {
t.Errorf("read bytes from decoder error = %v", err)
return
}
})
}
}

View File

@ -1 +0,0 @@
dRzX3p5ZYqAlp7lLSs9Zr0rw1iEZy23bB670x4ch2w97x14Zwpk1UXbKU4C2sOS7uZ0NB5QM7ve9GnSrr2JHxP74hVNONwVV77CdOOVb807317KvtI5Yd6h08d0c5W88rdV46C235YGDjUSZj5314YTzy0b6vgh4102P7E273r911Nl464XV83Hr00rkAHkk791iMGSJH95GztN28u2Nv5s9Xx38V69o4a8aIXxbx0g1EM0623OEtbtO9zsqCJfj6MhU7T8iVS6M3q19xhq6707E6r7wzPO6Yp4BwBmgg4F95Lfl0vyF7YO6699tb5LMnr7iFx29o98hoh3O3Rd8h9Juu8P1wG7vdnO5YtRlykhUluYQblNn7XwjBJ53HAyKVraWN5dG7pv7OMl1s0RykPh0p23qfYzAAMkZ1M422pEd07TA9OCKD1iybYxWH06xj6A8mzmcnYGT9P1a5Ytg2EF5LG3IknL2r3AUz99Y751au6Cr401mfAWK68WyEBe5

View File

@ -1 +0,0 @@
ZFJ6WDNwNVrjEJZB1o6QjkQV2ZbHSw/2Eb00q1+4z9SVWYyFWO1PcSQrJ5326ubLklmk2ab3AEyIKNUu8DFoAoAc9dpzpTmc+pdkBHjM/bW2jWx+dCyC8vMTHE+DHwaK14UEEGW47ZXMDi7PRCQ2Jpm/oXVdHTIlyrc+bRmKfMith0L2lFQ+nW8CCjV6ao5ydwkZhhNOmRdrCDcUXSJH9PveYwra9/wAmGKWSs9nemuMWKnbjp1PkcxNQexicirVTlLX7PVgRyFyzNyUXgu+R2S4WTmLwjd8UsOyW/dc2mEoYt+vY2lq1X4hFBtcQGOAZDeC+mxrN0EcW8tjS6P4TjOjiOKNMxIfMGSWkSKL3H7z5K7nR1AThW20H2bP/LcpsdaL0uZ/js1wFGpdIfFx9rnLC78itL0WwDleIqp9TBMX/NwakGgIPIbjBwfgyD8d8XKYuLEscIH0ZGdjsadB5XjybgdE3ppfeFEcQiqpnodlTaQRm3KDIF9ATClP0mTl8XlsSojsZ468xseS1Ib2iinx/0SkK3UtJDwp8DH3/+ELisgXd69Bf0pve7wbrQzzMUs9/Ogvvo6ULsIkQfApJ8cSegDYklzGXiLNH7hZYnXDLLSNejD7NvQouULSmGsBbGzhZ5If0NP/6AhSbpzqWLDlabTDgeWWnFeZpBnlK6SMxo+YFFk1Y0XLKsd69+jj

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1 +0,0 @@
yw7xWOyNQ8585Jwx3hjB49QLPKi38F89awnrQ0fq66NT9TDq1ppHNrFqhaDrU5AFk916D58I53h86304GqOFCCyFzBem68DqiXJ81bILEQwG3P3MOnoNzM820kNW9Lv9IJGNn9Xo497p82BLTm4hAX8JLBs0T2pilKvT429sK9jfg508GSk4d047Jxdz5Fou4aa33OkyFRBU3x430mgNBn04Lc9BzXUI2IGYXv3FGa9qE4Vb54kSjVv8ogbg47j3

View File

@ -1 +0,0 @@
eXc3eFdPeU6+3f7GVeF35bMpIEIQj5JWOWt7G+jsR68Hx3BUFBavkTQ8dpPdP0XBIwPe+OfdsnTGVQqPyg3GCtQSrkgA0mwSQdr4DPzKLkEZFX+Cf1V6ChyipOuC6KT37eAxWMdV1UHf9/OCvydr1dc6SWK1ijRUcP6IAHQhiB+mZLay7XXrSPo32WjdBkn9c9sa2SLtI48atj5kfZ4oOq6QGeld2JA3Z+3wwCe6uTHthKaEHY8ufDYodEe3qqrjYpzkdx55pCtxCQa1JiNqFmJigWm4m3CDzhuJ7YqnjbD+mXxLi7BP1+z4L6nccE2h+DGHVqpGjR9+4LBpe4WHB4DrAzVp2qQRRQJxeHd1v88=

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1 +0,0 @@
pUtyvqr0TgAvR95mNmY7DmNl386TsJNAEIz95CEcgIgJCcs28686O7llxD5E74ldn70xMtd5cG58TA5ILw09I8BOTf5EdHKd6wwPn689DUK13y3Req6H0P33my2miJ5bQ2AA22B8vp4V0NJ3hBqNtFf7cId48V6W51e1kwgu1xKKawxe9BByT92MFlqrFaKH32dB2zFgyd38l2P1outr4l2XLq48F9G17ptRz4W8Loxu28RvZgv0BzL26Ht9I2L5VCwMzzt7OeZ55iQs40Tr6k81QGraIUJj5zeBMgJRMTaSgi19hU5x5a08Qd662MbFhZZ0FjVvaDy1nbIDhrC62c1lX6wf70O45h4W42VxloBVeZ9Sef4V7cWrjrEjj3DJ5w2iu6Q9uoal2f4390kue42Um5HcDFWqv3m56k6O89bRV424PaRra1k9Cd2L56IN2zfBYqNo2WP5VC68G8w1hfflOY0O52h4WdcpoHSjZm4b35N7l47dT4dwEXj1U4J5

View File

@ -1 +0,0 @@
cFV0eXZxcjAF/IXJ9qJT1u5C3S5AgY9BoVtIQNBKfxQMt5hH7BF36ndIJGV5L6qw5h4G0IOIOOewdHmMCNfKJftHM4nv3B0iRlSdqJKdL08wO3sV0v8eZk0OiYAlxgseGcBquQWYS/0b5Lj/Ioi2NfpOthAY9vUiRPnfH3+7/2AJGudHjj4Gg1KkpPW3mXIKbsk+Ou9fhrUqs873BCdsmI6qRmVNhOkLaUcbG6Zin3XU0WkgnnjebR43S8N4bw5BTphFvhy42QvspnD7Ewb1tVZQMQ2N1s38nBjukdfCB9R6aRwITOvg2U7Lr0RjLpbrIn6A6iVilpINjK4VptuKUTlpDXQwgCjoqeHQaHNCWgYpdjB69lXn8km/BfzK7QyDbh0VgTikwAHF9tvPhin3AIDRcU0xsaWYKURRfJelX3pSN495ADlhXdEKL/+l60hVnY7t6iCMxJL3lOtdGtdUYUGUCc76PB1fX+0HTWCcfcwvXTEdczr9J1h2yTeJNqFQ5pNy8vX7Ws8k7vDQVFkw4llZjPhb0kg9aDNePTNIKSGwy/7eofrcUQlC9DI+qqqwQ5abA/93fNsPq6XU3uwawnrbBsdz8DDdjJiEDI7abkPIDIfr/uR0YzgBxW90t5bt6xAtuW+VSYAM7kGxI3RZTl7JgOT60MLyIWkYASrRhRPMGks8zL10ED/4yGTEB1nt

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1 +0,0 @@
zGxNk54pKJ0hDkAo80wHE80ycSWQ7z4m4E846zVy2sqCn14F42Y5S7GqeR11WpOV75sDLbE5dFP992t88l0pHy1yAQ49YK6YX6c543drBYLo55Hc4Y0Fyic6LQPiGqu2bG31r8vaq9wS9v63kg0X5VbnOD6RhO4t0RRhk3ajrA7p0iIy027z0L70LZjtw6E18H0D41nz6ASTx71otdF9z1QNC0JmCl51xvnb39zPExEXyKkV47S6QsK5hFh884QJ

View File

@ -1 +0,0 @@
ekd4Tms1NHC53JEDO/AKVyF+I0bj0hHB7CZeoLDGSApaQB9Oo/pJTBGA/RO+nk5RXLXdHsffLiY4e8kt3LNo6qMl7S89vkiSFxx4Uoq4bGDJ7Jc+bYL6lLsa3M4sBvXS4XcPChrMDz+LmrJMGG6ua2fYyIz1d6TCRUBf1JJgCIkBbDAEeMVYc13qApitiz/apGAPmAnveCaDhfD5GxWsF+RfQ2OcnvrnIXe80Feh/0jx763DlsOBI3eIede6t5zYHokWkZmVEF1jMrnlvsgbQK2EzUWMblmLMsTKNILyZazEoKUyulqmyLO/c/KYE+USPOXPcbjlYFmLhSGHK7sQB5aBR153Yp+xh61ooh2NGAA=

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -3,56 +3,77 @@ package tm
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt" "github.com/unlock-music/cli/algo/common"
"io"
"unlock-music.dev/cli/algo/common"
"unlock-music.dev/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 {
raw io.ReadSeeker // raw is the original file reader file []byte
audio []byte
headerMatch bool
audioExt string
}
offset int func (d *Decoder) GetCoverImage() []byte {
audio io.Reader // audio is the decrypted audio data return nil
}
func (d *Decoder) GetAudioData() []byte {
return d.audio
}
func (d *Decoder) GetAudioExt() string {
if d.audioExt != "" {
return "." + d.audioExt
}
return ""
}
func (d *Decoder) GetMeta() common.Meta {
return nil
} }
func (d *Decoder) Validate() error { func (d *Decoder) Validate() error {
header := make([]byte, 8) if len(d.file) < 8 {
if _, err := io.ReadFull(d.raw, header); err != nil { return errors.New("invalid file size")
return fmt.Errorf("tm read header: %w", err)
} }
if !bytes.Equal(magicHeader, d.file[:4]) {
if bytes.Equal(magicHeader, header[:len(magicHeader)]) { // replace m4a header return errors.New("not a valid tm file")
d.audio = io.MultiReader(bytes.NewReader(replaceHeader), d.raw) }
d.headerMatch = true
return nil return nil
} }
if _, ok := sniff.AudioExtension(header); ok { // not encrypted func (d *Decoder) Decode() error {
d.audio = io.MultiReader(bytes.NewReader(header), d.raw) d.audio = d.file
if d.headerMatch {
for i := 0; i < 8; i++ {
d.audio[i] = replaceHeader[i]
}
d.audioExt = "m4a"
}
return nil return nil
} }
return errors.New("tm: valid magic header") //goland:noinspection GoUnusedExportedFunction
func NewDecoder(data []byte) common.Decoder {
return &Decoder{file: data}
} }
func (d *Decoder) Read(buf []byte) (int, error) { func DecoderFuncWithExt(ext string) common.NewDecoderFunc {
return d.audio.Read(buf) return func(file []byte) common.Decoder {
return &Decoder{file: file, audioExt: ext}
} }
func NewTmDecoder(p *common.DecoderParams) common.Decoder {
return &Decoder{raw: p.Reader}
} }
func init() { func init() {
// QQ Music IOS M4a (replace header) // QQ Music IOS M4a
common.RegisterDecoder("tm2", false, NewTmDecoder) common.RegisterDecoder("tm2", DecoderFuncWithExt("m4a"))
common.RegisterDecoder("tm6", false, NewTmDecoder) common.RegisterDecoder("tm6", DecoderFuncWithExt("m4a"))
// QQ Music IOS Mp3
common.RegisterDecoder("tm0", common.NewRawDecoder)
common.RegisterDecoder("tm3", common.NewRawDecoder)
// QQ Music IOS Mp3 (not encrypted)
common.RegisterDecoder("tm0", false, NewTmDecoder)
common.RegisterDecoder("tm3", false, NewTmDecoder)
} }

View File

@ -1,91 +0,0 @@
package xiami
import (
"bytes"
"errors"
"fmt"
"io"
"unlock-music.dev/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)
}

View File

@ -1,21 +0,0 @@
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
}
}
}

View File

@ -1,34 +0,0 @@
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
}

Binary file not shown.

View File

@ -1,40 +0,0 @@
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
}

Binary file not shown.

View File

@ -1,57 +0,0 @@
package ximalaya
import (
"bytes"
"fmt"
"io"
"unlock-music.dev/cli/algo/common"
"unlock-music.dev/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 Normal file
View File

@ -0,0 +1,106 @@
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", NewDecoder)
// Xiami Typed Format
common.RegisterDecoder("wav", DecoderFuncWithExt("wav"))
common.RegisterDecoder("mp3", DecoderFuncWithExt("mp3"))
common.RegisterDecoder("flac", DecoderFuncWithExt("flac"))
common.RegisterDecoder("m4a", DecoderFuncWithExt("m4a"))
}

View File

@ -1,167 +1,66 @@
package main package main
import ( import (
"bytes"
"context"
"errors" "errors"
"fmt" "github.com/unlock-music/cli/algo/common"
"github.com/fsnotify/fsnotify" _ "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"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"runtime"
"runtime/debug"
"sort"
"strings" "strings"
"time"
"unlock-music.dev/cli/algo/common"
_ "unlock-music.dev/cli/algo/kgm"
_ "unlock-music.dev/cli/algo/kwm"
_ "unlock-music.dev/cli/algo/ncm"
"unlock-music.dev/cli/algo/qmc"
_ "unlock-music.dev/cli/algo/tm"
_ "unlock-music.dev/cli/algo/xiami"
_ "unlock-music.dev/cli/algo/ximalaya"
"unlock-music.dev/cli/internal/ffmpeg"
"unlock-music.dev/cli/internal/sniff"
"unlock-music.dev/cli/internal/utils"
) )
var AppVersion = "v0.2.11" var AppVersion = "0.0.4"
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://git.unlock-music.dev/um/cli", Usage: "Unlock your encrypted music file https://github.com/unlock-music/cli",
Version: fmt.Sprintf("%s (%s,%s/%s)", AppVersion, runtime.Version(), runtime.GOOS, runtime.GOARCH), Version: AppVersion,
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 qmc mmkv (.crc file also required)", Required: false},
&cli.StringFlag{Name: "qmc-mmkv-key", Aliases: []string{"key"}, Usage: "mmkv password (16 ascii chars)", Required: false},
&cli.BoolFlag{Name: "remove-source", Aliases: []string{"rs"}, Usage: "remove source file", Required: false, Value: false},
&cli.BoolFlag{Name: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true},
&cli.BoolFlag{Name: "verbose", Aliases: []string{"V"}, Usage: "verbose logging", Required: false, Value: false},
&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: fmt.Sprintf("Copyright (c) 2020 - %d Unlock Music https://git.unlock-music.dev/um/cli/src/branch/master/LICENSE", time.Now().Year()), Copyright: "Copyright (c) 2020 - 2021 Unlock Music https://github.com/unlock-music/cli/blob/master/LICENSE",
HideHelpCommand: true, HideHelpCommand: true,
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input", UsageText: "um [-o /path/to/output/dir] [-i] /path/to/input",
} }
err := app.Run(os.Args) err := app.Run(os.Args)
if err != nil { if err != nil {
logger.Fatal("run app failed", zap.Error(err)) logging.Log().Fatal("run app failed", zap.Error(err))
} }
} }
func printSupportedExtensions() { func appMain(c *cli.Context) error {
var exts []string
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)
}
sort.Strings(exts)
for _, ext := range exts {
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) {
logger = setupLogger(c.Bool("verbose"))
cwd, err := os.Getwd()
if err != nil {
return err
}
if c.Bool("supported-ext") {
printSupportedExtensions()
return nil
}
input := c.String("input") input := c.String("input")
if input == "" { if input == "" && c.Args().Present() {
switch c.Args().Len() { input = c.Args().Get(c.Args().Len() - 1)
case 0:
input = cwd
case 1:
input = c.Args().Get(0)
default:
return errors.New("please specify input file (or directory)")
}
}
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
}
}
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) {
@ -174,276 +73,81 @@ 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 mmkv := c.String("qmc-mmkv"); mmkv != "" {
// If key is not set, the mmkv vault will be treated as unencrypted.
key := c.String("qmc-mmkv-key")
err := qmc.OpenMMKV(mmkv, key, logger)
if err != nil {
return err
}
}
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"),
}
if inputStat.IsDir() { if inputStat.IsDir() {
watchDir := c.Bool("watch") return dealDirectory(input, output)
if !watchDir {
return proc.processDir(input)
} else { } else {
return proc.watchDir(input) ext := strings.TrimLeft(filepath.Ext(inputStat.Name()), ".")
allDec := common.GetDecoder(ext)
if len(allDec) == 0 {
logging.Log().Fatal("skipping while no suitable decoder")
} }
} else { return tryDecFile(input, output, allDec)
return proc.processFile(input)
} }
} }
func dealDirectory(inputDir string, outputDir string) error {
type processor struct {
logger *zap.Logger
inputDir string
outputDir string
skipNoopDecoder bool
removeSource bool
updateMetadata bool
overwriteOutput bool
}
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 { continue
lastError = err
} }
ext := strings.TrimLeft(filepath.Ext(item.Name()), ".")
allDec := common.GetDecoder(ext)
if len(allDec) == 0 {
logging.Log().Info("skipping while no suitable decoder", zap.String("file", item.Name()))
continue continue
} }
if err := p.processFile(filePath); err != nil { err := tryDecFile(filepath.Join(inputDir, item.Name()), outputDir, allDec)
lastError = err if err != nil {
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err)) logging.Log().Error("conversion failed", zap.String("source", item.Name()))
} }
} }
if lastError != nil {
return fmt.Errorf("last error: %w", lastError)
}
return nil return nil
} }
func (p *processor) processFile(filePath string) error { func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFunc) error {
p.logger.Debug("processFile", zap.String("file", filePath), zap.String("inputDir", p.inputDir)) file, err := os.ReadFile(inputFile)
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
if len(allDec) == 0 {
return errors.New("skipping while no suitable decoder")
}
if err := p.process(filePath, allDec); err != nil {
return err
}
// if source file need to be removed
if p.removeSource {
err := os.RemoveAll(filePath)
if err != nil { if err != nil {
return err return err
} }
logger.Info("source file removed after success conversion", zap.String("source", filePath))
var dec common.Decoder
for _, decFunc := range allDec {
dec = decFunc(file)
if err := dec.Validate(); err == nil {
break
} }
return nil logging.Log().Warn("try decode failed", zap.Error(err))
dec = nil
}
if dec == nil {
return errors.New("no any decoder can resolve the file")
}
if err := dec.Decode(); err != nil {
return errors.New("failed while decoding: " + err.Error())
} }
func (p *processor) findDecoder(decoders []common.DecoderFactory, params *common.DecoderParams) (*common.Decoder, *common.DecoderFactory, error) { outData := dec.GetAudioData()
for _, factory := range decoders { outExt := dec.GetAudioExt()
dec := factory.Create(params) if outExt == "" {
err := dec.Validate() if ext, ok := common.SniffAll(outData); ok {
if err == nil { outExt = ext
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 {
return err
}
defer file.Close()
logger := logger.With(zap.String("source", inputFile))
pDec, decoderFactory, err := p.findDecoder(allDec, &common.DecoderParams{
Reader: file,
Extension: filepath.Ext(inputFile),
FilePath: inputFile,
Logger: logger,
})
if err != nil {
return err
}
dec := *pDec
params := &ffmpeg.UpdateMetadataParams{}
header := bytes.NewBuffer(nil)
_, err = io.CopyN(header, dec, 64)
if err != nil {
return fmt.Errorf("read header failed: %w", err)
}
audio := io.MultiReader(header, dec)
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)
}
defer os.Remove(params.Audio)
params.Meta, err = audioMetaGetter.GetAudioMeta(ctx)
if err != nil {
logger.Warn("get audio meta failed", zap.Error(err))
}
if params.Meta == nil { // reset audio meta if failed
audio, err = os.Open(params.Audio)
if err != nil {
return fmt.Errorf("updateAudioMeta open temp file: %w", err)
}
}
}
}
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 {
params.AlbumArtExt = imgExt outExt = ".mp3"
params.AlbumArt = cover
}
} }
} }
filenameOnly := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
inputRelDir, err := filepath.Rel(p.inputDir, filepath.Dir(inputFile)) outPath := filepath.Join(outputDir, filenameOnly+outExt)
if err != nil { err = os.WriteFile(outPath, outData, 0644)
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
} }
defer outFile.Close() logging.Log().Info("successfully converted",
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
} }

28
go.mod
View File

@ -1,26 +1,12 @@
module unlock-music.dev/cli module github.com/unlock-music/cli
go 1.23.3 go 1.16
require ( require (
github.com/fsnotify/fsnotify v1.8.0 github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/go-flac/flacpicture v0.3.0
github.com/go-flac/flacvorbis v0.2.0
github.com/go-flac/go-flac v1.0.0
github.com/samber/lo v1.47.0
github.com/urfave/cli/v2 v2.27.5
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.29.0
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
golang.org/x/text v0.20.0
unlock-music.dev/mmkv v0.1.0
)
require (
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect github.com/ulikunitz/xz v0.5.10
go.uber.org/multierr v1.11.0 // indirect github.com/urfave/cli/v2 v2.3.0
golang.org/x/sys v0.27.0 // indirect go.uber.org/multierr v1.6.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect go.uber.org/zap v1.16.0
) )

113
go.sum
View File

@ -1,60 +1,65 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc= 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.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
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/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/go-flac/flacpicture v0.3.0 h1:LkmTxzFLIynwfhHiZsX0s8xcr3/u33MzvV89u+zOT8I= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/go-flac/flacpicture v0.3.0/go.mod h1:DPbrzVYQ3fJcvSgLFp9HXIrEQEdfdk/+m0nQCzwodZI= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/go-flac/flacvorbis v0.2.0 h1:KH0xjpkNTXFER4cszH4zeJxYcrHbUobz/RticWGOESs= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/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/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho= github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w= github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM= go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io= golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
unlock-music.dev/mmkv v0.1.0 h1:hgUHo0gJVoiKZ6bOcFOw2LHFqNiefIe+jb5o0OyL720= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
unlock-music.dev/mmkv v0.1.0/go.mod h1:qr34SM3x8xRxyUfGzefH/rSi+DUXkQZcSfXY/yfuTeo= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=

View File

@ -1,126 +0,0 @@
package ffmpeg
import (
"bytes"
"context"
"fmt"
"go.uber.org/zap"
"io"
"os"
"os/exec"
"strings"
"unlock-music.dev/cli/algo/common"
"unlock-music.dev/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
}

View File

@ -1,141 +0,0 @@
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
}

View File

@ -1,94 +0,0 @@
package ffmpeg
import (
"context"
"go.uber.org/zap"
"mime"
"strings"
"github.com/go-flac/flacpicture"
"github.com/go-flac/flacvorbis"
"github.com/go-flac/go-flac"
"golang.org/x/exp/slices"
)
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)
}

View File

@ -1,131 +0,0 @@
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)
}
}

View File

@ -0,0 +1,41 @@
package logging
import (
"os"
"sync"
"time"
)
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// newDefaultProductionLog configures a custom log that is
// intended for use by default if no other log is specified
// in a config. It writes to stderr, uses the console encoder,
// and enables INFO-level logs and higher.
func newDefaultProductionLog() *zap.Logger {
encCfg := zap.NewProductionEncoderConfig()
// if interactive terminal, make output more human-readable by default
encCfg.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(ts.Format("2006/01/02 15:04:05.000"))
}
encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
enc := zapcore.NewConsoleEncoder(encCfg)
core := zapcore.NewCore(enc, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(zap.DebugLevel))
return zap.New(core)
}
// Log returns the current default logger.
func Log() *zap.Logger {
defaultLoggerMu.RLock()
defer defaultLoggerMu.RUnlock()
return defaultLogger
}
var (
defaultLogger = newDefaultProductionLog()
defaultLoggerMu sync.RWMutex
)

View File

@ -1,14 +0,0 @@
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()
}

View File

@ -1,106 +0,0 @@
package sniff
import (
"bytes"
"encoding/binary"
"golang.org/x/exp/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
}

View File

@ -1,30 +0,0 @@
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
}

View File

@ -8,7 +8,7 @@ func PKCS7UnPadding(encrypt []byte) []byte {
return encrypt[:(length - unPadding)] return encrypt[:(length - unPadding)]
} }
func DecryptAES128ECB(data, key []byte) []byte { func DecryptAes128Ecb(data, key []byte) []byte {
cipher, _ := aes.NewCipher(key) cipher, _ := aes.NewCipher(key)
decrypted := make([]byte, len(data)) decrypted := make([]byte, len(data))
size := 16 size := 16

View File

@ -1,24 +0,0 @@
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
}

View File

@ -1,32 +0,0 @@
#!/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