Обновить Program.cs
This commit is contained in:
63
Program.cs
63
Program.cs
@@ -236,21 +236,72 @@ class WinHttpProxy : IWebProxy
|
|||||||
static class WinHttpHelper
|
static class WinHttpHelper
|
||||||
{
|
{
|
||||||
[DllImport("winhttp.dll", SetLastError = true)]
|
[DllImport("winhttp.dll", SetLastError = true)]
|
||||||
static extern bool WinHttpGetIEProxyConfigForCurrentUser(out WINHTTP_CURRENT_USER_IE_PROXY_CONFIG config);
|
static extern bool WinHttpGetIEProxyConfigForCurrentUser(
|
||||||
|
out WINHTTP_CURRENT_USER_IE_PROXY_CONFIG config);
|
||||||
|
|
||||||
public static Uri GetProxyForUrl(Uri url)
|
public static Uri GetProxyForUrl(Uri url)
|
||||||
{
|
{
|
||||||
WinHttpGetIEProxyConfigForCurrentUser(out var cfg);
|
if (!WinHttpGetIEProxyConfigForCurrentUser(out var cfg))
|
||||||
|
return url;
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(cfg.lpszProxy))
|
string proxy = PtrToString(cfg.lpszProxy);
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(proxy))
|
||||||
|
return url;
|
||||||
|
|
||||||
|
string selectedProxy = ParseProxy(proxy, url.Scheme);
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(selectedProxy))
|
||||||
|
return url;
|
||||||
|
|
||||||
|
return new Uri(NormalizeProxy(selectedProxy));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===== helpers =====
|
||||||
|
|
||||||
|
static string PtrToString(IntPtr ptr)
|
||||||
{
|
{
|
||||||
var parts = cfg.lpszProxy.Split(':');
|
if (ptr == IntPtr.Zero)
|
||||||
return new Uri($"http://{parts[0]}:{parts[1]}");
|
return null;
|
||||||
|
|
||||||
|
return Marshal.PtrToStringUni(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return url; // direct
|
static string ParseProxy(string proxy, string scheme)
|
||||||
|
{
|
||||||
|
// варианты:
|
||||||
|
// "proxy:8080"
|
||||||
|
// "http=proxy:8080;https=proxy2:8080"
|
||||||
|
|
||||||
|
if (!proxy.Contains("="))
|
||||||
|
return proxy;
|
||||||
|
|
||||||
|
var entries = proxy.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
foreach (var entry in entries)
|
||||||
|
{
|
||||||
|
var parts = entry.Split('=', 2);
|
||||||
|
if (parts.Length != 2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (parts[0].Equals(scheme, StringComparison.OrdinalIgnoreCase))
|
||||||
|
return parts[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fallback — берём первый
|
||||||
|
return entries[0].Split('=')[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
static string NormalizeProxy(string proxy)
|
||||||
|
{
|
||||||
|
if (!proxy.StartsWith("http://") && !proxy.StartsWith("https://"))
|
||||||
|
return "http://" + proxy;
|
||||||
|
|
||||||
|
return proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===== struct =====
|
||||||
|
|
||||||
struct WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
|
struct WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
|
||||||
{
|
{
|
||||||
public bool fAutoDetect;
|
public bool fAutoDetect;
|
||||||
|
|||||||
Reference in New Issue
Block a user