#region ========================================================================= USING =====================================================================================
using Lumina.Plugins.Contracts.Common.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Infrastructure.Common.Networking;
///
/// Creates the primary message handler used by the outbound HTTP clients, whose connect callback resolves each host and connects only to an address that was
/// checked to be public, so that a name that changes its answer between the check and the connection cannot make the host reach an internal address.
///
internal static class PublicHostConnectionGuard
{
private const int ATTEMPT_STAGGER_IN_MILLISECONDS = 250;
///
/// Creates the primary message handler of an outbound HTTP client, which follows no redirects and validates the address it connects to.
///
/// The validator that decides whether a host is public.
/// The logger that records the rejected connections.
/// The primary message handler of the client.
///
/// Automatic redirects are disabled because a redirect is a server controlled instruction, which the clients that follow one validate hop by hop instead.
/// Proxy use is disabled as well, because a proxy would make the connection to an address other than the target of the request, which could not be pinned to
/// the address that was checked. Every connection is therefore direct, so the connect callback is the sole and authoritative address check.
///
public static SocketsHttpHandler CreatePrimaryHandler(PublicHostValidator publicHostValidator, ILogger logger)
{
return new SocketsHttpHandler
{
// Redirects are handled by PublicHostRedirectHandler, which validates every hop. The primary handler must not follow one on its own.
AllowAutoRedirect = false,
// A proxy would connect to an address other than the validated target, so the guard requires direct connections.
UseProxy = false,
ConnectCallback = (connectionContext, cancellationToken) => ConnectAsync(
publicHostValidator,
connectionContext.DnsEndPoint.Host,
connectionContext.DnsEndPoint.Port,
connectionContext.InitialRequestMessage,
logger,
cancellationToken)
};
}
///
/// Resolves the host and connects to the first of its validated addresses that accepts the connection.
///
/// The validator that decides whether a host is public.
/// The host to connect to.
/// The port to connect to.
/// The request that triggered the connection, when it is known.
/// The logger that records the rejected connections.
/// Cancellation token that can be used to stop the execution.
/// The stream of the established connection.
/// The host could not be resolved.
/// The host resolves to an address that is not allowed, or none of its addresses accepts the connection.
internal static async ValueTask ConnectAsync(
PublicHostValidator publicHostValidator,
string host,
int port,
HttpRequestMessage? request,
ILogger logger,
CancellationToken cancellationToken)
{
try
{
// Resolve and validate the addresses before any socket is opened, so the connection can only target a checked endpoint.
IReadOnlyList endPoints = await publicHostValidator.ResolveEndPointsAsync(host, port, GetAllowedPrivateHost(request), cancellationToken).ConfigureAwait(false);
return await ConnectToFirstEndPointAsync(endPoints, cancellationToken).ConfigureAwait(false);
}
catch (PublicHostRejectedException exception)
{
// The address policy refused the target. This is deterministic, so it is logged as a rejection rather than as a transport failure.
logger.LogWarning("Rejected the outbound connection to the host '{Host}' because {RejectionReason}. The connection was not made.", host, exception.RejectionReason);
throw;
}
catch (PublicHostResolutionException)
{
// The host name could not be resolved. This is reported separately from a policy rejection because it can be transient.
logger.LogWarning("Could not resolve the outbound host '{Host}'. The connection was not made.", host);
throw;
}
}
///
/// Gets the single host the request was explicitly allowed to reach private or local addresses on, which is how a user opted into a provider that runs on the
/// local network, like a self hosted MusicBrainz instance.
///
/// The request that triggered the connection, when it is known.
/// The host that may be private, or when no such permission was granted.
private static string? GetAllowedPrivateHost(HttpRequestMessage? request)
{
if (request is not null && request.Options.TryGetValue(OutboundRequestOptions.AllowedPrivateHost, out string? allowedPrivateHost))
return allowedPrivateHost;
return null;
}
///
/// Connects to the first endpoint that accepts the connection, starting the next address shortly after the previous one, instead of waiting for it to fail,
/// so an address that silently drops the connection cannot delay the others by the operating system timeout.
///
/// The validated endpoints of the host.
/// Cancellation token that can be used to stop the execution.
/// The stream of the established connection.
/// None of the endpoints accepts the connection.
private static async ValueTask ConnectToFirstEndPointAsync(IReadOnlyList endPoints, CancellationToken cancellationToken)
{
// Every attempt shares this token, so the winner can cancel the attempts that are still running, while the caller keeps the ability to cancel all of them.
using CancellationTokenSource attemptCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
List> attempts = [];
// Each address starts a little later than the previous one, so a blackholed address cannot delay the next one by the whole operating system timeout.
for (int index = 0; index < endPoints.Count; index++)
attempts.Add(ConnectToEndPointAsync(endPoints[index], index, attemptCancellation.Token));
Exception? lastException = null;
try
{
// The first attempt that succeeds wins. A failed attempt is removed and the loop keeps waiting for the remaining ones.
while (attempts.Count > 0)
{
Task completedAttempt = await Task.WhenAny(attempts).ConfigureAwait(false);
// Remove the completed attempt before the cleanup below, so only the attempts that are still running are disposed there.
attempts.Remove(completedAttempt);
try
{
Stream stream = await completedAttempt.ConfigureAwait(false);
// A connection was established. Cancel and dispose every attempt that lost the race before returning the winner's stream.
attemptCancellation.Cancel();
await DisposeUnusedAttemptsAsync(attempts).ConfigureAwait(false);
attempts.Clear();
return stream;
}
catch (Exception exception) when (exception is SocketException or OperationCanceledException)
{
// One unreachable address must not stop the next one from being tried, but a cancellation of the whole request must propagate.
if (cancellationToken.IsCancellationRequested)
throw;
lastException = exception;
}
}
}
finally
{
// An unexpected exception, or a cancellation, must not leave the other attempts running or their sockets open.
if (attempts.Count > 0)
{
attemptCancellation.Cancel();
await DisposeUnusedAttemptsAsync(attempts).ConfigureAwait(false);
}
}
// Every validated address was tried and none of them accepted the connection.
throw new HttpRequestException("None of the validated addresses of the host accepted the connection.", lastException);
}
///
/// Connects to a single endpoint, after waiting for its position in the staggered attempts.
///
/// The endpoint to connect to.
/// The zero based position of the attempt among the endpoints of the host.
/// Cancellation token that can be used to stop the execution.
/// The stream of the established connection.
private static async Task ConnectToEndPointAsync(IPEndPoint endPoint, int attemptIndex, CancellationToken cancellationToken)
{
// The first address connects immediately. Each later address waits for its turn in the stagger, giving the earlier address a short head start.
if (attemptIndex > 0)
await Task.Delay(ATTEMPT_STAGGER_IN_MILLISECONDS * attemptIndex, cancellationToken).ConfigureAwait(false);
Socket socket = new(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
try
{
await socket.ConnectAsync(endPoint, cancellationToken).ConfigureAwait(false);
return new NetworkStream(socket, ownsSocket: true);
}
catch
{
// Whether the attempt failed or was canceled by a winner, its socket must be released before the exception continues.
socket.Dispose();
throw;
}
}
///
/// Disposes the connections established by the attempts that lost the race, and observes their failures so no exception is left unhandled.
///
/// The attempts that are no longer needed.
/// A task that completes when every attempt was observed.
private static async Task DisposeUnusedAttemptsAsync(List> attempts)
{
foreach (Task attempt in attempts)
{
try
{
// A losing attempt can still have connected just before it was canceled, so its stream must be disposed too.
Stream stream = await attempt.ConfigureAwait(false);
stream.Dispose();
}
catch
{
// The attempt failed or was canceled, so it has no stream to dispose. Its exception is expected and must not mask the winner.
}
}
}
}