Ensure requests use system proxy

This commit is contained in:
2026-04-09 20:09:26 +03:00
parent 4503b2a1c1
commit 6a945bb6ad

View File

@@ -160,7 +160,8 @@ class Program
if (resp == null || !resp.Raw.Contains("200")) if (resp == null || !resp.Raw.Contains("200"))
{ {
Log.Error($"[conn:{connectionId}] upstream CONNECT rejected"); var status = resp?.Raw?.Split("\r\n", StringSplitOptions.None).FirstOrDefault() ?? "<no response>";
Log.Error($"[conn:{connectionId}] upstream CONNECT rejected: {status}");
await clientStream.WriteAsync(Encoding.ASCII.GetBytes("HTTP/1.1 502 Bad Gateway\r\n\r\n")); await clientStream.WriteAsync(Encoding.ASCII.GetBytes("HTTP/1.1 502 Bad Gateway\r\n\r\n"));
return; return;
} }
@@ -272,13 +273,42 @@ class Program
} }
catch (Exception ex) 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 finally
{ {
ArrayPool<byte>.Shared.Return(buffer); ArrayPool<byte>.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 ================= // ================= WINHTTP =================