Merge pull request #6486 from fredriklindberg/support-forwarded-headers-for-api-url

This commit is contained in:
Claus Vium 2021-10-25 22:41:32 +02:00 committed by GitHub
commit 5eda5eb636
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 11 deletions

View File

@ -1067,9 +1067,9 @@ namespace Emby.Server.Implementations
/// <summary> /// <summary>
/// Gets the system status. /// Gets the system status.
/// </summary> /// </summary>
/// <param name="source">Where this request originated.</param> /// <param name="request">Where this request originated.</param>
/// <returns>SystemInfo.</returns> /// <returns>SystemInfo.</returns>
public SystemInfo GetSystemInfo(IPAddress source) public SystemInfo GetSystemInfo(HttpRequest request)
{ {
return new SystemInfo return new SystemInfo
{ {
@ -1091,7 +1091,7 @@ namespace Emby.Server.Implementations
CanLaunchWebBrowser = CanLaunchWebBrowser, CanLaunchWebBrowser = CanLaunchWebBrowser,
TranscodingTempPath = ConfigurationManager.GetTranscodePath(), TranscodingTempPath = ConfigurationManager.GetTranscodePath(),
ServerName = FriendlyName, ServerName = FriendlyName,
LocalAddress = GetSmartApiUrl(source), LocalAddress = GetSmartApiUrl(request),
SupportsLibraryMonitor = true, SupportsLibraryMonitor = true,
SystemArchitecture = RuntimeInformation.OSArchitecture, SystemArchitecture = RuntimeInformation.OSArchitecture,
PackageName = _startupOptions.PackageName PackageName = _startupOptions.PackageName
@ -1103,7 +1103,7 @@ namespace Emby.Server.Implementations
.Select(i => new WakeOnLanInfo(i)) .Select(i => new WakeOnLanInfo(i))
.ToList(); .ToList();
public PublicSystemInfo GetPublicSystemInfo(IPAddress address) public PublicSystemInfo GetPublicSystemInfo(HttpRequest request)
{ {
return new PublicSystemInfo return new PublicSystemInfo
{ {
@ -1112,7 +1112,7 @@ namespace Emby.Server.Implementations
Id = SystemId, Id = SystemId,
OperatingSystem = MediaBrowser.Common.System.OperatingSystem.Id.ToString(), OperatingSystem = MediaBrowser.Common.System.OperatingSystem.Id.ToString(),
ServerName = FriendlyName, ServerName = FriendlyName,
LocalAddress = GetSmartApiUrl(address), LocalAddress = GetSmartApiUrl(request),
StartupWizardCompleted = ConfigurationManager.CommonConfiguration.IsStartupWizardCompleted StartupWizardCompleted = ConfigurationManager.CommonConfiguration.IsStartupWizardCompleted
}; };
} }
@ -1140,6 +1140,18 @@ namespace Emby.Server.Implementations
/// <inheritdoc/> /// <inheritdoc/>
public string GetSmartApiUrl(HttpRequest request, int? port = null) public string GetSmartApiUrl(HttpRequest request, int? port = null)
{ {
// Return the host in the HTTP request as the API url
if (ConfigurationManager.GetNetworkConfiguration().EnablePublishedServerUriByRequest)
{
int? requestPort = request.Host.Port;
if ((requestPort == 80 && string.Equals(request.Scheme, "http", StringComparison.OrdinalIgnoreCase)) || (requestPort == 443 && string.Equals(request.Scheme, "https", StringComparison.OrdinalIgnoreCase)))
{
requestPort = -1;
}
return GetLocalApiUrl(request.Host.Host, request.Scheme, requestPort);
}
// Published server ends with a / // Published server ends with a /
if (!string.IsNullOrEmpty(PublishedServerUrl)) if (!string.IsNullOrEmpty(PublishedServerUrl))
{ {

View File

@ -66,7 +66,7 @@ namespace Jellyfin.Api.Controllers
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<SystemInfo> GetSystemInfo() public ActionResult<SystemInfo> GetSystemInfo()
{ {
return _appHost.GetSystemInfo(Request.HttpContext.Connection.RemoteIpAddress ?? IPAddress.Loopback); return _appHost.GetSystemInfo(Request);
} }
/// <summary> /// <summary>
@ -78,7 +78,7 @@ namespace Jellyfin.Api.Controllers
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<PublicSystemInfo> GetPublicSystemInfo() public ActionResult<PublicSystemInfo> GetPublicSystemInfo()
{ {
return _appHost.GetPublicSystemInfo(Request.HttpContext.Connection.RemoteIpAddress ?? IPAddress.Loopback); return _appHost.GetPublicSystemInfo(Request);
} }
/// <summary> /// <summary>

View File

@ -226,5 +226,10 @@ namespace Jellyfin.Networking.Configuration
/// Gets or sets the known proxies. If the proxy is a network, it's added to the KnownNetworks. /// Gets or sets the known proxies. If the proxy is a network, it's added to the KnownNetworks.
/// </summary> /// </summary>
public string[] KnownProxies { get; set; } = Array.Empty<string>(); public string[] KnownProxies { get; set; } = Array.Empty<string>();
/// <summary>
/// Gets or sets a value indicating whether the published server uri is based on information in HTTP requests.
/// </summary>
public bool EnablePublishedServerUriByRequest { get; set; } = false;
} }
} }

View File

@ -188,7 +188,8 @@ namespace Jellyfin.Server.Extensions
// https://github.com/dotnet/aspnetcore/blob/master/src/Middleware/HttpOverrides/src/ForwardedHeadersMiddleware.cs // 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. // Enable debug logging on Microsoft.AspNetCore.HttpOverrides.ForwardedHeadersMiddleware to help investigate issues.
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost;
if (config.KnownProxies.Length == 0) if (config.KnownProxies.Length == 0)
{ {
options.KnownNetworks.Clear(); options.KnownNetworks.Clear();

View File

@ -50,11 +50,11 @@ namespace MediaBrowser.Controller
/// <summary> /// <summary>
/// Gets the system info. /// Gets the system info.
/// </summary> /// </summary>
/// <param name="source">The originator of the request.</param> /// <param name="request">The HTTP request.</param>
/// <returns>SystemInfo.</returns> /// <returns>SystemInfo.</returns>
SystemInfo GetSystemInfo(IPAddress source); SystemInfo GetSystemInfo(HttpRequest request);
PublicSystemInfo GetPublicSystemInfo(IPAddress address); PublicSystemInfo GetPublicSystemInfo(HttpRequest request);
/// <summary> /// <summary>
/// Gets a URL specific for the request. /// Gets a URL specific for the request.