|
| 1 | +// based on the sample provided here: https://github.com/git-for-windows/git/issues/387 |
| 2 | +// probably inspired by https://msdn.microsoft.com/en-us/library/aa384122(v=vs.85).aspx |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | +#define WIN32_LEAN_AND_MEAN |
| 6 | +#include <windows.h> |
| 7 | +#include <winhttp.h> |
| 8 | +#include <shellapi.h> |
| 9 | + |
| 10 | +LPCWSTR get_proxy_for_url(LPCWSTR url) |
| 11 | +{ |
| 12 | + WINHTTP_CURRENT_USER_IE_PROXY_CONFIG config; |
| 13 | + WINHTTP_AUTOPROXY_OPTIONS options; |
| 14 | + WINHTTP_PROXY_INFO info; |
| 15 | + |
| 16 | + memset(&config, 0, sizeof(config)); |
| 17 | + |
| 18 | + if (!WinHttpGetIEProxyConfigForCurrentUser(&config)) |
| 19 | + config.fAutoDetect = FALSE; |
| 20 | + else if (config.lpszAutoConfigUrl) |
| 21 | + config.fAutoDetect = TRUE; |
| 22 | + |
| 23 | + if (config.fAutoDetect) { |
| 24 | + HINTERNET handle = WinHttpOpen(L"Proxy Lookup/1.0", |
| 25 | + WINHTTP_ACCESS_TYPE_NO_PROXY, |
| 26 | + WINHTTP_NO_PROXY_NAME, |
| 27 | + WINHTTP_NO_PROXY_BYPASS, 0); |
| 28 | + |
| 29 | + memset(&options, 0, sizeof(options)); |
| 30 | + |
| 31 | + // use pac file URL from IE proxy configuration |
| 32 | + options.lpszAutoConfigUrl = config.lpszAutoConfigUrl; |
| 33 | + |
| 34 | + options.fAutoLogonIfChallenged = TRUE; |
| 35 | + options.dwFlags = options.lpszAutoConfigUrl ? |
| 36 | + WINHTTP_AUTOPROXY_CONFIG_URL : |
| 37 | + WINHTTP_AUTOPROXY_AUTO_DETECT; |
| 38 | + if (!options.lpszAutoConfigUrl) |
| 39 | + options.dwAutoDetectFlags = |
| 40 | + WINHTTP_AUTO_DETECT_TYPE_DHCP | |
| 41 | + WINHTTP_AUTO_DETECT_TYPE_DNS_A; |
| 42 | + |
| 43 | + config.fAutoDetect = WinHttpGetProxyForUrl(handle, url, |
| 44 | + &options, &info ); |
| 45 | + |
| 46 | + WinHttpCloseHandle(handle); |
| 47 | + } |
| 48 | + |
| 49 | + if (config.fAutoDetect) |
| 50 | + return info.lpszProxy; |
| 51 | + return config.lpszProxy; |
| 52 | +} |
| 53 | + |
| 54 | +int main(int argc, char **argv) |
| 55 | +{ |
| 56 | + LPWSTR *wargv; |
| 57 | + int i, wargc; |
| 58 | + |
| 59 | + wargv = CommandLineToArgvW(GetCommandLineW(), &wargc); |
| 60 | + wprintf(L"Proxy lookup\n"); |
| 61 | + |
| 62 | + for (i = 1; i < wargc; i++) |
| 63 | + wprintf(L"URL: %s, proxy: %s\n", |
| 64 | + wargv[i], get_proxy_for_url(wargv[i])); |
| 65 | + |
| 66 | + return 0; |
| 67 | +} |
0 commit comments