Use Windows user for upstream proxy
This commit is contained in:
28
Program.cs
28
Program.cs
@@ -14,12 +14,8 @@ class Program
|
|||||||
{
|
{
|
||||||
static string USER = "user";
|
static string USER = "user";
|
||||||
static string PASS = "pass";
|
static string PASS = "pass";
|
||||||
static readonly string UPSTREAM_USER = Environment.GetEnvironmentVariable("UPSTREAM_USER");
|
static readonly string UPSTREAM_USER = ResolveUpstreamUser();
|
||||||
static readonly string UPSTREAM_PASS = Environment.GetEnvironmentVariable("UPSTREAM_PASS");
|
static readonly string UPSTREAM_PASS = Environment.GetEnvironmentVariable("UPSTREAM_PASS") ?? string.Empty;
|
||||||
static readonly bool UPSTREAM_ALLOW_EMPTY_PASSWORD = string.Equals(
|
|
||||||
Environment.GetEnvironmentVariable("UPSTREAM_ALLOW_EMPTY_PASSWORD"),
|
|
||||||
"1",
|
|
||||||
StringComparison.OrdinalIgnoreCase);
|
|
||||||
static readonly string UPSTREAM_PROXY_AUTH_HEADER = BuildUpstreamProxyAuthHeader();
|
static readonly string UPSTREAM_PROXY_AUTH_HEADER = BuildUpstreamProxyAuthHeader();
|
||||||
static long _connectionSeq;
|
static long _connectionSeq;
|
||||||
|
|
||||||
@@ -32,6 +28,8 @@ class Program
|
|||||||
Log.Info($"http://127.0.0.1:{port}");
|
Log.Info($"http://127.0.0.1:{port}");
|
||||||
Log.Info($"login: {USER}");
|
Log.Info($"login: {USER}");
|
||||||
Log.Info($"pass : {PASS}");
|
Log.Info($"pass : {PASS}");
|
||||||
|
Log.Info($"upstream auth user: {UPSTREAM_USER}");
|
||||||
|
Log.Info("upstream auth pass: <empty>");
|
||||||
|
|
||||||
var listener = new TcpListener(IPAddress.Any, port);
|
var listener = new TcpListener(IPAddress.Any, port);
|
||||||
listener.Start();
|
listener.Start();
|
||||||
@@ -365,11 +363,8 @@ class Program
|
|||||||
static string BuildUpstreamProxyAuthHeader()
|
static string BuildUpstreamProxyAuthHeader()
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(UPSTREAM_USER))
|
if (string.IsNullOrWhiteSpace(UPSTREAM_USER))
|
||||||
return string.Empty;
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(UPSTREAM_PASS) && !UPSTREAM_ALLOW_EMPTY_PASSWORD)
|
|
||||||
{
|
{
|
||||||
Log.Info("UPSTREAM_PASS is empty; upstream Basic auth header is disabled");
|
Log.Warn("UPSTREAM_USER is empty; upstream Proxy-Authorization is disabled");
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -377,6 +372,19 @@ class Program
|
|||||||
string encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{UPSTREAM_USER}:{pass}"));
|
string encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{UPSTREAM_USER}:{pass}"));
|
||||||
return $"Proxy-Authorization: Basic {encoded}\r\n";
|
return $"Proxy-Authorization: Basic {encoded}\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static string ResolveUpstreamUser()
|
||||||
|
{
|
||||||
|
var fromEnv = Environment.GetEnvironmentVariable("UPSTREAM_USER");
|
||||||
|
if (!string.IsNullOrWhiteSpace(fromEnv))
|
||||||
|
return fromEnv;
|
||||||
|
|
||||||
|
var domain = Environment.UserDomainName;
|
||||||
|
var user = Environment.UserName;
|
||||||
|
if (!string.IsNullOrWhiteSpace(domain) && !string.IsNullOrWhiteSpace(user))
|
||||||
|
return $"{domain}\\{user}";
|
||||||
|
return user ?? string.Empty;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= WINHTTP =================
|
// ================= WINHTTP =================
|
||||||
|
|||||||
Reference in New Issue
Block a user