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