| | 1 | | using System; |
| | 2 | | using System.Net; |
| | 3 | | using System.Net.Http; |
| | 4 | | using System.Net.Http.Headers; |
| | 5 | | using System.Net.Mime; |
| | 6 | | using System.Text; |
| | 7 | | using Emby.Server.Implementations.EntryPoints; |
| | 8 | | using Jellyfin.Api.Middleware; |
| | 9 | | using Jellyfin.LiveTv.Extensions; |
| | 10 | | using Jellyfin.LiveTv.Recordings; |
| | 11 | | using Jellyfin.MediaEncoding.Hls.Extensions; |
| | 12 | | using Jellyfin.Networking; |
| | 13 | | using Jellyfin.Networking.HappyEyeballs; |
| | 14 | | using Jellyfin.Server.Extensions; |
| | 15 | | using Jellyfin.Server.HealthChecks; |
| | 16 | | using Jellyfin.Server.Implementations; |
| | 17 | | using Jellyfin.Server.Implementations.Extensions; |
| | 18 | | using Jellyfin.Server.Infrastructure; |
| | 19 | | using MediaBrowser.Common.Net; |
| | 20 | | using MediaBrowser.Controller.Configuration; |
| | 21 | | using MediaBrowser.Controller.Extensions; |
| | 22 | | using MediaBrowser.XbmcMetadata; |
| | 23 | | using Microsoft.AspNetCore.Builder; |
| | 24 | | using Microsoft.AspNetCore.Hosting; |
| | 25 | | using Microsoft.AspNetCore.Mvc; |
| | 26 | | using Microsoft.AspNetCore.Mvc.Infrastructure; |
| | 27 | | using Microsoft.AspNetCore.StaticFiles; |
| | 28 | | using Microsoft.Extensions.Configuration; |
| | 29 | | using Microsoft.Extensions.DependencyInjection; |
| | 30 | | using Microsoft.Extensions.FileProviders; |
| | 31 | | using Microsoft.Extensions.Hosting; |
| | 32 | | using Prometheus; |
| | 33 | |
|
| | 34 | | namespace Jellyfin.Server |
| | 35 | | { |
| | 36 | | /// <summary> |
| | 37 | | /// Startup configuration for the Kestrel webhost. |
| | 38 | | /// </summary> |
| | 39 | | public class Startup |
| | 40 | | { |
| | 41 | | private readonly CoreAppHost _serverApplicationHost; |
| | 42 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Initializes a new instance of the <see cref="Startup" /> class. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="appHost">The server application host.</param> |
| | 48 | | public Startup(CoreAppHost appHost) |
| | 49 | | { |
| 22 | 50 | | _serverApplicationHost = appHost; |
| 22 | 51 | | _serverConfigurationManager = appHost.ConfigurationManager; |
| 22 | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Configures the service collection for the webhost. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="services">The service collection.</param> |
| | 58 | | public void ConfigureServices(IServiceCollection services) |
| | 59 | | { |
| 22 | 60 | | services.AddResponseCompression(); |
| 22 | 61 | | services.AddHttpContextAccessor(); |
| 22 | 62 | | services.AddHttpsRedirection(options => |
| 22 | 63 | | { |
| 22 | 64 | | options.HttpsPort = _serverApplicationHost.HttpsPort; |
| 22 | 65 | | }); |
| | 66 | |
|
| | 67 | | // TODO remove once this is fixed upstream https://github.com/dotnet/aspnetcore/issues/34371 |
| 22 | 68 | | services.AddSingleton<IActionResultExecutor<PhysicalFileResult>, SymlinkFollowingPhysicalFileResultExecutor> |
| 22 | 69 | | services.AddJellyfinApi(_serverApplicationHost.GetApiPluginAssemblies(), _serverConfigurationManager.GetNetw |
| 22 | 70 | | services.AddJellyfinDbContext(); |
| 22 | 71 | | services.AddJellyfinApiSwagger(); |
| | 72 | |
|
| | 73 | | // configure custom legacy authentication |
| 22 | 74 | | services.AddCustomAuthentication(); |
| | 75 | |
|
| 22 | 76 | | services.AddJellyfinApiAuthorization(); |
| | 77 | |
|
| 22 | 78 | | var productHeader = new ProductInfoHeaderValue( |
| 22 | 79 | | _serverApplicationHost.Name.Replace(' ', '-'), |
| 22 | 80 | | _serverApplicationHost.ApplicationVersionString); |
| 22 | 81 | | var acceptJsonHeader = new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json, 1.0); |
| 22 | 82 | | var acceptXmlHeader = new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Xml, 0.9); |
| 22 | 83 | | var acceptAnyHeader = new MediaTypeWithQualityHeaderValue("*/*", 0.8); |
| 22 | 84 | | Func<IServiceProvider, HttpMessageHandler> eyeballsHttpClientHandlerDelegate = (_) => new SocketsHttpHandler |
| 22 | 85 | | { |
| 22 | 86 | | AutomaticDecompression = DecompressionMethods.All, |
| 22 | 87 | | RequestHeaderEncodingSelector = (_, _) => Encoding.UTF8, |
| 22 | 88 | | ConnectCallback = HttpClientExtension.OnConnect |
| 22 | 89 | | }; |
| | 90 | |
|
| 22 | 91 | | Func<IServiceProvider, HttpMessageHandler> defaultHttpClientHandlerDelegate = (_) => new SocketsHttpHandler( |
| 22 | 92 | | { |
| 22 | 93 | | AutomaticDecompression = DecompressionMethods.All, |
| 22 | 94 | | RequestHeaderEncodingSelector = (_, _) => Encoding.UTF8 |
| 22 | 95 | | }; |
| | 96 | |
|
| 22 | 97 | | services.AddHttpClient(NamedClient.Default, c => |
| 22 | 98 | | { |
| 22 | 99 | | c.DefaultRequestHeaders.UserAgent.Add(productHeader); |
| 22 | 100 | | c.DefaultRequestHeaders.Accept.Add(acceptJsonHeader); |
| 22 | 101 | | c.DefaultRequestHeaders.Accept.Add(acceptXmlHeader); |
| 22 | 102 | | c.DefaultRequestHeaders.Accept.Add(acceptAnyHeader); |
| 22 | 103 | | }) |
| 22 | 104 | | .ConfigurePrimaryHttpMessageHandler(eyeballsHttpClientHandlerDelegate); |
| | 105 | |
|
| 22 | 106 | | services.AddHttpClient(NamedClient.MusicBrainz, c => |
| 22 | 107 | | { |
| 22 | 108 | | c.DefaultRequestHeaders.UserAgent.Add(productHeader); |
| 22 | 109 | | c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue($"({_serverApplicationHost.Applicat |
| 22 | 110 | | c.DefaultRequestHeaders.Accept.Add(acceptXmlHeader); |
| 22 | 111 | | c.DefaultRequestHeaders.Accept.Add(acceptAnyHeader); |
| 22 | 112 | | }) |
| 22 | 113 | | .ConfigurePrimaryHttpMessageHandler(eyeballsHttpClientHandlerDelegate); |
| | 114 | |
|
| 22 | 115 | | services.AddHttpClient(NamedClient.DirectIp, c => |
| 22 | 116 | | { |
| 22 | 117 | | c.DefaultRequestHeaders.UserAgent.Add(productHeader); |
| 22 | 118 | | c.DefaultRequestHeaders.Accept.Add(acceptJsonHeader); |
| 22 | 119 | | c.DefaultRequestHeaders.Accept.Add(acceptXmlHeader); |
| 22 | 120 | | c.DefaultRequestHeaders.Accept.Add(acceptAnyHeader); |
| 22 | 121 | | }) |
| 22 | 122 | | .ConfigurePrimaryHttpMessageHandler(defaultHttpClientHandlerDelegate); |
| | 123 | |
|
| 22 | 124 | | services.AddHealthChecks() |
| 22 | 125 | | .AddCheck<DbContextFactoryHealthCheck<JellyfinDbContext>>(nameof(JellyfinDbContext)); |
| | 126 | |
|
| 22 | 127 | | services.AddHlsPlaylistGenerator(); |
| 22 | 128 | | services.AddLiveTvServices(); |
| | 129 | |
|
| 22 | 130 | | services.AddHostedService<RecordingsHost>(); |
| 22 | 131 | | services.AddHostedService<AutoDiscoveryHost>(); |
| 22 | 132 | | services.AddHostedService<PortForwardingHost>(); |
| 22 | 133 | | services.AddHostedService<NfoUserDataSaver>(); |
| 22 | 134 | | services.AddHostedService<LibraryChangedNotifier>(); |
| 22 | 135 | | services.AddHostedService<UserDataChangeNotifier>(); |
| 22 | 136 | | services.AddHostedService<RecordingNotifier>(); |
| 22 | 137 | | } |
| | 138 | |
|
| | 139 | | /// <summary> |
| | 140 | | /// Configures the app builder for the webhost. |
| | 141 | | /// </summary> |
| | 142 | | /// <param name="app">The application builder.</param> |
| | 143 | | /// <param name="env">The webhost environment.</param> |
| | 144 | | /// <param name="appConfig">The application config.</param> |
| | 145 | | public void Configure( |
| | 146 | | IApplicationBuilder app, |
| | 147 | | IWebHostEnvironment env, |
| | 148 | | IConfiguration appConfig) |
| | 149 | | { |
| 22 | 150 | | app.UseBaseUrlRedirection(); |
| | 151 | |
|
| | 152 | | // Wrap rest of configuration so everything only listens on BaseUrl. |
| 22 | 153 | | var config = _serverConfigurationManager.GetNetworkConfiguration(); |
| 22 | 154 | | app.Map(config.BaseUrl, mainApp => |
| 22 | 155 | | { |
| 22 | 156 | | if (env.IsDevelopment()) |
| 22 | 157 | | { |
| 22 | 158 | | mainApp.UseDeveloperExceptionPage(); |
| 22 | 159 | | } |
| 22 | 160 | |
|
| 22 | 161 | | mainApp.UseForwardedHeaders(); |
| 22 | 162 | | mainApp.UseMiddleware<ExceptionMiddleware>(); |
| 22 | 163 | |
|
| 22 | 164 | | mainApp.UseMiddleware<ResponseTimeMiddleware>(); |
| 22 | 165 | |
|
| 22 | 166 | | mainApp.UseWebSockets(); |
| 22 | 167 | |
|
| 22 | 168 | | mainApp.UseResponseCompression(); |
| 22 | 169 | |
|
| 22 | 170 | | mainApp.UseCors(); |
| 22 | 171 | |
|
| 22 | 172 | | if (config.RequireHttps && _serverApplicationHost.ListenWithHttps) |
| 22 | 173 | | { |
| 22 | 174 | | mainApp.UseHttpsRedirection(); |
| 22 | 175 | | } |
| 22 | 176 | |
|
| 22 | 177 | | // This must be injected before any path related middleware. |
| 22 | 178 | | mainApp.UsePathTrim(); |
| 22 | 179 | |
|
| 22 | 180 | | if (appConfig.HostWebClient()) |
| 22 | 181 | | { |
| 22 | 182 | | var extensionProvider = new FileExtensionContentTypeProvider(); |
| 22 | 183 | |
|
| 22 | 184 | | // subtitles octopus requires .data, .mem files. |
| 22 | 185 | | extensionProvider.Mappings.Add(".data", MediaTypeNames.Application.Octet); |
| 22 | 186 | | extensionProvider.Mappings.Add(".mem", MediaTypeNames.Application.Octet); |
| 22 | 187 | | mainApp.UseDefaultFiles(new DefaultFilesOptions |
| 22 | 188 | | { |
| 22 | 189 | | FileProvider = new PhysicalFileProvider(_serverConfigurationManager.ApplicationPaths.WebPath), |
| 22 | 190 | | RequestPath = "/web" |
| 22 | 191 | | }); |
| 22 | 192 | | mainApp.UseStaticFiles(new StaticFileOptions |
| 22 | 193 | | { |
| 22 | 194 | | FileProvider = new PhysicalFileProvider(_serverConfigurationManager.ApplicationPaths.WebPath), |
| 22 | 195 | | RequestPath = "/web", |
| 22 | 196 | | ContentTypeProvider = extensionProvider |
| 22 | 197 | | }); |
| 22 | 198 | |
|
| 22 | 199 | | mainApp.UseRobotsRedirection(); |
| 22 | 200 | | } |
| 22 | 201 | |
|
| 22 | 202 | | mainApp.UseStaticFiles(); |
| 22 | 203 | | mainApp.UseAuthentication(); |
| 22 | 204 | | mainApp.UseJellyfinApiSwagger(_serverConfigurationManager); |
| 22 | 205 | | mainApp.UseQueryStringDecoding(); |
| 22 | 206 | | mainApp.UseRouting(); |
| 22 | 207 | | mainApp.UseAuthorization(); |
| 22 | 208 | |
|
| 22 | 209 | | mainApp.UseLanFiltering(); |
| 22 | 210 | | mainApp.UseIPBasedAccessValidation(); |
| 22 | 211 | | mainApp.UseWebSocketHandler(); |
| 22 | 212 | | mainApp.UseServerStartupMessage(); |
| 22 | 213 | |
|
| 22 | 214 | | if (_serverConfigurationManager.Configuration.EnableMetrics) |
| 22 | 215 | | { |
| 22 | 216 | | // Must be registered after any middleware that could change HTTP response codes or the data will be |
| 22 | 217 | | mainApp.UseHttpMetrics(); |
| 22 | 218 | | } |
| 22 | 219 | |
|
| 22 | 220 | | mainApp.UseEndpoints(endpoints => |
| 22 | 221 | | { |
| 22 | 222 | | endpoints.MapControllers(); |
| 22 | 223 | | if (_serverConfigurationManager.Configuration.EnableMetrics) |
| 22 | 224 | | { |
| 22 | 225 | | endpoints.MapMetrics(); |
| 22 | 226 | | } |
| 22 | 227 | |
|
| 22 | 228 | | endpoints.MapHealthChecks("/health"); |
| 22 | 229 | | }); |
| 22 | 230 | | }); |
| 22 | 231 | | } |
| | 232 | | } |
| | 233 | | } |