-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyProperties.cs
294 lines (278 loc) · 10.4 KB
/
ProxyProperties.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.Collections;
using System.Net;
using System.Runtime.InteropServices;
using System.Web.Services.Protocols;
using WebServiceStudio.Dialogs;
namespace WebServiceStudio
{
internal class ProxyProperties
{
private IAdditionalProperties additionalProperties;
private bool allowAutoRedirect;
private ServerProperties httpProxy;
private bool preAuthenticate;
private static Hashtable proxyTypeHandlers;
private ServerProperties server;
private int timeout;
private static string typeNotFoundMessage = "ProxyPropertiesType {0} specified in WebServiceStudio.exe.options is not found";
private bool useCookieContainer;
private static string NTLM = "NTLM";
private static string BASIC = "Basic";
public ProxyProperties()
{
}
public ProxyProperties(HttpWebClientProtocol proxy)
{
this.Timeout = proxy.Timeout;
this.AllowAutoRedirect = proxy.AllowAutoRedirect;
this.PreAuthenticate = proxy.PreAuthenticate;
if (proxy.CookieContainer == null)
{
this.UseCookieContainer = true;
}
this.Server = new ServerProperties();
this.Server.Url = proxy.Url;
this.SetCredentialValues(proxy.Credentials, new Uri(this.Server.Url), out this.Server.UseDefaultCredentials, out this.Server.UserNameForBasicAuth, out this.Server.PasswordForBasicAuth);
WebProxy proxy2 = proxy.Proxy as WebProxy;
if (proxy2 != null)
{
this.HttpProxy = new ServerProperties();
this.HttpProxy.Url = proxy2.Address.ToString();
this.SetCredentialValues(proxy2.Credentials, new Uri(this.HttpProxy.Url), out this.HttpProxy.UseDefaultCredentials, out this.HttpProxy.UserNameForBasicAuth, out this.HttpProxy.PasswordForBasicAuth);
}
this.InitAdditionalProperties(proxy);
}
private void InitAdditionalProperties(HttpWebClientProtocol proxy)
{
if (proxyTypeHandlers == null)
{
proxyTypeHandlers = new Hashtable();
CustomHandler[] proxyProperties = Configuration.MasterConfig.ProxyProperties;
if ((proxyProperties != null) && (proxyProperties.Length > 0))
{
foreach (CustomHandler handler in proxyProperties)
{
string typeName = handler.TypeName;
string str2 = handler.Handler;
if (((typeName != null) && (typeName.Length != 0)) && ((str2 != null) && (str2.Length != 0)))
{
Type key = Type.GetType(typeName);
if (key == null)
{
NewMainForm.ShowMessage(this, MessageType.Warning, string.Format(typeNotFoundMessage, typeName));
}
else
{
Type type = Type.GetType(str2);
if (type == null)
{
NewMainForm.ShowMessage(this, MessageType.Warning, string.Format(typeNotFoundMessage, str2));
}
else
{
proxyTypeHandlers.Add(key, type);
}
}
}
}
}
}
for (Type type3 = proxy.GetType(); type3 != typeof(object); type3 = type3.BaseType)
{
Type type4 = proxyTypeHandlers[type3] as Type;
if (type4 != null)
{
this.AdditionalProperties = (IAdditionalProperties)Activator.CreateInstance(type4, new object[] { proxy });
break;
}
}
}
private ICredentials ReadCredentials(ICredentials credentials, Uri uri, bool useDefaultCredentials, string userName, string password)
{
CredentialCache cache = credentials as CredentialCache;
if (cache == null)
{
cache = new CredentialCache();
}
if (useDefaultCredentials)
{
if (cache.GetCredential(uri, NTLM) == null)
{
cache.Add(uri, NTLM, (NetworkCredential)CredentialCache.DefaultCredentials);
}
}
else
{
if (cache.GetCredential(uri, NTLM) != null)
{
cache.Remove(uri, NTLM);
}
}
if ((((userName != null) && (userName.Length > 0)) || ((password != null) && (password.Length > 0))))
{
if (cache.GetCredential(uri, BASIC) == null)
{
NetworkCredential cred = new NetworkCredential(userName, password);
cache.Add(uri, BASIC, cred);
}
else
{
cache.GetCredential(uri, BASIC).UserName = userName;
cache.GetCredential(uri, BASIC).Password = password;
}
}
return cache;
}
private void SetCredentialValues(ICredentials credentials, Uri uri, out bool useDefaultCredentials, out string userName, out string password)
{
useDefaultCredentials = false;
userName = string.Empty;
password = string.Empty;
if (((credentials == null) || (credentials is CredentialCache)) && (credentials != null))
{
NetworkCredential credential = null;
CredentialCache cache = credentials as CredentialCache;
if (cache != null)
{
if (CredentialCache.DefaultCredentials == cache.GetCredential(uri, NTLM))
{
useDefaultCredentials = true;
}
credential = cache.GetCredential(uri, BASIC);
}
else if (credentials == CredentialCache.DefaultCredentials)
{
useDefaultCredentials = true;
}
else
{
credential = credentials as NetworkCredential;
}
if (credential != null)
{
userName = credential.UserName;
password = credential.Password;
}
}
}
public void UpdateProxy(HttpWebClientProtocol proxy)
{
proxy.Timeout = this.Timeout;
proxy.AllowAutoRedirect = this.AllowAutoRedirect;
proxy.PreAuthenticate = this.PreAuthenticate;
if (this.UseCookieContainer)
{
if (proxy.CookieContainer == null)
{
proxy.CookieContainer = new CookieContainer();
}
}
else
{
proxy.CookieContainer = null;
}
proxy.Url = this.Server.Url;
proxy.Credentials = this.ReadCredentials(proxy.Credentials, new Uri(this.Server.Url), this.Server.UseDefaultCredentials, this.Server.UserNameForBasicAuth, this.Server.PasswordForBasicAuth);
if (((this.HttpProxy != null) && (this.HttpProxy.Url != null)) && (this.HttpProxy.Url.Length > 0))
{
Uri uri = new Uri(this.HttpProxy.Url);
if (proxy.Proxy == null)
{
proxy.Proxy = new WebProxy();
}
WebProxy proxy2 = proxy.Proxy as WebProxy;
proxy2.Address = uri;
proxy2.Credentials = this.ReadCredentials(proxy2.Credentials, uri, this.Server.UseDefaultCredentials, this.Server.UserNameForBasicAuth, this.Server.PasswordForBasicAuth);
}
if (this.additionalProperties != null)
{
this.additionalProperties.UpdateProxy(proxy);
}
}
public IAdditionalProperties AdditionalProperties
{
get
{
return this.additionalProperties;
}
set
{
this.additionalProperties = value;
}
}
public bool AllowAutoRedirect
{
get
{
return this.allowAutoRedirect;
}
set
{
this.allowAutoRedirect = value;
}
}
public ServerProperties HttpProxy
{
get
{
return this.httpProxy;
}
set
{
this.httpProxy = value;
}
}
public bool PreAuthenticate
{
get
{
return this.preAuthenticate;
}
set
{
this.preAuthenticate = value;
}
}
public ServerProperties Server
{
get
{
return this.server;
}
set
{
this.server = value;
}
}
public int Timeout
{
get
{
return this.timeout;
}
set
{
this.timeout = value;
}
}
public bool UseCookieContainer
{
get
{
return this.useCookieContainer;
}
set
{
this.useCookieContainer = value;
}
}
public class ServerProperties
{
public string PasswordForBasicAuth;
public string Url;
public bool UseDefaultCredentials;
public string UserNameForBasicAuth;
}
}
}