Add upstream proxy login logging
This commit is contained in:
40
Program.cs
40
Program.cs
@@ -117,6 +117,7 @@ class Program
|
|||||||
}
|
}
|
||||||
|
|
||||||
Log.Info($"[conn:{connectionId}] HTTP upstream {proxyUri.Host}:{proxyUri.Port} for {req.Url}");
|
Log.Info($"[conn:{connectionId}] HTTP upstream {proxyUri.Host}:{proxyUri.Port} for {req.Url}");
|
||||||
|
LogUpstreamUser(connectionId, proxyUri.Host);
|
||||||
|
|
||||||
using var upstream = new TcpClient();
|
using var upstream = new TcpClient();
|
||||||
await upstream.ConnectAsync(proxyUri.Host, proxyUri.Port);
|
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}");
|
Log.Info($"[conn:{connectionId}] CONNECT {host}:{port} via {proxyUri.Host}:{proxyUri.Port}");
|
||||||
|
LogUpstreamUser(connectionId, proxyUri.Host);
|
||||||
|
|
||||||
using var upstream = new TcpClient();
|
using var upstream = new TcpClient();
|
||||||
await upstream.ConnectAsync(proxyUri.Host, proxyUri.Port);
|
await upstream.ConnectAsync(proxyUri.Host, proxyUri.Port);
|
||||||
@@ -265,38 +267,28 @@ class Program
|
|||||||
static async Task<byte[]> ReadHeaderBytes(Stream stream)
|
static async Task<byte[]> ReadHeaderBytes(Stream stream)
|
||||||
{
|
{
|
||||||
const int maxHeaderBytes = 64 * 1024;
|
const int maxHeaderBytes = 64 * 1024;
|
||||||
var data = new List<byte>(4096);
|
var data = new List<byte>(1024);
|
||||||
var chunk = new byte[2048];
|
var one = new byte[1];
|
||||||
|
|
||||||
while (data.Count < maxHeaderBytes)
|
while (data.Count < maxHeaderBytes)
|
||||||
{
|
{
|
||||||
int read = await stream.ReadAsync(chunk, 0, chunk.Length);
|
int read = await stream.ReadAsync(one, 0, 1);
|
||||||
if (read <= 0)
|
if (read <= 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
data.AddRange(chunk.AsSpan(0, read).ToArray());
|
data.Add(one[0]);
|
||||||
if (ContainsHeaderTerminator(data))
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return data.Count == 0 ? null : data.ToArray();
|
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 =================
|
// ================= STREAM =================
|
||||||
|
|
||||||
static async Task Pump(Stream from, Stream to, string context)
|
static async Task Pump(Stream from, Stream to, string context)
|
||||||
@@ -456,6 +448,14 @@ class Program
|
|||||||
return user ?? string.Empty;
|
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(
|
static async Task<HttpRequest> PerformUpstreamConnectHandshake(
|
||||||
NetworkStream upstreamStream,
|
NetworkStream upstreamStream,
|
||||||
string targetHost,
|
string targetHost,
|
||||||
|
|||||||
Reference in New Issue
Block a user