From d73a64014203c2e7254771686a5a2e47e7be66eb Mon Sep 17 00:00:00 2001 From: kosipov Date: Sat, 11 Apr 2026 11:52:47 +0300 Subject: [PATCH] Add upstream proxy login logging --- Program.cs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/Program.cs b/Program.cs index a72983b..a3b74db 100644 --- a/Program.cs +++ b/Program.cs @@ -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 ReadHeaderBytes(Stream stream) { const int maxHeaderBytes = 64 * 1024; - var data = new List(4096); - var chunk = new byte[2048]; + var data = new List(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 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 PerformUpstreamConnectHandshake( NetworkStream upstreamStream, string targetHost,