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