< Summary - Jellyfin

Information
Class: Jellyfin.Api.Middleware.LanFilteringMiddleware
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Middleware/LanFilteringMiddleware.cs
Line coverage
100%
Covered lines: 2
Uncovered lines: 0
Coverable lines: 2
Total lines: 51
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Middleware/LanFilteringMiddleware.cs

#LineLine coverage
 1using System.Net;
 2using System.Threading.Tasks;
 3using MediaBrowser.Common.Extensions;
 4using MediaBrowser.Common.Net;
 5using MediaBrowser.Controller.Configuration;
 6using Microsoft.AspNetCore.Http;
 7
 8namespace Jellyfin.Api.Middleware;
 9
 10/// <summary>
 11/// Validates the LAN host IP based on application configuration.
 12/// </summary>
 13public class LanFilteringMiddleware
 14{
 15    private readonly RequestDelegate _next;
 16
 17    /// <summary>
 18    /// Initializes a new instance of the <see cref="LanFilteringMiddleware"/> class.
 19    /// </summary>
 20    /// <param name="next">The next delegate in the pipeline.</param>
 21    public LanFilteringMiddleware(RequestDelegate next)
 22    {
 2223        _next = next;
 2224    }
 25
 26    /// <summary>
 27    /// Executes the middleware action.
 28    /// </summary>
 29    /// <param name="httpContext">The current HTTP context.</param>
 30    /// <param name="networkManager">The network manager.</param>
 31    /// <param name="serverConfigurationManager">The server configuration manager.</param>
 32    /// <returns>The async task.</returns>
 33    public async Task Invoke(HttpContext httpContext, INetworkManager networkManager, IServerConfigurationManager server
 34    {
 35        if (serverConfigurationManager.GetNetworkConfiguration().EnableRemoteAccess)
 36        {
 37            await _next(httpContext).ConfigureAwait(false);
 38            return;
 39        }
 40
 41        var host = httpContext.GetNormalizedRemoteIP();
 42        if (!networkManager.IsInLocalNetwork(host))
 43        {
 44            // No access from network, respond with 503 instead of 200.
 45            httpContext.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
 46            return;
 47        }
 48
 49        await _next(httpContext).ConfigureAwait(false);
 50    }
 51}