SEXP R_proxy_info(){ WINHTTP_CURRENT_USER_IE_PROXY_CONFIG MyProxyConfig; if(!WinHttpGetIEProxyConfigForCurrentUser(&MyProxyConfig)){ return R_NilValue; } char buffer[500]; SEXP vec = PROTECT(allocVector(VECSXP, 4)); SET_VECTOR_ELT(vec, 0, ScalarLogical(MyProxyConfig.fAutoDetect)); if(MyProxyConfig.lpszAutoConfigUrl != NULL) { wcstombs(buffer, MyProxyConfig.lpszAutoConfigUrl, 500); SET_VECTOR_ELT(vec, 1, mkString(buffer)); } if(MyProxyConfig.lpszProxy != NULL) { wcstombs(buffer, MyProxyConfig.lpszProxy, 500); SET_VECTOR_ELT(vec, 2, mkString(buffer)); } if(MyProxyConfig.lpszProxyBypass != NULL) { wcstombs(buffer, MyProxyConfig.lpszProxyBypass, 500); SET_VECTOR_ELT(vec, 3, mkString(buffer)); } setAttrib(vec, R_NamesSymbol, proxy_namesvec()); UNPROTECT(1); return vec; }
/*---------------------------------------------------------------------- | NPT_HttpProxySelector::GetSystemDefault +---------------------------------------------------------------------*/ NPT_HttpProxySelector* NPT_HttpProxySelector::GetSystemDefault() { NPT_AutoLock lock(NPT_Win32HttpLock); if (NPT_Win32HttpHandle == INVALID_HANDLE_VALUE) { WINHTTP_CURRENT_USER_IE_PROXY_CONFIG config; BOOL result = WinHttpGetIEProxyConfigForCurrentUser(&config); } return NULL; }
void HttpProtocol::initialize(dictionary* ini, const char* section) { char* url = INI::GetString(ini, section, FS_URL, NULL); if(!url) { Log::Error("Missing URL for HTTP protocol"); return; } Log::Info("Setup session for: %s", url); int usz = MultiByteToWideChar(CP_ACP, 0, url, strlen(url), 0, 0); this->url = (wchar_t*) malloc((usz + 1) * sizeof(wchar_t)); MultiByteToWideChar(CP_ACP, 0, url, strlen(url), this->url, usz); this->url[usz] = 0; memset(&urlc, 0, sizeof(URL_COMPONENTS)); urlc.dwStructSize = sizeof(URL_COMPONENTS); urlc.dwSchemeLength = 1; urlc.dwHostNameLength = 1; urlc.dwUserNameLength = 1; urlc.dwPasswordLength = 1; urlc.dwUrlPathLength = 1; urlc.dwExtraInfoLength = 1; WinHttpCrackUrl(this->url, usz, 0, &urlc); this->host = (wchar_t*) malloc((urlc.dwHostNameLength + 1) * sizeof(wchar_t)); memcpy(this->host, urlc.lpszHostName, urlc.dwHostNameLength*sizeof(wchar_t)); this->host[urlc.dwHostNameLength] = 0; this->path = (wchar_t*) malloc((urlc.dwUrlPathLength + 1) * sizeof(wchar_t)); memcpy(this->path, urlc.lpszUrlPath, urlc.dwUrlPathLength*sizeof(wchar_t)); this->path[urlc.dwUrlPathLength] = 0; // Determine proxy char* proxy = INI::GetString(ini, section, FS_PROXY, NULL); if(proxy) { Log::Info("Using proxy: %s", proxy); int psz = MultiByteToWideChar(CP_ACP, 0, url, strlen(url), 0, 0); wchar_t* wproxy = (wchar_t*) malloc((psz + 1) * sizeof(wchar_t)); MultiByteToWideChar(CP_ACP, 0, proxy, strlen(proxy), wproxy, psz); wproxy[psz] = 0; hSession = WinHttpOpen(USER_AGENT, WINHTTP_ACCESS_TYPE_NO_PROXY, wproxy, 0, 0); free(wproxy); } else { WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxy; WinHttpGetIEProxyConfigForCurrentUser(&proxy); int proxyType = WINHTTP_ACCESS_TYPE_NO_PROXY; if(proxy.lpszProxy) { proxyType = WINHTTP_ACCESS_TYPE_NAMED_PROXY; Log::Info("Using proxy: %s", proxy.lpszProxy); } hSession = WinHttpOpen(USER_AGENT, proxyType, proxy.lpszProxy, proxy.lpszProxyBypass, 0); } }
JNIEXPORT jobject JNICALL Java_com_btr_proxy_search_desktop_win_Win32ProxyUtils_winHttpGetIEProxyConfigForCurrentUser (JNIEnv *env, jobject source) { WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieProxyInfo; // Retrieve the IE proxy configuration. BOOL result = WinHttpGetIEProxyConfigForCurrentUser( &ieProxyInfo ); if (result == FALSE) { DWORD errorCode = GetLastError(); return NULL; } jboolean autoDetect = ieProxyInfo.fAutoDetect; jstring autoConfigUrl = NULL; jstring proxy = NULL; jstring proxyBypass = NULL; if (ieProxyInfo.lpszAutoConfigUrl != NULL) { autoConfigUrl = env->NewString((jchar*)ieProxyInfo.lpszAutoConfigUrl, wcslen(ieProxyInfo.lpszAutoConfigUrl)); GlobalFree( ieProxyInfo.lpszAutoConfigUrl ); } if (ieProxyInfo.lpszProxy != NULL) { proxy = env->NewString((jchar*)ieProxyInfo.lpszProxy, wcslen(ieProxyInfo.lpszProxy)); GlobalFree( ieProxyInfo.lpszProxy ); } if (ieProxyInfo.lpszProxyBypass != NULL) { proxyBypass = env->NewString((jchar*)ieProxyInfo.lpszProxyBypass, wcslen(ieProxyInfo.lpszProxyBypass)); GlobalFree( ieProxyInfo.lpszProxyBypass ); } // Build result container object. jclass retValueClass = env->FindClass("com/btr/proxy/search/desktop/win/Win32IESettings"); if ( retValueClass == NULL ) { return NULL; } jmethodID jmid = env->GetMethodID(retValueClass, "<init>", "(ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); if (jmid == NULL) { return NULL; } // Win32IESettings(boolean autoDetect, String autoConfigUrl, String proxy, String proxyBypass) jobject retValue = env->NewObject(retValueClass, jmid, autoDetect, autoConfigUrl, proxy, proxyBypass); return retValue; }
Proxy* WinHttpIO::getautoproxy() { Proxy* proxy = new Proxy(); proxy->setProxyType(Proxy::NONE); WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieProxyConfig = { 0 }; if (WinHttpGetIEProxyConfigForCurrentUser(&ieProxyConfig) == TRUE) { if (ieProxyConfig.lpszProxy) { string proxyURL; proxy->setProxyType(Proxy::CUSTOM); int len = wcslen(ieProxyConfig.lpszProxy); proxyURL.assign((const char*)ieProxyConfig.lpszProxy, len * sizeof(wchar_t) + 1); // only save one proxy for (int i = 0; i < len; i++) { wchar_t* character = (wchar_t*)(proxyURL.data() + i * sizeof(wchar_t)); if (*character == ' ' || *character == ';') { proxyURL.resize(i*sizeof(wchar_t)); len = i; break; } } // remove protocol prefix, if any for (int i = len - 1; i >= 0; i--) { wchar_t* character = (wchar_t*)(proxyURL.data() + i * sizeof(wchar_t)); if (*character == '/') { proxyURL = proxyURL.substr((i + 1) * sizeof(wchar_t)); break; } } proxy->setProxyURL(&proxyURL); } else if (ieProxyConfig.lpszAutoConfigUrl || ieProxyConfig.fAutoDetect == TRUE) { WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions; if (ieProxyConfig.lpszAutoConfigUrl) { autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL; autoProxyOptions.lpszAutoConfigUrl = ieProxyConfig.lpszAutoConfigUrl; autoProxyOptions.dwAutoDetectFlags = 0; } else { autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT; autoProxyOptions.lpszAutoConfigUrl = NULL; autoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A; } autoProxyOptions.fAutoLogonIfChallenged = TRUE; autoProxyOptions.lpvReserved = NULL; autoProxyOptions.dwReserved = 0; WINHTTP_PROXY_INFO proxyInfo; if (WinHttpGetProxyForUrl(hSession, L"https://g.api.mega.co.nz/", &autoProxyOptions, &proxyInfo)) { if (proxyInfo.lpszProxy) { string proxyURL; proxy->setProxyType(Proxy::CUSTOM); proxyURL.assign((const char*)proxyInfo.lpszProxy, wcslen(proxyInfo.lpszProxy) * sizeof(wchar_t)); proxy->setProxyURL(&proxyURL); } } } } if (ieProxyConfig.lpszProxy) { GlobalFree(ieProxyConfig.lpszProxy); } if (ieProxyConfig.lpszProxyBypass) { GlobalFree(ieProxyConfig.lpszProxyBypass); } if (ieProxyConfig.lpszAutoConfigUrl) { GlobalFree(ieProxyConfig.lpszAutoConfigUrl); } return proxy; }
DWORD ProxyResolver::ResolveProxy( _In_ HINTERNET hSession, _In_z_ PCWSTR pwszUrl ) /*++ Routine Description: Uses the users IE settings to get the proxy for the URL. Arguments: pwszUrl - The URL to get the proxy for. hSession - The session to use for the proxy resolution. Return Value: WIN32 Error codes. --*/ { DWORD dwError = ERROR_SUCCESS; WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ProxyConfig = {}; PWSTR pwszProxy = NULL; PWSTR pwszProxyBypass = NULL; BOOL fFailOverValid = FALSE; if (m_fInit) { dwError = ERROR_INVALID_OPERATION; goto quit; } if (!WinHttpGetIEProxyConfigForCurrentUser(&ProxyConfig)) { dwError = GetLastError(); if (dwError != ERROR_FILE_NOT_FOUND) { goto quit; } // // No IE proxy settings found, just do autodetect. // ProxyConfig.fAutoDetect = TRUE; dwError = ERROR_SUCCESS; } // // Begin processing the proxy settings in the following order: // 1) Auto-Detect if configured. // 2) Auto-Config URL if configured. // 3) Static Proxy Settings if configured. // // Once any of these methods succeed in finding a proxy we are finished. // In the event one mechanism fails with an expected error code it is // required to fall back to the next mechanism. If the request fails // after exhausting all detected proxies, there should be no attempt // to discover additional proxies. // if (ProxyConfig.fAutoDetect) { fFailOverValid = TRUE; // // Detect Proxy Settings. // dwError = GetProxyForAutoSettings(hSession, pwszUrl, NULL, &pwszProxy, &pwszProxyBypass); if (dwError == ERROR_SUCCESS) { goto commit; } if (!IsRecoverableAutoProxyError(dwError)) { goto quit; } // // Fall back to Autoconfig URL or Static settings. Application can // optionally take some action such as logging, or creating a mechanism // to expose multiple error codes in the class. // dwError = ERROR_SUCCESS; } if (ProxyConfig.lpszAutoConfigUrl) { fFailOverValid = TRUE; // // Run autoproxy with AutoConfig URL. // dwError = GetProxyForAutoSettings(hSession, pwszUrl, ProxyConfig.lpszAutoConfigUrl, &pwszProxy, &pwszProxyBypass); if (dwError == ERROR_SUCCESS) { goto commit; } if (!IsRecoverableAutoProxyError(dwError)) { goto quit; } // // Fall back to Static Settings. Application can optionally take some // action such as logging, or creating a mechanism to to expose multiple // error codes in the class. // dwError = ERROR_SUCCESS; } fFailOverValid = FALSE; // // Static Proxy Config. Failover is not valid for static proxy since // it is always either a single proxy or a list containing protocol // specific proxies such as "proxy" or http=httpproxy;https=sslproxy // pwszProxy = ProxyConfig.lpszProxy; ProxyConfig.lpszProxy = NULL; pwszProxyBypass = ProxyConfig.lpszProxyBypass; ProxyConfig.lpszProxyBypass = NULL; commit: m_fProxyFailOverValid = fFailOverValid; if (pwszProxy == NULL) { m_wpiProxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY; } else { m_wpiProxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY; } m_wpiProxyInfo.lpszProxy = pwszProxy; pwszProxy = NULL; m_wpiProxyInfo.lpszProxyBypass = pwszProxyBypass; pwszProxyBypass = NULL; m_fInit = TRUE; quit: if (pwszProxy != NULL) { GlobalFree(pwszProxy); pwszProxy = NULL; } if (pwszProxyBypass != NULL) { GlobalFree(pwszProxyBypass); pwszProxyBypass = NULL; } if (ProxyConfig.lpszAutoConfigUrl != NULL) { GlobalFree(ProxyConfig.lpszAutoConfigUrl); ProxyConfig.lpszAutoConfigUrl = NULL; } if (ProxyConfig.lpszProxy != NULL) { GlobalFree(ProxyConfig.lpszProxy); ProxyConfig.lpszProxy = NULL; } if (ProxyConfig.lpszProxyBypass != NULL) { GlobalFree(ProxyConfig.lpszProxyBypass); ProxyConfig.lpszProxyBypass = NULL; } return dwError; }
/*! * @brief Prepare a winHTTP request with the given context. * @param ctx Pointer to the HTTP transport context to prepare the request from. * @param isGet Indication of whether this request is a GET request, otherwise POST is used. * @param direction String representing the direction of the communications (for debug). * @return An Internet request handle. */ static HINTERNET get_request_winhttp(HttpTransportContext *ctx, BOOL isGet, const char *direction) { HINTERNET hReq = NULL; DWORD flags = WINHTTP_FLAG_BYPASS_PROXY_CACHE; if (ctx->ssl) { flags |= WINHTTP_FLAG_SECURE; dprintf("[%s] Setting secure flag..", direction); } vdprintf("[%s] opening request on connection %x to %S", direction, ctx->connection, ctx->uri); hReq = WinHttpOpenRequest(ctx->connection, isGet ? L"GET" : L"POST", ctx->uri, NULL, NULL, NULL, flags); if (hReq == NULL) { dprintf("[%s] Failed WinHttpOpenRequest: %u", direction, GetLastError()); SetLastError(ERROR_NOT_FOUND); return NULL; } // if no proxy is set, we should look to see if we can (and should) use the system // proxy settings for the given user. if (!ctx->proxy) { if (!ctx->proxy_configured) { WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieConfig = { 0 }; if (WinHttpGetIEProxyConfigForCurrentUser(&ieConfig)) { dprintf("[PROXY] Got IE configuration"); dprintf("[PROXY] AutoDetect: %s", ieConfig.fAutoDetect ? "yes" : "no"); dprintf("[PROXY] Auto URL: %S", ieConfig.lpszAutoConfigUrl); dprintf("[PROXY] Proxy: %S", ieConfig.lpszProxy); dprintf("[PROXY] Proxy Bypass: %S", ieConfig.lpszProxyBypass); if (ieConfig.lpszAutoConfigUrl || ieConfig.fAutoDetect) { WINHTTP_AUTOPROXY_OPTIONS autoProxyOpts = { 0 }; WINHTTP_PROXY_INFO proxyInfo = { 0 }; if (ieConfig.fAutoDetect) { dprintf("[PROXY] IE config set to autodetect with DNS or DHCP"); autoProxyOpts.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT; autoProxyOpts.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A; autoProxyOpts.lpszAutoConfigUrl = 0; } else if (ieConfig.lpszAutoConfigUrl) { dprintf("[PROXY] IE config set to autodetect with URL %S", ieConfig.lpszAutoConfigUrl); autoProxyOpts.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL; autoProxyOpts.dwAutoDetectFlags = 0; autoProxyOpts.lpszAutoConfigUrl = ieConfig.lpszAutoConfigUrl; } autoProxyOpts.fAutoLogonIfChallenged = TRUE; if (WinHttpGetProxyForUrl(ctx->internet, ctx->url, &autoProxyOpts, &proxyInfo)) { ctx->proxy_for_url = malloc(sizeof(WINHTTP_PROXY_INFO)); memcpy(ctx->proxy_for_url, &proxyInfo, sizeof(WINHTTP_PROXY_INFO)); } } else if (ieConfig.lpszProxy) { WINHTTP_PROXY_INFO* proxyInfo = (WINHTTP_PROXY_INFO*)calloc(1, sizeof(WINHTTP_PROXY_INFO)); ctx->proxy_for_url = proxyInfo; dprintf("[PROXY] IE config set to proxy %S with bypass %S", ieConfig.lpszProxy, ieConfig.lpszProxyBypass); proxyInfo->dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY; proxyInfo->lpszProxy = ieConfig.lpszProxy; proxyInfo->lpszProxyBypass = ieConfig.lpszProxyBypass; // stop the cleanup code from removing these as we're using them behind the scenes and they will // be freed later instead. ieConfig.lpszProxy = NULL; ieConfig.lpszProxyBypass = NULL;; } if (ieConfig.lpszAutoConfigUrl) { GlobalFree(ieConfig.lpszAutoConfigUrl); } if (ieConfig.lpszProxy) { GlobalFree(ieConfig.lpszProxy); } if (ieConfig.lpszProxyBypass) { GlobalFree(ieConfig.lpszProxyBypass); } } // mark as "configured" so we don't attempt to do this horrible PoS mess again. ctx->proxy_configured = TRUE; } if (ctx->proxy_for_url && !WinHttpSetOption(hReq, WINHTTP_OPTION_PROXY, ctx->proxy_for_url, sizeof(WINHTTP_PROXY_INFO))) { dprintf("[%s] Unable to set proxy options: %u", GetLastError()); } } else { if (ctx->proxy_user) { dprintf("[%s] Setting proxy username to %S", direction, ctx->proxy_user); if (!WinHttpSetOption(hReq, WINHTTP_OPTION_PROXY_USERNAME, ctx->proxy_user, (DWORD)(wcslen(ctx->proxy_user)))); { dprintf("[%s] Failed to set username %u", direction, GetLastError()); } } if (ctx->proxy_pass) { dprintf("[%s] Setting proxy password to %S", direction, ctx->proxy_pass); if (!WinHttpSetOption(hReq, WINHTTP_OPTION_PROXY_PASSWORD, ctx->proxy_pass, (DWORD)(wcslen(ctx->proxy_pass)))); { dprintf("[%s] Failed to set password %u", direction, GetLastError()); } } } if (ctx->ssl) { flags = SECURITY_FLAG_IGNORE_UNKNOWN_CA | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID | SECURITY_FLAG_IGNORE_CERT_CN_INVALID | SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE; if (!WinHttpSetOption(hReq, WINHTTP_OPTION_SECURITY_FLAGS, &flags, sizeof(flags))) { dprintf("[%s] failed to set the security flags on the request", direction); } } return hReq; }
/* Implementation of _pymfclib_winhttp */ static PyObject *__pyx_f_17_pymfclib_winhttp_getIEProxyConfigForCurrentUser(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ static PyObject *__pyx_f_17_pymfclib_winhttp_getIEProxyConfigForCurrentUser(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { WINHTTP_CURRENT_USER_IE_PROXY_CONFIG __pyx_v_ieconfig; struct __pyx_obj_17_pymfclib_winhttp_CurrentUserIEProxyConfig *__pyx_v_ret; PyObject *__pyx_r; BOOL __pyx_1; PyObject *__pyx_2 = 0; int __pyx_3; static char *__pyx_argnames[] = {0}; if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "", __pyx_argnames)) return 0; __pyx_v_ret = ((struct __pyx_obj_17_pymfclib_winhttp_CurrentUserIEProxyConfig *)Py_None); Py_INCREF(Py_None); /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":62 */ memset((&__pyx_v_ieconfig),0,(sizeof(__pyx_v_ieconfig))); /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":63 */ __pyx_1 = WinHttpGetIEProxyConfigForCurrentUser((&__pyx_v_ieconfig)); if (__pyx_1) { /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":64 */ __pyx_2 = PyObject_CallObject(((PyObject *)__pyx_ptype_17_pymfclib_winhttp_CurrentUserIEProxyConfig), 0); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; goto __pyx_L1;} Py_DECREF(((PyObject *)__pyx_v_ret)); __pyx_v_ret = ((struct __pyx_obj_17_pymfclib_winhttp_CurrentUserIEProxyConfig *)__pyx_2); __pyx_2 = 0; /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":65 */ __pyx_2 = PyInt_FromLong(__pyx_v_ieconfig.fAutoDetect); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; goto __pyx_L1;} Py_DECREF(__pyx_v_ret->autodetect); __pyx_v_ret->autodetect = __pyx_2; __pyx_2 = 0; /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":66 */ /*try:*/ { /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":67 */ __pyx_3 = (__pyx_v_ieconfig.lpszAutoConfigUrl != 0); if (__pyx_3) { __pyx_2 = _fromWideChar(__pyx_v_ieconfig.lpszAutoConfigUrl); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 68; goto __pyx_L4;} Py_DECREF(__pyx_v_ret->autoconfigurl); __pyx_v_ret->autoconfigurl = __pyx_2; __pyx_2 = 0; goto __pyx_L6; } __pyx_L6:; /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":69 */ __pyx_3 = (__pyx_v_ieconfig.lpszProxy != 0); if (__pyx_3) { __pyx_2 = _fromWideChar(__pyx_v_ieconfig.lpszProxy); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 70; goto __pyx_L4;} Py_DECREF(__pyx_v_ret->proxy); __pyx_v_ret->proxy = __pyx_2; __pyx_2 = 0; goto __pyx_L7; } __pyx_L7:; /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":71 */ __pyx_3 = (__pyx_v_ieconfig.lpszProxyBypass != 0); if (__pyx_3) { __pyx_2 = _fromWideChar(__pyx_v_ieconfig.lpszProxyBypass); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 72; goto __pyx_L4;} Py_DECREF(__pyx_v_ret->proxybypass); __pyx_v_ret->proxybypass = __pyx_2; __pyx_2 = 0; goto __pyx_L8; } __pyx_L8:; } /*finally:*/ { int __pyx_why; PyObject *__pyx_exc_type, *__pyx_exc_value, *__pyx_exc_tb; int __pyx_exc_lineno; __pyx_why = 0; goto __pyx_L5; __pyx_L4: { __pyx_why = 4; Py_XDECREF(__pyx_2); __pyx_2 = 0; PyErr_Fetch(&__pyx_exc_type, &__pyx_exc_value, &__pyx_exc_tb); __pyx_exc_lineno = __pyx_lineno; goto __pyx_L5; } __pyx_L5:; /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":74 */ __pyx_3 = (__pyx_v_ieconfig.lpszAutoConfigUrl != 0); if (__pyx_3) { GlobalFree(__pyx_v_ieconfig.lpszAutoConfigUrl); goto __pyx_L10; } __pyx_L10:; /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":76 */ __pyx_3 = (__pyx_v_ieconfig.lpszProxy != 0); if (__pyx_3) { GlobalFree(__pyx_v_ieconfig.lpszProxy); goto __pyx_L11; } __pyx_L11:; /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":78 */ __pyx_3 = (__pyx_v_ieconfig.lpszProxyBypass != 0); if (__pyx_3) { GlobalFree(__pyx_v_ieconfig.lpszProxyBypass); goto __pyx_L12; } __pyx_L12:; switch (__pyx_why) { case 4: { PyErr_Restore(__pyx_exc_type, __pyx_exc_value, __pyx_exc_tb); __pyx_lineno = __pyx_exc_lineno; __pyx_exc_type = 0; __pyx_exc_value = 0; __pyx_exc_tb = 0; goto __pyx_L1; } } } /* "c:\src\ip\pymfc\pymfclib\_pymfclib_winhttp.pyx":80 */ Py_INCREF(((PyObject *)__pyx_v_ret)); __pyx_r = ((PyObject *)__pyx_v_ret); goto __pyx_L0; goto __pyx_L2; } __pyx_L2:; __pyx_r = Py_None; Py_INCREF(Py_None); goto __pyx_L0; __pyx_L1:; Py_XDECREF(__pyx_2); __Pyx_AddTraceback("_pymfclib_winhttp.getIEProxyConfigForCurrentUser"); __pyx_r = 0; __pyx_L0:; Py_DECREF(__pyx_v_ret); return __pyx_r; }
static void InitializeWin32ProxyConfig() { static bool initialized = false; if (initialized) return; WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieProxyConfig; ZeroMemory(&ieProxyConfig, sizeof(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG)); if (WinHttpGetIEProxyConfigForCurrentUser(&ieProxyConfig)) { if (ieProxyConfig.fAutoDetect) { useProxyAutoConfig = true; } if (ieProxyConfig.lpszAutoConfigUrl != NULL) { // We using an auto proxy configuration, but this one // has a URL which we must contact to get the configuration info. autoConfigURL = ieProxyConfig.lpszAutoConfigUrl; } // We always keep IE proxy information in case auto proxy // determination fails. We want to it as a fallback. if (ieProxyConfig.lpszProxy) { std::string bypassList; if (ieProxyConfig.lpszProxyBypass) { std::wstring bypassW = ieProxyConfig.lpszProxyBypass; bypassList = string(bypassW.begin(), bypassW.end()); } std::wstring proxyListW = ieProxyConfig.lpszProxy; string proxyList = string(proxyListW.begin(), proxyListW.end()); ParseProxyList(proxyList, bypassList, ieProxies); } } else { // If there is no IE configuration information, we default to // attempting to get auto proxy information. useProxyAutoConfig = true; } if (useProxyAutoConfig || !autoConfigURL.empty()) { // We failed to open an HINTERNET handle! WTF. We'll have to have // disable auto proxy support, because we can't do a lookup. if (!httpSession.GetHandle()) { useProxyAutoConfig = false; autoConfigURL = L""; } } if (ieProxyConfig.lpszProxy) GlobalFree(ieProxyConfig.lpszProxy); if (ieProxyConfig.lpszProxyBypass) GlobalFree(ieProxyConfig.lpszProxyBypass); if (ieProxyConfig.lpszAutoConfigUrl) GlobalFree(ieProxyConfig.lpszAutoConfigUrl); }
Connection::Connection(HttpContext &context, const std::string &scheme, const std::string &host, int port) : _scheme(scheme), _host(host), _port(port), _context(context), _resolver(context.service()), _connected(false), _busy(false) { std::string schemeAddress; #ifdef WINVER std::string proxyAddress; WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxy; if (WinHttpGetIEProxyConfigForCurrentUser(&proxy)) { if (proxy.lpszProxy) { proxyAddress = boost::locale::conv::utf_to_utf<char>(proxy.lpszProxy); GlobalFree(proxy.lpszProxy); } if (proxy.lpszProxyBypass) { GlobalFree(proxy.lpszProxyBypass); } if (proxy.lpszAutoConfigUrl) { GlobalFree(proxy.lpszProxyBypass); } } enum State { Key, Value } state = Key; std::string key; std::string value; std::map<std::string, std::string> list; for (auto c : proxyAddress) { switch (state) { case Key: if (c == '=') state = Value; else key += c; break; case Value: if (c == ';') { list[key] = value; key = ""; value = ""; state = Key; } else value += c; break; } } if (!value.empty()) list[key] = value; if (list.size() == 0) { schemeAddress = proxyAddress; } else { auto it = list.find(scheme); if (it != list.end()) schemeAddress = it->second; } #else char * data = getenv("https_proxy"); if (data) schemeAddress = data; #endif if (!schemeAddress.empty()) { Url proxyUrl(schemeAddress); _proxy = proxyUrl.host(); _proxyPort = proxyUrl.port(); auto url = host + ":" + boost::lexical_cast<std::string>(port); _proxyGreeting = "CONNECT " + url + " HTTP/1.1\r\n"+ "Host: " + url + "\r\n" + "Connection: Keep-Alive\r\n" + "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36\r\n" + "\r\n"; } }
static LPWSTR QueryWindowsProxySettings(const url_scheme scheme, LPCSTR host) { LPWSTR proxy = NULL; BOOL auto_detect = TRUE; LPWSTR auto_config_url = NULL; WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxy_config; if (WinHttpGetIEProxyConfigForCurrentUser(&proxy_config)) { proxy = proxy_config.lpszProxy; auto_detect = proxy_config.fAutoDetect; auto_config_url = proxy_config.lpszAutoConfigUrl; GlobalFree(proxy_config.lpszProxyBypass); } if (auto_detect) { LPWSTR old_url = auto_config_url; DWORD flags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A; if (WinHttpDetectAutoProxyConfigUrl(flags, &auto_config_url)) GlobalFree(old_url); } if (auto_config_url) { HINTERNET session = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (session) { int size = _snwprintf(NULL, 0, L"%s://%S", UrlSchemeStr(scheme), host) + 1; LPWSTR url = malloc(size * sizeof(WCHAR)); if (url) { _snwprintf(url, size, L"%s://%S", UrlSchemeStr(scheme), host); LPWSTR old_proxy = proxy; WINHTTP_PROXY_INFO proxy_info; WINHTTP_AUTOPROXY_OPTIONS options = { .fAutoLogonIfChallenged = TRUE, .dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL, .lpszAutoConfigUrl = auto_config_url, .dwAutoDetectFlags = 0, .lpvReserved = NULL, .dwReserved = 0 }; if (WinHttpGetProxyForUrl(session, url, &options, &proxy_info)) { GlobalFree(old_proxy); GlobalFree(proxy_info.lpszProxyBypass); proxy = proxy_info.lpszProxy; } free(url); } WinHttpCloseHandle(session); } GlobalFree(auto_config_url); } return proxy; } static VOID ParseProxyString(LPWSTR proxy_str, url_scheme scheme, LPCSTR *type, LPCWSTR *host, LPCWSTR *port) { if (proxy_str == NULL) return; LPCWSTR delim = L"; "; LPWSTR token = wcstok(proxy_str, delim); LPCWSTR scheme_str = UrlSchemeStr(scheme); LPCWSTR socks_str = UrlSchemeStr(SOCKS_URL); /* Token format: [<scheme>=][<scheme>"://"]<server>[":"<port>] */ while (token) { BOOL match = FALSE; LPWSTR eq = wcschr(token, '='); LPWSTR css = wcsstr(token, L"://"); /* * If the token has a <scheme>, test for the one we're looking for. * If we're looking for a https proxy, socks will also do. * If it's a proxy without a <scheme> it's only good for https. */ if (eq || css) { if (wcsbegins(token, scheme_str)) { match = TRUE; } else if (scheme == HTTPS_URL && wcsbegins(token, socks_str)) { match = TRUE; scheme = SOCKS_URL; } } else if (scheme == HTTPS_URL) { match = TRUE; } if (match) { LPWSTR server = token; if (css) server = css + 3; else if (eq) server = eq + 1; /* IPv6 addresses are surrounded by brackets */ LPWSTR port_delim; if (server[0] == '[') { server += 1; LPWSTR end = wcschr(server, ']'); if (end == NULL) continue; *end++ = '\0'; port_delim = (*end == ':' ? end : NULL); } else { port_delim = wcsrchr(server, ':'); if (port_delim) *port_delim = '\0'; } *type = (scheme == HTTPS_URL ? "HTTP" : "SOCKS"); *host = server; if (port_delim) *port = port_delim + 1; else *port = (scheme == HTTPS_URL ? L"80": L"1080"); break; } token = wcstok(NULL, delim); } } /* * Respond to management interface PROXY notifications * Input format: REMOTE_NO,PROTOCOL,HOST */ void OnProxy(connection_t *c, char *line) { LPSTR proto, host; char *pos = strchr(line, ','); if (pos == NULL) return; proto = ++pos; pos = strchr(pos, ','); if (pos == NULL) return; *pos = '\0'; host = ++pos; if (host[0] == '\0') return; LPCSTR type = "NONE"; LPCWSTR addr = L"", port = L""; LPWSTR proxy_str = NULL; if (o.proxy_source == manual) { if (o.proxy_type == http && streq(proto, "TCP")) { type = "HTTP"; addr = o.proxy_http_address; port = o.proxy_http_port; } else if (o.proxy_type == socks) { type = "SOCKS"; addr = o.proxy_socks_address; port = o.proxy_socks_port; } } else if (o.proxy_source == windows) { url_scheme scheme = (streq(proto, "TCP") ? HTTPS_URL : SOCKS_URL); proxy_str = QueryWindowsProxySettings(scheme, host); ParseProxyString(proxy_str, scheme, &type, &addr, &port); } char cmd[128]; snprintf(cmd, sizeof(cmd), "proxy %s %S %S", type, addr, port); cmd[sizeof(cmd) - 1] = '\0'; ManagementCommand(c, cmd, NULL, regular); GlobalFree(proxy_str); }