2020-04-21 21:37:37 +00:00
|
|
|
using System.Collections.Generic;
|
2019-01-13 19:22:24 +00:00
|
|
|
using System;
|
2020-04-17 11:47:00 +00:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
using System.Threading;
|
2019-01-13 19:22:24 +00:00
|
|
|
using System.Threading.Tasks;
|
2019-01-06 20:50:43 +00:00
|
|
|
using MediaBrowser.Controller.Net;
|
2013-05-09 17:38:02 +00:00
|
|
|
using MediaBrowser.Controller.Session;
|
2015-03-08 19:48:30 +00:00
|
|
|
using MediaBrowser.Model.Events;
|
2020-04-17 11:47:00 +00:00
|
|
|
using MediaBrowser.Model.Net;
|
2014-04-16 02:17:48 +00:00
|
|
|
using MediaBrowser.Model.Serialization;
|
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>
|
|
|
|
/// Class SessionWebSocketListener
|
|
|
|
/// </summary>
|
2015-03-08 19:48:30 +00:00
|
|
|
public 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>
|
|
|
|
public readonly int WebSocketLostTimeout = 60;
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-21 21:37:37 +00:00
|
|
|
/// The keep-alive timer factor; controls how often the timer will check on the status of the WebSockets.
|
2020-04-17 11:47:00 +00:00
|
|
|
/// </summary>
|
|
|
|
public readonly double TimerFactor = 0.2;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The ForceKeepAlive factor; controls when a ForceKeepAlive is sent.
|
|
|
|
/// </summary>
|
|
|
|
public readonly double ForceKeepAliveFactor = 0.75;
|
|
|
|
|
2013-05-09 17:38:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The _session manager
|
|
|
|
/// </summary>
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
|
|
|
/// <summary>
|
2013-05-10 12:18:07 +00:00
|
|
|
/// The _logger
|
|
|
|
/// </summary>
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
2013-09-04 17:02:19 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The _dto service
|
|
|
|
/// </summary>
|
2014-04-16 02:17:48 +00:00
|
|
|
private readonly IJsonSerializer _json;
|
2013-05-10 12:18:07 +00:00
|
|
|
|
2015-03-08 19:48:30 +00:00
|
|
|
private readonly IHttpServer _httpServer;
|
|
|
|
|
2020-04-17 11:47:00 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The KeepAlive timer.
|
|
|
|
/// </summary>
|
|
|
|
private Timer _keepAliveTimer;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The WebSocket watchlist.
|
|
|
|
/// </summary>
|
|
|
|
private readonly ConcurrentDictionary<IWebSocketConnection, byte> _webSockets = new ConcurrentDictionary<IWebSocketConnection, byte>();
|
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>
|
|
|
|
/// <param name="sessionManager">The session manager.</param>
|
2018-12-13 13:18:25 +00:00
|
|
|
/// <param name="loggerFactory">The logger factory.</param>
|
2014-05-18 19:58:42 +00:00
|
|
|
/// <param name="json">The json.</param>
|
2015-03-08 19:48:30 +00:00
|
|
|
/// <param name="httpServer">The HTTP server.</param>
|
2018-12-13 13:18:25 +00:00
|
|
|
public SessionWebSocketListener(ISessionManager sessionManager, ILoggerFactory loggerFactory, IJsonSerializer json, IHttpServer httpServer)
|
2013-05-09 17:38:02 +00:00
|
|
|
{
|
|
|
|
_sessionManager = sessionManager;
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger = loggerFactory.CreateLogger(GetType().Name);
|
2014-04-16 02:17:48 +00:00
|
|
|
_json = json;
|
2015-03-08 19:48:30 +00:00
|
|
|
_httpServer = httpServer;
|
2018-09-12 17:26:21 +00:00
|
|
|
httpServer.WebSocketConnected += _serverManager_WebSocketConnected;
|
2015-03-08 19:48:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
void _serverManager_WebSocketConnected(object sender, GenericEventArgs<IWebSocketConnection> e)
|
2015-03-08 19:48:30 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var session = GetSession(e.Argument.QueryString, e.Argument.RemoteEndPoint);
|
2015-03-08 19:48:30 +00:00
|
|
|
|
|
|
|
if (session != null)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
EnsureController(session, e.Argument);
|
2020-04-17 11:47:00 +00:00
|
|
|
KeepAliveWebSocket(e.Argument);
|
2015-03-08 19:48:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogWarning("Unable to determine session based on url: {0}", e.Argument.Url);
|
2015-03-08 19:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 13:23:39 +00:00
|
|
|
private SessionInfo GetSession(IQueryCollection queryString, string remoteEndpoint)
|
2015-03-08 19:48:30 +00:00
|
|
|
{
|
2015-03-16 16:47:14 +00:00
|
|
|
if (queryString == null)
|
|
|
|
{
|
2019-11-24 14:27:58 +00:00
|
|
|
return null;
|
2015-03-16 16:47:14 +00:00
|
|
|
}
|
|
|
|
|
2015-03-08 19:48:30 +00:00
|
|
|
var token = queryString["api_key"];
|
2015-03-10 01:47:52 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(token))
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return null;
|
2015-03-10 01:47:52 +00:00
|
|
|
}
|
2019-11-24 14:27:58 +00:00
|
|
|
|
2015-03-13 01:55:22 +00:00
|
|
|
var deviceId = queryString["deviceId"];
|
|
|
|
return _sessionManager.GetSessionByAuthenticationToken(token, deviceId, remoteEndpoint);
|
2015-03-08 19:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
_httpServer.WebSocketConnected -= _serverManager_WebSocketConnected;
|
2020-04-17 11:47:00 +00:00
|
|
|
StopKeepAliveTimer();
|
2013-05-09 17:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes the message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2019-02-24 02:16:19 +00:00
|
|
|
public Task ProcessMessageAsync(WebSocketMessageInfo message)
|
|
|
|
=> Task.CompletedTask;
|
2013-10-03 01:22:50 +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
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var controllerInfo = session.EnsureController<WebSocketController>(s => new WebSocketController(s, _logger, _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);
|
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>
|
2020-04-21 21:37:37 +00:00
|
|
|
private void OnWebSocketClosed(object sender, EventArgs e)
|
2020-04-17 11:47:00 +00:00
|
|
|
{
|
|
|
|
var webSocket = (IWebSocketConnection) sender;
|
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>
|
|
|
|
private async void KeepAliveWebSocket(IWebSocketConnection webSocket)
|
|
|
|
{
|
2020-04-21 21:37:37 +00:00
|
|
|
if (!_webSockets.TryAdd(webSocket, 0))
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Multiple attempts to keep alive single WebSocket {0}", webSocket);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
webSocket.Closed += OnWebSocketClosed;
|
2020-04-17 11:47:00 +00:00
|
|
|
webSocket.LastKeepAliveDate = DateTime.UtcNow;
|
|
|
|
|
|
|
|
// Notify WebSocket about timeout
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await SendForceKeepAlive(webSocket);
|
|
|
|
}
|
|
|
|
catch (WebSocketException exception)
|
|
|
|
{
|
2020-04-21 21:37:37 +00:00
|
|
|
_logger.LogWarning(exception, "Error sending ForceKeepAlive message to WebSocket.");
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StartKeepAliveTimer();
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
webSocket.Closed -= OnWebSocketClosed;
|
|
|
|
_webSockets.TryRemove(webSocket, out _);
|
|
|
|
}
|
|
|
|
|
2020-04-17 11:47:00 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Starts the KeepAlive timer.
|
|
|
|
/// </summary>
|
|
|
|
private void StartKeepAliveTimer()
|
|
|
|
{
|
|
|
|
if (_keepAliveTimer == null)
|
|
|
|
{
|
|
|
|
_keepAliveTimer = new Timer(
|
|
|
|
KeepAliveSockets,
|
|
|
|
null,
|
|
|
|
TimeSpan.FromSeconds(WebSocketLostTimeout * TimerFactor),
|
|
|
|
TimeSpan.FromSeconds(WebSocketLostTimeout * TimerFactor)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Stops the KeepAlive timer.
|
|
|
|
/// </summary>
|
|
|
|
private void StopKeepAliveTimer()
|
|
|
|
{
|
|
|
|
if (_keepAliveTimer != null)
|
|
|
|
{
|
|
|
|
_keepAliveTimer.Dispose();
|
|
|
|
_keepAliveTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var pair in _webSockets)
|
|
|
|
{
|
2020-04-21 21:37:37 +00:00
|
|
|
pair.Key.Closed -= OnWebSocketClosed;
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks status of KeepAlive of WebSockets.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
private async void KeepAliveSockets(object state)
|
|
|
|
{
|
|
|
|
var inactive = _webSockets.Keys.Where(i =>
|
|
|
|
{
|
|
|
|
var elapsed = (DateTime.UtcNow - i.LastKeepAliveDate).TotalSeconds;
|
|
|
|
return (elapsed > WebSocketLostTimeout * ForceKeepAliveFactor) && (elapsed < WebSocketLostTimeout);
|
|
|
|
});
|
|
|
|
var lost = _webSockets.Keys.Where(i => (DateTime.UtcNow - i.LastKeepAliveDate).TotalSeconds >= WebSocketLostTimeout);
|
|
|
|
|
|
|
|
if (inactive.Any())
|
|
|
|
{
|
2020-04-21 21:37:37 +00:00
|
|
|
_logger.LogDebug("Sending ForceKeepAlive message to {0} inactive WebSockets.", inactive.Count());
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var webSocket in inactive)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await SendForceKeepAlive(webSocket);
|
|
|
|
}
|
|
|
|
catch (WebSocketException exception)
|
|
|
|
{
|
2020-04-21 21:37:37 +00:00
|
|
|
_logger.LogInformation(exception, "Error sending ForceKeepAlive message to WebSocket.");
|
2020-04-17 11:47:00 +00:00
|
|
|
lost.Append(webSocket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lost.Any())
|
|
|
|
{
|
2020-04-21 21:37:37 +00:00
|
|
|
_logger.LogInformation("Lost {0} WebSockets.", lost.Count());
|
|
|
|
foreach (var webSocket in lost)
|
|
|
|
{
|
|
|
|
// TODO: handle session relative to the lost webSocket
|
|
|
|
RemoveWebSocket(webSocket);
|
|
|
|
}
|
2020-04-17 11:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_webSockets.Any())
|
|
|
|
{
|
|
|
|
StopKeepAliveTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
}
|
2013-05-09 17:38:02 +00:00
|
|
|
}
|
|
|
|
}
|