mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-10-16 22:07:30 +02:00
fix: database migration, config validation and provider client defects
Completes the correctness pass. Download database migration no longer destroys dedupe state The pre-source-column migration carried every legacy row over with source='' and then dropped downloads_legacy, while IsDownloaded matched source exactly. The result was that every previously recorded download looked new, the user silently re-downloaded their whole library, and the original rows were gone. Now the file is copied to <path>.bak-<unixtime> first, downloads_legacy is kept, and IsDownloaded falls back to the sourceless rows (over-matching across sources costs a skipped re-download, which is the cheaper error). Schema state is tracked in PRAGMA user_version so later migrations can be ordered explicitly. Databases already migrated by the old code are detected on open. Also enables WAL and a 10s busy_timeout and caps the pool at one connection, so a concurrent reader no longer gets SQLITE_BUSY with no retry, and builds the DSN through url.URL so a path containing '?' is not parsed as query parameters. --quality now applies to every source applyGlobalConfigOverrides set only qobuz, tidal, deezer and soundcloud, so `rip -q 4 url <beatport-url>` was a silent no-op even though qualityForSource reads Yandex and Beatport. All six are set, with the per-source floor applied and reported rather than silently ignored. config: validation and a fixed reset Load checked nothing but misc.version, so a negative max_connections or a bogus conversion codec was accepted and only surfaced mid-rip. Adds Validate() covering per-source quality ranges, max_connections, requests_per_minute and truncate_to, and normalizes the conversion codec from the file through the same path as --codec. `config reset` assigned one value to both Config.File and Config.Session, so their Secrets slices shared a backing array; it now takes separate copies. Tests cover the shipped defaults and config.toml.example, which is how the over-strict artwork max-width bound in the first draft was caught: -1 is the documented "do not downscale" sentinel. soundcloud: use the shared HTTP client It was the only provider constructing its own http.Client, so it silently ignored downloads.verify_ssl and max_connections and bypassed the transport that redacts credentials from -vv logs. It also had no rate limiter at all. qobuz: mobile fallback could not download a long track http.Client.Timeout bounds the whole request including the body read, so the 30s API client aborted any track that took longer than 30s to stream. The body copy now uses a no-timeout client, as internal/download already does. qobuz: HKDF no longer fails silently hexDecodeOrNil discarded its error, so a non-hex app secret keyed HKDF on nil, derived a wrong KEK and produced silently corrupt audio instead of an error. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
9 files changed
+570
-51
No files matched your search
@@ -59,11 +59,12 @@ var (
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
cfg *config.Config
|
||||
http *http.Client
|
||||
limiter *ratelimit.Limiter
|
||||
baseURL string
|
||||
fetchCfg func(ctx context.Context) (string, []string, error)
|
||||
cfg *config.Config
|
||||
http *http.Client
|
||||
streamHTTP *http.Client
|
||||
limiter *ratelimit.Limiter
|
||||
baseURL string
|
||||
fetchCfg func(ctx context.Context) (string, []string, error)
|
||||
// authMu guards loggedIn and the login/refresh path, which download
|
||||
// workers can reach concurrently.
|
||||
authMu sync.Mutex
|
||||
@@ -91,11 +92,16 @@ type mobileFileURL struct {
|
||||
|
||||
func New(cfg *config.Config) *Client {
|
||||
return &Client{
|
||||
cfg: cfg,
|
||||
http: netutil.NewHTTPClient(30*time.Second, cfg.Session.Downloads.VerifySSL, cfg.Session.Downloads.MaxConnections),
|
||||
limiter: ratelimit.New(cfg.Session.Downloads.RequestsPerMinute),
|
||||
baseURL: baseURL,
|
||||
fetchCfg: nil,
|
||||
cfg: cfg,
|
||||
http: netutil.NewHTTPClient(30*time.Second, cfg.Session.Downloads.VerifySSL, cfg.Session.Downloads.MaxConnections),
|
||||
// Separate client with no Timeout for streaming a whole track body.
|
||||
// http.Client.Timeout bounds the entire request including the body
|
||||
// read, so the 30s API client aborted any track that took longer than
|
||||
// 30s to stream. internal/download does the same for this reason.
|
||||
streamHTTP: netutil.NewHTTPClient(0, cfg.Session.Downloads.VerifySSL, cfg.Session.Downloads.MaxConnections),
|
||||
limiter: ratelimit.New(cfg.Session.Downloads.RequestsPerMinute),
|
||||
baseURL: baseURL,
|
||||
fetchCfg: nil,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1171,7 +1177,11 @@ func (c *Client) mobileCopyURLToWriter(ctx context.Context, sourceURL string, ou
|
||||
return err
|
||||
}
|
||||
c.setMobileHeaders(req)
|
||||
resp, err := c.http.Do(req)
|
||||
client := c.streamHTTP
|
||||
if client == nil {
|
||||
client = c.http
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1316,7 +1326,17 @@ func (c *Client) mobileDeriveTrackKey(encryptedKey string) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode mobile info: %w", err)
|
||||
}
|
||||
reader := hkdf.New(func() hash.Hash { return sha256.New() }, hexDecodeOrNil(mobileAppSecret), salt, info)
|
||||
// A non-hex secret used to be swallowed and HKDF keyed on nil, which
|
||||
// derives a wrong KEK and yields silently corrupt audio rather than an
|
||||
// error.
|
||||
secretKey, err := hex.DecodeString(mobileAppSecret)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode mobile app secret: %w", err)
|
||||
}
|
||||
if len(secretKey) == 0 {
|
||||
return nil, errors.New("mobile app secret is empty")
|
||||
}
|
||||
reader := hkdf.New(func() hash.Hash { return sha256.New() }, secretKey, salt, info)
|
||||
kek := make([]byte, 16)
|
||||
if _, err = io.ReadFull(reader, kek); err != nil {
|
||||
return nil, fmt.Errorf("mobile hkdf derive failed: %w", err)
|
||||
@@ -1325,11 +1345,6 @@ func (c *Client) mobileDeriveTrackKey(encryptedKey string) ([]byte, error) {
|
||||
return unwrapQobuzTrackKey(encryptedKey, kek)
|
||||
}
|
||||
|
||||
func hexDecodeOrNil(s string) []byte {
|
||||
b, _ := hex.DecodeString(s)
|
||||
return b
|
||||
}
|
||||
|
||||
func unwrapQobuzTrackKey(encryptedKey string, kek []byte) ([]byte, error) {
|
||||
parts := strings.SplitN(encryptedKey, ".", 3)
|
||||
if len(parts) != 3 || parts[0] != mobileSessionProf {
|
||||
|
||||
Reference in new issue
Block a user