mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-10-16 22:07:30 +02:00
fix: resource leaks, cancellation, races and path traversal
Six correctness bugs that the existing tests could not see. The suite passed and `go test -race` was clean throughout, because nothing exercised concurrent token refresh, shared-folder artwork, or hostile metadata. main(): extract run() int main() was a single 1196-line function that registered 20 `defer mainApp.Close()` handlers and then called os.Exit 150 times, so none of them ever ran. Close() is what closes the SQLite handle, flushes the progress bars and removes the artwork temp dirs, so every failed rip leaked temp directories. main() is now a wrapper around run() int and the command bodies return exit codes. Ctrl-C now stops a rip ctx was threaded everywhere but never checked: zero ctx.Err()/ctx.Done() in internal/app and internal/download. Cancellation left Rip() iterating the whole remaining queue, and the retry ladder retried on a dead context, so one Ctrl-C cost three doomed attempts per remaining track. Adds checks to the dispatch loops, the worker-spawn loops and the retry ladder, and routes the MarkFailed/MarkDownloaded bookkeeping through a detached context so the records survive cancellation. Convert and TagFLAC now take a ctx and use exec.CommandContext, so a stalled ffmpeg/ffprobe no longer outlives Ctrl-C. Config race Provider token refresh mutated the shared *config.Config and called SaveFile() from inside download workers with no lock, racing the struct fields and issuing concurrent os.WriteFile against one path. Adds a mutex to config.Config plus UpdateTokens(), and routes all five refresh sites through it. Each provider client gets an authMu guarding loggedIn and its login/refresh path; beatport needed it most, since ensureToken is reached from apiRequest. Artwork cover race artwork.Prepare was check-then-write on a path shared by every worker in a playlist rip. Two workers both saw a missing cover.jpg, both created it, and the loser's cleanup deleted the winner's finished file. Adds a per-folder lock and downloads via a unique temp file + rename. The regression test fails against the old code with 15 of 16 workers getting no cover. Path traversal from remote metadata naming.CleanName stripped <>:"/\|?* and control chars but not "." , so a title of ".." survived into filepath.Join and escaped the download root. SoundCloud and Tidal playlist names are user-generated. Now rejects "." and "..", trims trailing dots, handles Windows device names, adds DEL to the invalid set and caps components at 255 bytes without splitting a rune. app.go gains safeJoin as a second line of defence. Credentials no longer world-readable The config holds plaintext provider passwords, Deezer ARL cookies and OAuth tokens and was written 0o644. Now 0o600, with 0o700 directories, and an existing 0o644 file is tightened on write. Also: deletes the 9 duplicated *-rip-smoke dev commands (~300 lines fully subsumed by `rip id`) and the unreachable second dev-help arm; registers image/png so PNG covers are downscaled instead of being silently saved as cover.jpg containing PNG bytes; adds the missing .gitignore, which was keeping a 21MB build artifact one `git add -A` away from the repo. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
19 files changed
+915
-562
No files matched your search
@@ -11,6 +11,7 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"streamrip-go/internal/config"
|
||||
@@ -18,6 +19,7 @@ import (
|
||||
"streamrip-go/internal/netutil"
|
||||
"streamrip-go/internal/provider"
|
||||
"streamrip-go/internal/ratelimit"
|
||||
"streamrip-go/internal/verbose"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -38,8 +40,13 @@ type Client struct {
|
||||
http *http.Client
|
||||
limiter *ratelimit.Limiter
|
||||
baseURL string
|
||||
loggedIn bool
|
||||
sessionID string
|
||||
|
||||
// authMu serializes the token refresh / re-login path and guards loggedIn.
|
||||
// ensureToken is reached from apiRequest, which concurrent download workers
|
||||
// call, so without this several workers would refresh at once.
|
||||
authMu sync.Mutex
|
||||
loggedIn bool
|
||||
}
|
||||
|
||||
func New(cfg *config.Config) *Client {
|
||||
@@ -55,9 +62,15 @@ func New(cfg *config.Config) *Client {
|
||||
|
||||
func (c *Client) Source() string { return "beatport" }
|
||||
|
||||
func (c *Client) LoggedIn() bool { return c.loggedIn }
|
||||
func (c *Client) LoggedIn() bool {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
return c.loggedIn
|
||||
}
|
||||
|
||||
func (c *Client) Login(ctx context.Context) error {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
b := &c.cfg.Session.Beatport
|
||||
b.Username = strings.TrimSpace(b.Username)
|
||||
b.Password = strings.TrimSpace(b.Password)
|
||||
@@ -82,7 +95,7 @@ func (c *Client) Login(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errNotLoggedIn
|
||||
}
|
||||
switch mediaType {
|
||||
@@ -129,7 +142,7 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
|
||||
}
|
||||
|
||||
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errNotLoggedIn
|
||||
}
|
||||
if limit <= 0 {
|
||||
@@ -162,7 +175,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
||||
}
|
||||
|
||||
func (c *Client) GetDownloadable(ctx context.Context, item string, quality int) (*provider.Downloadable, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errNotLoggedIn
|
||||
}
|
||||
q := beatportQuality(quality)
|
||||
@@ -296,11 +309,8 @@ func (c *Client) apiRequest(ctx context.Context, method, endpoint string, params
|
||||
return nil, err
|
||||
}
|
||||
if status == http.StatusUnauthorized {
|
||||
c.cfg.Session.Beatport.TokenExpiry = 0
|
||||
if err = c.refresh(ctx); err != nil {
|
||||
if loginErr := c.loginPasswordFlow(ctx); loginErr != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.reauthorize(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, status, err = c.rawRequest(ctx, method, endpoint, params, payload, contentType, true)
|
||||
if err != nil {
|
||||
@@ -314,6 +324,13 @@ func (c *Client) apiRequest(ctx context.Context, method, endpoint string, params
|
||||
}
|
||||
|
||||
func (c *Client) ensureToken(ctx context.Context) error {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
return c.ensureTokenLocked(ctx)
|
||||
}
|
||||
|
||||
// ensureTokenLocked requires c.authMu to be held.
|
||||
func (c *Client) ensureTokenLocked(ctx context.Context) error {
|
||||
if strings.TrimSpace(c.cfg.Session.Beatport.AccessToken) != "" && time.Now().Unix()+300 < c.cfg.Session.Beatport.TokenExpiry {
|
||||
return nil
|
||||
}
|
||||
@@ -325,6 +342,21 @@ func (c *Client) ensureToken(ctx context.Context) error {
|
||||
return c.loginPasswordFlow(ctx)
|
||||
}
|
||||
|
||||
// reauthorize forces a token refresh after a 401. Holding authMu means that
|
||||
// when several workers get a 401 at once only the first re-authenticates and
|
||||
// the rest reuse the resulting token.
|
||||
func (c *Client) reauthorize(ctx context.Context) error {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
c.cfg.Session.Beatport.TokenExpiry = 0
|
||||
if err := c.refresh(ctx); err != nil {
|
||||
if loginErr := c.loginPasswordFlow(ctx); loginErr != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) loginPasswordFlow(ctx context.Context) error {
|
||||
b := &c.cfg.Session.Beatport
|
||||
if strings.TrimSpace(b.Username) == "" || strings.TrimSpace(b.Password) == "" {
|
||||
@@ -385,15 +417,19 @@ func (c *Client) issueToken(ctx context.Context, payload map[string]string) erro
|
||||
if expiresIn <= 0 {
|
||||
expiresIn = 3600
|
||||
}
|
||||
c.cfg.Session.Beatport.AccessToken = access
|
||||
if refresh != "" {
|
||||
c.cfg.Session.Beatport.RefreshToken = refresh
|
||||
expiry := time.Now().Unix() + expiresIn
|
||||
if err := c.cfg.UpdateTokens(func(file, session *config.ConfigData) {
|
||||
session.Beatport.AccessToken = access
|
||||
if refresh != "" {
|
||||
session.Beatport.RefreshToken = refresh
|
||||
}
|
||||
session.Beatport.TokenExpiry = expiry
|
||||
file.Beatport.AccessToken = session.Beatport.AccessToken
|
||||
file.Beatport.RefreshToken = session.Beatport.RefreshToken
|
||||
file.Beatport.TokenExpiry = session.Beatport.TokenExpiry
|
||||
}); err != nil {
|
||||
verbose.Printf(verbose.V, "beatport: could not persist refreshed token: %v\n", err)
|
||||
}
|
||||
c.cfg.Session.Beatport.TokenExpiry = time.Now().Unix() + expiresIn
|
||||
c.cfg.File.Beatport.AccessToken = c.cfg.Session.Beatport.AccessToken
|
||||
c.cfg.File.Beatport.RefreshToken = c.cfg.Session.Beatport.RefreshToken
|
||||
c.cfg.File.Beatport.TokenExpiry = c.cfg.Session.Beatport.TokenExpiry
|
||||
_ = c.cfg.SaveFile()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"streamrip-go/internal/config"
|
||||
@@ -22,6 +23,7 @@ import (
|
||||
"streamrip-go/internal/netutil"
|
||||
"streamrip-go/internal/provider"
|
||||
"streamrip-go/internal/ratelimit"
|
||||
"streamrip-go/internal/verbose"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -47,9 +49,12 @@ var (
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
cfg *config.Config
|
||||
http *http.Client
|
||||
limiter *ratelimit.Limiter
|
||||
cfg *config.Config
|
||||
http *http.Client
|
||||
limiter *ratelimit.Limiter
|
||||
// authMu guards loggedIn and the login/refresh path, which download
|
||||
// workers can reach concurrently.
|
||||
authMu sync.Mutex
|
||||
loggedIn bool
|
||||
ua string
|
||||
deviceID string
|
||||
@@ -82,6 +87,8 @@ func (c *Client) Source() string {
|
||||
}
|
||||
|
||||
func (c *Client) Login(ctx context.Context) error {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
c.arl = strings.TrimSpace(c.cfg.Session.Deezer.ARL)
|
||||
c.sid = ""
|
||||
c.jwt = ""
|
||||
@@ -114,6 +121,8 @@ func (c *Client) ensureLaunchSession(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (c *Client) LoggedIn() bool {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
return c.loggedIn
|
||||
}
|
||||
|
||||
@@ -122,7 +131,7 @@ func (c *Client) Close() error {
|
||||
}
|
||||
|
||||
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("deezer client not logged in")
|
||||
}
|
||||
if limit <= 0 {
|
||||
@@ -151,7 +160,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
||||
}
|
||||
|
||||
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("deezer client not logged in")
|
||||
}
|
||||
|
||||
@@ -471,10 +480,11 @@ func (c *Client) persistRefreshToken() {
|
||||
if rt == "" {
|
||||
return
|
||||
}
|
||||
c.cfg.Session.Deezer.RefreshToken = rt
|
||||
c.cfg.File.Deezer.RefreshToken = rt
|
||||
if strings.TrimSpace(c.cfg.Path) != "" {
|
||||
_ = c.cfg.SaveFile()
|
||||
if err := c.cfg.UpdateTokens(func(file, session *config.ConfigData) {
|
||||
file.Deezer.RefreshToken = rt
|
||||
session.Deezer.RefreshToken = rt
|
||||
}); err != nil {
|
||||
verbose.Printf(verbose.V, "deezer: could not persist refreshed token: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ import (
|
||||
"streamrip-go/internal/netutil"
|
||||
"streamrip-go/internal/provider"
|
||||
"streamrip-go/internal/ratelimit"
|
||||
"streamrip-go/internal/verbose"
|
||||
|
||||
"github.com/vbauerster/mpb/v8"
|
||||
"github.com/vbauerster/mpb/v8/decor"
|
||||
@@ -63,6 +64,9 @@ type Client struct {
|
||||
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
|
||||
loggedIn bool
|
||||
secret string
|
||||
uat string
|
||||
@@ -100,10 +104,14 @@ func (c *Client) Source() string {
|
||||
}
|
||||
|
||||
func (c *Client) LoggedIn() bool {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
return c.loggedIn
|
||||
}
|
||||
|
||||
func (c *Client) Login(ctx context.Context) error {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
q := &c.cfg.Session.Qobuz
|
||||
q.EmailOrUserID = strings.TrimSpace(q.EmailOrUserID)
|
||||
q.PasswordOrToken = strings.TrimSpace(q.PasswordOrToken)
|
||||
@@ -203,14 +211,18 @@ func (c *Client) refreshAppCredentials(ctx context.Context, q *config.QobuzConfi
|
||||
return errors.New("qobuz app credential refresh returned no secrets")
|
||||
}
|
||||
q.Secrets = append([]string(nil), clean...)
|
||||
c.cfg.File.Qobuz.AppID = q.AppID
|
||||
c.cfg.File.Qobuz.Secrets = append([]string(nil), clean...)
|
||||
_ = c.cfg.SaveFile()
|
||||
refreshedAppID := q.AppID
|
||||
if err := c.cfg.UpdateTokens(func(file, session *config.ConfigData) {
|
||||
file.Qobuz.AppID = refreshedAppID
|
||||
file.Qobuz.Secrets = append([]string(nil), clean...)
|
||||
}); err != nil {
|
||||
verbose.Printf(verbose.V, "qobuz: could not persist refreshed credentials: %v\n", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errNotLoggedIn
|
||||
}
|
||||
if mediaType == "playlist" {
|
||||
@@ -260,7 +272,7 @@ func (c *Client) GetTrackMetadata(ctx context.Context, id string) (*TrackMetadat
|
||||
}
|
||||
|
||||
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errNotLoggedIn
|
||||
}
|
||||
if limit <= 0 {
|
||||
@@ -283,7 +295,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
||||
}
|
||||
|
||||
func (c *Client) GetDownloadable(ctx context.Context, item string, quality int) (*provider.Downloadable, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errNotLoggedIn
|
||||
}
|
||||
if quality < 1 || quality > 4 {
|
||||
|
||||
@@ -26,13 +26,17 @@ var soundcloudSearchBaseURL = "https://soundcloud.com"
|
||||
type commandRunner func(ctx context.Context, name string, args ...string) ([]byte, error)
|
||||
|
||||
type Client struct {
|
||||
cfg *config.Config
|
||||
cfg *config.Config
|
||||
bin string
|
||||
run commandRunner
|
||||
http *http.Client
|
||||
mu sync.Mutex
|
||||
cache map[string]map[string]any
|
||||
|
||||
// authMu guards loggedIn. Kept separate from mu, which guards cache, so a
|
||||
// cache operation can never block a login check.
|
||||
authMu sync.Mutex
|
||||
loggedIn bool
|
||||
bin string
|
||||
run commandRunner
|
||||
http *http.Client
|
||||
mu sync.Mutex
|
||||
cache map[string]map[string]any
|
||||
}
|
||||
|
||||
func New(cfg *config.Config) *Client {
|
||||
@@ -50,6 +54,8 @@ func (c *Client) Source() string {
|
||||
}
|
||||
|
||||
func (c *Client) LoggedIn() bool {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
return c.loggedIn
|
||||
}
|
||||
|
||||
@@ -57,12 +63,14 @@ func (c *Client) Login(context.Context) error {
|
||||
if _, err := exec.LookPath(c.bin); err != nil {
|
||||
return fmt.Errorf("yt-dlp is required for soundcloud downloads/search. install it and ensure it is in $PATH (e.g. pipx install yt-dlp): %w", err)
|
||||
}
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
c.loggedIn = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("soundcloud client not logged in")
|
||||
}
|
||||
if limit <= 0 {
|
||||
@@ -201,7 +209,7 @@ func (c *Client) searchPlaylists(ctx context.Context, query string, limit int) (
|
||||
}
|
||||
|
||||
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("soundcloud client not logged in")
|
||||
}
|
||||
|
||||
@@ -273,7 +281,7 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
|
||||
}
|
||||
|
||||
func (c *Client) GetDownloadable(ctx context.Context, item string, _ int) (*provider.Downloadable, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("soundcloud client not logged in")
|
||||
}
|
||||
info, err := c.trackInfo(ctx, item)
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"streamrip-go/internal/config"
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
"streamrip-go/internal/netutil"
|
||||
"streamrip-go/internal/provider"
|
||||
"streamrip-go/internal/ratelimit"
|
||||
"streamrip-go/internal/verbose"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -58,7 +60,10 @@ type Client struct {
|
||||
baseURL string
|
||||
lyricsAPI string
|
||||
openAPI string
|
||||
loggedIn bool
|
||||
// authMu guards loggedIn and the login/refresh path, which download
|
||||
// workers can reach concurrently.
|
||||
authMu sync.Mutex
|
||||
loggedIn bool
|
||||
}
|
||||
|
||||
func New(cfg *config.Config) *Client {
|
||||
@@ -77,10 +82,14 @@ func (c *Client) Source() string {
|
||||
}
|
||||
|
||||
func (c *Client) LoggedIn() bool {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
return c.loggedIn
|
||||
}
|
||||
|
||||
func (c *Client) Login(ctx context.Context) error {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
if strings.TrimSpace(c.cfg.Session.Tidal.AccessToken) == "" {
|
||||
return ErrMissingTidalToken
|
||||
}
|
||||
@@ -161,22 +170,25 @@ func (c *Client) refreshAccessToken(ctx context.Context) error {
|
||||
expiresIn = 7 * 24 * 3600
|
||||
}
|
||||
|
||||
c.cfg.Session.Tidal.AccessToken = newToken
|
||||
c.cfg.File.Tidal.AccessToken = newToken
|
||||
if newRefresh != "" {
|
||||
c.cfg.Session.Tidal.RefreshToken = newRefresh
|
||||
c.cfg.File.Tidal.RefreshToken = newRefresh
|
||||
}
|
||||
expiry := time.Now().Unix() + expiresIn
|
||||
c.cfg.Session.Tidal.TokenExpiry = expiry
|
||||
c.cfg.File.Tidal.TokenExpiry = expiry
|
||||
_ = c.cfg.SaveFile()
|
||||
if err := c.cfg.UpdateTokens(func(file, session *config.ConfigData) {
|
||||
file.Tidal.AccessToken = newToken
|
||||
session.Tidal.AccessToken = newToken
|
||||
if newRefresh != "" {
|
||||
file.Tidal.RefreshToken = newRefresh
|
||||
session.Tidal.RefreshToken = newRefresh
|
||||
}
|
||||
file.Tidal.TokenExpiry = expiry
|
||||
session.Tidal.TokenExpiry = expiry
|
||||
}); err != nil {
|
||||
verbose.Printf(verbose.V, "tidal: could not persist refreshed token: %v\n", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("tidal client not logged in")
|
||||
}
|
||||
|
||||
@@ -257,7 +269,7 @@ func (c *Client) fetchTrackLyrics(ctx context.Context, trackID string) (string,
|
||||
}
|
||||
|
||||
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("tidal client not logged in")
|
||||
}
|
||||
if limit <= 0 {
|
||||
@@ -284,7 +296,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
||||
}
|
||||
|
||||
func (c *Client) GetDownloadable(ctx context.Context, trackID string, quality int) (*provider.Downloadable, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("tidal client not logged in")
|
||||
}
|
||||
if quality < 0 || quality > 4 {
|
||||
@@ -601,7 +613,7 @@ func formatsForQuality(quality int, preferAtmos bool) []string {
|
||||
}
|
||||
|
||||
func (c *Client) GetVideoDownloadable(ctx context.Context, videoID string) (*provider.Downloadable, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("tidal client not logged in")
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"streamrip-go/internal/config"
|
||||
@@ -25,6 +26,7 @@ import (
|
||||
"streamrip-go/internal/netutil"
|
||||
"streamrip-go/internal/provider"
|
||||
"streamrip-go/internal/ratelimit"
|
||||
"streamrip-go/internal/verbose"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -40,10 +42,13 @@ const (
|
||||
var ErrMissingYandexToken = errors.New("missing yandex access_token")
|
||||
|
||||
type Client struct {
|
||||
cfg *config.Config
|
||||
http *http.Client
|
||||
limiter *ratelimit.Limiter
|
||||
baseURL string
|
||||
cfg *config.Config
|
||||
http *http.Client
|
||||
limiter *ratelimit.Limiter
|
||||
baseURL string
|
||||
// authMu guards loggedIn and the login/refresh path, which download
|
||||
// workers can reach concurrently.
|
||||
authMu sync.Mutex
|
||||
loggedIn bool
|
||||
userID string
|
||||
}
|
||||
@@ -63,10 +68,14 @@ func (c *Client) Source() string {
|
||||
}
|
||||
|
||||
func (c *Client) LoggedIn() bool {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
return c.loggedIn
|
||||
}
|
||||
|
||||
func (c *Client) Login(ctx context.Context) error {
|
||||
c.authMu.Lock()
|
||||
defer c.authMu.Unlock()
|
||||
if strings.TrimSpace(c.cfg.Session.Yandex.AccessToken) == "" {
|
||||
return ErrMissingYandexToken
|
||||
}
|
||||
@@ -80,16 +89,19 @@ func (c *Client) Login(ctx context.Context) error {
|
||||
result, _ := resp["result"].(map[string]any)
|
||||
if uid := strings.TrimSpace(jsonutil.StringFromAny(result["uid"])); uid != "" {
|
||||
c.userID = uid
|
||||
c.cfg.Session.Yandex.UserID = uid
|
||||
c.cfg.File.Yandex.UserID = uid
|
||||
_ = c.cfg.SaveFile()
|
||||
if err := c.cfg.UpdateTokens(func(file, session *config.ConfigData) {
|
||||
file.Yandex.UserID = uid
|
||||
session.Yandex.UserID = uid
|
||||
}); err != nil {
|
||||
verbose.Printf(verbose.V, "yandex: could not persist user id: %v\n", err)
|
||||
}
|
||||
}
|
||||
c.loggedIn = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("yandex client not logged in")
|
||||
}
|
||||
switch mediaType {
|
||||
@@ -107,7 +119,7 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
|
||||
}
|
||||
|
||||
func (c *Client) Search(ctx context.Context, mediaType, query string, limit int) ([]map[string]any, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("yandex client not logged in")
|
||||
}
|
||||
if limit <= 0 {
|
||||
@@ -137,7 +149,7 @@ func (c *Client) Search(ctx context.Context, mediaType, query string, limit int)
|
||||
}
|
||||
|
||||
func (c *Client) GetDownloadable(ctx context.Context, item string, quality int) (*provider.Downloadable, error) {
|
||||
if !c.loggedIn {
|
||||
if !c.LoggedIn() {
|
||||
return nil, errors.New("yandex client not logged in")
|
||||
}
|
||||
trackID, _ := splitTrackRef(item)
|
||||
|
||||
Reference in new issue
Block a user