2019-01-13 14:22:24 -05:00
|
|
|
using System;
|
2020-04-28 08:12:06 -04:00
|
|
|
using System.Collections.Generic;
|
2020-04-17 07:47:00 -04:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
using System.Threading;
|
2019-01-13 14:22:24 -05:00
|
|
|
using System.Threading.Tasks;
|
2019-01-06 15:50:43 -05:00
|
|
|
using MediaBrowser.Controller.Net;
|
2013-05-09 13:38:02 -04:00
|
|
|
using MediaBrowser.Controller.Session;
|
2015-03-08 15:48:30 -04:00
|
|
|
using MediaBrowser.Model.Events;
|
2020-04-17 07:47:00 -04:00
|
|
|
using MediaBrowser.Model.Net;
|
2019-02-27 08:23:39 -05:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-01-13 14:22:24 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-05-09 13:38:02 -04:00
|
|
|
|
2016-11-03 19:35:19 -04:00
|
|
|
namespace Emby.Server.Implementations.Session
|
2013-05-09 13:38:02 -04:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class SessionWebSocketListener
|
|
|
|
/// </summary>
|
2019-12-17 17:15:02 -05:00
|
|
|
public sealed class SessionWebSocketListener : IWebSocketListener, IDisposable
|
2013-05-09 13:38:02 -04:00
|
|
|
{
|
2020-04-17 07:47:00 -04:00
|
|
|
/// <summary>
|
|
|
|
/// The timeout in seconds after which a WebSocket is considered to be lost.
|
|
|
|
/// </summary>
|
2020-05-09 08:34:07 -04:00
|
|
|
public const int WebSocketLostTimeout = 60;
|
2020-04-17 07:47:00 -04:00
|
|
|
|
|
|
|
/// <summary>
|
2020-04-28 08:12:06 -04:00
|
|
|
/// The keep-alive interval factor; controls how often the watcher will check on the status of the WebSockets.
|
2020-04-17 07:47:00 -04:00
|
|
|
/// </summary>
|
2020-05-09 08:34:07 -04:00
|
|
|
public const float IntervalFactor = 0.2f;
|
2020-04-17 07:47:00 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The ForceKeepAlive factor; controls when a ForceKeepAlive is sent.
|
|
|
|
/// </summary>
|
2020-05-09 08:34:07 -04:00
|
|
|
public const float ForceKeepAliveFactor = 0.75f;
|
2020-04-17 07:47:00 -04:00
|
|
|
|
2013-05-09 13:38:02 -04:00
|
|
|
/// <summary>
|
|
|
|
/// The _session manager
|
|
|
|
/// </summary>
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
|
|
|
/// <summary>
|
2013-05-10 08:18:07 -04:00
|
|
|
/// The _logger
|
|
|
|
/// </summary>
|
|
|
|
private readonly ILogger _logger;
|
2019-12-17 17:15:02 -05:00
|
|
|
private readonly ILoggerFactory _loggerFactory;
|
2013-05-10 08:18:07 -04:00
|
|
|
|
2015-03-08 15:48:30 -04:00
|
|
|
private readonly IHttpServer _httpServer;
|
|
|
|
|
2020-04-17 07:47:00 -04:00
|
|
|
/// <summary>
|
2020-04-28 08:12:06 -04:00
|
|
|
/// The KeepAlive cancellation token.
|
|
|
|
/// </summary>
|
|
|
|
private CancellationTokenSource _keepAliveCancellationToken;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Lock used for accesing the KeepAlive cancellation token.
|
2020-04-17 07:47:00 -04:00
|
|
|
/// </summary>
|
2020-04-28 08:12:06 -04:00
|
|
|
private readonly object _keepAliveLock = new object();
|
2020-04-17 07:47:00 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The WebSocket watchlist.
|
|
|
|
/// </summary>
|
2020-04-28 08:12:06 -04:00
|
|
|
private readonly HashSet<IWebSocketConnection> _webSockets = new HashSet<IWebSocketConnection>();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Lock used for accesing the WebSockets watchlist.
|
|
|
|
/// </summary>
|
|
|
|
private readonly object _webSocketsLock = new object();
|
2015-03-08 15:48:30 -04:00
|
|
|
|
2013-05-10 08:18:07 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="SessionWebSocketListener" /> class.
|
2013-05-09 13:38:02 -04:00
|
|
|
/// </summary>
|
2019-12-17 17:15:02 -05:00
|
|
|
/// <param name="logger">The logger.</param>
|
2013-05-09 13:38:02 -04:00
|
|
|
/// <param name="sessionManager">The session manager.</param>
|
2018-12-13 08:18:25 -05:00
|
|
|
/// <param name="loggerFactory">The logger factory.</param>
|
2015-03-08 15:48:30 -04:00
|
|
|
/// <param name="httpServer">The HTTP server.</param>
|
2019-12-17 17:15:02 -05:00
|
|
|
public SessionWebSocketListener(
|
|
|
|
ILogger<SessionWebSocketListener> logger,
|
|
|
|
ISessionManager sessionManager,
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
IHttpServer httpServer)
|
2013-05-09 13:38:02 -04:00
|
|
|
{
|
2019-12-17 17:15:02 -05:00
|
|
|
_logger = logger;
|
2013-05-09 13:38:02 -04:00
|
|
|
_sessionManager = sessionManager;
|
2019-12-17 17:15:02 -05:00
|
|
|
_loggerFactory = loggerFactory;
|
2015-03-08 15:48:30 -04:00
|
|
|
_httpServer = httpServer;
|
2019-12-17 17:15:02 -05:00
|
|
|
|
2020-05-04 13:46:02 -04:00
|
|
|
httpServer.WebSocketConnected += OnServerManagerWebSocketConnected;
|
2015-03-08 15:48:30 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 17:15:02 -05:00
|
|
|
private void OnServerManagerWebSocketConnected(object sender, GenericEventArgs<IWebSocketConnection> e)
|
2015-03-08 15:48:30 -04:00
|
|
|
{
|
2019-12-17 17:15:02 -05:00
|
|
|
var session = GetSession(e.Argument.QueryString, e.Argument.RemoteEndPoint.ToString());
|
2015-03-08 15:48:30 -04:00
|
|
|
if (session != null)
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
EnsureController(session, e.Argument);
|
2020-04-17 07:47:00 -04:00
|
|
|
KeepAliveWebSocket(e.Argument);
|
2015-03-08 15:48:30 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-26 14:57:46 -05:00
|
|
|
_logger.LogWarning("Unable to determine session based on query string: {0}", e.Argument.QueryString);
|
2015-03-08 15:48:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 08:23:39 -05:00
|
|
|
private SessionInfo GetSession(IQueryCollection queryString, string remoteEndpoint)
|
2015-03-08 15:48:30 -04:00
|
|
|
{
|
2015-03-16 12:47:14 -04:00
|
|
|
if (queryString == null)
|
|
|
|
{
|
2019-11-24 09:27:58 -05:00
|
|
|
return null;
|
2015-03-16 12:47:14 -04:00
|
|
|
}
|
|
|
|
|
2015-03-08 15:48:30 -04:00
|
|
|
var token = queryString["api_key"];
|
2015-03-09 21:47:52 -04:00
|
|
|
if (string.IsNullOrWhiteSpace(token))
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
return null;
|
2015-03-09 21:47:52 -04:00
|
|
|
}
|
2019-11-24 09:27:58 -05:00
|
|
|
|
2015-03-12 21:55:22 -04:00
|
|
|
var deviceId = queryString["deviceId"];
|
|
|
|
return _sessionManager.GetSessionByAuthenticationToken(token, deviceId, remoteEndpoint);
|
2015-03-08 15:48:30 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 17:15:02 -05:00
|
|
|
/// <inheritdoc />
|
2015-03-08 15:48:30 -04:00
|
|
|
public void Dispose()
|
|
|
|
{
|
2020-05-04 13:46:02 -04:00
|
|
|
_httpServer.WebSocketConnected -= OnServerManagerWebSocketConnected;
|
2020-04-28 08:12:06 -04:00
|
|
|
StopKeepAlive();
|
2013-05-09 13:38:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes the message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2019-02-23 21:16:19 -05:00
|
|
|
public Task ProcessMessageAsync(WebSocketMessageInfo message)
|
|
|
|
=> Task.CompletedTask;
|
2013-10-02 21:22:50 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private void EnsureController(SessionInfo session, IWebSocketConnection connection)
|
2014-04-15 22:17:48 -04:00
|
|
|
{
|
2019-12-17 17:15:02 -05:00
|
|
|
var controllerInfo = session.EnsureController<WebSocketController>(
|
|
|
|
s => new WebSocketController(_loggerFactory.CreateLogger<WebSocketController>(), s, _sessionManager));
|
2014-04-15 22:17:48 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
var controller = (WebSocketController)controllerInfo.Item1;
|
|
|
|
controller.AddWebSocket(connection);
|
2013-10-02 21:22:50 -04:00
|
|
|
}
|
2020-04-17 07:47:00 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when a WebSocket is closed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">The WebSocket.</param>
|
|
|
|
/// <param name="e">The event arguments.</param>
|
2020-04-21 17:37:37 -04:00
|
|
|
private void OnWebSocketClosed(object sender, EventArgs e)
|
2020-04-17 07:47:00 -04:00
|
|
|
{
|
|
|
|
var webSocket = (IWebSocketConnection) sender;
|
2020-05-04 13:46:02 -04:00
|
|
|
_logger.LogDebug("WebSocket {0} is closed.", webSocket);
|
2020-04-21 17:37:37 -04:00
|
|
|
RemoveWebSocket(webSocket);
|
2020-04-17 07:47:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a WebSocket to the KeepAlive watchlist.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="webSocket">The WebSocket to monitor.</param>
|
2020-05-04 13:46:02 -04:00
|
|
|
private void KeepAliveWebSocket(IWebSocketConnection webSocket)
|
2020-04-17 07:47:00 -04:00
|
|
|
{
|
2020-04-28 08:12:06 -04:00
|
|
|
lock (_webSocketsLock)
|
2020-04-21 17:37:37 -04:00
|
|
|
{
|
2020-04-28 08:12:06 -04:00
|
|
|
if (!_webSockets.Add(webSocket))
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Multiple attempts to keep alive single WebSocket {0}", webSocket);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
webSocket.Closed += OnWebSocketClosed;
|
|
|
|
webSocket.LastKeepAliveDate = DateTime.UtcNow;
|
|
|
|
|
|
|
|
StartKeepAlive();
|
2020-04-21 17:37:37 -04:00
|
|
|
}
|
2020-04-17 07:47:00 -04:00
|
|
|
|
|
|
|
// Notify WebSocket about timeout
|
|
|
|
try
|
|
|
|
{
|
2020-05-04 13:46:02 -04:00
|
|
|
SendForceKeepAlive(webSocket).Wait();
|
2020-04-17 07:47:00 -04:00
|
|
|
}
|
|
|
|
catch (WebSocketException exception)
|
|
|
|
{
|
2020-05-04 13:46:02 -04:00
|
|
|
_logger.LogWarning(exception, "Cannot send ForceKeepAlive message to WebSocket {0}.", webSocket);
|
2020-04-17 07:47:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:37:37 -04: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 08:12:06 -04:00
|
|
|
lock (_webSocketsLock)
|
|
|
|
{
|
|
|
|
if (!_webSockets.Remove(webSocket))
|
|
|
|
{
|
|
|
|
_logger.LogWarning("WebSocket {0} not on watchlist.", webSocket);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
webSocket.Closed -= OnWebSocketClosed;
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 17:37:37 -04:00
|
|
|
}
|
|
|
|
|
2020-04-17 07:47:00 -04:00
|
|
|
/// <summary>
|
2020-04-28 08:12:06 -04:00
|
|
|
/// Starts the KeepAlive watcher.
|
2020-04-17 07:47:00 -04:00
|
|
|
/// </summary>
|
2020-04-28 08:12:06 -04:00
|
|
|
private void StartKeepAlive()
|
2020-04-17 07:47:00 -04:00
|
|
|
{
|
2020-04-28 08:12:06 -04:00
|
|
|
lock (_keepAliveLock)
|
2020-04-17 07:47:00 -04:00
|
|
|
{
|
2020-04-28 08:12:06 -04:00
|
|
|
if (_keepAliveCancellationToken == null)
|
|
|
|
{
|
|
|
|
_keepAliveCancellationToken = new CancellationTokenSource();
|
|
|
|
// Start KeepAlive watcher
|
2020-05-09 08:34:07 -04:00
|
|
|
_ = RepeatAsyncCallbackEvery(
|
2020-05-04 13:46:02 -04:00
|
|
|
KeepAliveSockets,
|
2020-04-28 08:12:06 -04:00
|
|
|
TimeSpan.FromSeconds(WebSocketLostTimeout * IntervalFactor),
|
|
|
|
_keepAliveCancellationToken.Token);
|
|
|
|
}
|
2020-04-17 07:47:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-28 08:12:06 -04:00
|
|
|
/// Stops the KeepAlive watcher.
|
2020-04-17 07:47:00 -04:00
|
|
|
/// </summary>
|
2020-04-28 08:12:06 -04:00
|
|
|
private void StopKeepAlive()
|
2020-04-17 07:47:00 -04:00
|
|
|
{
|
2020-04-28 08:12:06 -04:00
|
|
|
lock (_keepAliveLock)
|
2020-04-17 07:47:00 -04:00
|
|
|
{
|
2020-04-28 08:12:06 -04:00
|
|
|
if (_keepAliveCancellationToken != null)
|
|
|
|
{
|
|
|
|
_keepAliveCancellationToken.Cancel();
|
|
|
|
_keepAliveCancellationToken = null;
|
|
|
|
}
|
2020-04-17 07:47:00 -04:00
|
|
|
}
|
|
|
|
|
2020-04-28 08:12:06 -04:00
|
|
|
lock (_webSocketsLock)
|
2020-04-17 07:47:00 -04:00
|
|
|
{
|
2020-04-28 08:12:06 -04:00
|
|
|
foreach (var webSocket in _webSockets)
|
|
|
|
{
|
|
|
|
webSocket.Closed -= OnWebSocketClosed;
|
|
|
|
}
|
2020-05-09 08:34:07 -04:00
|
|
|
|
2020-04-28 08:12:06 -04:00
|
|
|
_webSockets.Clear();
|
2020-04-17 07:47:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-05-04 13:46:02 -04:00
|
|
|
/// Checks status of KeepAlive of WebSockets.
|
2020-04-17 07:47:00 -04:00
|
|
|
/// </summary>
|
2020-05-04 13:46:02 -04:00
|
|
|
private async Task KeepAliveSockets()
|
2020-04-17 07:47:00 -04:00
|
|
|
{
|
2020-05-09 08:34:07 -04:00
|
|
|
List<IWebSocketConnection> inactive;
|
|
|
|
List<IWebSocketConnection> lost;
|
2020-05-04 13:46:02 -04:00
|
|
|
|
|
|
|
lock (_webSocketsLock)
|
2020-04-17 07:47:00 -04:00
|
|
|
{
|
2020-05-04 13:46:02 -04:00
|
|
|
_logger.LogDebug("Watching {0} WebSockets.", _webSockets.Count);
|
2020-04-17 07:47:00 -04:00
|
|
|
|
2020-05-04 13:46:02 -04:00
|
|
|
inactive = _webSockets.Where(i =>
|
2020-04-28 08:12:06 -04:00
|
|
|
{
|
2020-05-04 13:46:02 -04:00
|
|
|
var elapsed = (DateTime.UtcNow - i.LastKeepAliveDate).TotalSeconds;
|
|
|
|
return (elapsed > WebSocketLostTimeout * ForceKeepAliveFactor) && (elapsed < WebSocketLostTimeout);
|
2020-05-09 08:34:07 -04:00
|
|
|
}).ToList();
|
|
|
|
lost = _webSockets.Where(i => (DateTime.UtcNow - i.LastKeepAliveDate).TotalSeconds >= WebSocketLostTimeout).ToList();
|
2020-05-04 13:46:02 -04:00
|
|
|
}
|
2020-04-17 07:47:00 -04:00
|
|
|
|
2020-05-04 13:46:02 -04:00
|
|
|
if (inactive.Any())
|
|
|
|
{
|
|
|
|
_logger.LogInformation("Sending ForceKeepAlive message to {0} inactive WebSockets.", inactive.Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var webSocket in inactive)
|
|
|
|
{
|
|
|
|
try
|
2020-04-17 07:47:00 -04:00
|
|
|
{
|
2020-05-04 13:46:02 -04:00
|
|
|
await SendForceKeepAlive(webSocket);
|
2020-04-17 07:47:00 -04:00
|
|
|
}
|
2020-05-04 13:46:02 -04:00
|
|
|
catch (WebSocketException exception)
|
2020-04-17 07:47:00 -04:00
|
|
|
{
|
2020-05-04 13:46:02 -04:00
|
|
|
_logger.LogInformation(exception, "Error sending ForceKeepAlive message to WebSocket.");
|
2020-05-09 08:34:07 -04:00
|
|
|
lost.Add(webSocket);
|
2020-04-17 07:47:00 -04:00
|
|
|
}
|
2020-05-04 13:46:02 -04:00
|
|
|
}
|
2020-04-17 07:47:00 -04:00
|
|
|
|
2020-05-04 13:46:02 -04:00
|
|
|
lock (_webSocketsLock)
|
|
|
|
{
|
|
|
|
if (lost.Any())
|
2020-04-21 17:37:37 -04:00
|
|
|
{
|
2020-05-04 13:46:02 -04:00
|
|
|
_logger.LogInformation("Lost {0} WebSockets.", lost.Count());
|
2020-05-09 08:34:07 -04:00
|
|
|
foreach (var webSocket in lost)
|
2020-04-28 08:12:06 -04:00
|
|
|
{
|
2020-05-04 13:46:02 -04:00
|
|
|
// TODO: handle session relative to the lost webSocket
|
|
|
|
RemoveWebSocket(webSocket);
|
2020-04-28 08:12:06 -04:00
|
|
|
}
|
2020-04-21 17:37:37 -04:00
|
|
|
}
|
2020-04-17 07:47:00 -04:00
|
|
|
|
2020-05-04 13:46:02 -04:00
|
|
|
if (!_webSockets.Any())
|
2020-04-28 08:12:06 -04:00
|
|
|
{
|
2020-05-04 13:46:02 -04:00
|
|
|
StopKeepAlive();
|
2020-04-28 08:12:06 -04:00
|
|
|
}
|
2020-04-17 07:47:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sends a ForceKeepAlive message to a WebSocket.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="webSocket">The WebSocket.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
private Task SendForceKeepAlive(IWebSocketConnection webSocket)
|
|
|
|
{
|
|
|
|
return webSocket.SendAsync(new WebSocketMessage<int>
|
|
|
|
{
|
|
|
|
MessageType = "ForceKeepAlive",
|
|
|
|
Data = WebSocketLostTimeout
|
|
|
|
}, CancellationToken.None);
|
|
|
|
}
|
2020-05-04 13:46:02 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Runs a given async callback once every specified interval time, until cancelled.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="callback">The async callback.</param>
|
|
|
|
/// <param name="interval">The interval time.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
private async Task RepeatAsyncCallbackEvery(Func<Task> callback, TimeSpan interval, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
await callback();
|
|
|
|
Task task = Task.Delay(interval, cancellationToken);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await task;
|
|
|
|
}
|
|
|
|
catch (TaskCanceledException)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-09 13:38:02 -04:00
|
|
|
}
|
|
|
|
}
|