Pass OS proxy settings to libcurl

Closes #186
This commit is contained in:
Floris Bos 2021-11-18 11:16:30 +01:00
parent 6a1744e29d
commit e79467b626

View file

@ -20,6 +20,7 @@
#include <QProcess> #include <QProcess>
#include <QSettings> #include <QSettings>
#include <QtConcurrent/QtConcurrent> #include <QtConcurrent/QtConcurrent>
#include <QtNetwork/QNetworkProxy>
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
#include <sys/ioctl.h> #include <sys/ioctl.h>
@ -350,6 +351,37 @@ void DownloadThread::run()
if (!_useragent.isEmpty()) if (!_useragent.isEmpty())
curl_easy_setopt(_c, CURLOPT_USERAGENT, _useragent.constData()); curl_easy_setopt(_c, CURLOPT_USERAGENT, _useragent.constData());
if (_proxy.isEmpty())
{
#ifndef QT_NO_NETWORKPROXY
/* Ask OS for proxy information. */
QNetworkProxyQuery npq{QUrl{_url}};
QList<QNetworkProxy> proxyList = QNetworkProxyFactory::systemProxyForQuery(npq);
if (!proxyList.isEmpty())
{
QNetworkProxy proxy = proxyList.first();
if (proxy.type() != proxy.NoProxy)
{
QUrl proxyUrl;
proxyUrl.setScheme(proxy.type() == proxy.Socks5Proxy ? "socks5h" : "http");
proxyUrl.setHost(proxy.hostName());
proxyUrl.setPort(proxy.port());
qDebug() << "Using proxy server:" << proxyUrl;
if (!proxy.user().isEmpty())
{
proxyUrl.setUserName(proxy.user());
proxyUrl.setPassword(proxy.password());
}
_proxy = proxyUrl.toEncoded();
}
}
#endif
}
if (!_proxy.isEmpty()) if (!_proxy.isEmpty())
curl_easy_setopt(_c, CURLOPT_PROXY, _proxy.constData()); curl_easy_setopt(_c, CURLOPT_PROXY, _proxy.constData());