Ensure requests use system proxy
This commit is contained in:
34
Program.cs
34
Program.cs
@@ -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 =================
|
||||||
|
|||||||
Reference in New Issue
Block a user