From 6a945bb6adae42e9eca58b3752611d1f59c3ce30 Mon Sep 17 00:00:00 2001 From: kosipov Date: Thu, 9 Apr 2026 20:09:26 +0300 Subject: [PATCH] Ensure requests use system proxy --- Program.cs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/Program.cs b/Program.cs index f914d02..809f5f6 100644 --- a/Program.cs +++ b/Program.cs @@ -160,7 +160,8 @@ class Program if (resp == null || !resp.Raw.Contains("200")) { - Log.Error($"[conn:{connectionId}] upstream CONNECT rejected"); + var status = resp?.Raw?.Split("\r\n", StringSplitOptions.None).FirstOrDefault() ?? ""; + Log.Error($"[conn:{connectionId}] upstream CONNECT rejected: {status}"); await clientStream.WriteAsync(Encoding.ASCII.GetBytes("HTTP/1.1 502 Bad Gateway\r\n\r\n")); return; } @@ -272,13 +273,42 @@ class Program } catch (Exception ex) { - Log.Error($"[{context}] stream pump failed", ex); + if (IsExpectedDisconnect(ex)) + Log.Info($"[{context}] stream closed: {OneLine(ex.Message)}"); + else + Log.Error($"[{context}] stream pump failed", ex); } finally { ArrayPool.Shared.Return(buffer); } } + + static bool IsExpectedDisconnect(Exception ex) + { + if (ex is ObjectDisposedException) + return true; + if (ex is OperationCanceledException) + return true; + + if (ex is IOException io && io.InnerException is SocketException se) + { + return se.SocketErrorCode == SocketError.ConnectionAborted + || se.SocketErrorCode == SocketError.ConnectionReset + || se.SocketErrorCode == SocketError.Shutdown + || se.SocketErrorCode == SocketError.OperationAborted + || se.SocketErrorCode == SocketError.TimedOut; + } + + return false; + } + + static string OneLine(string message) + { + if (string.IsNullOrEmpty(message)) + return string.Empty; + return message.Replace("\r", " ").Replace("\n", " ").Trim(); + } } // ================= WINHTTP =================