2019-01-13 19:22:24 +00:00
|
|
|
using System;
|
2020-04-28 12:12:06 +00:00
|
|
|
using System.Collections.Generic;
|
2020-04-17 11:47:00 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
using System.Threading;
|
2019-01-13 19:22:24 +00:00
|
|
|
using System.Threading.Tasks;
|
2022-10-06 11:57:47 +00:00
|
|
|
using Jellyfin.Api.Extensions;
|
2019-01-06 20:50:43 +00:00
|
|
|
using MediaBrowser.Controller.Net;
|
2023-07-02 22:14:44 +00:00
|
|
|
using MediaBrowser.Controller.Net.WebSocketMessages.Outbound;
|
2013-05-09 17:38:02 +00:00
|
|
|
using MediaBrowser.Controller.Session;
|
2019-02-27 13:23:39 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-01-13 19:22:24 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-05-09 17:38:02 +00:00
|
|
|
|
2016-11-03 23:35:19 +00:00
|
|
|
namespace Emby.Server.Implementations.Session
|
2013-05-09 17:38:02 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class SessionWebSocketListener.
|
2013-05-09 17:38:02 +00:00
|
|
|
/// </summary>
|
2020-11-28 08:50:16 +00:00
|
|
|
public sealed class SessionWebSocketListener : IWebSocketListener, IDisposable
|
2013-05-09 17:38:02 +00:00
|
|
|
{
|
2020-04-17 11:47:00 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The timeout in seconds after which a WebSocket is considered to be lost.
|
|
|
|
/// </summary>
|
2020-11-28 10:21:53 +00:00
|
|
|
private const int WebSocketLostTimeout = 60;
|
2020-04-17 11:47:00 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-04-28 12:12:06 +00:00
|
|
|
/// The keep-alive interval factor; controls how often the watcher will check on the status of the WebSockets.
|
2020-04-17 11:47:00 +00:00
|
|
|
/// </summary>
|
2020-11-28 10:21:53 +00:00
|
|
|
private const float IntervalFactor = 0.2f;
|
2020-04-17 11:47:00 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The ForceKeepAlive factor; controls when a ForceKeepAlive is sent.
|
|
|
|
/// </summary>
|
2020-11-28 10:21:53 +00:00
|
|
|
private const float ForceKeepAliveFactor = 0.75f;
|
2020-04-17 11:47:00 +00:00
|
|
|
|
2013-05-09 17:38:02 +00:00
|
|
|
/// <summary>
|
2020-11-28 10:21:53 +00:00
|
|
|
/// The WebSocket watchlist.
|
2013-05-10 12:18:07 +00:00
|
|
|
/// </summary>
|
2020-11-28 10:21:53 +00:00
|
|
|
private readonly HashSet<IWebSocketConnection> _webSockets = new HashSet<IWebSocketConnection>();
|
2020-11-28 08:50:16 +00:00
|
|
|
|
2020-04-17 11:47:00 +00:00
|
|
|
/// <summary>
|
2020-11-28 10:21:53 +00:00
|
|
|
/// Lock used for accessing the WebSockets watchlist.
|
2020-04-28 12:12:06 +00:00
|
|
|
/// </summary>
|
2020-11-28 10:21:53 +00:00
|
|
|
private readonly object _webSocketsLock = new object();
|
2020-04-28 12:12:06 +00:00
|
|
|
|
2020-11-28 10:21:53 +00:00
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
private readonly ILogger<SessionWebSocketListener> _logger;
|
|
|
|
private readonly ILoggerFactory _loggerFactory;
|
2020-04-28 12:12:06 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-11-28 10:21:53 +00:00
|
|
|
/// The KeepAlive cancellation token.
|
2020-04-28 12:12:06 +00:00
|
|
|
/// </summary>
|
2024-04-17 16:44:39 +00:00
|
|
|
private System.Timers.Timer _keepAlive;
|
2015-03-08 19:48:30 +00:00
|
|
|
|
2013-05-10 12:18:07 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="SessionWebSocketListener" /> class.
|
2013-05-09 17:38:02 +00:00
|
|
|
/// </summary>
|
2019-12-17 22:15:02 +00:00
|
|
|
/// <param name="logger">The logger.</param>
|
2013-05-09 17:38:02 +00:00
|
|
|
/// <param name="sessionManager">The session manager.</param>
|
2018-12-13 13:18:25 +00:00
|
|
|
/// <param name="loggerFactory">The logger factory.</param>
|
2019-12-17 22:15:02 +00:00
|
|
|
public SessionWebSocketListener(
|
|
|
|
ILogger<SessionWebSocketListener> logger,
|
|
|
|
ISessionManager sessionManager,
|
2022-10-06 11:57:47 +00:00
|
|
|
ILoggerFactory loggerFactory)
|
2013-05-09 17:38:02 +00:00
|
|
|
{
|
2019-12-17 22:15:02 +00:00
|
|
|
_logger = logger;
|
2013-05-09 17:38:02 +00:00
|
|
|
_sessionManager = sessionManager;
|
2019-12-17 22:15:02 +00:00
|
|
|
_loggerFactory = loggerFactory;
|
2024-04-17 16:44:39 +00:00
|
|
|
_keepAlive = new System.Timers.Timer(TimeSpan.FromSeconds(WebSocketLostTimeout * IntervalFactor))
|
|
|
|
{
|
|
|
|
AutoReset = true,
|
|
|
|
Enabled = false
|
|
|
|
};
|
|
|
|
_keepAlive.Elapsed += KeepAliveSockets;
|
2020-11-28 10:21:53 +00:00
|
|
|
}
|
2020-11-28 08:50:16 +00:00
|
|
|
|
2020-11-28 10:21:53 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2024-04-17 16:44:39 +00:00
|
|
|
if (_keepAlive is not null)
|
|
|
|
{
|
|
|
|
_keepAlive.Stop();
|
|
|
|
_keepAlive.Elapsed -= KeepAliveSockets;
|
|
|
|
_keepAlive.Dispose();
|
|
|
|
_keepAlive = null!;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_webSocketsLock)
|
|
|
|
{
|
|
|
|
foreach (var webSocket in _webSockets)
|
|
|
|
{
|
|
|
|
webSocket.Closed -= OnWebSocketClosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
_webSockets.Clear();
|
|
|
|
}
|
2015-03-08 19:48:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 10:21:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Processes the message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
public Task ProcessMessageAsync(WebSocketMessageInfo message)
|
|
|
|
=> Task.CompletedTask;
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2022-01-01 16:59:25 +00:00
|
|
|
public async Task ProcessWebSocketConnectedAsync(IWebSocketConnection connection, HttpContext httpContext)
|
2015-03-08 19:48:30 +00:00
|
|
|
{
|
2022-01-01 16:59:25 +00:00
|
|
|
var session = await GetSession(httpContext, connection.RemoteEndPoint?.ToString()).ConfigureAwait(false);
|
2022-12-05 14:01:13 +00:00
|
|
|
if (session is not null)
|
2015-03-08 19:48:30 +00:00
|
|
|
{
|
2020-11-28 10:21:53 +00:00
|
|
|
EnsureController(session, connection);
|
|
|
|
await KeepAliveWebSocket(connection).ConfigureAwait(false);
|
2015-03-08 19:48:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-01 16:59:25 +00:00
|
|
|
_logger.LogWarning("Unable to determine session based on query string: {0}", httpContext.Request.QueryString);
|
2015-03-08 19:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 22:41:28 +00:00
|
|
|
private async Task<SessionInfo?> GetSession(HttpContext httpContext, string? remoteEndpoint)
|
2015-03-08 19:48:30 +00:00
|
|
|
{
|
2022-10-06 11:57:47 +00:00
|
|
|
if (!httpContext.User.Identity?.IsAuthenticated ?? false)
|
2015-03-16 16:47:14 +00:00
|
|
|
{
|
2019-11-24 14:27:58 +00:00
|
|
|
return null;
|
2015-03-16 16:47:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 11:57:47 +00:00
|
|
|
var deviceId = httpContext.User.GetDeviceId();
|
2022-01-01 16:59:25 +00:00
|
|
|
if (httpContext.Request.Query.TryGetValue("deviceId", out var queryDeviceId))
|
2015-03-10 01:47:52 +00:00
|
|
|
{
|
2022-01-01 16:59:25 +00:00
|
|
|
deviceId = queryDeviceId;
|
2015-03-10 01:47:52 +00:00
|
|
|
}
|
2019-11-24 14:27:58 +00:00
|
|
|
|
2022-10-06 11:57:47 +00:00
|
|
|
return await _sessionManager.GetSessionByAuthenticationToken(httpContext.User.GetToken(), deviceId, remoteEndpoint)
|
2022-01-01 16:59:25 +00:00
|
|
|
.ConfigureAwait(false);
|
2015-03-08 19:48:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private void EnsureController(SessionInfo session, IWebSocketConnection connection)
|
2014-04-16 02:17:48 +00:00
|
|
|
{
|
2019-12-17 22:15:02 +00:00
|
|
|
var controllerInfo = session.EnsureController<WebSocketController>(
|
|
|
|
s => new WebSocketController(_loggerFactory.CreateLogger<WebSocketController>(), s, _sessionManager));
|
2014-04-16 02:17:48 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var controller = (WebSocketController)controllerInfo.Item1;
|
|
|
|
controller.AddWebSocket(connection);
|
2020-12-07 00:04:48 +00:00
|
|
|
|
|
|
|
_sessionManager.OnSessionControllerConnected(session);
|
2013-10-03 01:22:50 +00:00
|
|
|
}
|
2020-04-17 11:47:00 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when a WebSocket is closed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">The WebSocket.</param>
|
|
|
|
/// <param name="e">The event arguments.</param>
|
2023-02-15 22:41:28 +00:00
|
|
|
private void OnWebSocketClosed(object? sender, EventArgs e)
|
2020-04-17 11:47:00 +00:00
|
|
|
{
|
2023-02-15 22:41:28 +00:00
|
|
|
if (sender is null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:37:52 +00:00
|
|
|
var webSocket = (IWebSocketConnection)sender;
|
2020-05-04 17:46:02 +00:00
|
|
|
_logger.LogDebug("WebSocket {0} is closed.", webSocket);
|
2020-04-21 21:37:37 +00:00
|
|
|
RemoveWebSocket(webSocket);
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a WebSocket to the KeepAlive watchlist.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="webSocket">The WebSocket to monitor.</param>
|
2020-05-26 09:37:52 +00:00
|
|
|
private async Task KeepAliveWebSocket(IWebSocketConnection webSocket)
|
2020-04-17 11:47:00 +00:00
|
|
|
{
|
2020-04-28 12:12:06 +00:00
|
|
|
lock (_webSocketsLock)
|
2020-04-21 21:37:37 +00:00
|
|
|
{
|
2020-04-28 12:12:06 +00:00
|
|
|
if (!_webSockets.Add(webSocket))
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Multiple attempts to keep alive single WebSocket {0}", webSocket);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2020-04-28 12:12:06 +00:00
|
|
|
webSocket.Closed += OnWebSocketClosed;
|
|
|
|
webSocket.LastKeepAliveDate = DateTime.UtcNow;
|
|
|
|
|
2024-04-17 16:44:39 +00:00
|
|
|
_keepAlive.Start();
|
2020-04-21 21:37:37 +00:00
|
|
|
}
|
2020-04-17 11:47:00 +00:00
|
|
|
|
|
|
|
// Notify WebSocket about timeout
|
|
|
|
try
|
|
|
|
{
|
2020-07-24 14:37:54 +00:00
|
|
|
await SendForceKeepAlive(webSocket).ConfigureAwait(false);
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
|
|
|
catch (WebSocketException exception)
|
|
|
|
{
|
2020-05-04 17:46:02 +00:00
|
|
|
_logger.LogWarning(exception, "Cannot send ForceKeepAlive message to WebSocket {0}.", webSocket);
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 21:37:37 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Removes a WebSocket from the KeepAlive watchlist.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="webSocket">The WebSocket to remove.</param>
|
|
|
|
private void RemoveWebSocket(IWebSocketConnection webSocket)
|
|
|
|
{
|
2020-04-28 12:12:06 +00:00
|
|
|
lock (_webSocketsLock)
|
|
|
|
{
|
2024-04-17 16:44:39 +00:00
|
|
|
if (_webSockets.Remove(webSocket))
|
2020-04-28 12:12:06 +00:00
|
|
|
{
|
|
|
|
webSocket.Closed -= OnWebSocketClosed;
|
|
|
|
}
|
2024-04-17 16:44:39 +00:00
|
|
|
else
|
2020-04-28 12:12:06 +00:00
|
|
|
{
|
2024-04-17 16:44:39 +00:00
|
|
|
_logger.LogWarning("WebSocket {0} not on watchlist.", webSocket);
|
2020-04-28 12:12:06 +00:00
|
|
|
}
|
2020-04-17 11:47:00 +00:00
|
|
|
|
2024-04-17 16:44:39 +00:00
|
|
|
if (_webSockets.Count == 0)
|
2020-04-28 12:12:06 +00:00
|
|
|
{
|
2024-04-17 16:44:39 +00:00
|
|
|
_keepAlive.Stop();
|
2020-04-28 12:12:06 +00:00
|
|
|
}
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-05-04 17:46:02 +00:00
|
|
|
/// Checks status of KeepAlive of WebSockets.
|
2020-04-17 11:47:00 +00:00
|
|
|
/// </summary>
|
2024-04-17 16:44:39 +00:00
|
|
|
private async void KeepAliveSockets(object? o, EventArgs? e)
|
2020-04-17 11:47:00 +00:00
|
|
|
{
|
2020-05-09 12:34:07 +00:00
|
|
|
List<IWebSocketConnection> inactive;
|
|
|
|
List<IWebSocketConnection> lost;
|
2020-05-04 17:46:02 +00:00
|
|
|
|
|
|
|
lock (_webSocketsLock)
|
2020-04-17 11:47:00 +00:00
|
|
|
{
|
2020-05-04 17:46:02 +00:00
|
|
|
_logger.LogDebug("Watching {0} WebSockets.", _webSockets.Count);
|
2020-04-17 11:47:00 +00:00
|
|
|
|
2020-05-04 17:46:02 +00:00
|
|
|
inactive = _webSockets.Where(i =>
|
2020-04-28 12:12:06 +00:00
|
|
|
{
|
2020-05-04 17:46:02 +00:00
|
|
|
var elapsed = (DateTime.UtcNow - i.LastKeepAliveDate).TotalSeconds;
|
|
|
|
return (elapsed > WebSocketLostTimeout * ForceKeepAliveFactor) && (elapsed < WebSocketLostTimeout);
|
2020-05-09 12:34:07 +00:00
|
|
|
}).ToList();
|
|
|
|
lost = _webSockets.Where(i => (DateTime.UtcNow - i.LastKeepAliveDate).TotalSeconds >= WebSocketLostTimeout).ToList();
|
2020-05-04 17:46:02 +00:00
|
|
|
}
|
2020-04-17 11:47:00 +00:00
|
|
|
|
2020-07-24 14:37:54 +00:00
|
|
|
if (inactive.Count > 0)
|
2020-05-04 17:46:02 +00:00
|
|
|
{
|
2020-05-15 18:06:41 +00:00
|
|
|
_logger.LogInformation("Sending ForceKeepAlive message to {0} inactive WebSockets.", inactive.Count);
|
2020-05-04 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var webSocket in inactive)
|
|
|
|
{
|
|
|
|
try
|
2020-04-17 11:47:00 +00:00
|
|
|
{
|
2020-07-24 14:37:54 +00:00
|
|
|
await SendForceKeepAlive(webSocket).ConfigureAwait(false);
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
2020-05-04 17:46:02 +00:00
|
|
|
catch (WebSocketException exception)
|
2020-04-17 11:47:00 +00:00
|
|
|
{
|
2020-05-04 17:46:02 +00:00
|
|
|
_logger.LogInformation(exception, "Error sending ForceKeepAlive message to WebSocket.");
|
2020-05-09 12:34:07 +00:00
|
|
|
lost.Add(webSocket);
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
2020-05-04 17:46:02 +00:00
|
|
|
}
|
2020-04-17 11:47:00 +00:00
|
|
|
|
2020-05-04 17:46:02 +00:00
|
|
|
lock (_webSocketsLock)
|
|
|
|
{
|
2020-07-24 14:37:54 +00:00
|
|
|
if (lost.Count > 0)
|
2020-04-21 21:37:37 +00:00
|
|
|
{
|
2020-05-15 18:06:41 +00:00
|
|
|
_logger.LogInformation("Lost {0} WebSockets.", lost.Count);
|
2020-05-09 12:34:07 +00:00
|
|
|
foreach (var webSocket in lost)
|
2020-04-28 12:12:06 +00:00
|
|
|
{
|
2020-05-04 17:46:02 +00:00
|
|
|
// TODO: handle session relative to the lost webSocket
|
|
|
|
RemoveWebSocket(webSocket);
|
2020-04-28 12:12:06 +00:00
|
|
|
}
|
2020-04-21 21:37:37 +00:00
|
|
|
}
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sends a ForceKeepAlive message to a WebSocket.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="webSocket">The WebSocket.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
private Task SendForceKeepAlive(IWebSocketConnection webSocket)
|
|
|
|
{
|
2020-07-24 14:37:54 +00:00
|
|
|
return webSocket.SendAsync(
|
2023-07-02 22:14:44 +00:00
|
|
|
new ForceKeepAliveMessage(WebSocketLostTimeout),
|
2020-07-24 14:37:54 +00:00
|
|
|
CancellationToken.None);
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
2013-05-09 17:38:02 +00:00
|
|
|
}
|
|
|
|
}
|