WebHostBuilderExtensions.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Security.Cryptography.X509Certificates;
  6. using Jellyfin.Server.Helpers;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Controller.Extensions;
  9. using MediaBrowser.Model.Net;
  10. using Microsoft.AspNetCore.Hosting;
  11. using Microsoft.AspNetCore.Server.Kestrel.Core;
  12. using Microsoft.Extensions.Configuration;
  13. using Microsoft.Extensions.Hosting;
  14. using Microsoft.Extensions.Logging;
  15. namespace Jellyfin.Server.Extensions;
  16. /// <summary>
  17. /// Extensions for configuring the web host builder.
  18. /// </summary>
  19. public static class WebHostBuilderExtensions
  20. {
  21. /// <summary>
  22. /// Configure the web host builder.
  23. /// </summary>
  24. /// <param name="builder">The builder to configure.</param>
  25. /// <param name="appHost">The application host.</param>
  26. /// <param name="startupConfig">The application configuration.</param>
  27. /// <param name="appPaths">The application paths.</param>
  28. /// <param name="logger">The logger.</param>
  29. /// <returns>The configured web host builder.</returns>
  30. public static IWebHostBuilder ConfigureWebHostBuilder(
  31. this IWebHostBuilder builder,
  32. CoreAppHost appHost,
  33. IConfiguration startupConfig,
  34. IApplicationPaths appPaths,
  35. ILogger logger)
  36. {
  37. return builder
  38. .UseKestrel((builderContext, options) =>
  39. {
  40. SetupJellyfinWebServer(
  41. appHost.NetManager.GetAllBindInterfaces(false),
  42. appHost.HttpPort,
  43. appHost.ListenWithHttps ? appHost.HttpsPort : null,
  44. appHost.Certificate,
  45. startupConfig,
  46. appPaths,
  47. logger,
  48. builderContext,
  49. options);
  50. })
  51. .UseStartup(context => new Startup(appHost, context.Configuration));
  52. }
  53. /// <summary>
  54. /// Configures a Kestrel type webServer to bind to the specific arguments.
  55. /// </summary>
  56. /// <param name="addresses">The IP addresses that should be listend to.</param>
  57. /// <param name="httpPort">The http port.</param>
  58. /// <param name="httpsPort">If set the https port. If set you must also set the certificate.</param>
  59. /// <param name="certificate">The certificate used for https port.</param>
  60. /// <param name="startupConfig">The startup config.</param>
  61. /// <param name="appPaths">The app paths.</param>
  62. /// <param name="logger">A logger.</param>
  63. /// <param name="builderContext">The kestrel build pipeline context.</param>
  64. /// <param name="options">The kestrel server options.</param>
  65. /// <exception cref="InvalidOperationException">Will be thrown when a https port is set but no or an invalid certificate is provided.</exception>
  66. public static void SetupJellyfinWebServer(
  67. IReadOnlyList<IPData> addresses,
  68. int httpPort,
  69. int? httpsPort,
  70. X509Certificate2? certificate,
  71. IConfiguration startupConfig,
  72. IApplicationPaths appPaths,
  73. ILogger logger,
  74. WebHostBuilderContext builderContext,
  75. KestrelServerOptions options)
  76. {
  77. bool flagged = false;
  78. foreach (var netAdd in addresses)
  79. {
  80. var address = netAdd.Address;
  81. logger.LogInformation("Kestrel is listening on {Address}", address.Equals(IPAddress.IPv6Any) ? "all interfaces" : address);
  82. options.Listen(netAdd.Address, httpPort);
  83. if (httpsPort.HasValue)
  84. {
  85. if (builderContext.HostingEnvironment.IsDevelopment())
  86. {
  87. try
  88. {
  89. options.Listen(
  90. address,
  91. httpsPort.Value,
  92. listenOptions => listenOptions.UseHttps());
  93. }
  94. catch (InvalidOperationException)
  95. {
  96. if (!flagged)
  97. {
  98. 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");
  99. flagged = true;
  100. }
  101. }
  102. }
  103. else
  104. {
  105. if (certificate is null)
  106. {
  107. throw new InvalidOperationException("Cannot run jellyfin with https without setting a valid certificate.");
  108. }
  109. options.Listen(
  110. address,
  111. httpsPort.Value,
  112. listenOptions => listenOptions.UseHttps(certificate));
  113. }
  114. }
  115. }
  116. // Bind to unix socket (only on unix systems)
  117. if (startupConfig.UseUnixSocket() && Environment.OSVersion.Platform == PlatformID.Unix)
  118. {
  119. var socketPath = StartupHelpers.GetUnixSocketPath(startupConfig, appPaths);
  120. // Workaround for https://github.com/aspnet/AspNetCore/issues/14134
  121. if (File.Exists(socketPath))
  122. {
  123. File.Delete(socketPath);
  124. }
  125. options.ListenUnixSocket(socketPath);
  126. logger.LogInformation("Kestrel listening to unix socket {SocketPath}", socketPath);
  127. }
  128. }
  129. }