Merge pull request #8598 from Neuheit/ipv4fallback
This commit is contained in:
commit
e1a30a4aef
|
@ -164,7 +164,7 @@ namespace Emby.Dlna.Eventing
|
|||
|
||||
try
|
||||
{
|
||||
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
|
||||
using var response = await _httpClientFactory.CreateClient(NamedClient.DirectIp)
|
||||
.SendAsync(options, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
|
|
120
Jellyfin.Networking/HappyEyeballs/HttpClientExtension.cs
Normal file
120
Jellyfin.Networking/HappyEyeballs/HttpClientExtension.cs
Normal file
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) .NET Foundation and Contributors
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Jellyfin.Networking.HappyEyeballs
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the <see cref="HttpClientExtension"/> class.
|
||||
///
|
||||
/// Implementation taken from https://github.com/ppy/osu-framework/pull/4191 .
|
||||
/// </summary>
|
||||
public static class HttpClientExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the client should use IPv6.
|
||||
/// </summary>
|
||||
public static bool UseIPv6 { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Implements the httpclient callback method.
|
||||
/// </summary>
|
||||
/// <param name="context">The <see cref="SocketsHttpConnectionContext"/> instance.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> instance.</param>
|
||||
/// <returns>The http steam.</returns>
|
||||
public static async ValueTask<Stream> OnConnect(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!UseIPv6)
|
||||
{
|
||||
return await AttemptConnection(AddressFamily.InterNetwork, context, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
using var cancelIPv6 = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
var tryConnectAsyncIPv6 = AttemptConnection(AddressFamily.InterNetworkV6, context, cancelIPv6.Token);
|
||||
|
||||
// GetAwaiter().GetResult() is used instead of .Result as this results in improved exception handling.
|
||||
// The tasks have already been completed.
|
||||
// See https://github.com/dotnet/corefx/pull/29792/files#r189415885 for more details.
|
||||
if (await Task.WhenAny(tryConnectAsyncIPv6, Task.Delay(200, cancelIPv6.Token)).ConfigureAwait(false) == tryConnectAsyncIPv6 && tryConnectAsyncIPv6.IsCompletedSuccessfully)
|
||||
{
|
||||
cancelIPv6.Cancel();
|
||||
return tryConnectAsyncIPv6.GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
using var cancelIPv4 = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
var tryConnectAsyncIPv4 = AttemptConnection(AddressFamily.InterNetwork, context, cancelIPv4.Token);
|
||||
|
||||
if (await Task.WhenAny(tryConnectAsyncIPv6, tryConnectAsyncIPv4).ConfigureAwait(false) == tryConnectAsyncIPv6)
|
||||
{
|
||||
if (tryConnectAsyncIPv6.IsCompletedSuccessfully)
|
||||
{
|
||||
cancelIPv4.Cancel();
|
||||
return tryConnectAsyncIPv6.GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
return tryConnectAsyncIPv4.GetAwaiter().GetResult();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tryConnectAsyncIPv4.IsCompletedSuccessfully)
|
||||
{
|
||||
cancelIPv6.Cancel();
|
||||
return tryConnectAsyncIPv4.GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
return tryConnectAsyncIPv6.GetAwaiter().GetResult();
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<Stream> AttemptConnection(AddressFamily addressFamily, SocketsHttpConnectionContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
// The following socket constructor will create a dual-mode socket on systems where IPV6 is available.
|
||||
var socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp)
|
||||
{
|
||||
// Turn off Nagle's algorithm since it degrades performance in most HttpClient scenarios.
|
||||
NoDelay = true
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
await socket.ConnectAsync(context.DnsEndPoint, cancellationToken).ConfigureAwait(false);
|
||||
// The stream should take the ownership of the underlying socket,
|
||||
// closing it when it's disposed.
|
||||
return new NetworkStream(socket, ownsSocket: true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
socket.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -592,6 +592,7 @@ namespace Jellyfin.Networking.Manager
|
|||
|
||||
IsIP4Enabled = Socket.OSSupportsIPv4 && config.EnableIPV4;
|
||||
IsIP6Enabled = Socket.OSSupportsIPv6 && config.EnableIPV6;
|
||||
HappyEyeballs.HttpClientExtension.UseIPv6 = IsIP6Enabled;
|
||||
|
||||
if (!IsIP6Enabled && !IsIP4Enabled)
|
||||
{
|
||||
|
@ -836,9 +837,19 @@ namespace Jellyfin.Networking.Manager
|
|||
try
|
||||
{
|
||||
await Task.Delay(2000).ConfigureAwait(false);
|
||||
InitialiseInterfaces();
|
||||
// Recalculate LAN caches.
|
||||
InitialiseLAN(_configurationManager.GetNetworkConfiguration());
|
||||
|
||||
var config = _configurationManager.GetNetworkConfiguration();
|
||||
// Have we lost IPv6 capability?
|
||||
if (IsIP6Enabled && !Socket.OSSupportsIPv6)
|
||||
{
|
||||
UpdateSettings(config);
|
||||
}
|
||||
else
|
||||
{
|
||||
InitialiseInterfaces();
|
||||
// Recalculate LAN caches.
|
||||
InitialiseLAN(config);
|
||||
}
|
||||
|
||||
NetworkChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ using System.Text;
|
|||
using Jellyfin.Api.Middleware;
|
||||
using Jellyfin.MediaEncoding.Hls.Extensions;
|
||||
using Jellyfin.Networking.Configuration;
|
||||
using Jellyfin.Networking.HappyEyeballs;
|
||||
using Jellyfin.Server.Extensions;
|
||||
using Jellyfin.Server.HealthChecks;
|
||||
using Jellyfin.Server.Implementations;
|
||||
|
@ -26,6 +27,7 @@ using Microsoft.Extensions.Configuration;
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.VisualBasic;
|
||||
using Prometheus;
|
||||
|
||||
namespace Jellyfin.Server
|
||||
|
@ -78,6 +80,13 @@ namespace Jellyfin.Server
|
|||
var acceptJsonHeader = new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json, 1.0);
|
||||
var acceptXmlHeader = new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Xml, 0.9);
|
||||
var acceptAnyHeader = new MediaTypeWithQualityHeaderValue("*/*", 0.8);
|
||||
Func<IServiceProvider, HttpMessageHandler> eyeballsHttpClientHandlerDelegate = (_) => new SocketsHttpHandler()
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.All,
|
||||
RequestHeaderEncodingSelector = (_, _) => Encoding.UTF8,
|
||||
ConnectCallback = HttpClientExtension.OnConnect
|
||||
};
|
||||
|
||||
Func<IServiceProvider, HttpMessageHandler> defaultHttpClientHandlerDelegate = (_) => new SocketsHttpHandler()
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.All,
|
||||
|
@ -91,7 +100,7 @@ namespace Jellyfin.Server
|
|||
c.DefaultRequestHeaders.Accept.Add(acceptXmlHeader);
|
||||
c.DefaultRequestHeaders.Accept.Add(acceptAnyHeader);
|
||||
})
|
||||
.ConfigurePrimaryHttpMessageHandler(defaultHttpClientHandlerDelegate);
|
||||
.ConfigurePrimaryHttpMessageHandler(eyeballsHttpClientHandlerDelegate);
|
||||
|
||||
services.AddHttpClient(NamedClient.MusicBrainz, c =>
|
||||
{
|
||||
|
@ -100,6 +109,15 @@ namespace Jellyfin.Server
|
|||
c.DefaultRequestHeaders.Accept.Add(acceptXmlHeader);
|
||||
c.DefaultRequestHeaders.Accept.Add(acceptAnyHeader);
|
||||
})
|
||||
.ConfigurePrimaryHttpMessageHandler(eyeballsHttpClientHandlerDelegate);
|
||||
|
||||
services.AddHttpClient(NamedClient.DirectIp, c =>
|
||||
{
|
||||
c.DefaultRequestHeaders.UserAgent.Add(productHeader);
|
||||
c.DefaultRequestHeaders.Accept.Add(acceptJsonHeader);
|
||||
c.DefaultRequestHeaders.Accept.Add(acceptXmlHeader);
|
||||
c.DefaultRequestHeaders.Accept.Add(acceptAnyHeader);
|
||||
})
|
||||
.ConfigurePrimaryHttpMessageHandler(defaultHttpClientHandlerDelegate);
|
||||
|
||||
services.AddHttpClient(NamedClient.Dlna, c =>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace MediaBrowser.Common.Net
|
||||
namespace MediaBrowser.Common.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// Registered http client names.
|
||||
|
@ -6,7 +6,7 @@
|
|||
public static class NamedClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the value for the default named http client.
|
||||
/// Gets the value for the default named http client which implements happy eyeballs.
|
||||
/// </summary>
|
||||
public const string Default = nameof(Default);
|
||||
|
||||
|
@ -19,5 +19,10 @@
|
|||
/// Gets the value for the DLNA named http client.
|
||||
/// </summary>
|
||||
public const string Dlna = nameof(Dlna);
|
||||
|
||||
/// <summary>
|
||||
/// Non happy eyeballs implementation.
|
||||
/// </summary>
|
||||
public const string DirectIp = nameof(DirectIp);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user