From 93bd8543b158c952b50e56d4339e4a3c699770b0 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 17 Sep 2022 17:43:10 -0700 Subject: [PATCH] Fixed: Ping plex.tv to keep auth token active Closes #5154 --- .../Notifications/Plex/PlexTv/PlexTvProxy.cs | 26 +++++++++++++++++++ .../Plex/PlexTv/PlexTvService.cs | 16 ++++++++++-- .../Notifications/Plex/Server/PlexServer.cs | 7 ++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/NzbDrone.Core/Notifications/Plex/PlexTv/PlexTvProxy.cs b/src/NzbDrone.Core/Notifications/Plex/PlexTv/PlexTvProxy.cs index c4fafe006..dab64798d 100644 --- a/src/NzbDrone.Core/Notifications/Plex/PlexTv/PlexTvProxy.cs +++ b/src/NzbDrone.Core/Notifications/Plex/PlexTv/PlexTvProxy.cs @@ -1,3 +1,4 @@ +using System; using System.Net; using NLog; using NzbDrone.Common.EnvironmentInfo; @@ -10,6 +11,7 @@ namespace NzbDrone.Core.Notifications.Plex.PlexTv public interface IPlexTvProxy { string GetAuthToken(string clientIdentifier, int pinId); + bool Ping(string clientIdentifier, string authToken); } public class PlexTvProxy : IPlexTvProxy @@ -38,6 +40,30 @@ namespace NzbDrone.Core.Notifications.Plex.PlexTv return response.AuthToken; } + public bool Ping(string clientIdentifier, string authToken) + { + try + { + // Allows us to tell plex.tv that we're still active and tokens should not be expired. + + var request = BuildRequest(clientIdentifier); + + request.ResourceUrl = "/api/v2/ping"; + request.AddQueryParam("X-Plex-Token", authToken); + + ProcessRequest(request); + + return true; + } + catch (Exception e) + { + // Catch all exceptions and log at trace, this information could be interesting in debugging, but expired tokens will be handled elsewhere. + _logger.Trace(e, "Unable to ping plex.tv"); + } + + return false; + } + private HttpRequestBuilder BuildRequest(string clientIdentifier) { var requestBuilder = new HttpRequestBuilder("https://plex.tv") diff --git a/src/NzbDrone.Core/Notifications/Plex/PlexTv/PlexTvService.cs b/src/NzbDrone.Core/Notifications/Plex/PlexTv/PlexTvService.cs index ba12fee74..280c73bd2 100644 --- a/src/NzbDrone.Core/Notifications/Plex/PlexTv/PlexTvService.cs +++ b/src/NzbDrone.Core/Notifications/Plex/PlexTv/PlexTvService.cs @@ -1,6 +1,8 @@ +using System; using System.Linq; using System.Net.Http; using System.Text; +using NzbDrone.Common.Cache; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Http; using NzbDrone.Core.Configuration; @@ -12,7 +14,7 @@ namespace NzbDrone.Core.Notifications.Plex.PlexTv PlexTvPinUrlResponse GetPinUrl(); PlexTvSignInUrlResponse GetSignInUrl(string callbackUrl, int pinId, string pinCode); string GetAuthToken(int pinId); - + void Ping(string authToken); HttpRequest GetWatchlist(string authToken); } @@ -20,11 +22,13 @@ namespace NzbDrone.Core.Notifications.Plex.PlexTv { private readonly IPlexTvProxy _proxy; private readonly IConfigService _configService; + private readonly ICached _cache; - public PlexTvService(IPlexTvProxy proxy, IConfigService configService) + public PlexTvService(IPlexTvProxy proxy, IConfigService configService, ICacheManager cacheManager) { _proxy = proxy; _configService = configService; + _cache = cacheManager.GetCache(GetType()); } public PlexTvPinUrlResponse GetPinUrl() @@ -84,8 +88,16 @@ namespace NzbDrone.Core.Notifications.Plex.PlexTv return authToken; } + public void Ping(string authToken) + { + // Ping plex.tv if we haven't done so in the last 24 hours for this auth token. + _cache.Get(authToken, () => _proxy.Ping(_configService.PlexClientIdentifier, authToken), TimeSpan.FromHours(24)); + } + public HttpRequest GetWatchlist(string authToken) { + Ping(authToken); + var clientIdentifier = _configService.PlexClientIdentifier; var requestBuilder = new HttpRequestBuilder("https://metadata.provider.plex.tv/library/sections/watchlist/all") diff --git a/src/NzbDrone.Core/Notifications/Plex/Server/PlexServer.cs b/src/NzbDrone.Core/Notifications/Plex/Server/PlexServer.cs index ba1caa0a4..0c74646a5 100644 --- a/src/NzbDrone.Core/Notifications/Plex/Server/PlexServer.cs +++ b/src/NzbDrone.Core/Notifications/Plex/Server/PlexServer.cs @@ -64,6 +64,8 @@ namespace NzbDrone.Core.Notifications.Plex.Server private void UpdateIfEnabled(Series series) { + _plexTvService.Ping(Settings.AuthToken); + if (Settings.UpdateLibrary) { _logger.Debug("Scheduling library update for series {0} {1}", series.Id, series.Title); @@ -77,7 +79,8 @@ namespace NzbDrone.Core.Notifications.Plex.Server public override void ProcessQueue() { - PlexUpdateQueue queue = _pendingSeriesCache.Find(Settings.Host); + var queue = _pendingSeriesCache.Find(Settings.Host); + if (queue == null) { return; @@ -130,6 +133,8 @@ namespace NzbDrone.Core.Notifications.Plex.Server public override ValidationResult Test() { + _plexTvService.Ping(Settings.AuthToken); + var failures = new List(); failures.AddIfNotNull(_plexServerService.Test(Settings));