LanFilteringMiddleware.cs 1.7 KB

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