Ensure requests use system proxy
This commit is contained in:
32
Program.cs
32
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() ?? "<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"));
|
||||
return;
|
||||
}
|
||||
@@ -272,6 +273,9 @@ class Program
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (IsExpectedDisconnect(ex))
|
||||
Log.Info($"[{context}] stream closed: {OneLine(ex.Message)}");
|
||||
else
|
||||
Log.Error($"[{context}] stream pump failed", ex);
|
||||
}
|
||||
finally
|
||||
@@ -279,6 +283,32 @@ class Program
|
||||
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 =================
|
||||
|
||||
Reference in New Issue
Block a user