2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015, 2018 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "downloadmanager.h"
36 #include <QNetworkAccessManager>
37 #include <QNetworkCookie>
38 #include <QNetworkCookieJar>
39 #include <QNetworkProxy>
40 #include <QNetworkReply>
41 #include <QNetworkRequest>
45 #include "base/global.h"
46 #include "base/logger.h"
47 #include "base/preferences.h"
48 #include "downloadhandlerimpl.h"
49 #include "proxyconfigurationmanager.h"
53 // Disguise as Firefox to avoid web server banning
54 const char DEFAULT_USER_AGENT
[] = "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0";
57 class Net::DownloadManager::NetworkCookieJar final
: public QNetworkCookieJar
60 explicit NetworkCookieJar(QObject
*parent
= nullptr)
61 : QNetworkCookieJar(parent
)
63 const QDateTime now
= QDateTime::currentDateTime();
64 QList
<QNetworkCookie
> cookies
= Preferences::instance()->getNetworkCookies();
65 for (const QNetworkCookie
&cookie
: asConst(Preferences::instance()->getNetworkCookies()))
67 if (cookie
.isSessionCookie() || (cookie
.expirationDate() <= now
))
68 cookies
.removeAll(cookie
);
71 setAllCookies(cookies
);
74 ~NetworkCookieJar() override
76 const QDateTime now
= QDateTime::currentDateTime();
77 QList
<QNetworkCookie
> cookies
= allCookies();
78 for (const QNetworkCookie
&cookie
: asConst(allCookies()))
80 if (cookie
.isSessionCookie() || (cookie
.expirationDate() <= now
))
81 cookies
.removeAll(cookie
);
84 Preferences::instance()->setNetworkCookies(cookies
);
87 using QNetworkCookieJar::allCookies
;
88 using QNetworkCookieJar::setAllCookies
;
90 QList
<QNetworkCookie
> cookiesForUrl(const QUrl
&url
) const override
92 const QDateTime now
= QDateTime::currentDateTime();
93 QList
<QNetworkCookie
> cookies
= QNetworkCookieJar::cookiesForUrl(url
);
94 for (const QNetworkCookie
&cookie
: asConst(QNetworkCookieJar::cookiesForUrl(url
)))
96 if (!cookie
.isSessionCookie() && (cookie
.expirationDate() <= now
))
97 cookies
.removeAll(cookie
);
103 bool setCookiesFromUrl(const QList
<QNetworkCookie
> &cookieList
, const QUrl
&url
) override
105 const QDateTime now
= QDateTime::currentDateTime();
106 QList
<QNetworkCookie
> cookies
= cookieList
;
107 for (const QNetworkCookie
&cookie
: cookieList
)
109 if (!cookie
.isSessionCookie() && (cookie
.expirationDate() <= now
))
110 cookies
.removeAll(cookie
);
113 return QNetworkCookieJar::setCookiesFromUrl(cookies
, url
);
117 Net::DownloadManager
*Net::DownloadManager::m_instance
= nullptr;
119 Net::DownloadManager::DownloadManager(QObject
*parent
)
121 , m_networkCookieJar
{new NetworkCookieJar(this)}
122 , m_networkManager
{new QNetworkAccessManager(this)}
124 m_networkManager
->setCookieJar(m_networkCookieJar
);
125 connect(m_networkManager
, &QNetworkAccessManager::sslErrors
, this
126 , [](QNetworkReply
*reply
, const QList
<QSslError
> &errors
)
128 QStringList errorList
;
129 for (const QSslError
&error
: errors
)
130 errorList
+= error
.errorString();
131 LogMsg(tr("Ignoring SSL error, URL: \"%1\", errors: \"%2\"").arg(reply
->url().toString(), errorList
.join(u
". ")), Log::WARNING
);
133 // Ignore all SSL errors
134 reply
->ignoreSslErrors();
137 connect(ProxyConfigurationManager::instance(), &ProxyConfigurationManager::proxyConfigurationChanged
138 , this, &DownloadManager::applyProxySettings
);
139 connect(Preferences::instance(), &Preferences::changed
, this, &DownloadManager::applyProxySettings
);
140 applyProxySettings();
143 void Net::DownloadManager::initInstance()
146 m_instance
= new DownloadManager
;
149 void Net::DownloadManager::freeInstance()
152 m_instance
= nullptr;
155 Net::DownloadManager
*Net::DownloadManager::instance()
160 Net::DownloadHandler
*Net::DownloadManager::download(const DownloadRequest
&downloadRequest
, const bool useProxy
)
162 // Process download request
163 const ServiceID id
= ServiceID::fromURL(downloadRequest
.url());
164 const bool isSequentialService
= m_sequentialServices
.contains(id
);
166 auto *downloadHandler
= new DownloadHandlerImpl(this, downloadRequest
, useProxy
);
167 connect(downloadHandler
, &DownloadHandler::finished
, downloadHandler
, &QObject::deleteLater
);
168 connect(downloadHandler
, &QObject::destroyed
, this, [this, id
, downloadHandler
]()
170 m_waitingJobs
[id
].removeOne(downloadHandler
);
173 if (isSequentialService
&& m_busyServices
.contains(id
))
175 m_waitingJobs
[id
].enqueue(downloadHandler
);
179 qDebug("Downloading %s...", qUtf8Printable(downloadRequest
.url()));
180 if (isSequentialService
)
181 m_busyServices
.insert(id
);
182 processRequest(downloadHandler
);
185 return downloadHandler
;
188 void Net::DownloadManager::registerSequentialService(const Net::ServiceID
&serviceID
)
190 m_sequentialServices
.insert(serviceID
);
193 QList
<QNetworkCookie
> Net::DownloadManager::cookiesForUrl(const QUrl
&url
) const
195 return m_networkCookieJar
->cookiesForUrl(url
);
198 bool Net::DownloadManager::setCookiesFromUrl(const QList
<QNetworkCookie
> &cookieList
, const QUrl
&url
)
200 return m_networkCookieJar
->setCookiesFromUrl(cookieList
, url
);
203 QList
<QNetworkCookie
> Net::DownloadManager::allCookies() const
205 return m_networkCookieJar
->allCookies();
208 void Net::DownloadManager::setAllCookies(const QList
<QNetworkCookie
> &cookieList
)
210 m_networkCookieJar
->setAllCookies(cookieList
);
213 bool Net::DownloadManager::deleteCookie(const QNetworkCookie
&cookie
)
215 return m_networkCookieJar
->deleteCookie(cookie
);
218 bool Net::DownloadManager::hasSupportedScheme(const QString
&url
)
220 const QStringList schemes
= QNetworkAccessManager().supportedSchemes();
221 return std::any_of(schemes
.cbegin(), schemes
.cend(), [&url
](const QString
&scheme
)
223 return url
.startsWith((scheme
+ u
':'), Qt::CaseInsensitive
);
227 void Net::DownloadManager::applyProxySettings()
229 const auto *proxyManager
= ProxyConfigurationManager::instance();
230 const ProxyConfiguration proxyConfig
= proxyManager
->proxyConfiguration();
232 m_proxy
= QNetworkProxy(QNetworkProxy::NoProxy
);
234 if ((proxyConfig
.type
== Net::ProxyType::None
) || (proxyConfig
.type
== ProxyType::SOCKS4
))
238 if (proxyConfig
.type
== ProxyType::SOCKS5
)
240 qDebug() << Q_FUNC_INFO
<< "using SOCKS proxy";
241 m_proxy
.setType(QNetworkProxy::Socks5Proxy
);
245 qDebug() << Q_FUNC_INFO
<< "using HTTP proxy";
246 m_proxy
.setType(QNetworkProxy::HttpProxy
);
249 m_proxy
.setHostName(proxyConfig
.ip
);
250 m_proxy
.setPort(proxyConfig
.port
);
253 if (proxyConfig
.authEnabled
)
255 qDebug("Proxy requires authentication, authenticating...");
256 m_proxy
.setUser(proxyConfig
.username
);
257 m_proxy
.setPassword(proxyConfig
.password
);
260 if (proxyConfig
.hostnameLookupEnabled
)
261 m_proxy
.setCapabilities(m_proxy
.capabilities() | QNetworkProxy::HostNameLookupCapability
);
263 m_proxy
.setCapabilities(m_proxy
.capabilities() & ~QNetworkProxy::HostNameLookupCapability
);
266 void Net::DownloadManager::handleDownloadFinished(DownloadHandlerImpl
*finishedHandler
)
268 const ServiceID id
= ServiceID::fromURL(finishedHandler
->url());
269 const auto waitingJobsIter
= m_waitingJobs
.find(id
);
270 if ((waitingJobsIter
== m_waitingJobs
.end()) || waitingJobsIter
.value().isEmpty())
272 // No more waiting jobs for given ServiceID
273 m_busyServices
.remove(id
);
277 auto *handler
= waitingJobsIter
.value().dequeue();
278 qDebug("Downloading %s...", qUtf8Printable(handler
->url()));
279 processRequest(handler
);
280 handler
->disconnect(this);
283 void Net::DownloadManager::processRequest(DownloadHandlerImpl
*downloadHandler
)
285 m_networkManager
->setProxy((downloadHandler
->useProxy() == true) ? m_proxy
: QNetworkProxy(QNetworkProxy::NoProxy
));
287 const DownloadRequest downloadRequest
= downloadHandler
->downloadRequest();
288 QNetworkRequest request
{downloadRequest
.url()};
290 if (downloadRequest
.userAgent().isEmpty())
291 request
.setRawHeader("User-Agent", DEFAULT_USER_AGENT
);
293 request
.setRawHeader("User-Agent", downloadRequest
.userAgent().toUtf8());
295 // Spoof HTTP Referer to allow adding torrent link from Torcache/KickAssTorrents
296 request
.setRawHeader("Referer", request
.url().toEncoded().data());
297 #ifdef QT_NO_COMPRESS
298 // The macro "QT_NO_COMPRESS" defined in QT will disable the zlib related features
299 // and reply data auto-decompression in QT will also be disabled. But we can support
300 // gzip encoding and manually decompress the reply data.
301 request
.setRawHeader("Accept-Encoding", "gzip");
303 // Qt doesn't support Magnet protocol so we need to handle redirections manually
304 request
.setAttribute(QNetworkRequest::RedirectPolicyAttribute
, QNetworkRequest::ManualRedirectPolicy
);
306 QNetworkReply
*reply
= m_networkManager
->get(request
);
307 connect(reply
, &QNetworkReply::finished
, this, [this, downloadHandler
]
309 handleDownloadFinished(downloadHandler
);
311 downloadHandler
->assignNetworkReply(reply
);
314 Net::DownloadRequest::DownloadRequest(const QString
&url
)
319 QString
Net::DownloadRequest::url() const
324 Net::DownloadRequest
&Net::DownloadRequest::url(const QString
&value
)
330 QString
Net::DownloadRequest::userAgent() const
335 Net::DownloadRequest
&Net::DownloadRequest::userAgent(const QString
&value
)
341 qint64
Net::DownloadRequest::limit() const
346 Net::DownloadRequest
&Net::DownloadRequest::limit(const qint64 value
)
352 bool Net::DownloadRequest::saveToFile() const
357 Net::DownloadRequest
&Net::DownloadRequest::saveToFile(const bool value
)
359 m_saveToFile
= value
;
363 Path
Net::DownloadRequest::destFileName() const
365 return m_destFileName
;
368 Net::DownloadRequest
&Net::DownloadRequest::destFileName(const Path
&value
)
370 m_destFileName
= value
;
374 Net::ServiceID
Net::ServiceID::fromURL(const QUrl
&url
)
376 return {url
.host(), url
.port(80)};
379 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
380 std::size_t Net::qHash(const ServiceID
&serviceID
, const std::size_t seed
)
382 return qHashMulti(seed
, serviceID
.hostName
, serviceID
.port
);
385 uint
Net::qHash(const ServiceID
&serviceID
, const uint seed
)
387 return ::qHash(serviceID
.hostName
, seed
) ^ ::qHash(serviceID
.port
);
391 bool Net::operator==(const ServiceID
&lhs
, const ServiceID
&rhs
)
393 return ((lhs
.hostName
== rhs
.hostName
) && (lhs
.port
== rhs
.port
));