|
@@ -6,11 +6,12 @@ using System.Diagnostics;
|
|
using System.IO;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
using System.Net.Sockets;
|
|
using System.Net.Sockets;
|
|
|
|
+using System.Net.WebSockets;
|
|
using System.Reflection;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
-using Emby.Server.Implementations.Net;
|
|
|
|
using Emby.Server.Implementations.Services;
|
|
using Emby.Server.Implementations.Services;
|
|
|
|
+using Emby.Server.Implementations.SocketSharp;
|
|
using MediaBrowser.Common.Extensions;
|
|
using MediaBrowser.Common.Extensions;
|
|
using MediaBrowser.Common.Net;
|
|
using MediaBrowser.Common.Net;
|
|
using MediaBrowser.Controller;
|
|
using MediaBrowser.Controller;
|
|
@@ -22,6 +23,7 @@ using MediaBrowser.Model.Globalization;
|
|
using MediaBrowser.Model.Serialization;
|
|
using MediaBrowser.Model.Serialization;
|
|
using MediaBrowser.Model.Services;
|
|
using MediaBrowser.Model.Services;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
+using Microsoft.AspNetCore.Http.Extensions;
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Hosting;
|
|
@@ -30,7 +32,7 @@ using ServiceStack.Text.Jsv;
|
|
|
|
|
|
namespace Emby.Server.Implementations.HttpServer
|
|
namespace Emby.Server.Implementations.HttpServer
|
|
{
|
|
{
|
|
- public class HttpListenerHost : IHttpServer, IDisposable
|
|
|
|
|
|
+ public class HttpListenerHost : IHttpServer
|
|
{
|
|
{
|
|
/// <summary>
|
|
/// <summary>
|
|
/// The key for a setting that specifies the default redirect path
|
|
/// The key for a setting that specifies the default redirect path
|
|
@@ -39,17 +41,17 @@ namespace Emby.Server.Implementations.HttpServer
|
|
public const string DefaultRedirectKey = "HttpListenerHost:DefaultRedirectPath";
|
|
public const string DefaultRedirectKey = "HttpListenerHost:DefaultRedirectPath";
|
|
|
|
|
|
private readonly ILogger _logger;
|
|
private readonly ILogger _logger;
|
|
|
|
+ private readonly ILoggerFactory _loggerFactory;
|
|
private readonly IServerConfigurationManager _config;
|
|
private readonly IServerConfigurationManager _config;
|
|
private readonly INetworkManager _networkManager;
|
|
private readonly INetworkManager _networkManager;
|
|
private readonly IServerApplicationHost _appHost;
|
|
private readonly IServerApplicationHost _appHost;
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
private readonly IXmlSerializer _xmlSerializer;
|
|
private readonly IXmlSerializer _xmlSerializer;
|
|
- private readonly IHttpListener _socketListener;
|
|
|
|
private readonly Func<Type, Func<string, object>> _funcParseFn;
|
|
private readonly Func<Type, Func<string, object>> _funcParseFn;
|
|
private readonly string _defaultRedirectPath;
|
|
private readonly string _defaultRedirectPath;
|
|
private readonly string _baseUrlPrefix;
|
|
private readonly string _baseUrlPrefix;
|
|
|
|
+
|
|
private readonly Dictionary<Type, Type> _serviceOperationsMap = new Dictionary<Type, Type>();
|
|
private readonly Dictionary<Type, Type> _serviceOperationsMap = new Dictionary<Type, Type>();
|
|
- private readonly List<IWebSocketConnection> _webSocketConnections = new List<IWebSocketConnection>();
|
|
|
|
private readonly IHostEnvironment _hostEnvironment;
|
|
private readonly IHostEnvironment _hostEnvironment;
|
|
|
|
|
|
private IWebSocketListener[] _webSocketListeners = Array.Empty<IWebSocketListener>();
|
|
private IWebSocketListener[] _webSocketListeners = Array.Empty<IWebSocketListener>();
|
|
@@ -63,10 +65,10 @@ namespace Emby.Server.Implementations.HttpServer
|
|
INetworkManager networkManager,
|
|
INetworkManager networkManager,
|
|
IJsonSerializer jsonSerializer,
|
|
IJsonSerializer jsonSerializer,
|
|
IXmlSerializer xmlSerializer,
|
|
IXmlSerializer xmlSerializer,
|
|
- IHttpListener socketListener,
|
|
|
|
ILocalizationManager localizationManager,
|
|
ILocalizationManager localizationManager,
|
|
ServiceController serviceController,
|
|
ServiceController serviceController,
|
|
- IHostEnvironment hostEnvironment)
|
|
|
|
|
|
+ IHostEnvironment hostEnvironment,
|
|
|
|
+ ILoggerFactory loggerFactory)
|
|
{
|
|
{
|
|
_appHost = applicationHost;
|
|
_appHost = applicationHost;
|
|
_logger = logger;
|
|
_logger = logger;
|
|
@@ -76,11 +78,9 @@ namespace Emby.Server.Implementations.HttpServer
|
|
_networkManager = networkManager;
|
|
_networkManager = networkManager;
|
|
_jsonSerializer = jsonSerializer;
|
|
_jsonSerializer = jsonSerializer;
|
|
_xmlSerializer = xmlSerializer;
|
|
_xmlSerializer = xmlSerializer;
|
|
- _socketListener = socketListener;
|
|
|
|
ServiceController = serviceController;
|
|
ServiceController = serviceController;
|
|
-
|
|
|
|
- _socketListener.WebSocketConnected = OnWebSocketConnected;
|
|
|
|
_hostEnvironment = hostEnvironment;
|
|
_hostEnvironment = hostEnvironment;
|
|
|
|
+ _loggerFactory = loggerFactory;
|
|
|
|
|
|
_funcParseFn = t => s => JsvReader.GetParseFn(t)(s);
|
|
_funcParseFn = t => s => JsvReader.GetParseFn(t)(s);
|
|
|
|
|
|
@@ -172,38 +172,6 @@ namespace Emby.Server.Implementations.HttpServer
|
|
return attributes;
|
|
return attributes;
|
|
}
|
|
}
|
|
|
|
|
|
- private void OnWebSocketConnected(WebSocketConnectEventArgs e)
|
|
|
|
- {
|
|
|
|
- if (_disposed)
|
|
|
|
- {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- var connection = new WebSocketConnection(e.WebSocket, e.Endpoint, _jsonSerializer, _logger)
|
|
|
|
- {
|
|
|
|
- OnReceive = ProcessWebSocketMessageReceived,
|
|
|
|
- Url = e.Url,
|
|
|
|
- QueryString = e.QueryString
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- connection.Closed += OnConnectionClosed;
|
|
|
|
-
|
|
|
|
- lock (_webSocketConnections)
|
|
|
|
- {
|
|
|
|
- _webSocketConnections.Add(connection);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- WebSocketConnected?.Invoke(this, new GenericEventArgs<IWebSocketConnection>(connection));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void OnConnectionClosed(object sender, EventArgs e)
|
|
|
|
- {
|
|
|
|
- lock (_webSocketConnections)
|
|
|
|
- {
|
|
|
|
- _webSocketConnections.Remove((IWebSocketConnection)sender);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
private static Exception GetActualException(Exception ex)
|
|
private static Exception GetActualException(Exception ex)
|
|
{
|
|
{
|
|
if (ex is AggregateException agg)
|
|
if (ex is AggregateException agg)
|
|
@@ -289,32 +257,6 @@ namespace Emby.Server.Implementations.HttpServer
|
|
.Replace(_config.ApplicationPaths.ProgramDataPath, string.Empty, StringComparison.OrdinalIgnoreCase);
|
|
.Replace(_config.ApplicationPaths.ProgramDataPath, string.Empty, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
|
- /// Shut down the Web Service
|
|
|
|
- /// </summary>
|
|
|
|
- public void Stop()
|
|
|
|
- {
|
|
|
|
- List<IWebSocketConnection> connections;
|
|
|
|
-
|
|
|
|
- lock (_webSocketConnections)
|
|
|
|
- {
|
|
|
|
- connections = _webSocketConnections.ToList();
|
|
|
|
- _webSocketConnections.Clear();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- foreach (var connection in connections)
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- connection.Dispose();
|
|
|
|
- }
|
|
|
|
- catch (Exception ex)
|
|
|
|
- {
|
|
|
|
- _logger.LogError(ex, "Error disposing connection");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
public static string RemoveQueryStringByKey(string url, string key)
|
|
public static string RemoveQueryStringByKey(string url, string key)
|
|
{
|
|
{
|
|
var uri = new Uri(url);
|
|
var uri = new Uri(url);
|
|
@@ -430,31 +372,46 @@ namespace Emby.Server.Implementations.HttpServer
|
|
/// <returns>True if the request is valid, or false if the request is not valid and an HTTPS redirect is required.</returns>
|
|
/// <returns>True if the request is valid, or false if the request is not valid and an HTTPS redirect is required.</returns>
|
|
private bool ValidateSsl(string remoteIp, string urlString)
|
|
private bool ValidateSsl(string remoteIp, string urlString)
|
|
{
|
|
{
|
|
- if (_config.Configuration.RequireHttps && _appHost.ListenWithHttps)
|
|
|
|
|
|
+ if (_config.Configuration.RequireHttps
|
|
|
|
+ && _appHost.ListenWithHttps
|
|
|
|
+ && !urlString.Contains("https://", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
{
|
|
- if (urlString.IndexOf("https://", StringComparison.OrdinalIgnoreCase) == -1)
|
|
|
|
|
|
+ // These are hacks, but if these ever occur on ipv6 in the local network they could be incorrectly redirected
|
|
|
|
+ if (urlString.IndexOf("system/ping", StringComparison.OrdinalIgnoreCase) != -1
|
|
|
|
+ || urlString.IndexOf("dlna/", StringComparison.OrdinalIgnoreCase) != -1)
|
|
{
|
|
{
|
|
- // These are hacks, but if these ever occur on ipv6 in the local network they could be incorrectly redirected
|
|
|
|
- if (urlString.IndexOf("system/ping", StringComparison.OrdinalIgnoreCase) != -1
|
|
|
|
- || urlString.IndexOf("dlna/", StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
- {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
|
|
- if (!_networkManager.IsInLocalNetwork(remoteIp))
|
|
|
|
- {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
|
|
+ if (!_networkManager.IsInLocalNetwork(remoteIp))
|
|
|
|
+ {
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
|
+ public Task RequestHandler(HttpContext context)
|
|
|
|
+ {
|
|
|
|
+ if (context.WebSockets.IsWebSocketRequest)
|
|
|
|
+ {
|
|
|
|
+ return WebSocketRequestHandler(context);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var request = context.Request;
|
|
|
|
+ var response = context.Response;
|
|
|
|
+ var localPath = context.Request.Path.ToString();
|
|
|
|
+
|
|
|
|
+ var req = new WebSocketSharpRequest(request, response, request.Path, _logger);
|
|
|
|
+ return RequestHandler(req, request.GetDisplayUrl(), request.Host.ToString(), localPath, context.RequestAborted);
|
|
|
|
+ }
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Overridable method that can be used to implement a custom handler.
|
|
/// Overridable method that can be used to implement a custom handler.
|
|
/// </summary>
|
|
/// </summary>
|
|
- public async Task RequestHandler(IHttpRequest httpReq, string urlString, string host, string localPath, CancellationToken cancellationToken)
|
|
|
|
|
|
+ private async Task RequestHandler(IHttpRequest httpReq, string urlString, string host, string localPath, CancellationToken cancellationToken)
|
|
{
|
|
{
|
|
var stopWatch = new Stopwatch();
|
|
var stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
stopWatch.Start();
|
|
@@ -582,6 +539,43 @@ namespace Emby.Server.Implementations.HttpServer
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private async Task WebSocketRequestHandler(HttpContext context)
|
|
|
|
+ {
|
|
|
|
+ if (_disposed)
|
|
|
|
+ {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ _logger.LogInformation("WS {IP} request", context.Connection.RemoteIpAddress);
|
|
|
|
+
|
|
|
|
+ WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);
|
|
|
|
+
|
|
|
|
+ var connection = new WebSocketConnection(
|
|
|
|
+ _loggerFactory.CreateLogger<WebSocketConnection>(),
|
|
|
|
+ webSocket,
|
|
|
|
+ context.Connection.RemoteIpAddress,
|
|
|
|
+ context.Request.Query)
|
|
|
|
+ {
|
|
|
|
+ OnReceive = ProcessWebSocketMessageReceived
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ WebSocketConnected?.Invoke(this, new GenericEventArgs<IWebSocketConnection>(connection));
|
|
|
|
+
|
|
|
|
+ await connection.ProcessAsync().ConfigureAwait(false);
|
|
|
|
+ _logger.LogInformation("WS {IP} closed", context.Connection.RemoteIpAddress);
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex) // Otherwise ASP.Net will ignore the exception
|
|
|
|
+ {
|
|
|
|
+ _logger.LogError(ex, "WS {IP} WebSocketRequestHandler error", context.Connection.RemoteIpAddress);
|
|
|
|
+ if (!context.Response.HasStarted)
|
|
|
|
+ {
|
|
|
|
+ context.Response.StatusCode = 500;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
// Entry point for HttpListener
|
|
// Entry point for HttpListener
|
|
public ServiceHandler GetServiceHandler(IHttpRequest httpReq)
|
|
public ServiceHandler GetServiceHandler(IHttpRequest httpReq)
|
|
{
|
|
{
|
|
@@ -689,11 +683,6 @@ namespace Emby.Server.Implementations.HttpServer
|
|
return _jsonSerializer.DeserializeFromStreamAsync(stream, type);
|
|
return _jsonSerializer.DeserializeFromStreamAsync(stream, type);
|
|
}
|
|
}
|
|
|
|
|
|
- public Task ProcessWebSocketRequest(HttpContext context)
|
|
|
|
- {
|
|
|
|
- return _socketListener.ProcessWebSocketRequest(context);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
private string NormalizeEmbyRoutePath(string path)
|
|
private string NormalizeEmbyRoutePath(string path)
|
|
{
|
|
{
|
|
_logger.LogDebug("Normalizing /emby route");
|
|
_logger.LogDebug("Normalizing /emby route");
|
|
@@ -712,28 +701,6 @@ namespace Emby.Server.Implementations.HttpServer
|
|
return _baseUrlPrefix + NormalizeUrlPath(path);
|
|
return _baseUrlPrefix + NormalizeUrlPath(path);
|
|
}
|
|
}
|
|
|
|
|
|
- /// <inheritdoc />
|
|
|
|
- public void Dispose()
|
|
|
|
- {
|
|
|
|
- Dispose(true);
|
|
|
|
- GC.SuppressFinalize(this);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- protected virtual void Dispose(bool disposing)
|
|
|
|
- {
|
|
|
|
- if (_disposed)
|
|
|
|
- {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (disposing)
|
|
|
|
- {
|
|
|
|
- Stop();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- _disposed = true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Processes the web socket message received.
|
|
/// Processes the web socket message received.
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -745,8 +712,6 @@ namespace Emby.Server.Implementations.HttpServer
|
|
return Task.CompletedTask;
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
|
|
- _logger.LogDebug("Websocket message received: {0}", result.MessageType);
|
|
|
|
-
|
|
|
|
IEnumerable<Task> GetTasks()
|
|
IEnumerable<Task> GetTasks()
|
|
{
|
|
{
|
|
foreach (var x in _webSocketListeners)
|
|
foreach (var x in _webSocketListeners)
|