Fixed: Ping plex.tv to keep auth token active

Closes #2981

(cherry picked from commit 93bd8543b158c952b50e56d4339e4a3c699770b0)
pull/3245/head
Mark McDowall 2 years ago committed by Qstick
parent fc4170d9dc
commit b606c68f80

@ -1,3 +1,4 @@
using System;
using System.Net; using System.Net;
using NLog; using NLog;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
@ -10,6 +11,7 @@ namespace NzbDrone.Core.Notifications.Plex.PlexTv
public interface IPlexTvProxy public interface IPlexTvProxy
{ {
string GetAuthToken(string clientIdentifier, int pinId); string GetAuthToken(string clientIdentifier, int pinId);
bool Ping(string clientIdentifier, string authToken);
} }
public class PlexTvProxy : IPlexTvProxy public class PlexTvProxy : IPlexTvProxy
@ -38,6 +40,29 @@ namespace NzbDrone.Core.Notifications.Plex.PlexTv
return response.AuthToken; 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) private HttpRequestBuilder BuildRequest(string clientIdentifier)
{ {
var requestBuilder = new HttpRequestBuilder("https://plex.tv") var requestBuilder = new HttpRequestBuilder("https://plex.tv")

@ -1,6 +1,8 @@
using System;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using NzbDrone.Common.Cache;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
@ -12,17 +14,20 @@ namespace NzbDrone.Core.Notifications.Plex.PlexTv
PlexTvPinUrlResponse GetPinUrl(); PlexTvPinUrlResponse GetPinUrl();
PlexTvSignInUrlResponse GetSignInUrl(string callbackUrl, int pinId, string pinCode); PlexTvSignInUrlResponse GetSignInUrl(string callbackUrl, int pinId, string pinCode);
string GetAuthToken(int pinId); string GetAuthToken(int pinId);
void Ping(string authToken);
} }
public class PlexTvService : IPlexTvService public class PlexTvService : IPlexTvService
{ {
private readonly IPlexTvProxy _proxy; private readonly IPlexTvProxy _proxy;
private readonly IConfigService _configService; private readonly IConfigService _configService;
private readonly ICached<bool> _cache;
public PlexTvService(IPlexTvProxy proxy, IConfigService configService) public PlexTvService(IPlexTvProxy proxy, IConfigService configService, ICacheManager cacheManager)
{ {
_proxy = proxy; _proxy = proxy;
_configService = configService; _configService = configService;
_cache = cacheManager.GetCache<bool>(GetType());
} }
public PlexTvPinUrlResponse GetPinUrl() public PlexTvPinUrlResponse GetPinUrl()
@ -81,5 +86,11 @@ namespace NzbDrone.Core.Notifications.Plex.PlexTv
return authToken; 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));
}
} }
} }

@ -55,6 +55,8 @@ namespace NzbDrone.Core.Notifications.Plex.Server
private void UpdateIfEnabled(Artist artist) private void UpdateIfEnabled(Artist artist)
{ {
_plexTvService.Ping(Settings.AuthToken);
if (Settings.UpdateLibrary) if (Settings.UpdateLibrary)
{ {
_logger.Debug("Scheduling library update for artist {0} {1}", artist.Id, artist.Name); _logger.Debug("Scheduling library update for artist {0} {1}", artist.Id, artist.Name);
@ -68,7 +70,8 @@ namespace NzbDrone.Core.Notifications.Plex.Server
public override void ProcessQueue() public override void ProcessQueue()
{ {
PlexUpdateQueue queue = _pendingArtistCache.Find(Settings.Host); var queue = _pendingArtistCache.Find(Settings.Host);
if (queue == null) if (queue == null)
{ {
return; return;
@ -121,6 +124,8 @@ namespace NzbDrone.Core.Notifications.Plex.Server
public override ValidationResult Test() public override ValidationResult Test()
{ {
_plexTvService.Ping(Settings.AuthToken);
var failures = new List<ValidationFailure>(); var failures = new List<ValidationFailure>();
failures.AddIfNotNull(_plexServerService.Test(Settings)); failures.AddIfNotNull(_plexServerService.Test(Settings));

Loading…
Cancel
Save