LanFilteringMiddleware.cs 1.5 KB

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