Skip to content

Commit 066391d

Browse files
committed
detect non-ssl proxy tunnel
1 parent 36e30df commit 066391d

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal sealed partial class HttpConnection : HttpConnectionBase
4343
private static readonly ulong s_http11Bytes = BitConverter.ToUInt64("HTTP/1.1"u8);
4444

4545
private readonly HttpConnectionPool _pool;
46-
private readonly Stream _stream;
46+
internal readonly Stream _stream;
4747
private readonly TransportContext? _transportContext;
4848

4949
private HttpRequestMessage? _currentRequest;

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,12 @@ public ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool
15651565
case HttpConnectionKind.ProxyTunnel:
15661566
case HttpConnectionKind.SslProxyTunnel:
15671567
stream = await EstablishProxyTunnelAsync(async, cancellationToken).ConfigureAwait(false);
1568+
1569+
if (stream is HttpContentStream contentStream && contentStream._connection?._stream is Stream innerStream)
1570+
{
1571+
remoteEndPoint = GetRemoteEndPoint(innerStream);
1572+
}
1573+
15681574
break;
15691575

15701576
case HttpConnectionKind.SocksTunnel:

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpContentStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace System.Net.Http
55
{
66
internal abstract class HttpContentStream : HttpBaseStream
77
{
8-
protected HttpConnection? _connection;
8+
protected internal HttpConnection? _connection;
99

1010
public HttpContentStream(HttpConnection connection)
1111
{

src/libraries/System.Net.Http/tests/FunctionalTests/TelemetryTest.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,52 @@ await GetFactoryForVersion(version).CreateServerAsync((originalServer, originalU
824824
}, UseVersion.ToString()).Dispose();
825825
}
826826

827+
[OuterLoop]
828+
[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
829+
[InlineData(false)]
830+
[InlineData(true)]
831+
public void EventSource_Proxy_LogsIPAddress(bool useSsl)
832+
{
833+
if (UseVersion.Major == 3)
834+
{
835+
return;
836+
}
837+
838+
RemoteExecutor.Invoke(static async (string useVersionString, string useSslString) =>
839+
{
840+
using var listener = new TestEventListener("System.Net.Http", EventLevel.Verbose, eventCounterInterval: 0.1d);
841+
listener.AddActivityTracking();
842+
var events = new ConcurrentQueue<(EventWrittenEventArgs Event, Guid ActivityId)>();
843+
844+
await listener.RunWithCallbackAsync(e => events.Enqueue((e, e.ActivityId)), async () =>
845+
{
846+
using LoopbackProxyServer proxyServer = LoopbackProxyServer.Create();
847+
848+
await LoopbackServer.CreateClientAndServerAsync(async uri =>
849+
{
850+
using (HttpClientHandler handler = CreateHttpClientHandler(useVersionString))
851+
using (HttpClient client = CreateHttpClient(handler, useVersionString))
852+
{
853+
handler.Proxy = new WebProxy(proxyServer.Uri);
854+
await client.GetAsync(uri);
855+
}
856+
}, server => server.HandleRequestAsync(), options: new LoopbackServer.Options() { UseSsl = bool.Parse(useSslString) });
857+
858+
await WaitForEventCountersAsync(events);
859+
});
860+
861+
EventWrittenEventArgs[] connectionsEstablishedEvents = events.Select(e => e.Event).Where(e => e.EventName == "ConnectionEstablished").ToArray();
862+
863+
foreach (EventWrittenEventArgs e in connectionsEstablishedEvents)
864+
{
865+
IPAddress ip = IPAddress.Parse((string)e.Payload[6]);
866+
Assert.True(ip.Equals(IPAddress.Loopback.MapToIPv6()) ||
867+
ip.Equals(IPAddress.Loopback) ||
868+
ip.Equals(IPAddress.IPv6Loopback));
869+
}
870+
}, UseVersion.ToString(), useSsl.ToString()).Dispose();
871+
}
872+
827873
protected static async Task WaitForEventCountersAsync(ConcurrentQueue<(EventWrittenEventArgs Event, Guid ActivityId)> events)
828874
{
829875
DateTime startTime = DateTime.UtcNow;

0 commit comments

Comments
 (0)