| | | 1 | | using System.Net; |
| | | 2 | | using System.Net.Sockets; |
| | | 3 | | using Elsa.ExternalAuthentication.Validation; |
| | | 4 | | |
| | | 5 | | namespace Elsa.ExternalAuthentication.Services; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Pins each outbound connection to an address that was resolved and validated at connection time. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class ValidatedOutboundConnectionFactory(OutboundDestinationValidator destinationValidator, IValidatedAddr |
| | | 11 | | { |
| | | 12 | | public async ValueTask<Stream> ConnectAsync(DnsEndPoint endpoint, CancellationToken cancellationToken = default) |
| | | 13 | | { |
| | | 14 | | var address = await destinationValidator.ResolveApprovedAddressAsync(endpoint, cancellationToken); |
| | | 15 | | return await connector.ConnectAsync(address, endpoint.Port, cancellationToken); |
| | | 16 | | } |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | public interface IValidatedAddressConnector |
| | | 20 | | { |
| | | 21 | | ValueTask<Stream> ConnectAsync(IPAddress address, int port, CancellationToken cancellationToken = default); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | public sealed class SocketValidatedAddressConnector : IValidatedAddressConnector |
| | | 25 | | { |
| | | 26 | | public async ValueTask<Stream> ConnectAsync(IPAddress address, int port, CancellationToken cancellationToken = defau |
| | | 27 | | { |
| | 0 | 28 | | var socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); |
| | | 29 | | try |
| | | 30 | | { |
| | 0 | 31 | | await socket.ConnectAsync(new IPEndPoint(address, port), cancellationToken); |
| | 0 | 32 | | return new NetworkStream(socket, ownsSocket: true); |
| | | 33 | | } |
| | 0 | 34 | | catch |
| | | 35 | | { |
| | 0 | 36 | | socket.Dispose(); |
| | 0 | 37 | | throw; |
| | | 38 | | } |
| | 0 | 39 | | } |
| | | 40 | | } |