2020-04-19 10:24:32 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-09-10 02:05:46 -07:00
|
|
|
using System.Net;
|
2021-01-07 17:05:15 -07:00
|
|
|
using System.Net.Sockets;
|
2020-05-08 07:40:37 -07:00
|
|
|
using System.Reflection;
|
2023-02-08 15:55:26 -07:00
|
|
|
using System.Security.Claims;
|
2020-11-06 13:00:14 -07:00
|
|
|
using Emby.Server.Implementations;
|
2019-11-23 11:43:30 -07:00
|
|
|
using Jellyfin.Api.Auth;
|
2021-09-10 02:44:50 -07:00
|
|
|
using Jellyfin.Api.Auth.AnonymousLanAccessPolicy;
|
2020-06-15 11:49:54 -07:00
|
|
|
using Jellyfin.Api.Auth.DefaultAuthorizationPolicy;
|
2023-02-08 15:55:26 -07:00
|
|
|
using Jellyfin.Api.Auth.FirstTimeSetupPolicy;
|
2020-08-06 07:17:45 -07:00
|
|
|
using Jellyfin.Api.Auth.LocalAccessOrRequiresElevationPolicy;
|
2020-12-03 02:43:44 -07:00
|
|
|
using Jellyfin.Api.Auth.SyncPlayAccessPolicy;
|
2023-02-08 15:55:26 -07:00
|
|
|
using Jellyfin.Api.Auth.UserPermissionPolicy;
|
2019-11-24 11:25:46 -07:00
|
|
|
using Jellyfin.Api.Constants;
|
2019-11-23 11:43:30 -07:00
|
|
|
using Jellyfin.Api.Controllers;
|
2023-01-13 20:23:22 -07:00
|
|
|
using Jellyfin.Api.Formatters;
|
2020-11-21 11:58:35 -07:00
|
|
|
using Jellyfin.Api.ModelBinders;
|
2020-12-03 02:43:44 -07:00
|
|
|
using Jellyfin.Data.Enums;
|
2021-06-19 09:02:33 -07:00
|
|
|
using Jellyfin.Extensions.Json;
|
2020-09-05 12:02:53 -07:00
|
|
|
using Jellyfin.Server.Configuration;
|
2020-09-01 16:26:49 -07:00
|
|
|
using Jellyfin.Server.Filters;
|
2023-11-10 08:49:40 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
2020-06-02 10:47:00 -07:00
|
|
|
using MediaBrowser.Model.Entities;
|
2022-03-23 07:32:33 -07:00
|
|
|
using MediaBrowser.Model.Session;
|
2019-11-23 11:43:30 -07:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-06-17 07:05:30 -07:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2020-09-05 08:10:05 -07:00
|
|
|
using Microsoft.AspNetCore.Cors.Infrastructure;
|
2020-06-17 07:05:30 -07:00
|
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
2019-11-23 11:43:30 -07:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-11-06 13:00:14 -07:00
|
|
|
using Microsoft.OpenApi.Any;
|
|
|
|
using Microsoft.OpenApi.Interfaces;
|
2019-11-23 11:43:30 -07:00
|
|
|
using Microsoft.OpenApi.Models;
|
2020-05-08 07:40:37 -07:00
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
2020-09-10 02:05:46 -07:00
|
|
|
using AuthenticationSchemes = Jellyfin.Api.Constants.AuthenticationSchemes;
|
2019-11-23 11:43:30 -07:00
|
|
|
|
2019-11-24 07:27:58 -07:00
|
|
|
namespace Jellyfin.Server.Extensions
|
2019-11-23 11:43:30 -07:00
|
|
|
{
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <summary>
|
|
|
|
/// API specific extensions for the service collection.
|
|
|
|
/// </summary>
|
2019-11-23 11:43:30 -07:00
|
|
|
public static class ApiServiceCollectionExtensions
|
|
|
|
{
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Adds jellyfin API authorization policies to the DI container.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
|
|
|
/// <returns>The updated service collection.</returns>
|
2019-11-23 11:43:30 -07:00
|
|
|
public static IServiceCollection AddJellyfinApiAuthorization(this IServiceCollection serviceCollection)
|
|
|
|
{
|
2023-02-08 15:55:26 -07:00
|
|
|
// The default handler must be first so that it is evaluated first
|
2020-06-15 11:49:54 -07:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, DefaultAuthorizationHandler>();
|
2023-02-08 15:55:26 -07:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, UserPermissionHandler>();
|
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, FirstTimeSetupHandler>();
|
2021-09-10 02:44:50 -07:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, AnonymousLanAccessHandler>();
|
2020-12-03 02:43:44 -07:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, SyncPlayAccessHandler>();
|
2023-09-23 14:58:03 -07:00
|
|
|
serviceCollection.AddSingleton<IAuthorizationHandler, LocalAccessOrRequiresElevationHandler>();
|
2023-02-08 15:55:26 -07:00
|
|
|
|
2019-11-23 11:43:30 -07:00
|
|
|
return serviceCollection.AddAuthorizationCore(options =>
|
|
|
|
{
|
2023-02-08 15:55:26 -07:00
|
|
|
options.DefaultPolicy = new AuthorizationPolicyBuilder()
|
|
|
|
.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication)
|
2023-02-09 05:15:58 -07:00
|
|
|
.AddRequirements(new DefaultAuthorizationRequirement())
|
2023-02-08 15:55:26 -07:00
|
|
|
.Build();
|
|
|
|
|
2023-02-14 11:04:18 -07:00
|
|
|
options.AddPolicy(Policies.AnonymousLanAccessPolicy, new AnonymousLanAccessRequirement());
|
|
|
|
options.AddPolicy(Policies.CollectionManagement, new UserPermissionRequirement(PermissionKind.EnableCollectionManagement));
|
2023-02-08 15:55:26 -07:00
|
|
|
options.AddPolicy(Policies.Download, new UserPermissionRequirement(PermissionKind.EnableContentDownloading));
|
2023-02-09 13:06:51 -07:00
|
|
|
options.AddPolicy(Policies.FirstTimeSetupOrDefault, new FirstTimeSetupRequirement(requireAdmin: false));
|
|
|
|
options.AddPolicy(Policies.FirstTimeSetupOrElevated, new FirstTimeSetupRequirement());
|
|
|
|
options.AddPolicy(Policies.FirstTimeSetupOrIgnoreParentalControl, new FirstTimeSetupRequirement(false, false));
|
2023-02-08 15:55:26 -07:00
|
|
|
options.AddPolicy(Policies.IgnoreParentalControl, new DefaultAuthorizationRequirement(validateParentalSchedule: false));
|
2023-02-14 11:04:18 -07:00
|
|
|
options.AddPolicy(Policies.LiveTvAccess, new UserPermissionRequirement(PermissionKind.EnableLiveTvAccess));
|
|
|
|
options.AddPolicy(Policies.LiveTvManagement, new UserPermissionRequirement(PermissionKind.EnableLiveTvManagement));
|
|
|
|
options.AddPolicy(Policies.LocalAccessOrRequiresElevation, new LocalAccessOrRequiresElevationRequirement());
|
2023-02-08 15:55:26 -07:00
|
|
|
options.AddPolicy(Policies.SyncPlayHasAccess, new SyncPlayAccessRequirement(SyncPlayAccessRequirementType.HasAccess));
|
|
|
|
options.AddPolicy(Policies.SyncPlayCreateGroup, new SyncPlayAccessRequirement(SyncPlayAccessRequirementType.CreateGroup));
|
|
|
|
options.AddPolicy(Policies.SyncPlayJoinGroup, new SyncPlayAccessRequirement(SyncPlayAccessRequirementType.JoinGroup));
|
|
|
|
options.AddPolicy(Policies.SyncPlayIsInGroup, new SyncPlayAccessRequirement(SyncPlayAccessRequirementType.IsInGroup));
|
2023-10-16 14:57:08 -07:00
|
|
|
options.AddPolicy(Policies.SubtitleManagement, new UserPermissionRequirement(PermissionKind.EnableSubtitleManagement));
|
2020-06-15 11:49:54 -07:00
|
|
|
options.AddPolicy(
|
|
|
|
Policies.RequiresElevation,
|
2023-02-08 15:55:26 -07:00
|
|
|
policy => policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication)
|
|
|
|
.RequireClaim(ClaimTypes.Role, UserRoles.Administrator));
|
2019-11-23 11:43:30 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Adds custom legacy authentication to the service collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
|
|
|
/// <returns>The updated service collection.</returns>
|
2019-11-23 11:43:30 -07:00
|
|
|
public static AuthenticationBuilder AddCustomAuthentication(this IServiceCollection serviceCollection)
|
|
|
|
{
|
2019-11-24 11:25:46 -07:00
|
|
|
return serviceCollection.AddAuthentication(AuthenticationSchemes.CustomAuthentication)
|
|
|
|
.AddScheme<AuthenticationSchemeOptions, CustomAuthenticationHandler>(AuthenticationSchemes.CustomAuthentication, null);
|
2019-11-23 11:43:30 -07:00
|
|
|
}
|
|
|
|
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <summary>
|
2022-07-20 00:48:20 -07:00
|
|
|
/// Extension method for adding the Jellyfin API to the service collection.
|
2019-11-23 12:31:17 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
2020-09-02 07:03:15 -07:00
|
|
|
/// <param name="pluginAssemblies">An IEnumerable containing all plugin assemblies with API controllers.</param>
|
2021-01-07 17:05:15 -07:00
|
|
|
/// <param name="config">The <see cref="NetworkConfiguration"/>.</param>
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <returns>The MVC builder.</returns>
|
2021-01-19 03:36:37 -07:00
|
|
|
public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, IEnumerable<Assembly> pluginAssemblies, NetworkConfiguration config)
|
2019-11-23 11:43:30 -07:00
|
|
|
{
|
2020-08-10 07:12:22 -07:00
|
|
|
IMvcBuilder mvcBuilder = serviceCollection
|
2021-01-12 13:43:25 -07:00
|
|
|
.AddCors()
|
|
|
|
.AddTransient<ICorsPolicyProvider, CorsPolicyProvider>()
|
|
|
|
.Configure<ForwardedHeadersOptions>(options =>
|
2020-06-17 07:05:30 -07:00
|
|
|
{
|
2021-01-07 17:05:15 -07:00
|
|
|
// https://github.com/dotnet/aspnetcore/blob/master/src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs
|
|
|
|
// Enable debug logging on Microsoft.AspNetCore.HttpOverrides.ForwardedHeadersMiddleware to help investigate issues.
|
|
|
|
|
2021-08-31 13:22:55 -07:00
|
|
|
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost;
|
|
|
|
|
2021-01-07 17:05:15 -07:00
|
|
|
if (config.KnownProxies.Length == 0)
|
2020-09-10 02:05:46 -07:00
|
|
|
{
|
2021-01-07 17:05:15 -07:00
|
|
|
options.KnownNetworks.Clear();
|
2021-01-12 06:23:10 -07:00
|
|
|
options.KnownProxies.Clear();
|
2020-12-05 02:18:56 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-19 12:29:51 -07:00
|
|
|
AddProxyAddresses(config, config.KnownProxies, options);
|
2020-09-10 02:05:46 -07:00
|
|
|
}
|
2021-01-07 17:05:15 -07:00
|
|
|
|
|
|
|
// Only set forward limit if we have some known proxies or some known networks.
|
|
|
|
if (options.KnownProxies.Count != 0 || options.KnownNetworks.Count != 0)
|
|
|
|
{
|
|
|
|
options.ForwardLimit = null;
|
|
|
|
}
|
2020-06-17 07:05:30 -07:00
|
|
|
})
|
2020-06-01 10:03:08 -07:00
|
|
|
.AddMvc(opts =>
|
2019-11-23 11:43:30 -07:00
|
|
|
{
|
2020-08-20 10:17:27 -07:00
|
|
|
// Allow requester to change between camelCase and PascalCase
|
|
|
|
opts.RespectBrowserAcceptHeader = true;
|
|
|
|
|
2020-04-19 17:10:59 -07:00
|
|
|
opts.OutputFormatters.Insert(0, new CamelCaseJsonProfileFormatter());
|
|
|
|
opts.OutputFormatters.Insert(0, new PascalCaseJsonProfileFormatter());
|
2020-06-06 15:51:21 -07:00
|
|
|
|
|
|
|
opts.OutputFormatters.Add(new CssOutputFormatter());
|
2020-08-15 09:39:24 -07:00
|
|
|
opts.OutputFormatters.Add(new XmlOutputFormatter());
|
2020-11-21 11:58:35 -07:00
|
|
|
|
|
|
|
opts.ModelBinderProviders.Insert(0, new NullableEnumModelBinderProvider());
|
2019-11-23 11:43:30 -07:00
|
|
|
})
|
2019-11-23 12:31:17 -07:00
|
|
|
|
2019-11-23 11:43:30 -07:00
|
|
|
// Clear app parts to avoid other assemblies being picked up
|
|
|
|
.ConfigureApplicationPartManager(a => a.ApplicationParts.Clear())
|
|
|
|
.AddApplicationPart(typeof(StartupController).Assembly)
|
2020-04-14 23:24:15 -07:00
|
|
|
.AddJsonOptions(options =>
|
2020-04-12 18:17:46 -07:00
|
|
|
{
|
2020-05-21 07:44:15 -07:00
|
|
|
// Update all properties that are set in JsonDefaults
|
2021-03-08 21:57:38 -07:00
|
|
|
var jsonOptions = JsonDefaults.PascalCaseOptions;
|
2020-05-21 07:44:15 -07:00
|
|
|
|
|
|
|
// From JsonDefaults
|
|
|
|
options.JsonSerializerOptions.ReadCommentHandling = jsonOptions.ReadCommentHandling;
|
|
|
|
options.JsonSerializerOptions.WriteIndented = jsonOptions.WriteIndented;
|
2020-08-25 06:33:58 -07:00
|
|
|
options.JsonSerializerOptions.DefaultIgnoreCondition = jsonOptions.DefaultIgnoreCondition;
|
2020-08-26 07:22:48 -07:00
|
|
|
options.JsonSerializerOptions.NumberHandling = jsonOptions.NumberHandling;
|
2020-08-23 06:48:12 -07:00
|
|
|
|
2020-05-21 07:44:15 -07:00
|
|
|
options.JsonSerializerOptions.Converters.Clear();
|
|
|
|
foreach (var converter in jsonOptions.Converters)
|
|
|
|
{
|
|
|
|
options.JsonSerializerOptions.Converters.Add(converter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// From JsonDefaults.PascalCase
|
|
|
|
options.JsonSerializerOptions.PropertyNamingPolicy = jsonOptions.PropertyNamingPolicy;
|
2020-08-11 08:04:11 -07:00
|
|
|
});
|
2020-08-10 07:12:22 -07:00
|
|
|
|
2020-08-31 08:53:55 -07:00
|
|
|
foreach (Assembly pluginAssembly in pluginAssemblies)
|
2020-08-10 07:12:22 -07:00
|
|
|
{
|
2020-08-11 08:04:11 -07:00
|
|
|
mvcBuilder.AddApplicationPart(pluginAssembly);
|
2020-08-10 07:12:22 -07:00
|
|
|
}
|
|
|
|
|
2020-08-11 08:04:11 -07:00
|
|
|
return mvcBuilder.AddControllersAsServices();
|
2019-11-23 11:43:30 -07:00
|
|
|
}
|
|
|
|
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Adds Swagger to the service collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serviceCollection">The service collection.</param>
|
|
|
|
/// <returns>The updated service collection.</returns>
|
2019-11-23 11:43:30 -07:00
|
|
|
public static IServiceCollection AddJellyfinApiSwagger(this IServiceCollection serviceCollection)
|
|
|
|
{
|
|
|
|
return serviceCollection.AddSwaggerGen(c =>
|
|
|
|
{
|
2021-03-30 06:15:16 -07:00
|
|
|
var version = typeof(ApplicationHost).Assembly.GetName().Version?.ToString(3) ?? "0.0.1";
|
2020-11-06 13:00:14 -07:00
|
|
|
c.SwaggerDoc("api-docs", new OpenApiInfo
|
|
|
|
{
|
|
|
|
Title = "Jellyfin API",
|
2021-03-12 17:11:43 -07:00
|
|
|
Version = version,
|
2020-11-06 13:00:14 -07:00
|
|
|
Extensions = new Dictionary<string, IOpenApiExtension>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
"x-jellyfin-version",
|
2021-03-12 17:11:43 -07:00
|
|
|
new OpenApiString(version)
|
2020-11-06 13:00:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-01 10:12:33 -07:00
|
|
|
c.AddSecurityDefinition(AuthenticationSchemes.CustomAuthentication, new OpenApiSecurityScheme
|
|
|
|
{
|
|
|
|
Type = SecuritySchemeType.ApiKey,
|
|
|
|
In = ParameterLocation.Header,
|
2021-09-15 10:28:20 -07:00
|
|
|
Name = "Authorization",
|
2020-06-01 10:12:33 -07:00
|
|
|
Description = "API key header parameter"
|
|
|
|
});
|
|
|
|
|
2020-04-19 10:24:32 -07:00
|
|
|
// Add all xml doc files to swagger generator.
|
|
|
|
var xmlFiles = Directory.GetFiles(
|
|
|
|
AppContext.BaseDirectory,
|
|
|
|
"*.xml",
|
|
|
|
SearchOption.TopDirectoryOnly);
|
|
|
|
|
|
|
|
foreach (var xmlFile in xmlFiles)
|
|
|
|
{
|
|
|
|
c.IncludeXmlComments(xmlFile);
|
|
|
|
}
|
2020-04-26 22:28:32 -07:00
|
|
|
|
|
|
|
// Order actions by route path, then by http method.
|
|
|
|
c.OrderActionsBy(description =>
|
2020-06-25 16:44:11 -07:00
|
|
|
$"{description.ActionDescriptor.RouteValues["controller"]}_{description.RelativePath}");
|
2020-05-08 07:40:37 -07:00
|
|
|
|
|
|
|
// Use method name as operationId
|
2020-08-03 13:38:51 -07:00
|
|
|
c.CustomOperationIds(
|
|
|
|
description =>
|
|
|
|
{
|
|
|
|
description.TryGetMethodInfo(out MethodInfo methodInfo);
|
|
|
|
// Attribute name, method name, none.
|
2021-08-04 05:40:09 -07:00
|
|
|
return description?.ActionDescriptor.AttributeRouteInfo?.Name
|
2020-08-03 13:38:51 -07:00
|
|
|
?? methodInfo?.Name
|
|
|
|
?? null;
|
|
|
|
});
|
2020-06-02 10:47:00 -07:00
|
|
|
|
2021-01-24 14:36:36 -07:00
|
|
|
// Allow parameters to properly be nullable.
|
|
|
|
c.UseAllOfToExtendReferenceSchemas();
|
2021-02-15 10:08:38 -07:00
|
|
|
c.SupportNonNullableReferenceTypes();
|
2021-01-24 14:36:36 -07:00
|
|
|
|
2020-06-02 10:47:00 -07:00
|
|
|
// TODO - remove when all types are supported in System.Text.Json
|
|
|
|
c.AddSwaggerTypeMappings();
|
2020-09-01 16:26:49 -07:00
|
|
|
|
2023-11-09 14:00:29 -07:00
|
|
|
c.SchemaFilter<IgnoreEnumSchemaFilter>();
|
2020-12-02 14:59:57 -07:00
|
|
|
c.OperationFilter<SecurityRequirementsOperationFilter>();
|
2020-09-01 16:26:49 -07:00
|
|
|
c.OperationFilter<FileResponseFilter>();
|
2021-02-10 16:12:52 -07:00
|
|
|
c.OperationFilter<FileRequestFilter>();
|
2021-01-20 16:24:15 -07:00
|
|
|
c.OperationFilter<ParameterObsoleteFilter>();
|
2021-04-03 05:54:09 -07:00
|
|
|
c.DocumentFilter<AdditionalModelFilter>();
|
2019-11-23 11:43:30 -07:00
|
|
|
});
|
|
|
|
}
|
2020-06-02 10:47:00 -07:00
|
|
|
|
2023-02-08 15:55:26 -07:00
|
|
|
private static void AddPolicy(this AuthorizationOptions authorizationOptions, string policyName, IAuthorizationRequirement authorizationRequirement)
|
|
|
|
{
|
|
|
|
authorizationOptions.AddPolicy(policyName, policy =>
|
|
|
|
{
|
|
|
|
policy.AddAuthenticationSchemes(AuthenticationSchemes.CustomAuthentication).AddRequirements(authorizationRequirement);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-19 03:36:37 -07:00
|
|
|
/// <summary>
|
2022-07-20 00:48:20 -07:00
|
|
|
/// Sets up the proxy configuration based on the addresses/subnets in <paramref name="allowedProxies"/>.
|
2021-01-19 03:36:37 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="config">The <see cref="NetworkConfiguration"/> containing the config settings.</param>
|
2021-01-19 12:29:51 -07:00
|
|
|
/// <param name="allowedProxies">The string array to parse.</param>
|
2021-01-19 03:36:37 -07:00
|
|
|
/// <param name="options">The <see cref="ForwardedHeadersOptions"/> instance.</param>
|
2021-01-19 12:29:51 -07:00
|
|
|
internal static void AddProxyAddresses(NetworkConfiguration config, string[] allowedProxies, ForwardedHeadersOptions options)
|
2021-01-19 03:36:37 -07:00
|
|
|
{
|
2021-01-19 12:29:51 -07:00
|
|
|
for (var i = 0; i < allowedProxies.Length; i++)
|
2021-01-19 03:36:37 -07:00
|
|
|
{
|
2022-07-19 12:28:04 -07:00
|
|
|
if (IPAddress.TryParse(allowedProxies[i], out var addr))
|
2021-01-19 03:36:37 -07:00
|
|
|
{
|
2023-11-10 08:49:40 -07:00
|
|
|
AddIPAddress(config, options, addr, addr.AddressFamily == AddressFamily.InterNetwork ? NetworkConstants.MinimumIPv4PrefixSize : NetworkConstants.MinimumIPv6PrefixSize);
|
2021-01-19 03:36:37 -07:00
|
|
|
}
|
2023-11-10 08:59:45 -07:00
|
|
|
else if (NetworkUtils.TryParseToSubnet(allowedProxies[i], out var subnet))
|
2022-07-20 00:48:20 -07:00
|
|
|
{
|
2023-08-22 12:14:54 -07:00
|
|
|
if (subnet is not null)
|
2022-07-20 00:48:20 -07:00
|
|
|
{
|
2023-02-17 11:27:36 -07:00
|
|
|
AddIPAddress(config, options, subnet.Prefix, subnet.PrefixLength);
|
2022-07-20 00:48:20 -07:00
|
|
|
}
|
|
|
|
}
|
2023-11-10 08:59:45 -07:00
|
|
|
else if (NetworkUtils.TryParseHost(allowedProxies[i], out var addresses, config.EnableIPv4, config.EnableIPv6))
|
2021-01-19 03:36:37 -07:00
|
|
|
{
|
2022-07-21 13:09:54 -07:00
|
|
|
foreach (var address in addresses)
|
2021-01-19 03:36:37 -07:00
|
|
|
{
|
2023-11-10 08:49:40 -07:00
|
|
|
AddIPAddress(config, options, address, address.AddressFamily == AddressFamily.InterNetwork ? NetworkConstants.MinimumIPv4PrefixSize : NetworkConstants.MinimumIPv6PrefixSize);
|
2021-01-19 03:36:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 11:27:36 -07:00
|
|
|
private static void AddIPAddress(NetworkConfiguration config, ForwardedHeadersOptions options, IPAddress addr, int prefixLength)
|
2021-01-19 03:36:37 -07:00
|
|
|
{
|
2023-07-03 12:51:36 -07:00
|
|
|
if (addr.IsIPv4MappedToIPv6)
|
2021-01-19 03:36:37 -07:00
|
|
|
{
|
2023-07-03 12:51:36 -07:00
|
|
|
addr = addr.MapToIPv4();
|
2021-01-19 03:36:37 -07:00
|
|
|
}
|
|
|
|
|
2023-07-03 12:51:36 -07:00
|
|
|
if ((!config.EnableIPv4 && addr.AddressFamily == AddressFamily.InterNetwork) || (!config.EnableIPv6 && addr.AddressFamily == AddressFamily.InterNetworkV6))
|
2022-07-20 02:47:48 -07:00
|
|
|
{
|
2023-07-03 12:51:36 -07:00
|
|
|
return;
|
2022-07-20 02:47:48 -07:00
|
|
|
}
|
|
|
|
|
2023-11-10 08:49:40 -07:00
|
|
|
if (prefixLength == NetworkConstants.MinimumIPv4PrefixSize)
|
2021-01-19 03:36:37 -07:00
|
|
|
{
|
|
|
|
options.KnownProxies.Add(addr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
options.KnownNetworks.Add(new IPNetwork(addr, prefixLength));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 10:47:00 -07:00
|
|
|
private static void AddSwaggerTypeMappings(this SwaggerGenOptions options)
|
|
|
|
{
|
|
|
|
/*
|
2020-11-30 08:47:52 -07:00
|
|
|
* TODO remove when System.Text.Json properly supports non-string keys.
|
|
|
|
* Used in BaseItemDto.ImageBlurHashes
|
2020-06-02 10:47:00 -07:00
|
|
|
*/
|
|
|
|
options.MapType<Dictionary<ImageType, string>>(() =>
|
|
|
|
new OpenApiSchema
|
|
|
|
{
|
|
|
|
Type = "object",
|
2020-11-30 08:47:52 -07:00
|
|
|
AdditionalProperties = new OpenApiSchema
|
|
|
|
{
|
|
|
|
Type = "string"
|
|
|
|
}
|
2020-06-02 10:47:00 -07:00
|
|
|
});
|
2020-06-19 03:24:39 -07:00
|
|
|
|
2020-06-19 06:49:44 -07:00
|
|
|
/*
|
|
|
|
* Support BlurHash dictionary
|
|
|
|
*/
|
2020-06-19 03:24:39 -07:00
|
|
|
options.MapType<Dictionary<ImageType, Dictionary<string, string>>>(() =>
|
|
|
|
new OpenApiSchema
|
|
|
|
{
|
|
|
|
Type = "object",
|
|
|
|
Properties = typeof(ImageType).GetEnumNames().ToDictionary(
|
|
|
|
name => name,
|
2021-08-04 05:40:09 -07:00
|
|
|
_ => new OpenApiSchema
|
2020-06-19 03:24:39 -07:00
|
|
|
{
|
2020-11-30 08:47:52 -07:00
|
|
|
Type = "object",
|
|
|
|
AdditionalProperties = new OpenApiSchema
|
2020-06-19 06:49:44 -07:00
|
|
|
{
|
2020-11-30 08:47:52 -07:00
|
|
|
Type = "string"
|
2020-06-19 06:49:44 -07:00
|
|
|
}
|
2020-06-19 03:24:39 -07:00
|
|
|
})
|
|
|
|
});
|
2021-11-13 07:29:58 -07:00
|
|
|
|
|
|
|
// Support dictionary with nullable string value.
|
|
|
|
options.MapType<Dictionary<string, string?>>(() =>
|
|
|
|
new OpenApiSchema
|
|
|
|
{
|
|
|
|
Type = "object",
|
|
|
|
AdditionalProperties = new OpenApiSchema
|
|
|
|
{
|
|
|
|
Type = "string",
|
|
|
|
Nullable = true
|
|
|
|
}
|
|
|
|
});
|
2022-03-23 07:32:33 -07:00
|
|
|
|
|
|
|
// Manually describe Flags enum.
|
|
|
|
options.MapType<TranscodeReason>(() =>
|
|
|
|
new OpenApiSchema
|
|
|
|
{
|
2022-10-28 19:39:00 -07:00
|
|
|
Type = "array",
|
|
|
|
Items = new OpenApiSchema
|
|
|
|
{
|
|
|
|
Reference = new OpenApiReference
|
|
|
|
{
|
|
|
|
Id = nameof(TranscodeReason),
|
|
|
|
Type = ReferenceType.Schema,
|
|
|
|
}
|
|
|
|
}
|
2022-03-23 07:32:33 -07:00
|
|
|
});
|
2022-05-21 07:59:24 -07:00
|
|
|
|
|
|
|
// Swashbuckle doesn't use JsonOptions to describe responses, so we need to manually describe it.
|
|
|
|
options.MapType<Version>(() => new OpenApiSchema
|
|
|
|
{
|
|
|
|
Type = "string"
|
|
|
|
});
|
2020-06-02 10:47:00 -07:00
|
|
|
}
|
2019-11-23 11:43:30 -07:00
|
|
|
}
|
|
|
|
}
|