WebHostBuilderExtensions.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using Jellyfin.Server.Helpers;
  5. using MediaBrowser.Common.Configuration;
  6. using MediaBrowser.Controller.Extensions;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.Hosting;
  10. using Microsoft.Extensions.Logging;
  11. namespace Jellyfin.Server.Extensions;
  12. /// <summary>
  13. /// Extensions for configuring the web host builder.
  14. /// </summary>
  15. public static class WebHostBuilderExtensions
  16. {
  17. /// <summary>
  18. /// Configure the web host builder.
  19. /// </summary>
  20. /// <param name="builder">The builder to configure.</param>
  21. /// <param name="appHost">The application host.</param>
  22. /// <param name="startupConfig">The application configuration.</param>
  23. /// <param name="appPaths">The application paths.</param>
  24. /// <param name="logger">The logger.</param>
  25. /// <returns>The configured web host builder.</returns>
  26. public static IWebHostBuilder ConfigureWebHostBuilder(
  27. this IWebHostBuilder builder,
  28. CoreAppHost appHost,
  29. IConfiguration startupConfig,
  30. IApplicationPaths appPaths,
  31. ILogger logger)
  32. {
  33. return builder
  34. .UseKestrel((builderContext, options) =>
  35. {
  36. var addresses = appHost.NetManager.GetAllBindInterfaces(false);
  37. bool flagged = false;
  38. foreach (var netAdd in addresses)
  39. {
  40. var address = netAdd.Address;
  41. logger.LogInformation("Kestrel is listening on {Address}", address.Equals(IPAddress.IPv6Any) ? "all interfaces" : address);
  42. options.Listen(netAdd.Address, appHost.HttpPort);
  43. if (appHost.ListenWithHttps)
  44. {
  45. options.Listen(
  46. address,
  47. appHost.HttpsPort,
  48. listenOptions => listenOptions.UseHttps(appHost.Certificate));
  49. }
  50. else if (builderContext.HostingEnvironment.IsDevelopment())
  51. {
  52. try
  53. {
  54. options.Listen(
  55. address,
  56. appHost.HttpsPort,
  57. listenOptions => listenOptions.UseHttps());
  58. }
  59. catch (InvalidOperationException)
  60. {
  61. if (!flagged)
  62. {
  63. logger.LogWarning("Failed to listen to HTTPS using the ASP.NET Core HTTPS development certificate. Please ensure it has been installed and set as trusted");
  64. flagged = true;
  65. }
  66. }
  67. }
  68. }
  69. // Bind to unix socket (only on unix systems)
  70. if (startupConfig.UseUnixSocket() && Environment.OSVersion.Platform == PlatformID.Unix)
  71. {
  72. var socketPath = StartupHelpers.GetUnixSocketPath(startupConfig, appPaths);
  73. // Workaround for https://github.com/aspnet/AspNetCore/issues/14134
  74. if (File.Exists(socketPath))
  75. {
  76. File.Delete(socketPath);
  77. }
  78. options.ListenUnixSocket(socketPath);
  79. logger.LogInformation("Kestrel listening to unix socket {SocketPath}", socketPath);
  80. }
  81. })
  82. .UseStartup(_ => new Startup(appHost));
  83. }
  84. }