Add upstream proxy login logging

This commit is contained in:
2026-04-11 11:52:47 +03:00
parent b9b0974cfa
commit d73a640142

View File

@@ -117,6 +117,7 @@ class Program
}
Log.Info($"[conn:{connectionId}] HTTP upstream {proxyUri.Host}:{proxyUri.Port} for {req.Url}");
LogUpstreamUser(connectionId, proxyUri.Host);
using var upstream = new TcpClient();
await upstream.ConnectAsync(proxyUri.Host, proxyUri.Port);
@@ -152,6 +153,7 @@ class Program
}
Log.Info($"[conn:{connectionId}] CONNECT {host}:{port} via {proxyUri.Host}:{proxyUri.Port}");
LogUpstreamUser(connectionId, proxyUri.Host);
using var upstream = new TcpClient();
await upstream.ConnectAsync(proxyUri.Host, proxyUri.Port);
@@ -265,38 +267,28 @@ class Program
static async Task<byte[]> ReadHeaderBytes(Stream stream)
{
const int maxHeaderBytes = 64 * 1024;
var data = new List<byte>(4096);
var chunk = new byte[2048];
var data = new List<byte>(1024);
var one = new byte[1];
while (data.Count < maxHeaderBytes)
{
int read = await stream.ReadAsync(chunk, 0, chunk.Length);
int read = await stream.ReadAsync(one, 0, 1);
if (read <= 0)
break;
data.AddRange(chunk.AsSpan(0, read).ToArray());
if (ContainsHeaderTerminator(data))
data.Add(one[0]);
int n = data.Count;
if (n >= 4
&& data[n - 4] == (byte)'\r'
&& data[n - 3] == (byte)'\n'
&& data[n - 2] == (byte)'\r'
&& data[n - 1] == (byte)'\n')
break;
}
return data.Count == 0 ? null : data.ToArray();
}
static bool ContainsHeaderTerminator(List<byte> data)
{
if (data.Count < 4) return false;
int n = data.Count;
for (int i = Math.Max(0, n - 4096); i <= n - 4; i++)
{
if (data[i] == (byte)'\r'
&& data[i + 1] == (byte)'\n'
&& data[i + 2] == (byte)'\r'
&& data[i + 3] == (byte)'\n')
return true;
}
return false;
}
// ================= STREAM =================
static async Task Pump(Stream from, Stream to, string context)
@@ -456,6 +448,14 @@ class Program
return user ?? string.Empty;
}
static void LogUpstreamUser(long connectionId, string proxyHost)
{
if (string.Equals(proxyHost, "proxy.syktsu.ru", StringComparison.OrdinalIgnoreCase))
{
Log.Info($"[conn:{connectionId}] upstream auth login for {proxyHost}: '{UPSTREAM_USER}' (password: empty)");
}
}
static async Task<HttpRequest> PerformUpstreamConnectHandshake(
NetworkStream upstreamStream,
string targetHost,