From 48e93dcbceab0e603adc4b2ca28a402d26180ac9 Mon Sep 17 00:00:00 2001 From: Niels van Velzen Date: Mon, 21 Jul 2025 04:00:47 +0200 Subject: [PATCH] Use RequestHelpers.GetSession in SessionWebSocketListener (#14494) --- .../Session/SessionWebSocketListener.cs | 36 +++++-------------- Jellyfin.Api/Helpers/RequestHelpers.cs | 11 +++++- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/Emby.Server.Implementations/Session/SessionWebSocketListener.cs b/Emby.Server.Implementations/Session/SessionWebSocketListener.cs index 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.Session private readonly Lock _webSocketsLock = new(); private readonly ISessionManager _sessionManager; + private readonly IUserManager _userManager; private readonly ILogger _logger; private readonly ILoggerFactory _loggerFactory; @@ -57,14 +60,17 @@ namespace Emby.Server.Implementations.Session /// /// The logger. /// The session manager. + /// The user manager. /// The logger factory. public SessionWebSocketListener( ILogger 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 /// 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 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.cs index e10e940f21..5072f902da 100644 --- a/Jellyfin.Api/Helpers/RequestHelpers.cs +++ b/Jellyfin.Api/Helpers/RequestHelpers.cs @@ -111,7 +111,16 @@ public static class RequestHelpers return user.EnableUserPreferenceAccess; } - internal static async Task GetSession(ISessionManager sessionManager, IUserManager userManager, HttpContext httpContext, Guid? userId = null) + /// + /// Get the session based on http request. + /// + /// The session manager. + /// The user manager. + /// The http context. + /// The optional userid. + /// The session. + /// Session not found. + public static async Task GetSession(ISessionManager sessionManager, IUserManager userManager, HttpContext httpContext, Guid? userId = null) { userId ??= httpContext.User.GetUserId(); User? user = null;