Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WinInetProxyHelper.cs
Go to the documentation of this file.
3
4namespace System.Net.Http;
5
6internal sealed class WinInetProxyHelper
7{
8 private readonly string _autoConfigUrl;
9
10 private readonly string _proxy;
11
12 private readonly string _proxyBypass;
13
14 private readonly bool _autoDetect;
15
16 private readonly bool _useProxy;
17
19
21
22 public string AutoConfigUrl => _autoConfigUrl;
23
24 public bool AutoDetect => _autoDetect;
25
26 public bool AutoSettingsUsed
27 {
28 get
29 {
30 if (!AutoDetect)
31 {
32 return !string.IsNullOrEmpty(AutoConfigUrl);
33 }
34 return true;
35 }
36 }
37
38 public bool ManualSettingsUsed => !string.IsNullOrEmpty(Proxy);
39
41 {
42 get
43 {
45 {
46 return ManualSettingsUsed;
47 }
48 return false;
49 }
50 }
51
52 public string Proxy => _proxy;
53
54 public string ProxyBypass => _proxyBypass;
55
57 {
58 get
59 {
61 {
62 return Environment.TickCount - _lastTimeAutoDetectionFailed <= 120000;
63 }
64 return false;
65 }
66 }
67
69 {
70 global::Interop.WinHttp.WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyConfig = default(global::Interop.WinHttp.WINHTTP_CURRENT_USER_IE_PROXY_CONFIG);
71 try
72 {
73 if (global::Interop.WinHttp.WinHttpGetIEProxyConfigForCurrentUser(out proxyConfig))
74 {
75 _autoConfigUrl = Marshal.PtrToStringUni(proxyConfig.AutoConfigUrl);
76 _autoDetect = proxyConfig.AutoDetect;
77 _proxy = Marshal.PtrToStringUni(proxyConfig.Proxy);
78 _proxyBypass = Marshal.PtrToStringUni(proxyConfig.ProxyBypass);
79 if (System.Net.NetEventSource.Log.IsEnabled())
80 {
81 System.Net.NetEventSource.Info(this, $"AutoConfigUrl={AutoConfigUrl}, AutoDetect={AutoDetect}, Proxy={Proxy}, ProxyBypass={ProxyBypass}", ".ctor");
82 }
83 _useProxy = true;
84 }
85 else
86 {
87 int lastWin32Error = Marshal.GetLastWin32Error();
88 if (System.Net.NetEventSource.Log.IsEnabled())
89 {
90 System.Net.NetEventSource.Error(this, $"error={lastWin32Error}", ".ctor");
91 }
92 }
93 if (System.Net.NetEventSource.Log.IsEnabled())
94 {
95 System.Net.NetEventSource.Info(this, $"_useProxy={_useProxy}", ".ctor");
96 }
97 }
98 finally
99 {
100 Marshal.FreeHGlobal(proxyConfig.AutoConfigUrl);
101 Marshal.FreeHGlobal(proxyConfig.Proxy);
102 Marshal.FreeHGlobal(proxyConfig.ProxyBypass);
103 }
104 }
105
106 public bool GetProxyForUrl(global::Interop.WinHttp.SafeWinHttpHandle sessionHandle, Uri uri, out global::Interop.WinHttp.WINHTTP_PROXY_INFO proxyInfo)
107 {
108 proxyInfo.AccessType = 1u;
109 proxyInfo.Proxy = IntPtr.Zero;
110 proxyInfo.ProxyBypass = IntPtr.Zero;
111 if (!_useProxy)
112 {
113 return false;
114 }
115 bool flag = false;
116 Unsafe.SkipInit(out global::Interop.WinHttp.WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions);
117 autoProxyOptions.AutoConfigUrl = AutoConfigUrl;
118 autoProxyOptions.AutoDetectFlags = (AutoDetect ? 3u : 0u);
119 autoProxyOptions.AutoLoginIfChallenged = false;
120 autoProxyOptions.Flags = (AutoDetect ? 1u : 0u) | ((!string.IsNullOrEmpty(AutoConfigUrl)) ? 2u : 0u);
121 autoProxyOptions.Reserved1 = IntPtr.Zero;
122 autoProxyOptions.Reserved2 = 0u;
123 string text = uri.AbsoluteUri;
124 if (uri.Scheme == "wss")
125 {
126 text = "https" + text.Substring("wss".Length);
127 }
128 else if (uri.Scheme == "ws")
129 {
130 text = "http" + text.Substring("ws".Length);
131 }
132 bool flag2 = false;
133 do
134 {
135 _autoDetectionFailed = false;
136 if (global::Interop.WinHttp.WinHttpGetProxyForUrl(sessionHandle, text, ref autoProxyOptions, out proxyInfo))
137 {
138 if (System.Net.NetEventSource.Log.IsEnabled())
139 {
140 System.Net.NetEventSource.Info(this, "Using autoconfig proxy settings", "GetProxyForUrl");
141 }
142 flag = true;
143 break;
144 }
145 int lastWin32Error = Marshal.GetLastWin32Error();
146 if (System.Net.NetEventSource.Log.IsEnabled())
147 {
148 System.Net.NetEventSource.Error(this, $"error={lastWin32Error}", "GetProxyForUrl");
149 }
150 if ((long)lastWin32Error == 12015)
151 {
152 if (flag2)
153 {
154 break;
155 }
156 flag2 = true;
157 autoProxyOptions.AutoLoginIfChallenged = true;
158 continue;
159 }
160 if ((long)lastWin32Error == 12180)
161 {
164 }
165 break;
166 }
167 while (flag2);
168 if (!flag && !string.IsNullOrEmpty(Proxy))
169 {
170 proxyInfo.AccessType = 3u;
171 proxyInfo.Proxy = Marshal.StringToHGlobalUni(Proxy);
172 proxyInfo.ProxyBypass = (string.IsNullOrEmpty(ProxyBypass) ? IntPtr.Zero : Marshal.StringToHGlobalUni(ProxyBypass));
173 if (System.Net.NetEventSource.Log.IsEnabled())
174 {
175 System.Net.NetEventSource.Info(this, $"Fallback to Proxy={Proxy}, ProxyBypass={ProxyBypass}", "GetProxyForUrl");
176 }
177 flag = true;
178 }
179 if (System.Net.NetEventSource.Log.IsEnabled())
180 {
181 System.Net.NetEventSource.Info(this, $"useProxy={flag}", "GetProxyForUrl");
182 }
183 return flag;
184 }
185}
static int TickCount
bool GetProxyForUrl(global::Interop.WinHttp.SafeWinHttpHandle sessionHandle, Uri uri, out global::Interop.WinHttp.WINHTTP_PROXY_INFO proxyInfo)
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static unsafe? string PtrToStringUni(IntPtr ptr)
Definition Marshal.cs:652
static unsafe IntPtr StringToHGlobalUni(string? s)
Definition Marshal.cs:1310
string AbsoluteUri
Definition Uri.cs:266
string Scheme
Definition Uri.cs:505
static readonly IntPtr Zero
Definition IntPtr.cs:18