/** * Find the http proxy. * Look through the proxy settings script first. * @param targetURLString : Target remote URL string * @param logger : ref to log object. * @return Proxy object. */ ProxyInfo findHttpProxy(const std::string &targetURLString, Mantid::Kernel::Logger &logger) { ProxyInfo httpProxy; CFDictionaryRef dict = SCDynamicStoreCopyProxies(nullptr); if (!dict) { logger.debug("NetworkProxyOSX SCDynamicStoreCopyProxies returned NULL"); } // Query the proxy pac first. ProxyInfoVec info = proxyInformationFromPac(dict, targetURLString, logger); bool foundHttpProxy = false; for (const auto &proxyInfo : info) { if (proxyInfo.isHttpProxy()) { foundHttpProxy = true; httpProxy = proxyInfo; break; } } // Query the http proxy settings second. if (!foundHttpProxy) { ProxyInfo tempProxy = httpProxyFromSystem(dict); if (tempProxy.isHttpProxy()) { httpProxy = tempProxy; foundHttpProxy = true; } } if (!foundHttpProxy) { logger.debug("NetworkProxyOSX. No system HTTP Proxy set!"); } return httpProxy; }
void SocketStreamHandle::chooseProxy() { #ifndef BUILDING_ON_LEOPARD RetainPtr<CFDictionaryRef> proxyDictionary(AdoptCF, CFNetworkCopySystemProxySettings()); #else // We don't need proxy information often, so there is no need to set up a permanent dynamic store session. RetainPtr<CFDictionaryRef> proxyDictionary(AdoptCF, SCDynamicStoreCopyProxies(0)); #endif // SOCKS or HTTPS (AKA CONNECT) proxies are supported. // WebSocket protocol relies on handshake being transferred unchanged, so we need a proxy that will not modify headers. // Since HTTP proxies must add Via headers, they are highly unlikely to work. // Many CONNECT proxies limit connectivity to port 443, so we prefer SOCKS, if configured. if (!proxyDictionary) { m_connectionType = Direct; return; } // CFNetworkCopyProxiesForURL doesn't know about WebSocket schemes, so pretend to use http. // Always use "https" to get HTTPS proxies in result - we'll try to use those for ws:// even though many are configured to reject connections to ports other than 443. RetainPtr<CFArrayRef> proxyArray(AdoptCF, CFNetworkCopyProxiesForURL(m_httpsURL.get(), proxyDictionary.get())); chooseProxyFromArray(proxyArray.get()); }
/** * Determines the network proxy server to use (if any). * @param url absolute URL for which to get the proxy server (not used) * @return proxy URL, NULL if no proxy or error */ char *vlc_getProxyUrl(const char *url) { VLC_UNUSED(url); CFDictionaryRef proxies = SCDynamicStoreCopyProxies(NULL); char *proxy_url = NULL; if (proxies) { CFNumberRef cfn_httpProxyOn = (CFNumberRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesHTTPEnable); if (cfn_httpProxyOn) { int i_httpProxyOn; CFNumberGetValue(cfn_httpProxyOn, kCFNumberIntType, &i_httpProxyOn); CFRelease(cfn_httpProxyOn); if (i_httpProxyOn == 1) // http proxy is on { CFStringRef httpProxy = (CFStringRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesHTTPProxy); if (httpProxy) { CFNumberRef cfn_httpProxyPort = (CFNumberRef)CFDictionaryGetValue(proxies, kSCPropNetProxiesHTTPPort); int i_httpProxyPort; CFNumberGetValue(cfn_httpProxyPort, kCFNumberIntType, &i_httpProxyPort); CFRelease(cfn_httpProxyPort); CFMutableStringRef outputURL = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, httpProxy); if (i_httpProxyPort > 0) CFStringAppendFormat(outputURL, NULL, CFSTR(":%i"), i_httpProxyPort); char buffer[4096]; if (CFStringGetCString(outputURL, buffer, sizeof(buffer), kCFStringEncodingUTF8)) proxy_url = strdup(buffer); CFRelease(outputURL); } CFRelease(httpProxy); } } CFRelease(proxies); } return proxy_url; }
void SocketStreamHandle::chooseProxy() { // We don't need proxy information often, so there is no need to set up a permanent dynamic store session. RetainPtr<CFDictionaryRef> proxyDictionary(AdoptCF, SCDynamicStoreCopyProxies(0)); // SOCKS or HTTPS (AKA CONNECT) proxies are supported. // WebSocket protocol relies on handshake being transferred unchanged, so we need a proxy that will not modify headers. // Since HTTP proxies must add Via headers, they are highly unlikely to work. // Many CONNECT proxies limit connectivity to port 443, so we prefer SOCKS, if configured. if (!proxyDictionary) { m_connectionType = Direct; return; } // FIXME: check proxy bypass list and ExcludeSimpleHostnames. // FIXME: Support PAC files. CFTypeRef socksEnableCF = CFDictionaryGetValue(proxyDictionary.get(), kSCPropNetProxiesSOCKSEnable); int socksEnable; if (socksEnableCF && CFGetTypeID(socksEnableCF) == CFNumberGetTypeID() && CFNumberGetValue(static_cast<CFNumberRef>(socksEnableCF), kCFNumberIntType, &socksEnable) && socksEnable) { CFTypeRef proxyHost = CFDictionaryGetValue(proxyDictionary.get(), kSCPropNetProxiesSOCKSProxy); CFTypeRef proxyPort = CFDictionaryGetValue(proxyDictionary.get(), kSCPropNetProxiesSOCKSPort); if (proxyHost && CFGetTypeID(proxyHost) == CFStringGetTypeID() && proxyPort && CFGetTypeID(proxyPort) == CFNumberGetTypeID()) { m_proxyHost = static_cast<CFStringRef>(proxyHost); m_proxyPort = static_cast<CFNumberRef>(proxyPort); m_connectionType = SOCKSProxy; return; } } CFTypeRef httpsEnableCF = CFDictionaryGetValue(proxyDictionary.get(), kSCPropNetProxiesHTTPSEnable); int httpsEnable; if (httpsEnableCF && CFGetTypeID(httpsEnableCF) == CFNumberGetTypeID() && CFNumberGetValue(static_cast<CFNumberRef>(httpsEnableCF), kCFNumberIntType, &httpsEnable) && httpsEnable) { CFTypeRef proxyHost = CFDictionaryGetValue(proxyDictionary.get(), kSCPropNetProxiesHTTPSProxy); CFTypeRef proxyPort = CFDictionaryGetValue(proxyDictionary.get(), kSCPropNetProxiesHTTPSPort); if (proxyHost && CFGetTypeID(proxyHost) == CFStringGetTypeID() && proxyPort && CFGetTypeID(proxyPort) == CFNumberGetTypeID()) { m_proxyHost = static_cast<CFStringRef>(proxyHost); m_proxyPort = static_cast<CFNumberRef>(proxyPort); m_connectionType = CONNECTProxy; return; } } m_connectionType = Direct; }
return result; } } static PyObject* get_proxy_settings(PyObject* mod __attribute__((__unused__))) { CFDictionaryRef proxyDict = NULL; CFNumberRef aNum = NULL; CFArrayRef anArray = NULL; PyObject* result = NULL; PyObject* v; int r; proxyDict = SCDynamicStoreCopyProxies(NULL); if (!proxyDict) { Py_RETURN_NONE; } result = PyDict_New(); if (result == NULL) goto error; aNum = CFDictionaryGetValue(proxyDict, kSCPropNetProxiesExcludeSimpleHostnames); if (aNum == NULL) { v = PyBool_FromLong(0); } else { v = PyBool_FromLong(cfnum_to_int32(aNum)); }
/** @brief detects current system proxy * @return QUrl with proxy or empty */ QUrl System::systemProxy(void) { #if defined(Q_OS_LINUX) return QUrl(getenv("http_proxy")); #elif defined(Q_OS_WIN32) HKEY hk; wchar_t proxyval[80]; DWORD buflen = 80; long ret; DWORD enable; DWORD enalen = sizeof(DWORD); ret = RegOpenKeyEx(HKEY_CURRENT_USER, _TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"), 0, KEY_QUERY_VALUE, &hk); if(ret != ERROR_SUCCESS) return QUrl(""); ret = RegQueryValueEx(hk, _TEXT("ProxyServer"), NULL, NULL, (LPBYTE)proxyval, &buflen); if(ret != ERROR_SUCCESS) return QUrl(""); ret = RegQueryValueEx(hk, _TEXT("ProxyEnable"), NULL, NULL, (LPBYTE)&enable, &enalen); if(ret != ERROR_SUCCESS) return QUrl(""); RegCloseKey(hk); //LOG_INFO() << QString::fromWCharArray(proxyval) << QString("%1").arg(enable); if(enable != 0) return QUrl("http://" + QString::fromWCharArray(proxyval)); else return QUrl(""); #elif defined(Q_OS_MACX) CFDictionaryRef dictref; CFStringRef stringref; CFNumberRef numberref; int enable = 0; int port = 0; unsigned int bufsize = 0; char *buf; QUrl proxy; dictref = SCDynamicStoreCopyProxies(NULL); if(dictref == NULL) return proxy; numberref = (CFNumberRef)CFDictionaryGetValue(dictref, kSCPropNetProxiesHTTPEnable); if(numberref != NULL) CFNumberGetValue(numberref, kCFNumberIntType, &enable); if(enable == 1) { // get proxy string stringref = (CFStringRef)CFDictionaryGetValue(dictref, kSCPropNetProxiesHTTPProxy); if(stringref != NULL) { // get number of characters. CFStringGetLength uses UTF-16 code pairs bufsize = CFStringGetLength(stringref) * 2 + 1; buf = (char*)malloc(sizeof(char) * bufsize); if(buf == NULL) { LOG_ERROR() << "can't allocate memory for proxy string!"; CFRelease(dictref); return QUrl(""); } CFStringGetCString(stringref, buf, bufsize, kCFStringEncodingUTF16); numberref = (CFNumberRef)CFDictionaryGetValue(dictref, kSCPropNetProxiesHTTPPort); if(numberref != NULL) CFNumberGetValue(numberref, kCFNumberIntType, &port); proxy.setScheme("http"); proxy.setHost(QString::fromUtf16((unsigned short*)buf)); proxy.setPort(port); free(buf); } } CFRelease(dictref); return proxy; #else return QUrl(""); #endif }
char* DotMacQuery::ReadStream (CFURLRef url, size_t &responseLength) { SInt32 ito; CFNumberRef cfnTo = NULL; CFDictionaryRef proxyDict = NULL; // make a connection to the provided URL CFHTTPMessageRef httpRequestRef = CFHTTPMessageCreateRequest (kCFAllocatorDefault, CFSTR("GET"), url, kCFHTTPVersion1_1); if (httpRequestRef == NULL) { CSSMError::ThrowCSSMError (CSSMERR_DL_RECORD_NOT_FOUND); } // open the stream CFReadStreamRef httpStreamRef = CFReadStreamCreateForHTTPRequest (kCFAllocatorDefault, httpRequestRef); if (httpStreamRef == NULL) { CSSMError::ThrowCSSMError (CSSMERR_DL_RECORD_NOT_FOUND); } // set a reasonable timeout ito = READ_STREAM_TIMEOUT; cfnTo = CFNumberCreate(NULL, kCFNumberSInt32Type, &ito); if(!CFReadStreamSetProperty(httpStreamRef, _kCFStreamPropertyReadTimeout, cfnTo)) { // oh well - keep going } /* set up possible proxy info */ proxyDict = SCDynamicStoreCopyProxies(NULL); if(proxyDict) { CFReadStreamSetProperty(httpStreamRef, kCFStreamPropertyHTTPProxy, proxyDict); } if (CFReadStreamOpen (httpStreamRef) == false) { CFRelease(httpRequestRef); CFRelease(httpStreamRef); CFRelease(cfnTo); if(proxyDict) { CFRelease(proxyDict); } CSSMError::ThrowCSSMError (CSSMERR_DL_RECORD_NOT_FOUND); } char* response = (char*) malloc (kResponseIncrement); size_t responseBufferLength = kResponseIncrement; responseLength = 0; // read data from the stream CFIndex bytesRead = CFReadStreamRead (httpStreamRef, (UInt8*) response + responseLength, kResponseIncrement); while (bytesRead > 0) { responseLength += bytesRead; responseBufferLength = responseLength + kResponseIncrement; response = (char*) realloc (response, responseBufferLength); bytesRead = CFReadStreamRead (httpStreamRef, (UInt8*) response + responseLength, kResponseIncrement); } CFRelease (httpRequestRef); CFRelease (httpStreamRef); CFRelease(cfnTo); if(proxyDict) { CFRelease(proxyDict); } // check for error if (bytesRead < 0) { CSSMError::ThrowCSSMError (CSSMERR_DL_RECORD_NOT_FOUND); } return response; }
QList<QNetworkProxy> macQueryInternal(const QNetworkProxyQuery &query) { QList<QNetworkProxy> result; // obtain a dictionary to the proxy settings: CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL); if (!dict) { qWarning("QNetworkProxyFactory::systemProxyForQuery: SCDynamicStoreCopyProxies returned NULL"); return result; // failed } if (isHostExcluded(dict, query.peerHostName())) { CFRelease(dict); return result; // no proxy for this host } // is there a PAC enabled? If so, use it first. CFNumberRef pacEnabled; if ((pacEnabled = (CFNumberRef)CFDictionaryGetValue(dict, kSCPropNetProxiesProxyAutoConfigEnable))) { int enabled; if (CFNumberGetValue(pacEnabled, kCFNumberIntType, &enabled) && enabled) { // PAC is enabled CFStringRef cfPacLocation = (CFStringRef)CFDictionaryGetValue(dict, kSCPropNetProxiesProxyAutoConfigURLString); #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) { QCFType<CFDataRef> pacData; QCFType<CFURLRef> pacUrl = CFURLCreateWithString(kCFAllocatorDefault, cfPacLocation, NULL); SInt32 errorCode; if (!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, pacUrl, &pacData, NULL, NULL, &errorCode)) { QString pacLocation = QCFString::toQString(cfPacLocation); qWarning("Unable to get the PAC script at \"%s\" (%s)", qPrintable(pacLocation), cfurlErrorDescription(errorCode)); return result; } QCFType<CFStringRef> pacScript = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, pacData, kCFStringEncodingISOLatin1); if (!pacScript) { // This should never happen, but the documentation says it may return NULL if there was a problem creating the object. QString pacLocation = QCFString::toQString(cfPacLocation); qWarning("Unable to read the PAC script at \"%s\"", qPrintable(pacLocation)); return result; } QByteArray encodedURL = query.url().toEncoded(); // converted to UTF-8 if (encodedURL.isEmpty()) { return result; // Invalid URL, abort } QCFType<CFURLRef> targetURL = CFURLCreateWithBytes(kCFAllocatorDefault, (UInt8*)encodedURL.data(), encodedURL.size(), kCFStringEncodingUTF8, NULL); if (!targetURL) { return result; // URL creation problem, abort } QCFType<CFErrorRef> pacError; QCFType<CFArrayRef> proxies = CFNetworkCopyProxiesForAutoConfigurationScript(pacScript, targetURL, &pacError); if (!proxies) { QString pacLocation = QCFString::toQString(cfPacLocation); QCFType<CFStringRef> pacErrorDescription = CFErrorCopyDescription(pacError); qWarning("Execution of PAC script at \"%s\" failed: %s", qPrintable(pacLocation), qPrintable(QCFString::toQString(pacErrorDescription))); return result; } CFIndex size = CFArrayGetCount(proxies); for (CFIndex i = 0; i < size; ++i) { CFDictionaryRef proxy = (CFDictionaryRef)CFArrayGetValueAtIndex(proxies, i); result << proxyFromDictionary(proxy); } return result; } else #endif { QString pacLocation = QCFString::toQString(cfPacLocation); qWarning("Mac system proxy: PAC script at \"%s\" not handled", qPrintable(pacLocation)); } } } // no PAC, decide which proxy we're looking for based on the query bool isHttps = false; QString protocol = query.protocolTag().toLower(); // try the protocol-specific proxy QNetworkProxy protocolSpecificProxy; if (protocol == QLatin1String("ftp")) { protocolSpecificProxy = proxyFromDictionary(dict, QNetworkProxy::FtpCachingProxy, kSCPropNetProxiesFTPEnable, kSCPropNetProxiesFTPProxy, kSCPropNetProxiesFTPPort); } else if (protocol == QLatin1String("http")) { protocolSpecificProxy = proxyFromDictionary(dict, QNetworkProxy::HttpProxy, kSCPropNetProxiesHTTPEnable, kSCPropNetProxiesHTTPProxy, kSCPropNetProxiesHTTPPort); } else if (protocol == QLatin1String("https")) { isHttps = true; protocolSpecificProxy = proxyFromDictionary(dict, QNetworkProxy::HttpProxy, kSCPropNetProxiesHTTPSEnable, kSCPropNetProxiesHTTPSProxy, kSCPropNetProxiesHTTPSPort); } if (protocolSpecificProxy.type() != QNetworkProxy::DefaultProxy) result << protocolSpecificProxy; // let's add SOCKSv5 if present too QNetworkProxy socks5 = proxyFromDictionary(dict, QNetworkProxy::Socks5Proxy, kSCPropNetProxiesSOCKSEnable, kSCPropNetProxiesSOCKSProxy, kSCPropNetProxiesSOCKSPort); if (socks5.type() != QNetworkProxy::DefaultProxy) result << socks5; // let's add the HTTPS proxy if present (and if we haven't added // yet) if (!isHttps) { QNetworkProxy https = proxyFromDictionary(dict, QNetworkProxy::HttpProxy, kSCPropNetProxiesHTTPSEnable, kSCPropNetProxiesHTTPSProxy, kSCPropNetProxiesHTTPSPort); if (https.type() != QNetworkProxy::DefaultProxy && https != protocolSpecificProxy) result << https; } CFRelease(dict); return result; }
void WebService::autoDetectProxy() { Q_DEBUG_BLOCK; #ifdef Q_WS_MAC CFNumberRef enableNum; int enable; // Get the dictionary. CFDictionaryRef proxyDict = SCDynamicStoreCopyProxies( NULL ); bool result = (proxyDict != NULL); // Get the enable flag. This isn't a CFBoolean, but a CFNumber. if (result) { enableNum = (CFNumberRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPEnable ); result = (enableNum != NULL) && (CFGetTypeID(enableNum) == CFNumberGetTypeID()); } if (result) result = CFNumberGetValue( enableNum, kCFNumberIntType, &enable ) && (enable != 0); // Get the proxy host. DNS names must be in ASCII. If you // put a non-ASCII character in the "Secure Web Proxy" // field in the Network preferences panel, the CFStringGetCString // function will fail and this function will return false. CFStringRef hostStr; if (result) { hostStr = (CFStringRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPProxy ); result = (hostStr != NULL) && (CFGetTypeID(hostStr) == CFStringGetTypeID()); } if (result) m_proxyHost = UnicornUtils::CFStringToQString( hostStr ); ////// Get the proxy port. int portInt; CFNumberRef portNum; if (result) { portNum = (CFNumberRef) CFDictionaryGetValue( proxyDict, kSCPropNetProxiesHTTPPort ); result = (portNum != NULL) && (CFGetTypeID(portNum) == CFNumberGetTypeID()); } if (result) result = CFNumberGetValue( portNum, kCFNumberIntType, &portInt ); if (result) m_proxyPort = portInt; ////// Set that we found the proxy information m_isAutoDetectedProxy = true; ////// Clean up. if (proxyDict != NULL) CFRelease( proxyDict ); #elif defined Q_WS_WIN #undef QSettings QString const prefix = "CurrentVersion/Internet Settings/"; QSettings autoProxySettings( QSettings::NativeFormat, QSettings::UserScope, "Microsoft", "Windows", this ); m_isAutoDetectedProxy = autoProxySettings.value( prefix + "ProxyEnable", "" ).toBool(); if( m_isAutoDetectedProxy ) { QString proxySettings = autoProxySettings.value( prefix + "ProxyServer", "" ).toString(); QStringList proxySettingList = proxySettings.split( ":" ); m_proxyHost = proxySettingList.value( 0 ); m_proxyPort = proxySettingList.value( 1 ).toInt(); } #endif // qDebug() << m_proxyHost << ':' << m_proxyPort; }
QList<QNetworkProxy> macQueryInternal(const QNetworkProxyQuery &query) { QList<QNetworkProxy> result; // obtain a dictionary to the proxy settings: CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL); if (!dict) { qWarning("QNetworkProxyFactory::systemProxyForQuery: SCDynamicStoreCopyProxies returned NULL"); return result; // failed } if (isHostExcluded(dict, query.peerHostName())) return result; // no proxy for this host // is there a PAC enabled? If so, use it first. CFNumberRef pacEnabled; if ((pacEnabled = (CFNumberRef)CFDictionaryGetValue(dict, kSCPropNetProxiesProxyAutoConfigEnable))) { int enabled; if (CFNumberGetValue(pacEnabled, kCFNumberIntType, &enabled) && enabled) { // PAC is enabled CFStringRef pacUrl = (CFStringRef)CFDictionaryGetValue(dict, kSCPropNetProxiesProxyAutoConfigURLString); QString url = QCFString::toQString(pacUrl); // ### Use PAC somehow qDebug("Mac system proxy: found PAC script at \"%s\"", qPrintable(url)); } } // no PAC, decide which proxy we're looking for based on the query bool isHttps = false; QString protocol = query.protocolTag().toLower(); // try the protocol-specific proxy QNetworkProxy protocolSpecificProxy; if (protocol == QLatin1String("ftp")) { protocolSpecificProxy = proxyFromDictionary(dict, QNetworkProxy::FtpCachingProxy, kSCPropNetProxiesFTPEnable, kSCPropNetProxiesFTPProxy, kSCPropNetProxiesFTPPort); } else if (protocol == QLatin1String("http")) { protocolSpecificProxy = proxyFromDictionary(dict, QNetworkProxy::HttpProxy, kSCPropNetProxiesHTTPEnable, kSCPropNetProxiesHTTPProxy, kSCPropNetProxiesHTTPPort); } else if (protocol == QLatin1String("https")) { isHttps = true; protocolSpecificProxy = proxyFromDictionary(dict, QNetworkProxy::HttpProxy, kSCPropNetProxiesHTTPSEnable, kSCPropNetProxiesHTTPSProxy, kSCPropNetProxiesHTTPSPort); } if (protocolSpecificProxy.type() != QNetworkProxy::DefaultProxy) result << protocolSpecificProxy; // let's add SOCKSv5 if present too QNetworkProxy socks5 = proxyFromDictionary(dict, QNetworkProxy::Socks5Proxy, kSCPropNetProxiesSOCKSEnable, kSCPropNetProxiesSOCKSProxy, kSCPropNetProxiesSOCKSPort); if (socks5.type() != QNetworkProxy::DefaultProxy) result << socks5; // let's add the HTTPS proxy if present (and if we haven't added // yet) if (!isHttps) { QNetworkProxy https = proxyFromDictionary(dict, QNetworkProxy::HttpProxy, kSCPropNetProxiesHTTPSEnable, kSCPropNetProxiesHTTPSProxy, kSCPropNetProxiesHTTPSPort); if (https.type() != QNetworkProxy::DefaultProxy && https != protocolSpecificProxy) result << https; } return result; }