using System.Net; using System.Threading.Tasks; using Jellyfin.Networking.Configuration; using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; using Microsoft.AspNetCore.Http; namespace Jellyfin.Server.Middleware { /// /// Validates the IP of requests coming from local networks wrt. remote access. /// public class IpBasedAccessValidationMiddleware { private readonly RequestDelegate _next; /// /// Initializes a new instance of the class. /// /// The next delegate in the pipeline. public IpBasedAccessValidationMiddleware(RequestDelegate next) { _next = next; } /// /// Executes the middleware action. /// /// The current HTTP context. /// The network manager. /// The server configuration manager. /// The async task. public async Task Invoke(HttpContext httpContext, INetworkManager networkManager, IServerConfigurationManager serverConfigurationManager) { if (httpContext.IsLocal()) { // Running locally. await _next(httpContext).ConfigureAwait(false); return; } var remoteIp = httpContext.Connection.RemoteIpAddress ?? IPAddress.Loopback; if (!networkManager.HasRemoteAccess(remoteIp)) { return; } await _next(httpContext).ConfigureAwait(false); } } }