WebHostBuilderExtensions.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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();
  37. bool flagged = false;
  38. foreach (var netAdd in addresses)
  39. {
  40. logger.LogInformation("Kestrel is listening on {Address}", IPAddress.IPv6Any.Equals(netAdd.Address) ? "All IPv6 addresses" : netAdd.Address);
  41. options.Listen(netAdd.Address, appHost.HttpPort);
  42. if (appHost.ListenWithHttps)
  43. {
  44. options.Listen(
  45. netAdd.Address,
  46. appHost.HttpsPort,
  47. listenOptions => listenOptions.UseHttps(appHost.Certificate));
  48. }
  49. else if (builderContext.HostingEnvironment.IsDevelopment())
  50. {
  51. try
  52. {
  53. options.Listen(
  54. netAdd.Address,
  55. appHost.HttpsPort,
  56. listenOptions => listenOptions.UseHttps());
  57. }
  58. catch (InvalidOperationException)
  59. {
  60. if (!flagged)
  61. {
  62. 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");
  63. flagged = true;
  64. }
  65. }
  66. }
  67. }
  68. // Bind to unix socket (only on unix systems)
  69. if (startupConfig.UseUnixSocket() && Environment.OSVersion.Platform == PlatformID.Unix)
  70. {
  71. var socketPath = StartupHelpers.GetUnixSocketPath(startupConfig, appPaths);
  72. // Workaround for https://github.com/aspnet/AspNetCore/issues/14134
  73. if (File.Exists(socketPath))
  74. {
  75. File.Delete(socketPath);
  76. }
  77. options.ListenUnixSocket(socketPath);
  78. logger.LogInformation("Kestrel listening to unix socket {SocketPath}", socketPath);
  79. }
  80. })
  81. .UseStartup(_ => new Startup(appHost));
  82. }
  83. }