/** Set the IE proxy to NULL. */ BOOL RemoveConnectionProxy() { logger->debug("RemoveConnectionProxy -- START.\n\0"); INTERNET_PER_CONN_OPTION_LIST conn_options; BOOL bReturn; DWORD dwBufferSize = sizeof(conn_options); conn_options.dwSize = dwBufferSize; conn_options.pszConnection = NULL; //NULL - LAN conn_options.dwOptionCount = 1; conn_options.pOptions = new INTERNET_PER_CONN_OPTION[conn_options.dwOptionCount]; conn_options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; conn_options.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT ; bReturn = InternetSetOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION, &conn_options, dwBufferSize); delete [] conn_options.pOptions; InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); logger->debug("RemoveConnectionProxy -- OK.\n\0"); return bReturn; }
jboolean proxy(JNIEnv* env, const jobject& jobj, const jstring& proxyStr, const int flags) { char* proxyAddressStr = jstringToChar(env, proxyStr); INTERNET_PER_CONN_OPTION_LIST options; BOOL bReturn; DWORD dwBufferSize = sizeof(options); options.dwSize = dwBufferSize; options.pszConnection = NULL;//connNameStr;//NULL == LAN options.dwOptionCount = 3; options.pOptions = new INTERNET_PER_CONN_OPTION[3]; if(!options.pOptions) return FALSE; options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; options.pOptions[0].Value.dwValue = flags; options.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER; options.pOptions[1].Value.pszValue = proxyAddressStr; options.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; LPSTR loc = TEXT("local"); options.pOptions[2].Value.pszValue = loc; bReturn = InternetSetOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION, &options, dwBufferSize); delete [] options.pOptions; InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); delete [] proxyAddressStr; return bReturn; }
BOOL SetConnectionProxy(char * proxyAdressStr , char * connNameStr = NULL) { INTERNET_PER_CONN_OPTION_LIST conn_options; BOOL bReturn; DWORD dwBufferSize = sizeof(conn_options); conn_options.dwSize = dwBufferSize; conn_options.pszConnection = connNameStr;//NULL == LAN conn_options.dwOptionCount = 3; conn_options.pOptions = new INTERNET_PER_CONN_OPTION[3]; if(!conn_options.pOptions) return FALSE; conn_options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; conn_options.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT|PROXY_TYPE_PROXY; conn_options.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER; conn_options.pOptions[1].Value.pszValue = proxyAdressStr; conn_options.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; conn_options.pOptions[2].Value.pszValue = "local"; bReturn = InternetSetOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION, &conn_options, dwBufferSize); delete [] conn_options.pOptions; InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); return bReturn; }
bool BypassHttpProxy::disableProxy() { //conn_name: active connection name. INTERNET_PER_CONN_OPTION_LIST list; BOOL bReturn; DWORD dwBufSize = sizeof(list); // Fill out list struct. list.dwSize = sizeof(list); // NULL == LAN, otherwise connectoid name. list.pszConnection = NULL; // Set three options. list.dwOptionCount = 1; list.pOptions = new INTERNET_PER_CONN_OPTION[list.dwOptionCount]; // Make sure the memory was allocated. if(NULL == list.pOptions) { // Return FALSE if the memory wasn't allocated. LOG_ERROR("failed to allocat memory in DisableConnectionProxy()"); return false; } // Set flags. list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT ; // Set the options on the connection. bReturn = InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize); // Free the allocated memory. delete [] list.pOptions; InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); LOG_DEBUG("bReturn=%d", bReturn); return bReturn; }
/** Sets the proxy for IE and OS. */ BOOL SetConnectionProxy(char * proxyAdressStr , char *bypass) { sprintf_s(cbuffer, "SetConnectionProxy -- START:\nproxyAdressStr: '%s'\nbypass: '******'\n\0", proxyAdressStr, bypass); logger->debug(cbuffer); INTERNET_PER_CONN_OPTION_LIST conn_options; BOOL bReturn; DWORD dwBufferSize = sizeof(conn_options); conn_options.dwSize = dwBufferSize; conn_options.pszConnection = NULL;//NULL == LAN conn_options.dwOptionCount = 3; conn_options.pOptions = new INTERNET_PER_CONN_OPTION[3]; conn_options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; conn_options.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT|PROXY_TYPE_PROXY; conn_options.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER; conn_options.pOptions[1].Value.pszValue = proxyAdressStr; conn_options.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; conn_options.pOptions[2].Value.pszValue = bypass; bReturn = InternetSetOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION, &conn_options, dwBufferSize); delete [] conn_options.pOptions; InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); sprintf_s(cbuffer, "SetConnectionProxy -- OK:\nproxyAdressStr: '%s'\nbypass: '******'\n\0", proxyAdressStr, bypass); logger->debug(cbuffer); return bReturn; }
BOOL setProxy() { INTERNET_PER_CONN_OPTION_LIST list; INTERNET_PER_CONN_OPTION option[3]; unsigned long nSize = sizeof(INTERNET_PER_CONN_OPTION_LIST); option[0].dwOption = INTERNET_PER_CONN_PROXY_SERVER; option[0].Value.pszValue = proxyAddress; option[1].dwOption = INTERNET_PER_CONN_FLAGS; option[1].Value.dwValue = PROXY_TYPE_PROXY | PROXY_TYPE_DIRECT; option[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; option[2].Value.pszValue = L"<local>"; list.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LIST); list.pszConnection = NULL; list.dwOptionCount = 3; list.dwOptionError = 0; list.pOptions = option; if (!InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, nSize)) { return FALSE; } if (!InternetSetOption(NULL, INTERNET_OPTION_REFRESH, NULL, 0)) { return FALSE; } return TRUE; }
BOOL RemoveConnectionProxy(char* connectionNameStr = NULL) { INTERNET_PER_CONN_OPTION_LIST conn_options; BOOL bReturn; DWORD dwBufferSize = sizeof(conn_options); conn_options.dwSize = dwBufferSize; conn_options.pszConnection = connectionNameStr; //NULL - LAN conn_options.dwOptionCount = 1; conn_options.pOptions = new INTERNET_PER_CONN_OPTION[conn_options.dwOptionCount]; if(!conn_options.pOptions) return FALSE; conn_options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; conn_options.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT ; bReturn = InternetSetOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION, &conn_options, dwBufferSize); delete [] conn_options.pOptions; InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); return bReturn; }
jboolean setPacFile(char* proxyAddressStr, const int flags) { INTERNET_PER_CONN_OPTION_LIST options; BOOL bReturn; DWORD dwBufferSize = sizeof(options); options.dwSize = dwBufferSize; options.pszConnection = FindActiveConnection();//connNameStr;//NULL == LAN options.dwOptionCount = 2; options.pOptions = new INTERNET_PER_CONN_OPTION[2]; if(!options.pOptions) { return FALSE; } options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; options.pOptions[0].Value.dwValue = flags; options.pOptions[1].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL; options.pOptions[1].Value.pszValue = proxyAddressStr; //options.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; //options.pOptions[2].Value.pszValue = TEXT("local"); bReturn = InternetSetOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION, &options, dwBufferSize); delete [] options.pOptions; InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); return bReturn; }
//取消代理 //返回:true:成功,false:失败 bool common_delProxy() { HKEY hKey; DWORD dwValue=0; //打开注册表 if (RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, KEY_ALL_ACCESS, &hKey)) { return false; } //设置注册表键值 if (RegSetValueEx(hKey, L"ProxyEnable", 0, REG_DWORD,(LPBYTE)&dwValue, sizeof(DWORD))) { RegCloseKey(hKey); return false; } RegCloseKey(hKey); //发出通知 InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL ,INTERNET_OPTION_REFRESH , NULL, 0); return true; }
BOOL unsetProxy() { INTERNET_PER_CONN_OPTION_LIST list; INTERNET_PER_CONN_OPTION option[1]; BOOL bReturn; DWORD nSize = sizeof(list); option[0].dwOption = INTERNET_PER_CONN_FLAGS; option[0].Value.dwValue = PROXY_TYPE_DIRECT; list.dwSize = sizeof(list); list.pszConnection = NULL; list.dwOptionCount = 1; list.dwOptionError = 0; list.pOptions = option; if (!InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, nSize)) { return FALSE; } if (!InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0)) { return FALSE; } if (!InternetSetOption(NULL, INTERNET_OPTION_REFRESH, NULL, 0)) { return FALSE; } return TRUE; }
BOOL CFtpFile::Connect() { HANDLE hThread; DWORD dwThreadId; DWORD dwExitCode; if(m_UserProx == TRUE) { m_hHint = InternetOpen(NULL,INTERNET_OPEN_TYPE_PROXY,m_strProx.c_str(),NULL,INTERNET_FLAG_ASYNC); } else { m_hHint = InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_FLAG_ASYNC); } long timeout1 = 60000; long timeout2 = 120000; InternetSetOption(m_hHint,INTERNET_OPTION_CONNECT_TIMEOUT,(LPVOID)&timeout1,4); InternetSetOption(m_hHint,INTERNET_OPTION_SEND_TIMEOUT,(LPVOID)&timeout1,4); InternetSetOption(m_hHint,INTERNET_OPTION_RECEIVE_TIMEOUT,(LPVOID)&timeout2,4); InternetSetOption(m_hHint,INTERNET_OPTION_DATA_SEND_TIMEOUT,(LPVOID)&timeout1,4); InternetSetOption(m_hHint,INTERNET_OPTION_DATA_RECEIVE_TIMEOUT,(LPVOID)&timeout2,4); //wininet的超时设置是无效的,所以下面用线程来实现 hThread = CreateThread( NULL, 0, ConnectWork, this, 0, &dwThreadId); //90秒都连接不上,就认为连不上 if(WaitForSingleObject(hThread,90*1000)==WAIT_TIMEOUT) { if(m_hHint) { InternetCloseHandle(m_hHint); g_Datalog.Trace(LOGL_TOP,LOGT_WARNING, __TFILE__,__LINE__,_T("WaitTimeout 10s time not connect ftp")); } WaitForSingleObject(hThread, INFINITE); g_Datalog.Trace(LOGL_TOP,LOGT_WARNING, __TFILE__,__LINE__,_T("WaitThread Terminate")); m_hHint = NULL; return FALSE; } //g_Datalog.Trace(LOGL_TOP,LOGT_PROMPT, __TFILE__,__LINE__, _T("FTP连接完成")); dwExitCode = 0 ; GetExitCodeThread(hThread, &dwExitCode); g_Datalog.Trace(LOGL_TOP,LOGT_PROMPT, __TFILE__,__LINE__, _T("FTP连接完成 exitcode = %d"), dwExitCode); CloseHandle(hThread); if(dwExitCode) { return FALSE; } else { return TRUE; } }
int togglePac(bool turnOn, const char* pacUrl) { int ret = RET_NO_ERROR; INTERNET_PER_CONN_OPTION_LIST options; DWORD dwBufferSize = sizeof(options); options.dwSize = dwBufferSize; options.pszConnection = FindActiveConnection(); options.dwOptionCount = 2; options.pOptions = (INTERNET_PER_CONN_OPTION*)calloc(2, sizeof(INTERNET_PER_CONN_OPTION)); if(!options.pOptions) { return NO_MEMORY; } options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; options.pOptions[1].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL; if (turnOn) { options.pOptions[0].Value.dwValue = PROXY_TYPE_AUTO_PROXY_URL; wchar_t url[150]; if (swprintf(url, 150, L"%hs", pacUrl) == -1) { printf("Unable to convert pacUrl to wchar_t %s\n", strerror(errno)); ret = PAC_URL_CONVERSION_ERROR; goto cleanup; } options.pOptions[1].Value.pszValue = (char*)pacUrl; } else { options.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT; options.pOptions[1].Value.pszValue = ""; } BOOL result; result = InternetSetOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION, &options, dwBufferSize); if (!result) { reportWindowsError("setting options"); ret = SYSCALL_FAILED; goto cleanup; } result = InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); if (!result) { reportWindowsError("propagating changes"); ret = SYSCALL_FAILED; goto cleanup; } result = InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); if (!result) { reportWindowsError("refreshing"); ret = SYSCALL_FAILED; goto cleanup; } cleanup: free(options.pOptions); return ret; }
STDMETHODIMP CBHttpRequest::setProxyCredentials(BSTR strUserName, BSTR strPassword) { CBStringA strUser(strUserName), strPass(strPassword); if(!InternetSetOption(m_hFile, INTERNET_OPTION_PROXY_USERNAME, (LPSTR)(LPCSTR)strUser, strUser.GetLength() + 1)) return GetErrorResult(); if(!InternetSetOption(m_hFile, INTERNET_OPTION_PROXY_PASSWORD, (LPSTR)(LPCSTR)strPass, strPass.GetLength() + 1)) return GetErrorResult(); return S_OK; }
bool Downloader::openInternet() { if(internet) return true; //already opened #ifdef _DEBUG _TCHAR *atype; switch(internetOptions.accessType) { case INTERNET_OPEN_TYPE_DIRECT : atype = _T("INTERNET_OPEN_TYPE_DIRECT"); break; case INTERNET_OPEN_TYPE_PRECONFIG : atype = _T("INTERNET_OPEN_TYPE_PRECONFIG"); break; case INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY: atype = _T("INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY"); break; case INTERNET_OPEN_TYPE_PROXY : atype = _T("INTERNET_OPEN_TYPE_PROXY"); break; default: atype = _T("Unknown (error)!"); } TRACE(_T("Opening internet...")); TRACE(_T(" access type: %s"), atype); TRACE(_T(" proxy name : %s"), internetOptions.proxyName.empty() ? _T("(none)") : internetOptions.proxyName.c_str()); #endif if(!internet) if(!(internet = InternetOpen(internetOptions.userAgent.c_str(), internetOptions.accessType, internetOptions.proxyName.empty() ? NULL : internetOptions.proxyName.c_str(), NULL, 0))) return false; TRACE(_T("Setting timeouts...")); if(internetOptions.connectTimeout != TIMEOUT_DEFAULT) InternetSetOption(internet, INTERNET_OPTION_CONNECT_TIMEOUT, &internetOptions.connectTimeout, sizeof(DWORD)); if(internetOptions.sendTimeout != TIMEOUT_DEFAULT) InternetSetOption(internet, INTERNET_OPTION_SEND_TIMEOUT, &internetOptions.sendTimeout, sizeof(DWORD)); if(internetOptions.receiveTimeout != TIMEOUT_DEFAULT) InternetSetOption(internet, INTERNET_OPTION_RECEIVE_TIMEOUT, &internetOptions.receiveTimeout, sizeof(DWORD)); #ifdef _DEBUG DWORD connectTimeout, sendTimeout, receiveTimeout, bufSize = sizeof(DWORD); InternetQueryOption(internet, INTERNET_OPTION_CONNECT_TIMEOUT, &connectTimeout, &bufSize); InternetQueryOption(internet, INTERNET_OPTION_SEND_TIMEOUT, &sendTimeout, &bufSize); InternetQueryOption(internet, INTERNET_OPTION_RECEIVE_TIMEOUT, &receiveTimeout, &bufSize); TRACE(_T("Internet options:")); TRACE(_T(" Connect timeout: %d"), connectTimeout); TRACE(_T(" Send timeout : %d"), sendTimeout); TRACE(_T(" Receive timeout: %d"), receiveTimeout); #endif return true; }
//------------------------------------------------------------------------------ void GetCSRFToken(VIRUSTOTAL_STR *vts) { HINTERNET M_connexion = 0; if (!use_other_proxy)M_connexion = InternetOpen("",/*INTERNET_OPEN_TYPE_DIRECT*/INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE); else M_connexion = InternetOpen("",/*INTERNET_OPEN_TYPE_DIRECT*/INTERNET_OPEN_TYPE_PROXY, proxy_ch_auth, NULL, 0); if (M_connexion==NULL)M_connexion = InternetOpen("",INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE); if (M_connexion==NULL)return; //init connexion HINTERNET M_session = InternetConnect(M_connexion, "www.virustotal.com",443,"","",INTERNET_SERVICE_HTTP,0,0); if (M_session==NULL) { InternetCloseHandle(M_connexion); return; } //connexion HINTERNET M_requete = HttpOpenRequest(M_session,"GET","www.virustotal.com",NULL,"",NULL, INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_SECURE |INTERNET_FLAG_IGNORE_CERT_CN_INVALID|INTERNET_FLAG_IGNORE_CERT_DATE_INVALID,0); if (use_proxy_advanced_settings) { InternetSetOption(M_requete,INTERNET_OPTION_PROXY_USERNAME,proxy_ch_user,sizeof(proxy_ch_user)); InternetSetOption(M_requete,INTERNET_OPTION_PROXY_PASSWORD,proxy_ch_password,sizeof(proxy_ch_password)); } if (M_requete==NULL) { InternetCloseHandle(M_session); InternetCloseHandle(M_connexion); return; }else if (HttpSendRequest(M_requete, NULL, 0, NULL, 0)) { //traitement !!! char buffer[MAX_PATH]; DWORD dwNumberOfBytesRead = MAX_PATH; if(HttpQueryInfo(M_requete,HTTP_QUERY_SET_COOKIE, buffer, &dwNumberOfBytesRead, 0)) { if (dwNumberOfBytesRead>42)buffer[42]=0; //on passe : csrftoken= strcpy(vts->token,buffer+10); } InternetCloseHandle(M_requete); } //close InternetCloseHandle(M_session); InternetCloseHandle(M_connexion); }
BOOL SetWindowsProxy(WCHAR* szProxy, const WCHAR* szProxyInterface=NULL) { INTERNET_PER_CONN_OPTION_LIST conn_options; BOOL bReturn; DWORD dwBufferSize = sizeof(conn_options); if (wcslen(szProxy) == 0) { conn_options.dwSize = dwBufferSize; conn_options.pszConnection = (WCHAR*)szProxyInterface; conn_options.dwOptionCount = 1; conn_options.pOptions = (INTERNET_PER_CONN_OPTION*)malloc(sizeof(INTERNET_PER_CONN_OPTION)*conn_options.dwOptionCount); conn_options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; conn_options.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT; } else if (wcsstr(szProxy, L"://") != NULL) { conn_options.dwSize = dwBufferSize; conn_options.pszConnection = (WCHAR*)szProxyInterface; conn_options.dwOptionCount = 3; conn_options.pOptions = (INTERNET_PER_CONN_OPTION*)malloc(sizeof(INTERNET_PER_CONN_OPTION)*conn_options.dwOptionCount); conn_options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; conn_options.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT | PROXY_TYPE_AUTO_PROXY_URL; conn_options.pOptions[1].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL; conn_options.pOptions[1].Value.pszValue = szProxy; conn_options.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; conn_options.pOptions[2].Value.pszValue = L"<local>"; } else { conn_options.dwSize = dwBufferSize; conn_options.pszConnection = (WCHAR*)szProxyInterface; conn_options.dwOptionCount = 3; conn_options.pOptions = (INTERNET_PER_CONN_OPTION*)malloc(sizeof(INTERNET_PER_CONN_OPTION)*conn_options.dwOptionCount); conn_options.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; conn_options.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT | PROXY_TYPE_PROXY; conn_options.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER; conn_options.pOptions[1].Value.pszValue = szProxy; conn_options.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; conn_options.pOptions[2].Value.pszValue = L"<local>"; } bReturn = InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &conn_options, dwBufferSize); free(conn_options.pOptions); InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); return bReturn; }
HINTERNET hook_InternetConnect( HINTERNET hInternet, LPCTSTR lpszServerName, INTERNET_PORT nServerPort, LPCTSTR lpszUsername, LPCTSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext ) { if (Setting::m_bProxy) { INTERNET_PER_CONN_OPTION_LIST List; INTERNET_PER_CONN_OPTION Option[2]; unsigned long nSize = sizeof(INTERNET_PER_CONN_OPTION_LIST); Option[0].dwOption = INTERNET_PER_CONN_FLAGS; Option[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER; List.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LIST); List.pszConnection = NULL; List.dwOptionCount = 2; List.dwOptionError = 0; List.pOptions = Option; Option[1].Value.pszValue = L"172.19.1.2:9217"; Option[0].Value.dwValue = PROXY_TYPE_PROXY ; BOOL bRet = InternetSetOption(hInternet, INTERNET_OPTION_PER_CONNECTION_OPTION, &List, nSize); } return g_originConnect(hInternet, lpszServerName, nServerPort, lpszUsername, lpszPassword, dwService, dwFlags, dwContext); }
static DWORD wininet_set_timeout( struct soap * soap, struct wininet_data * a_pData, const char * a_pszTimeout, DWORD a_dwOption, int a_nTimeout ) { UNUSED_ARG( soap ); UNUSED_ARG( a_pszTimeout ); if ( a_nTimeout > 0 ) { DWORD dwTimeout = a_nTimeout * 1000; if ( !InternetSetOption( a_pData->hInternet, a_dwOption, &dwTimeout, sizeof(DWORD) ) ) { DWORD dwErrorCode = GetLastError(); DBGLOG(TEST, SOAP_MESSAGE(fdebug, "wininet %p: failed to set %s timeout, error %d (%s)\n", soap, a_pszTimeout, dwErrorCode, wininet_error_message(soap,dwErrorCode) )); return dwErrorCode; } } return 0; }
int KSI_HttpClientImpl_init(KSI_HttpClient *client) { KSI_HttpClient *http = (KSI_HttpClient *)client; HINTERNET internet_handle; ULONG buf; KSI_CTX *ctx = NULL; int res; if (client == NULL) { res = KSI_INVALID_ARGUMENT; goto cleanup; } ctx = client->parent.ctx; KSI_ERR_clearErrors(ctx); //Initializes an application's use of the Win32 Internet functions. internet_handle = InternetOpenA(http->agentName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (internet_handle == NULL) { WININET_ERROR(ctx, GetLastError(), KSI_NETWORK_ERROR, "WinINet: Unable to init."); } /* By default WinINet allows just two simultaneous connections to one server. */ buf = 1024; res = InternetSetOption(NULL, INTERNET_OPTION_MAX_CONNS_PER_SERVER, &buf, sizeof(buf)); if (res != TRUE) { WININET_ERROR(ctx, GetLastError(), KSI_UNKNOWN_ERROR, "WinINet: Unable to init."); } res = InternetSetOption(NULL, INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER, &buf, sizeof(buf)); if (res != TRUE) { WININET_ERROR(ctx, GetLastError(), KSI_UNKNOWN_ERROR, "WinINet: Unable to init."); } http->implCtx = internet_handle; http->implCtx_free = implCtx_free; internet_handle = NULL; http->sendRequest = wininetSendRequest; res = KSI_OK; cleanup: if (internet_handle) InternetCloseHandle(internet_handle); return res; }
void openConnection (URL_COMPONENTS& uc, HINTERNET sessionHandle, URL::OpenStreamProgressCallback* progressCallback, void* progressCallbackContext) { int disable = 1; InternetSetOption (sessionHandle, INTERNET_OPTION_DISABLE_AUTODIAL, &disable, sizeof (disable)); if (timeOutMs == 0) timeOutMs = 30000; else if (timeOutMs < 0) timeOutMs = -1; InternetSetOption (sessionHandle, INTERNET_OPTION_CONNECT_TIMEOUT, &timeOutMs, sizeof (timeOutMs)); const bool isFtp = address.startsWithIgnoreCase ("ftp:"); #if WORKAROUND_TIMEOUT_BUG connection = 0; { InternetConnectThread connectThread (uc, sessionHandle, connection, isFtp); connectThread.wait (timeOutMs); if (connection == 0) { InternetCloseHandle (sessionHandle); sessionHandle = 0; } } #else connection = InternetConnect (sessionHandle, uc.lpszHostName, uc.nPort, uc.lpszUserName, uc.lpszPassword, isFtp ? (DWORD) INTERNET_SERVICE_FTP : (DWORD) INTERNET_SERVICE_HTTP, 0, 0); #endif if (connection != 0) { if (isFtp) request = FtpOpenFile (connection, uc.lpszUrlPath, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_NEED_FILE, 0); else openHTTPConnection (uc, progressCallback, progressCallbackContext); } }
bool CHttp::SetAuthDetails (HINTERNET hRequest, DWORD httpstatus ) { const unsigned long testsize = 2048; char test[testsize]={0}; DWORD ret; // We have to read all outstanding data on the Internet handle // before we can resubmit request. Just discard the data. do { InternetReadFile (hRequest, (LPVOID)test, testsize-1, &ret ); } while (ret != 0 ); DWORD usrflg, pwdflg; const wyWChar *usrname = {0}, *pwd = {0}; if (httpstatus == HTTP_STATUS_DENIED ) { usrflg = INTERNET_OPTION_USERNAME; pwdflg = INTERNET_OPTION_PASSWORD; usrname = GetChallengeUserName(); pwd = GetChallengePwd(); } else { usrflg = INTERNET_OPTION_PROXY_USERNAME; pwdflg = INTERNET_OPTION_PROXY_PASSWORD; usrname = GetProxyUserName (); pwd = GetProxyPwd (); } /* check that the information is valid */ if (!usrname || !pwd ) return false; if (!InternetSetOption (m_InternetConnect, usrflg, (LPVOID) usrname, wcslen (usrname ) ) ) return false; if (!InternetSetOption (m_InternetConnect, pwdflg, (LPVOID) pwd, lstrlenW (pwd ) ) ) return false; return true; }
bool CHttp::PostData (char * encodeddata, DWORD encodelen ) { INTERNET_BUFFERS bufferin; DWORD flags = IGNORE_CERT, flaglen = 0, byteswritten=0, lasterr = 0; memset (&bufferin, 0, sizeof(INTERNET_BUFFERS)); bufferin.dwStructSize = sizeof(INTERNET_BUFFERS ); bufferin.dwBufferTotal = encodelen; retry: if (!yog_HttpSendRequestEx (m_HttpOpenRequest, &bufferin, NULL, HSR_INITIATE, 0) ) { lasterr = GetLastError(); if (lasterr == ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED ) { // Return ERROR_SUCCESS regardless of clicking on OK or Cancel if(InternetErrorDlg(GetDesktopWindow(), m_HttpOpenRequest, ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED, FLAGS_ERROR_UI_FILTER_FOR_ERRORS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL) != ERROR_SUCCESS ) return false; else goto retry; } else if (lasterr == ERROR_INTERNET_CANNOT_CONNECT ) { return false; } else if ((lasterr == ERROR_INTERNET_INVALID_CA ) || (lasterr == ERROR_INTERNET_SEC_CERT_CN_INVALID ) || (lasterr == ERROR_INTERNET_SEC_CERT_DATE_INVALID ) ) { InternetQueryOption (m_HttpOpenRequest, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&flags, &flaglen); flags |= IGNORE_CERT; InternetSetOption (m_HttpOpenRequest, INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags)); goto retry; } else { return false; } } if (!yog_InternetWriteFile(m_HttpOpenRequest, encodeddata, encodelen, &byteswritten ) ) return false; return true; }
void CKpInternetStream::EnsureInitialized() { if(m_bInitialized) return; m_bInitialized = true; m_hNet = InternetOpen(PWM_PRODUCT_NAME_SHORT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if(m_hNet == NULL) { m_hFile = NULL; return; } if(m_nConnectTimeOut >= 0) { DWORD dwTimeOut = static_cast<DWORD>(m_nConnectTimeOut); if(m_nConnectTimeOut == 0) dwTimeOut = DWORD_MAX; VERIFY(InternetSetOption(m_hNet, INTERNET_OPTION_CONNECT_TIMEOUT, &dwTimeOut, sizeof(DWORD))); } if(m_nConnectRetries >= 0) { DWORD dwRetries = static_cast<DWORD>(m_nConnectRetries); VERIFY(InternetSetOption(m_hNet, INTERNET_OPTION_CONNECT_RETRIES, &dwRetries, sizeof(DWORD))); } if(m_nTransferTimeOut >= 0) { DWORD dwTimeOut = static_cast<DWORD>(m_nTransferTimeOut); if(m_nTransferTimeOut == 0) dwTimeOut = DWORD_MAX; VERIFY(InternetSetOption(m_hNet, INTERNET_OPTION_SEND_TIMEOUT, &dwTimeOut, sizeof(DWORD))); VERIFY(InternetSetOption(m_hNet, INTERNET_OPTION_RECEIVE_TIMEOUT, &dwTimeOut, sizeof(DWORD))); } DWORD dwFlags = (INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD); if((m_strUrl.size() >= 6) && (_tcsnicmp(m_strUrl.c_str(), _T("https:"), 6) == 0)) dwFlags |= INTERNET_FLAG_SECURE; m_hFile = InternetOpenUrl(m_hNet, m_strUrl.c_str(), NULL, 0, dwFlags, 0); }
bool GetRealURL(std::string url, int maxsize, std::string &outstr, int timeout = 10) { char buffer[HTTP_BUFFER_LEN];//下载文件的缓冲区 DWORD bytes_read = 1;//下载的字节数 bool getre = false; outstr = ""; if(url.length() < 6) return false; //打开一个internet连接 HINTERNET internet=InternetOpen(_T("HTTP"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); if(!internet) return false; DWORD dtimeout = timeout * 300; InternetSetOption(internet, INTERNET_OPTION_CONNECT_TIMEOUT, &dtimeout, sizeof(DWORD)); InternetSetOption(internet, INTERNET_OPTION_SEND_TIMEOUT, &dtimeout, sizeof(DWORD)); InternetSetOption(internet, INTERNET_OPTION_RECEIVE_TIMEOUT, &dtimeout, sizeof(DWORD)); dtimeout = timeout/10; InternetSetOption(internet, INTERNET_OPTION_CONNECT_RETRIES, &dtimeout, sizeof(DWORD)); //打开一个http url地址 HINTERNET file_handle=InternetOpenUrl(internet, url.c_str(), NULL, 0, INTERNET_FLAG_RELOAD, 0); if(file_handle) { //从url地址中读取文件内容到缓冲区buffer BOOL b = 0; int readbyte = 0; while(bytes_read > 0) { b = InternetReadFile(file_handle, buffer, 512 , &bytes_read); if(!b) break; readbyte += bytes_read; buffer[bytes_read] = 0; outstr += buffer; } getre = true; } //关闭连接 InternetCloseHandle(internet); return getre; }
STDMETHODIMP CBHttpRequest::setProxy(short ProxySetting, BSTR strProxyServer, BSTR strBypassList) { CBStringA strServer(strProxyServer), strBypass(strBypassList); INTERNET_PROXY_INFO ipi = {ProxySetting, strServer, strBypass}; if(!InternetSetOption(NULL, INTERNET_OPTION_PROXY, &ipi, sizeof(ipi))) return GetErrorResult(); return S_OK; }
DWORD packet_transmit_via_http_wininet(Remote *remote, Packet *packet, PacketRequestCompletion *completion) { DWORD res = 0; HINTERNET hReq; HINTERNET hRes; DWORD retries = 5; DWORD flags; DWORD flen; unsigned char *buffer; flen = sizeof(flags); buffer = malloc( packet->payloadLength + sizeof(TlvHeader) ); if (! buffer) { SetLastError(ERROR_NOT_FOUND); return 0; } memcpy(buffer, &packet->header, sizeof(TlvHeader)); memcpy(buffer + sizeof(TlvHeader), packet->payload, packet->payloadLength); do { flags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_NO_UI; if (remote->transport == METERPRETER_TRANSPORT_HTTPS) { flags |= INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID; } hReq = HttpOpenRequest(remote->hConnection, "POST", remote->uri, NULL, NULL, NULL, flags, 0); if (hReq == NULL) { dprintf("[PACKET RECEIVE] Failed HttpOpenRequest: %d", GetLastError()); SetLastError(ERROR_NOT_FOUND); break; } if (remote->transport == METERPRETER_TRANSPORT_HTTPS) { InternetQueryOption( hReq, INTERNET_OPTION_SECURITY_FLAGS, &flags, &flen); flags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA | SECURITY_FLAG_IGNORE_CERT_CN_INVALID | SECURITY_FLAG_IGNORE_UNKNOWN_CA; InternetSetOption(hReq, INTERNET_OPTION_SECURITY_FLAGS, &flags, flen); } hRes = HttpSendRequest(hReq, NULL, 0, buffer, packet->payloadLength + sizeof(TlvHeader) ); if (! hRes) { dprintf("[PACKET RECEIVE] Failed HttpSendRequest: %d", GetLastError()); SetLastError(ERROR_NOT_FOUND); break; } } while(0); memset(buffer, 0, packet->payloadLength + sizeof(TlvHeader)); InternetCloseHandle(hReq); return res; }
//设置代理 //lpIP:IP地址 //lpPort:端口 //返回:true:成功,false:失败 bool common_setProxy(LPTSTR lpIP,LPTSTR lpPort) { INTERNET_PER_CONN_OPTION_LIST list; BOOL bRet; //填充结构体 list.dwSize = sizeof(list); // NULL等于Lan list.pszConnection = NULL; // 设置三个选项 list.dwOptionCount = 3; list.pOptions = new INTERNET_PER_CONN_OPTION[3]; // 确保内存被分配 if(!list.pOptions) { return false; } //设置标志 list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT | PROXY_TYPE_PROXY; //设置代理名称 list.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER; TCHAR proxyAddress[MAX_PATH]; //清零 memset(proxyAddress,0,MAX_PATH); //处理字符串 lstrcat(proxyAddress,L"http://"); lstrcat(proxyAddress,lpIP); lstrcat(proxyAddress,L":"); lstrcat(proxyAddress,lpPort); //设置连接字符串 list.pOptions[1].Value.pszValue =proxyAddress; //重写代理 list.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; list.pOptions[2].Value.pszValue = L"local"; //设置选项 bRet = InternetSetOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION, &list, sizeof(list)); //释放内存 delete [] list.pOptions; if (!bRet) { return false; } return true; }
bool BypassHttpProxy::enableProxy() { INTERNET_PER_CONN_OPTION_LIST list; BOOL bReturn; DWORD dwBufSize = sizeof(list); list.dwSize = sizeof(list); list.pszConnection = NULL; list.dwOptionCount = 3; list.pOptions = new INTERNET_PER_CONN_OPTION[3]; if(NULL == list.pOptions) { LOG_ERROR("failed to allocat memory in SetConnectionOptions"); return false; } // Set proxy name. list.pOptions[0].dwOption = INTERNET_PER_CONN_PROXY_SERVER; list.pOptions[0].Value.pszValue = L"http=http://127.0.0.1:8080"; // Set flags. list.pOptions[1].dwOption = INTERNET_PER_CONN_FLAGS; list.pOptions[1].Value.dwValue = PROXY_TYPE_DIRECT | PROXY_TYPE_PROXY; // Set proxy override. list.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; list.pOptions[2].Value.pszValue = L"<local>"; // Set the options on the connection. bReturn = InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize); // Free the allocated memory. delete [] list.pOptions; InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); LOG_DEBUG("bReturn=%d", bReturn); return (bool)bReturn; }
int ARDrone::getVersionInfo(void) { #if _WIN32 const char *filename = "version.txt"; // Initialize WinINet HINTERNET hInet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 ); // Set timeout [ms] DWORD ms = 500; InternetSetOption(hInet, INTERNET_OPTION_CONNECT_TIMEOUT, &ms, sizeof(ms)); // Connect to FTP server HINTERNET hConnection = InternetConnect(hInet, ip, ARDRONE_VERSION_PORT, "anonymous", "", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0); if (hConnection == NULL) { CVDRONE_ERROR("InternetConnect(port=%d) was failed. (%s, %d)\n", ARDRONE_VERSION_PORT, __FILE__, __LINE__); InternetCloseHandle(hInet); return 0; } // Clear version ZeroMemory(&version, sizeof(ARDRONE_VERSION)); // Get a file through FTP if (!FtpGetFile(hConnection, filename, filename, FALSE, FILE_ATTRIBUTE_NORMAL, INTERNET_FLAG_TRANSFER_BINARY, 0)) { CVDRONE_ERROR("FtpGetFile() was failed. (%s, %d)\n", __FILE__, __LINE__); InternetCloseHandle(hConnection); InternetCloseHandle(hInet); return 0; } // else { // Open the file FILE *file = fopen(filename, "r"); // Read FW version fscanf(file, "%d.%d.%d\n", &version.major, &version.minor, &version.revision); // Close the file fclose(file); // Delete the file remove(filename); } // Finalize WinINet InternetCloseHandle(hConnection); InternetCloseHandle(hInet); #endif return 1; }
BOOL SetIEProxy(UINT uFlag, wpString szProxyUrl, wpString szScriptUrl, wpString szByPass) { INTERNET_PER_CONN_OPTION_LIST coList; BOOL bReturn; DWORD dwBufSize = sizeof(coList); coList.dwSize = dwBufSize; coList.dwOptionCount = 4; coList.pszConnection = NULL; /*LAN */ coList.pOptions = (INTERNET_PER_CONN_OPTION *)malloc(sizeof(INTERNET_PER_CONN_OPTION)*4); if(coList.pOptions == NULL) { return FALSE; } coList.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS; coList.pOptions[0].Value.dwValue = uFlag | PROXY_TYPE_DIRECT; coList.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER; coList.pOptions[1].Value.pszValue = szProxyUrl; coList.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; coList.pOptions[2].Value.pszValue = szByPass; coList.pOptions[3].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL; coList.pOptions[3].Value.pszValue = szScriptUrl; bReturn = InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &coList, dwBufSize); free(coList.pOptions); if(bReturn == ERROR_SUCCESS) { InternetSetOption(NULL, INTERNET_OPTION_PROXY_SETTINGS_CHANGED, NULL, NULL); } return bReturn; }