The Free Software Media System - Server Backend & API
Use RequestHelpers.GetSession in SessionWebSocketListener (#14494)
Niels van Velzen48e93dcbceab
diff --git a/Emby.Server.Implementations/Session/SessionWebSocketListener.cs b/Emby.Server.Implementations/Session/SessionWebSocketListener.csindex d4606abd2b..6a26e92e14 100644--- a/Emby.Server.Implementations/Session/SessionWebSocketListener.cs+++ b/Emby.Server.Implementations/Session/SessionWebSocketListener.cs@@ -5,6 +5,8 @@ using System.Net.WebSockets;using System.Threading;using System.Threading.Tasks;using Jellyfin.Api.Extensions;+using Jellyfin.Api.Helpers;+using MediaBrowser.Controller.Library;using MediaBrowser.Controller.Net;using MediaBrowser.Controller.Net.WebSocketMessages.Outbound;using MediaBrowser.Controller.Session;@@ -44,6 +46,7 @@ namespace Emby.Server.Implementations.Sessionprivate readonly Lock _webSocketsLock = new();private readonly ISessionManager _sessionManager;+ private readonly IUserManager _userManager;private readonly ILogger<SessionWebSocketListener> _logger;private readonly ILoggerFactory _loggerFactory;@@ -57,14 +60,17 @@ namespace Emby.Server.Implementations.Session/// </summary>/// <param name="logger">The logger.</param>/// <param name="sessionManager">The session manager.</param>+ /// <param name="userManager">The user manager.</param>/// <param name="loggerFactory">The logger factory.</param>public SessionWebSocketListener(ILogger<SessionWebSocketListener> logger,ISessionManager sessionManager,+ IUserManager userManager,ILoggerFactory loggerFactory){_logger = logger;_sessionManager = sessionManager;+ _userManager = userManager;_loggerFactory = loggerFactory;_keepAlive = new System.Timers.Timer(TimeSpan.FromSeconds(WebSocketLostTimeout * IntervalFactor)){@@ -107,33 +113,9 @@ namespace Emby.Server.Implementations.Session/// <inheritdoc />public async Task ProcessWebSocketConnectedAsync(IWebSocketConnection connection, HttpContext httpContext){- var session = await GetSession(httpContext, connection.RemoteEndPoint?.ToString()).ConfigureAwait(false);- if (session is not null)- {- EnsureController(session, connection);- await KeepAliveWebSocket(connection).ConfigureAwait(false);- }- else- {- _logger.LogWarning("Unable to determine session based on query string: {0}", httpContext.Request.QueryString);- }- }-- private async Task<SessionInfo?> GetSession(HttpContext httpContext, string? remoteEndpoint)- {- if (!httpContext.User.Identity?.IsAuthenticated ?? false)- {- return null;- }-- var deviceId = httpContext.User.GetDeviceId();- if (httpContext.Request.Query.TryGetValue("deviceId", out var queryDeviceId))- {- deviceId = queryDeviceId;- }-- return await _sessionManager.GetSessionByAuthenticationToken(httpContext.User.GetToken(), deviceId, remoteEndpoint)- .ConfigureAwait(false);+ var session = await RequestHelpers.GetSession(_sessionManager, _userManager, httpContext).ConfigureAwait(false);+ EnsureController(session, connection);+ await KeepAliveWebSocket(connection).ConfigureAwait(false);}private void EnsureController(SessionInfo session, IWebSocketConnection connection)diff --git a/Jellyfin.Api/Helpers/RequestHelpers.cs b/Jellyfin.Api/Helpers/RequestHelpers.csindex e10e940f21..5072f902da 100644--- a/Jellyfin.Api/Helpers/RequestHelpers.cs+++ b/Jellyfin.Api/Helpers/RequestHelpers.cs@@ -111,7 +111,16 @@ public static class RequestHelpersreturn user.EnableUserPreferenceAccess;}- internal static async Task<SessionInfo> GetSession(ISessionManager sessionManager, IUserManager userManager, HttpContext httpContext, Guid? userId = null)+ /// <summary>+ /// Get the session based on http request.+ /// </summary>+ /// <param name="sessionManager">The session manager.</param>+ /// <param name="userManager">The user manager.</param>+ /// <param name="httpContext">The http context.</param>+ /// <param name="userId">The optional userid.</param>+ /// <returns>The session.</returns>+ /// <exception cref="ResourceNotFoundException">Session not found.</exception>+ public static async Task<SessionInfo> GetSession(ISessionManager sessionManager, IUserManager userManager, HttpContext httpContext, Guid? userId = null){userId ??= httpContext.User.GetUserId();User? user = null;