-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWinhttpProxyStatus.cpp
More file actions
572 lines (449 loc) · 12.9 KB
/
WinhttpProxyStatus.cpp
File metadata and controls
572 lines (449 loc) · 12.9 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
//
// Sample WinHttp application for determining the proxy for a particular URL.
// This sample demonstrates the core functionality for querying the proxy
// settings. Additional features may be added by the application/module basing
// their proxy code from this sample, including but not limited to:
// 1) Per URL proxy cache.
// 2) Network Change awareness.
// 3) Bad Proxy Filter.
//
#pragma comment(lib , "ws2_32.lib")
#include <windows.h>
#include <stdio.h>
#include <string>
#include "GetProxy.h"
#ifdef _CPPUNWIND
#include <new>
#endif
DWORD
SendRequestWithProxyFailover(
_In_ HINTERNET hRequest,
_In_ ProxyResolver *pProxyResolver
)
/*++
Routine Description:
Sends a request using the Request Handle specified and implements
proxy failover if supported.
Arguments:
hRequest - The request handle returned by WinHttpOpenRequest.
pProxyResolver - The Proxy Resolver to use for the request. The resolver
is used to set the proxy infomation on the request handle and
to implement proxy failover. At this point the proxy
resolver should have been initialized by calling Resolve().
Return Value:
Win32 Error codes.
--*/
{
DWORD dwError = ERROR_SUCCESS;
DWORD dwRequestError = ERROR_SUCCESS;
//
// Reset the proxy list to the beginning in case it is being reused.
//
pProxyResolver->ResetProxyCursor();
for(;;)
{
dwError = pProxyResolver->SetNextProxySetting(hRequest,
dwRequestError);
if (dwError == ERROR_NO_MORE_ITEMS)
{
//
// We reached the end of the list, failover is not supported,
// or the error was fatal.
// Fail with last sendrequest error.
//
dwError = dwRequestError;
goto quit;
}
if (dwError != ERROR_SUCCESS)
{
//
// Some other error occured such as a bad proxy setting, bad handle,
// out of memory, etc.
//
goto quit;
}
if (!WinHttpSendRequest(hRequest,
NULL, // headers
0, // headerslen
NULL, // entity
0, // entitylen
0, // totallen
NULL)) // context
{
dwRequestError = GetLastError();
continue;
}
if (!WinHttpReceiveResponse(hRequest,
NULL)) // reserved
{
dwRequestError = GetLastError();
continue;
}
dwError = ERROR_SUCCESS;
break;
}
quit:
return dwError;
}
DWORD
SendRequestToHost(
_In_ HINTERNET hSession,
_In_ ProxyResolver *pProxyResolver,
_In_z_ PCWSTR pwszHost,
_In_opt_z_ PCWSTR pwszPath,
_Out_ DWORD *pdwStatusCode
)
/*++
Routine Description:
Connects to a host with the specified proxy and returns the status code
to the caller.
Arguments:
hSession - The WinHTTP session to use for the connection.
pProxyResolver - The proxy resolver for the request.
pwszHost - The host name of the resource to connect to.
pwszPath - The path of the resource to connect to.
pdwStatusCode - The status code of the connection to the server.
Return Value:
Win32 Error codes.
--*/
{
DWORD dwError = ERROR_SUCCESS;
DWORD dwStatusCode = 0;
DWORD cbStatusCode = sizeof(dwStatusCode);
DWORD dwFlags = 0;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
PCWSTR pcwszAcceptTypes[] = {L"*/*", NULL};
*pdwStatusCode = 0;
//
// Connect session.
//
hConnect = WinHttpConnect(hSession,
pwszHost,
INTERNET_DEFAULT_HTTP_PORT,
0);
if (hConnect == NULL)
{
dwError = GetLastError();
goto quit;
}
//
// Open HTTP request.
//
hRequest = WinHttpOpenRequest(hConnect,
L"GET",
pwszPath,
NULL, // version
NULL, // referrer
pcwszAcceptTypes,
0); // flags
if (hRequest == NULL)
{
dwError = GetLastError();
goto quit;
}
//
// Send the HTTP request with proxy failover if valid.
//
dwError = SendRequestWithProxyFailover(hRequest,
pProxyResolver);
if (dwError != ERROR_SUCCESS)
{
goto quit;
}
//
// Get the status code from the response.
//
dwFlags = WINHTTP_QUERY_FLAG_NUMBER | WINHTTP_QUERY_STATUS_CODE;
if (!WinHttpQueryHeaders(hRequest,
dwFlags,
NULL, // name
&dwStatusCode,
&cbStatusCode,
NULL)) // index
{
dwError = GetLastError();
goto quit;
}
*pdwStatusCode = dwStatusCode;
quit:
if (hRequest != NULL)
{
WinHttpCloseHandle(hRequest);
hRequest = NULL;
}
if (hConnect != NULL)
{
WinHttpCloseHandle(hConnect);
hConnect = NULL;
}
return dwError;
}
_Must_inspect_result_
_Success_(return == ERROR_SUCCESS)
DWORD
CrackHostAndPath(
_In_z_ PCWSTR pwszUrl,
_Out_ PWSTR *ppwszHost,
_Out_ PWSTR *ppwszPath
)
/*++
Routine Description:
Cracks the Host name and Path from a URL and returns the result to the
caller.
Arguments:
pwszUrl - The URL to crack.
ppwszHost - The Host name cracked from the URL.
Free ppwszHost with free.
ppwszPath - The Path cracked from the URL or NULL if no path was provided.
Free ppwszPath with free.
Return Value:
Win32 Error codes.
--*/
{
DWORD dwError = ERROR_SUCCESS;
DWORD cbHost = 0;
DWORD cbPath = 0;
PWSTR pwszHost = NULL;
PWSTR pwszPath = NULL;
URL_COMPONENTS urlComponents = {};
//
// Get the length of each component.
//
urlComponents.dwStructSize = sizeof(urlComponents);
urlComponents.dwHostNameLength = (DWORD)-1;
urlComponents.dwUrlPathLength = (DWORD)-1;
if (!WinHttpCrackUrl(pwszUrl,
0,
0,
&urlComponents))
{
dwError = GetLastError();
}
if (dwError != ERROR_SUCCESS)
{
goto quit;
}
//
// Create a buffer to copy each component.
//
cbHost = urlComponents.dwHostNameLength * sizeof(WCHAR) + sizeof(WCHAR);
pwszHost = (PWSTR)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
cbHost);
if (pwszHost == NULL)
{
dwError = ERROR_NOT_ENOUGH_MEMORY;
goto quit;
}
//
// Account for '\0' in Length.
//
pwszHost[0] = '\0';
urlComponents.dwHostNameLength++;
if (urlComponents.dwUrlPathLength != 0)
{
cbPath = urlComponents.dwUrlPathLength * sizeof(WCHAR) + sizeof(WCHAR);
pwszPath = (PWSTR)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
cbPath);
if (pwszPath == NULL)
{
dwError = ERROR_NOT_ENOUGH_MEMORY;
goto quit;
}
//
// Account for '\0' in Length.
//
pwszPath[0] = '\0';
urlComponents.dwUrlPathLength++;
}
//
// Copy each component into new buffer.
//
urlComponents.lpszHostName = pwszHost;
urlComponents.lpszUrlPath = pwszPath;
if (!WinHttpCrackUrl(pwszUrl,
0,
0,
&urlComponents))
{
dwError = GetLastError();
}
if (dwError != ERROR_SUCCESS)
{
goto quit;
}
*ppwszHost = pwszHost;
pwszHost = NULL;
*ppwszPath = pwszPath;
pwszPath = NULL;
quit:
if (pwszHost != NULL)
{
HeapFree(GetProcessHeap(),
0,
pwszHost);
pwszHost = NULL;
}
if (pwszPath != NULL)
{
HeapFree(GetProcessHeap(),
0,
pwszPath);
pwszPath = NULL;
}
return dwError;
}
int
__cdecl
wmain(
int argc,
_In_reads_(argc) WCHAR **argv
)
{
DWORD dwError = ERROR_SUCCESS;
DWORD dwStatusCode = 0;
PWSTR pwszUrl = NULL;
PWSTR pwszHost = NULL;
PWSTR pwszPath = NULL;
HINTERNET hRequestSession = NULL;
HINTERNET hProxyResolveSession = NULL;
ProxyResolver * pProxyResolver = NULL;
if (argc != 2)
{
wprintf(L"Usage: %s <URL>\n", argv[0]);
argv[1] = L"https://support.microsoft.com/en-us";
//goto quit;
}
pwszUrl = argv[1];
wprintf(L"\nTrying URL: %s\n\n", argv[1]);
// Init WinSock
WSADATA wsa_Data;
int wsa_ReturnCode = WSAStartup(0x101, &wsa_Data);
// Get the local hostname and IP address
char szHostName[255];
gethostname(szHostName, 255);
struct hostent *host_entry;
host_entry = gethostbyname(szHostName);
char * szLocalIP;
szLocalIP = inet_ntoa(*(struct in_addr *)*host_entry->h_addr_list);
// Get username
#define INFO_BUFFER_SIZE 32767
TCHAR infoBuf[INFO_BUFFER_SIZE];
DWORD bufCharCount = INFO_BUFFER_SIZE;
GetUserName(infoBuf, &bufCharCount);
//char * szUserName;
//szUserName = getenv("USERNAME");
wprintf(L"USER: %s PC: %S IP: %S\n", infoBuf, szHostName, szLocalIP);
WSACleanup();
//
// Open a session in synchronous mode for http request.
//
hRequestSession = WinHttpOpen(USER_AGENT,
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
0);
if (hRequestSession == NULL)
{
dwError = GetLastError();
goto quit;
}
//
// Open a session in asynchronous mode for proxy resolve.
//
hProxyResolveSession = WinHttpOpen(USER_AGENT,
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
WINHTTP_FLAG_ASYNC);
if (hProxyResolveSession == NULL)
{
dwError = GetLastError();
goto quit;
}
//
// Create a proxy resolver to use for this URL.
//
#ifdef _CPPUNWIND
pProxyResolver = new(std::nothrow) ProxyResolver();
#else
pProxyResolver = new ProxyResolver();
#endif
if (pProxyResolver == NULL)
{
dwError = ERROR_NOT_ENOUGH_MEMORY;
goto quit;
}
//
// Resolve the proxy for the specified URL.
//
dwError = pProxyResolver->ResolveProxy(hProxyResolveSession,
pwszUrl);
if (dwError != ERROR_SUCCESS)
{
goto quit;
}
dwError = CrackHostAndPath(pwszUrl,
&pwszHost,
&pwszPath);
if (dwError != ERROR_SUCCESS)
{
goto quit;
}
//
// Attempt to connect to the host and retrieve a status code.
//
dwError = SendRequestToHost(hRequestSession,
pProxyResolver,
pwszHost,
pwszPath,
&dwStatusCode);
if (dwError != ERROR_SUCCESS)
{
goto quit;
}
wprintf(L"WinHttpConnect to: %s Path: %s Response: %d\n", pwszHost, pwszPath, dwStatusCode);
quit:
if (dwError != ERROR_SUCCESS)
{
wprintf(L"Failed with Error: 0x%x\n", dwError);
}
if (hRequestSession != NULL)
{
WinHttpCloseHandle(hRequestSession);
hRequestSession = NULL;
}
if (hProxyResolveSession != NULL)
{
WinHttpCloseHandle(hProxyResolveSession);
hProxyResolveSession = NULL;
}
if (pProxyResolver != NULL)
{
delete pProxyResolver;
pProxyResolver = NULL;
}
if (pwszHost != NULL)
{
HeapFree(GetProcessHeap(),
0,
pwszHost);
pwszHost = NULL;
}
if (pwszPath != NULL)
{
HeapFree(GetProcessHeap(),
0,
pwszPath);
pwszPath = NULL;
}
}