From b7f54054d868f4067a48dffa0a233f00252e3b8f Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Tue, 14 Nov 2023 17:36:07 -0800 Subject: [PATCH] correctly distinguish user/pass auth errors from other connectivity errors again --- .../mediaprovider/jellyfin/jellyfinmediaprovider.go | 11 +++++++++++ backend/mediaprovider/mediaprovider.go | 7 ++++++- backend/mediaprovider/subsonic/subsonicserver.go | 8 ++++++-- backend/servermanager.go | 12 ++++++++---- go.mod | 2 +- go.sum | 4 ++-- 6 files changed, 34 insertions(+), 10 deletions(-) diff --git a/backend/mediaprovider/jellyfin/jellyfinmediaprovider.go b/backend/mediaprovider/jellyfin/jellyfinmediaprovider.go index 912d260..4a5f03e 100644 --- a/backend/mediaprovider/jellyfin/jellyfinmediaprovider.go +++ b/backend/mediaprovider/jellyfin/jellyfinmediaprovider.go @@ -23,6 +23,17 @@ type JellyfinServer struct { jellyfin.Client } +func (j *JellyfinServer) Login(user, pass string) mediaprovider.LoginResponse { + if _, err := j.Ping(); err != nil { + return mediaprovider.LoginResponse{Error: err} + } + err := j.Client.Login(user, pass) + return mediaprovider.LoginResponse{ + Error: err, + IsAuthError: err != nil, + } +} + func (j *JellyfinServer) MediaProvider() mediaprovider.MediaProvider { return newJellyfinMediaProvider(&j.Client) } diff --git a/backend/mediaprovider/mediaprovider.go b/backend/mediaprovider/mediaprovider.go index 35b8e3b..be7bc8e 100644 --- a/backend/mediaprovider/mediaprovider.go +++ b/backend/mediaprovider/mediaprovider.go @@ -61,8 +61,13 @@ type Favorites struct { Tracks []*Track } +type LoginResponse struct { + Error error + IsAuthError bool +} + type Server interface { - Login(username, password string) error + Login(username, password string) LoginResponse MediaProvider() MediaProvider } diff --git a/backend/mediaprovider/subsonic/subsonicserver.go b/backend/mediaprovider/subsonic/subsonicserver.go index 5c1149f..b64fe07 100644 --- a/backend/mediaprovider/subsonic/subsonicserver.go +++ b/backend/mediaprovider/subsonic/subsonicserver.go @@ -9,9 +9,13 @@ type SubsonicServer struct { subsonicCli.Client } -func (s *SubsonicServer) Login(username, password string) error { +func (s *SubsonicServer) Login(username, password string) mediaprovider.LoginResponse { s.User = username - return s.Client.Authenticate(password) + err := s.Client.Authenticate(password) + return mediaprovider.LoginResponse{ + Error: err, + IsAuthError: err == subsonicCli.ErrAuthenticationFailure, + } } func (s *SubsonicServer) MediaProvider() mediaprovider.MediaProvider { diff --git a/backend/servermanager.go b/backend/servermanager.go index b974b55..265381e 100644 --- a/backend/servermanager.go +++ b/backend/servermanager.go @@ -199,12 +199,16 @@ func (s *ServerManager) connect(connection ServerConnection, password string) (m }, } } + var authError error pingChan := make(chan bool, 2) // false for primary hostname, true for alternate pingFunc := func(delay time.Duration, cli mediaprovider.Server, val bool) { <-time.After(delay) - if err := cli.Login(connection.Username, password); err == nil { - pingChan <- val + resp := cli.Login(connection.Username, password) + if resp.Error != nil && !resp.IsAuthError { + return } + authError = resp.Error + pingChan <- val // reached the server } go pingFunc(0, cli, false) if connection.AltHostname != "" { @@ -218,8 +222,8 @@ func (s *ServerManager) connect(connection ServerConnection, password string) (m return nil, ErrUnreachable case altPing := <-pingChan: if altPing { - return altCli, nil + return altCli, authError } - return cli, nil + return cli, authError } } diff --git a/go.mod b/go.mod index e02b3a6..dac7ff8 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/deluan/sanitize v0.0.0-20230310221930-6e18967d9fc1 github.com/dweymouth/go-jellyfin v0.0.0-20231114014232-1e011bb03a9f github.com/dweymouth/go-mpv v0.0.0-20230406003141-7f1858e503ee - github.com/dweymouth/go-subsonic v0.0.0-20231105161622-54b5aec28363 + github.com/dweymouth/go-subsonic v0.0.0-20231115012731-a3d1f90274c1 github.com/fsnotify/fsnotify v1.6.0 github.com/godbus/dbus/v5 v5.1.0 github.com/google/uuid v1.3.0 diff --git a/go.sum b/go.sum index 1d2fd35..374ca07 100644 --- a/go.sum +++ b/go.sum @@ -75,8 +75,8 @@ github.com/dweymouth/go-jellyfin v0.0.0-20231114014232-1e011bb03a9f h1:sJY4MjrNa github.com/dweymouth/go-jellyfin v0.0.0-20231114014232-1e011bb03a9f/go.mod h1:BMwS4vdjEYf1gmjPGSKCzWP/I6YlI6fkefJ9nsjBjaU= github.com/dweymouth/go-mpv v0.0.0-20230406003141-7f1858e503ee h1:ZGyJ6wp7CAfT31BugypcF/TPKEy2RrGR9JFq1JOjOpY= github.com/dweymouth/go-mpv v0.0.0-20230406003141-7f1858e503ee/go.mod h1:Ov0ieN90M7i+0k3OxhA/g1dozGs+UcPHDsMKqPgRDk0= -github.com/dweymouth/go-subsonic v0.0.0-20231105161622-54b5aec28363 h1:MIH7MAWWPPVRKEKxz+RJubn+ycyQPimHn1Zvoxs1KRI= -github.com/dweymouth/go-subsonic v0.0.0-20231105161622-54b5aec28363/go.mod h1:dVriurACA/XTnE7BgSOTapYOtxMq7jTOVExrIbwi84c= +github.com/dweymouth/go-subsonic v0.0.0-20231115012731-a3d1f90274c1 h1:1I5/hlV4lQ0B0NdBZvwWSqL8hvvKhRRbJK6fGsxFSLs= +github.com/dweymouth/go-subsonic v0.0.0-20231115012731-a3d1f90274c1/go.mod h1:OWtcumdQsan8uM6wmx6PqKhldaCthH10CQ+vb+94kzo= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=