LanFilteringMiddleware.cs 1.6 KB

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