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.Common.Net;
  7. using MediaBrowser.Controller.Extensions;
  8. using Microsoft.AspNetCore.Hosting;
  9. using Microsoft.Extensions.Configuration;
  10. using Microsoft.Extensions.Hosting;
  11. using Microsoft.Extensions.Logging;
  12. namespace Jellyfin.Server.Extensions;
  13. /// <summary>
  14. /// Extensions for configuring the web host builder.
  15. /// </summary>
  16. public static class WebHostBuilderExtensions
  17. {
  18. /// <summary>
  19. /// Configure the web host builder.
  20. /// </summary>
  21. /// <param name="builder">The builder to configure.</param>
  22. /// <param name="appHost">The application host.</param>
  23. /// <param name="startupConfig">The application configuration.</param>
  24. /// <param name="appPaths">The application paths.</param>
  25. /// <param name="logger">The logger.</param>
  26. /// <returns>The configured web host builder.</returns>
  27. public static IWebHostBuilder ConfigureWebHostBuilder(
  28. this IWebHostBuilder builder,
  29. CoreAppHost appHost,
  30. IConfiguration startupConfig,
  31. IApplicationPaths appPaths,
  32. ILogger logger)
  33. {
  34. return builder
  35. .UseKestrel((builderContext, options) =>
  36. {
  37. var addresses = appHost.NetManager.GetAllBindInterfaces();
  38. bool flagged = false;
  39. foreach (IPObject netAdd in addresses)
  40. {
  41. logger.LogInformation("Kestrel listening on {Address}", IPAddress.IPv6Any.Equals(netAdd.Address) ? "All Addresses" : netAdd);
  42. options.Listen(netAdd.Address, appHost.HttpPort);
  43. if (appHost.ListenWithHttps)
  44. {
  45. options.Listen(
  46. netAdd.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. netAdd.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. }