2015-03-08 15:06:15 -04: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;
|
2013-05-10 08:18:07 -04:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-04-15 22:17:48 -04:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using MediaBrowser.Model.Session;
|
2013-05-09 13:38:02 -04:00
|
|
|
|
using System;
|
2015-03-08 15:48:30 -04:00
|
|
|
|
using System.Collections.Specialized;
|
2014-04-15 22:17:48 -04:00
|
|
|
|
using System.Globalization;
|
2013-05-09 13:45:46 -04:00
|
|
|
|
using System.Linq;
|
2013-05-09 13:38:02 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2016-10-25 15:02:04 -04:00
|
|
|
|
using MediaBrowser.Model.Services;
|
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>
|
2015-03-08 15:48:30 -04:00
|
|
|
|
public class SessionWebSocketListener : IWebSocketListener, IDisposable
|
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;
|
|
|
|
|
|
2013-09-04 13:02:19 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _dto service
|
|
|
|
|
/// </summary>
|
2014-04-15 22:17:48 -04:00
|
|
|
|
private readonly IJsonSerializer _json;
|
2013-05-10 08:18:07 -04:00
|
|
|
|
|
2015-03-08 15:48:30 -04:00
|
|
|
|
private readonly IHttpServer _httpServer;
|
|
|
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
/// <param name="sessionManager">The session manager.</param>
|
2013-07-29 10:18:55 -04:00
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
2014-05-18 15:58:42 -04:00
|
|
|
|
/// <param name="json">The json.</param>
|
2015-03-08 15:48:30 -04:00
|
|
|
|
/// <param name="httpServer">The HTTP server.</param>
|
|
|
|
|
/// <param name="serverManager">The server manager.</param>
|
2018-09-12 13:26:21 -04:00
|
|
|
|
public SessionWebSocketListener(ISessionManager sessionManager, ILogManager logManager, IJsonSerializer json, IHttpServer httpServer)
|
2013-05-09 13:38:02 -04:00
|
|
|
|
{
|
|
|
|
|
_sessionManager = sessionManager;
|
2013-07-29 10:18:55 -04:00
|
|
|
|
_logger = logManager.GetLogger(GetType().Name);
|
2014-04-15 22:17:48 -04:00
|
|
|
|
_json = json;
|
2015-03-08 15:48:30 -04:00
|
|
|
|
_httpServer = httpServer;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
httpServer.WebSocketConnected += _serverManager_WebSocketConnected;
|
2015-03-08 15:48:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
void _serverManager_WebSocketConnected(object sender, GenericEventArgs<IWebSocketConnection> e)
|
2015-03-08 15:48:30 -04:00
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
|
var session = GetSession(e.Argument.QueryString, e.Argument.RemoteEndPoint);
|
2015-03-08 15:48:30 -04:00
|
|
|
|
|
|
|
|
|
if (session != null)
|
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
|
EnsureController(session, e.Argument);
|
2015-03-08 15:48:30 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.Warn("Unable to determine session based on url: {0}", e.Argument.Url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
private SessionInfo GetSession(QueryParamCollection queryString, string remoteEndpoint)
|
2015-03-08 15:48:30 -04:00
|
|
|
|
{
|
2015-03-16 12:47:14 -04:00
|
|
|
|
if (queryString == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("queryString");
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
|
_httpServer.WebSocketConnected -= _serverManager_WebSocketConnected;
|
2013-05-09 13:38:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes the message.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task ProcessMessage(WebSocketMessageInfo message)
|
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
|
return Task.CompletedTask;
|
2013-09-24 11:08:51 -04:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
|
var controllerInfo = session.EnsureController<WebSocketController>(s => new WebSocketController(s, _logger, _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
|
|
|
|
}
|
2013-05-09 13:38:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|