2020-08-31 07:47:38 -07:00
|
|
|
using System.Net.Http.Headers;
|
2020-10-02 12:30:31 -07:00
|
|
|
using System.Net.Mime;
|
2020-10-08 11:00:55 -07:00
|
|
|
using Jellyfin.Networking.Configuration;
|
2019-11-24 07:27:58 -07:00
|
|
|
using Jellyfin.Server.Extensions;
|
2020-09-03 12:44:49 -07:00
|
|
|
using Jellyfin.Server.Implementations;
|
2020-04-23 20:24:40 -07:00
|
|
|
using Jellyfin.Server.Middleware;
|
2020-08-19 05:31:45 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
2019-11-24 07:27:58 -07:00
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2020-09-02 07:03:15 -07:00
|
|
|
using MediaBrowser.Controller.Extensions;
|
2019-11-24 07:27:58 -07:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2020-10-02 12:30:31 -07:00
|
|
|
using Microsoft.AspNetCore.StaticFiles;
|
2020-09-02 07:03:15 -07:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2019-11-24 07:27:58 -07:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-09-02 07:03:15 -07:00
|
|
|
using Microsoft.Extensions.FileProviders;
|
2019-11-24 07:27:58 -07:00
|
|
|
using Microsoft.Extensions.Hosting;
|
2020-04-25 18:35:51 -07:00
|
|
|
using Prometheus;
|
2019-11-24 07:27:58 -07:00
|
|
|
|
|
|
|
namespace Jellyfin.Server
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Startup configuration for the Kestrel webhost.
|
|
|
|
/// </summary>
|
|
|
|
public class Startup
|
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _serverConfigurationManager;
|
2020-09-02 15:31:42 -07:00
|
|
|
private readonly IServerApplicationHost _serverApplicationHost;
|
2019-11-24 07:27:58 -07:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="Startup" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serverConfigurationManager">The server configuration manager.</param>
|
2020-09-02 15:31:42 -07:00
|
|
|
/// <param name="serverApplicationHost">The server application host.</param>
|
|
|
|
public Startup(
|
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
2021-01-19 03:36:37 -07:00
|
|
|
IServerApplicationHost serverApplicationHost)
|
2019-11-24 07:27:58 -07:00
|
|
|
{
|
|
|
|
_serverConfigurationManager = serverConfigurationManager;
|
2020-09-02 15:31:42 -07:00
|
|
|
_serverApplicationHost = serverApplicationHost;
|
2019-11-24 07:27:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Configures the service collection for the webhost.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="services">The service collection.</param>
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
|
|
|
services.AddResponseCompression();
|
|
|
|
services.AddHttpContextAccessor();
|
2020-09-02 15:38:52 -07:00
|
|
|
services.AddHttpsRedirection(options =>
|
|
|
|
{
|
|
|
|
options.HttpsPort = _serverApplicationHost.HttpsPort;
|
|
|
|
});
|
2021-01-19 03:36:37 -07:00
|
|
|
services.AddJellyfinApi(_serverApplicationHost.GetApiPluginAssemblies(), _serverConfigurationManager.GetNetworkConfiguration());
|
2019-11-24 07:27:58 -07:00
|
|
|
|
|
|
|
services.AddJellyfinApiSwagger();
|
|
|
|
|
|
|
|
// configure custom legacy authentication
|
|
|
|
services.AddCustomAuthentication();
|
|
|
|
|
|
|
|
services.AddJellyfinApiAuthorization();
|
2020-08-19 05:31:45 -07:00
|
|
|
|
2020-09-02 15:31:42 -07:00
|
|
|
var productHeader = new ProductInfoHeaderValue(
|
|
|
|
_serverApplicationHost.Name.Replace(' ', '-'),
|
|
|
|
_serverApplicationHost.ApplicationVersionString);
|
2020-11-19 07:35:34 -07:00
|
|
|
var acceptJsonHeader = new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json, 1.0);
|
|
|
|
var acceptXmlHeader = new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Xml, 0.9);
|
|
|
|
var acceptAnyHeader = new MediaTypeWithQualityHeaderValue("*/*", 0.8);
|
2020-08-19 05:31:45 -07:00
|
|
|
services
|
2020-08-31 07:47:38 -07:00
|
|
|
.AddHttpClient(NamedClient.Default, c =>
|
|
|
|
{
|
2020-08-31 08:15:20 -07:00
|
|
|
c.DefaultRequestHeaders.UserAgent.Add(productHeader);
|
2020-11-18 18:20:31 -07:00
|
|
|
c.DefaultRequestHeaders.Accept.Add(acceptJsonHeader);
|
2020-11-19 07:35:34 -07:00
|
|
|
c.DefaultRequestHeaders.Accept.Add(acceptXmlHeader);
|
2020-11-18 18:20:31 -07:00
|
|
|
c.DefaultRequestHeaders.Accept.Add(acceptAnyHeader);
|
2020-08-31 07:47:38 -07:00
|
|
|
})
|
|
|
|
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
|
|
|
|
|
|
|
|
services.AddHttpClient(NamedClient.MusicBrainz, c =>
|
|
|
|
{
|
2020-08-31 08:15:20 -07:00
|
|
|
c.DefaultRequestHeaders.UserAgent.Add(productHeader);
|
2020-09-02 15:31:42 -07:00
|
|
|
c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue($"({_serverApplicationHost.ApplicationUserAgentAddress})"));
|
2020-12-01 09:57:13 -07:00
|
|
|
c.DefaultRequestHeaders.Accept.Add(acceptXmlHeader);
|
2020-11-18 18:20:31 -07:00
|
|
|
c.DefaultRequestHeaders.Accept.Add(acceptAnyHeader);
|
2020-08-31 07:47:38 -07:00
|
|
|
})
|
|
|
|
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
|
2020-09-03 08:26:22 -07:00
|
|
|
|
|
|
|
services.AddHealthChecks()
|
2020-09-03 12:44:49 -07:00
|
|
|
.AddDbContextCheck<JellyfinDb>();
|
2019-11-24 07:27:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Configures the app builder for the webhost.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="app">The application builder.</param>
|
|
|
|
/// <param name="env">The webhost environment.</param>
|
2020-09-02 07:03:15 -07:00
|
|
|
/// <param name="appConfig">The application config.</param>
|
2019-11-24 07:27:58 -07:00
|
|
|
public void Configure(
|
|
|
|
IApplicationBuilder app,
|
|
|
|
IWebHostEnvironment env,
|
2020-09-02 07:03:15 -07:00
|
|
|
IConfiguration appConfig)
|
2019-11-24 07:27:58 -07:00
|
|
|
{
|
2020-09-07 18:10:14 -07:00
|
|
|
app.UseBaseUrlRedirection();
|
2019-11-24 07:27:58 -07:00
|
|
|
|
2020-09-03 16:11:12 -07:00
|
|
|
// Wrap rest of configuration so everything only listens on BaseUrl.
|
2020-12-01 14:16:36 -07:00
|
|
|
var config = _serverConfigurationManager.GetNetworkConfiguration();
|
|
|
|
app.Map(config.BaseUrl, mainApp =>
|
2020-09-03 16:11:12 -07:00
|
|
|
{
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
|
|
|
mainApp.UseDeveloperExceptionPage();
|
|
|
|
}
|
2020-04-21 06:36:22 -07:00
|
|
|
|
2020-09-10 02:05:46 -07:00
|
|
|
mainApp.UseForwardedHeaders();
|
2020-09-03 16:11:12 -07:00
|
|
|
mainApp.UseMiddleware<ExceptionMiddleware>();
|
2020-07-14 04:26:47 -07:00
|
|
|
|
2020-09-03 16:11:12 -07:00
|
|
|
mainApp.UseMiddleware<ResponseTimeMiddleware>();
|
2019-11-24 07:27:58 -07:00
|
|
|
|
2020-09-03 16:11:12 -07:00
|
|
|
mainApp.UseWebSockets();
|
2019-11-24 07:27:58 -07:00
|
|
|
|
2020-09-03 16:11:12 -07:00
|
|
|
mainApp.UseResponseCompression();
|
2020-09-03 05:05:16 -07:00
|
|
|
|
2020-09-05 08:10:05 -07:00
|
|
|
mainApp.UseCors();
|
2019-11-24 07:27:58 -07:00
|
|
|
|
2020-12-01 14:16:36 -07:00
|
|
|
if (config.RequireHttps && _serverApplicationHost.ListenWithHttps)
|
2020-09-02 07:03:15 -07:00
|
|
|
{
|
2020-09-03 16:11:12 -07:00
|
|
|
mainApp.UseHttpsRedirection();
|
|
|
|
}
|
2020-04-26 08:28:17 -07:00
|
|
|
|
2020-12-10 08:17:02 -07:00
|
|
|
// This must be injected before any path related middleware.
|
|
|
|
mainApp.UsePathTrim();
|
2020-09-03 16:11:12 -07:00
|
|
|
mainApp.UseStaticFiles();
|
|
|
|
if (appConfig.HostWebClient())
|
|
|
|
{
|
2020-10-02 12:30:31 -07:00
|
|
|
var extensionProvider = new FileExtensionContentTypeProvider();
|
|
|
|
|
2020-12-04 14:12:59 -07:00
|
|
|
// subtitles octopus requires .data, .mem files.
|
2020-10-02 12:30:31 -07:00
|
|
|
extensionProvider.Mappings.Add(".data", MediaTypeNames.Application.Octet);
|
2020-12-04 14:12:59 -07:00
|
|
|
extensionProvider.Mappings.Add(".mem", MediaTypeNames.Application.Octet);
|
2020-09-03 16:11:12 -07:00
|
|
|
mainApp.UseStaticFiles(new StaticFileOptions
|
|
|
|
{
|
|
|
|
FileProvider = new PhysicalFileProvider(_serverConfigurationManager.ApplicationPaths.WebPath),
|
2020-10-02 12:30:31 -07:00
|
|
|
RequestPath = "/web",
|
|
|
|
ContentTypeProvider = extensionProvider
|
2020-09-03 16:11:12 -07:00
|
|
|
});
|
2020-12-06 19:40:43 -07:00
|
|
|
|
|
|
|
mainApp.UseRobotsRedirection();
|
2020-09-03 16:11:12 -07:00
|
|
|
}
|
2020-09-02 15:31:42 -07:00
|
|
|
|
2020-09-03 16:11:12 -07:00
|
|
|
mainApp.UseAuthentication();
|
|
|
|
mainApp.UseJellyfinApiSwagger(_serverConfigurationManager);
|
|
|
|
mainApp.UseRouting();
|
|
|
|
mainApp.UseAuthorization();
|
2019-11-24 07:27:58 -07:00
|
|
|
|
2020-09-03 16:11:12 -07:00
|
|
|
mainApp.UseLanFiltering();
|
|
|
|
mainApp.UseIpBasedAccessValidation();
|
|
|
|
mainApp.UseWebSocketHandler();
|
|
|
|
mainApp.UseServerStartupMessage();
|
2020-09-02 15:31:42 -07:00
|
|
|
|
2020-04-26 08:28:17 -07:00
|
|
|
if (_serverConfigurationManager.Configuration.EnableMetrics)
|
|
|
|
{
|
2020-09-03 16:14:50 -07:00
|
|
|
// Must be registered after any middleware that could change HTTP response codes or the data will be bad
|
|
|
|
mainApp.UseHttpMetrics();
|
2020-04-26 08:28:17 -07:00
|
|
|
}
|
2020-09-03 16:14:50 -07:00
|
|
|
|
2020-09-03 16:11:12 -07:00
|
|
|
mainApp.UseEndpoints(endpoints =>
|
|
|
|
{
|
|
|
|
endpoints.MapControllers();
|
|
|
|
if (_serverConfigurationManager.Configuration.EnableMetrics)
|
|
|
|
{
|
|
|
|
endpoints.MapMetrics("/metrics");
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints.MapHealthChecks("/health");
|
|
|
|
});
|
2019-11-24 07:27:58 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|