< Summary - Jellyfin

Information
Class: Jellyfin.Api.Middleware.IPBasedAccessValidationMiddleware
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Middleware/IpBasedAccessValidationMiddleware.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/IpBasedAccessValidationMiddleware.cs

#LineLine coverage
 1using System.Net;
 2using System.Threading.Tasks;
 3using MediaBrowser.Common.Extensions;
 4using MediaBrowser.Common.Net;
 5using Microsoft.AspNetCore.Http;
 6
 7namespace Jellyfin.Api.Middleware;
 8
 9/// <summary>
 10/// Validates the IP of requests coming from local networks wrt. remote access.
 11/// </summary>
 12public class IPBasedAccessValidationMiddleware
 13{
 14    private readonly RequestDelegate _next;
 15
 16    /// <summary>
 17    /// Initializes a new instance of the <see cref="IPBasedAccessValidationMiddleware"/> class.
 18    /// </summary>
 19    /// <param name="next">The next delegate in the pipeline.</param>
 20    public IPBasedAccessValidationMiddleware(RequestDelegate next)
 21    {
 2222        _next = next;
 2223    }
 24
 25    /// <summary>
 26    /// Executes the middleware action.
 27    /// </summary>
 28    /// <param name="httpContext">The current HTTP context.</param>
 29    /// <param name="networkManager">The network manager.</param>
 30    /// <returns>The async task.</returns>
 31    public async Task Invoke(HttpContext httpContext, INetworkManager networkManager)
 32    {
 33        if (httpContext.IsLocal())
 34        {
 35            // Running locally.
 36            await _next(httpContext).ConfigureAwait(false);
 37            return;
 38        }
 39
 40        var remoteIP = httpContext.Connection.RemoteIpAddress ?? IPAddress.Loopback;
 41
 42        if (!networkManager.HasRemoteAccess(remoteIP))
 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}