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