667c3768a9
- Add detailed debug/warning logs to authentication flows (AuthService, AuthorizationContext, WebSocketManager) for better traceability of auth attempts and failures. - Log WebSocket authentication attempts, including IP, path, and token preview. - Introduce PostgresVersionMismatchException for pg_dump/pg_restore version mismatches; parse and log server/client versions, clean up failed backups, and provide upgrade guidance. - Add helper for parsing version mismatch errors. - Update using directives and minor code cleanups. - Update .csproj.user with new publish profile path.
117 lines
4.5 KiB
C#
117 lines
4.5 KiB
C#
// <copyright file="WebSocketManager.cs" company="PlaceholderCompany">
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
// </copyright>
|
|
|
|
#nullable disable
|
|
|
|
#pragma warning disable CS1591
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.WebSockets;
|
|
using System.Threading.Tasks;
|
|
using MediaBrowser.Common.Extensions;
|
|
using MediaBrowser.Controller.Net;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Emby.Server.Implementations.HttpServer
|
|
{
|
|
public class WebSocketManager : IWebSocketManager
|
|
{
|
|
private readonly IWebSocketListener[] _webSocketListeners;
|
|
private readonly IAuthService _authService;
|
|
private readonly ILogger<WebSocketManager> _logger;
|
|
private readonly ILoggerFactory _loggerFactory;
|
|
|
|
public WebSocketManager(
|
|
IAuthService authService,
|
|
IEnumerable<IWebSocketListener> webSocketListeners,
|
|
ILogger<WebSocketManager> logger,
|
|
ILoggerFactory loggerFactory)
|
|
{
|
|
_webSocketListeners = webSocketListeners.ToArray();
|
|
_authService = authService;
|
|
_logger = logger;
|
|
_loggerFactory = loggerFactory;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task WebSocketRequestHandler(HttpContext context)
|
|
{
|
|
_logger.LogDebug(
|
|
"WebSocket authentication attempt from {IP}, Path: {Path}, QueryString: {QueryString}",
|
|
context.Connection.RemoteIpAddress,
|
|
context.Request.Path,
|
|
context.Request.QueryString);
|
|
|
|
var authorizationInfo = await _authService.Authenticate(context.Request).ConfigureAwait(false);
|
|
if (!authorizationInfo.IsAuthenticated)
|
|
{
|
|
var tokenPreview = authorizationInfo.HasToken && authorizationInfo.Token?.Length > 8
|
|
? authorizationInfo.Token.Substring(0, 8) + "..."
|
|
: authorizationInfo.Token ?? "null";
|
|
_logger.LogDebug(
|
|
"WebSocket authentication failed from {IP}. HasToken: {HasToken}, Token: {Token}",
|
|
context.Connection.RemoteIpAddress,
|
|
authorizationInfo.HasToken,
|
|
tokenPreview);
|
|
throw new SecurityException("Token is required");
|
|
}
|
|
|
|
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,
|
|
authorizationInfo,
|
|
context.GetNormalizedRemoteIP())
|
|
{
|
|
OnReceive = ProcessWebSocketMessageReceived
|
|
};
|
|
await using (connection.ConfigureAwait(false))
|
|
{
|
|
var tasks = new Task[_webSocketListeners.Length];
|
|
for (var i = 0; i < _webSocketListeners.Length; ++i)
|
|
{
|
|
tasks[i] = _webSocketListeners[i].ProcessWebSocketConnectedAsync(connection, context);
|
|
}
|
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
await connection.ReceiveAsync().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;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processes the web socket message received.
|
|
/// </summary>
|
|
/// <param name="result">The result.</param>
|
|
private async Task ProcessWebSocketMessageReceived(WebSocketMessageInfo result)
|
|
{
|
|
var tasks = new Task[_webSocketListeners.Length];
|
|
for (var i = 0; i < _webSocketListeners.Length; ++i)
|
|
{
|
|
tasks[i] = _webSocketListeners[i].ProcessMessageAsync(result);
|
|
}
|
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|