2015-03-28 20:22:22 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
2018-06-21 17:50:40 +03:00
|
|
|
* Copyright (C) 2015, 2018 Vladimir Golovnev <glassez@yandex.ru>
|
2015-03-28 20:22:22 +03:00
|
|
|
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*/
|
|
|
|
|
2016-05-01 11:05:52 +03:00
|
|
|
#include "downloadmanager.h"
|
|
|
|
|
2018-12-29 15:38:51 +03:00
|
|
|
#include <algorithm>
|
|
|
|
|
2015-12-18 14:30:31 +03:00
|
|
|
#include <QDateTime>
|
2016-05-01 11:05:52 +03:00
|
|
|
#include <QDebug>
|
2015-03-28 20:22:22 +03:00
|
|
|
#include <QNetworkCookie>
|
2015-12-18 14:30:31 +03:00
|
|
|
#include <QNetworkCookieJar>
|
2016-05-01 11:05:52 +03:00
|
|
|
#include <QNetworkProxy>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2015-03-28 20:22:22 +03:00
|
|
|
#include <QSslError>
|
|
|
|
#include <QUrl>
|
|
|
|
|
2018-11-18 21:40:37 +03:00
|
|
|
#include "base/global.h"
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/preferences.h"
|
2015-03-28 20:22:22 +03:00
|
|
|
#include "downloadhandler.h"
|
2016-05-01 11:05:52 +03:00
|
|
|
#include "proxyconfigurationmanager.h"
|
2015-03-28 20:22:22 +03:00
|
|
|
|
2015-12-19 09:48:46 +03:00
|
|
|
// Spoof Firefox 38 user agent to avoid web server banning
|
|
|
|
const char DEFAULT_USER_AGENT[] = "Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0";
|
|
|
|
|
2015-12-18 14:30:31 +03:00
|
|
|
namespace
|
|
|
|
{
|
2018-04-14 22:53:45 +03:00
|
|
|
class NetworkCookieJar : public QNetworkCookieJar
|
2015-12-18 14:30:31 +03:00
|
|
|
{
|
|
|
|
public:
|
2018-04-15 13:06:31 +03:00
|
|
|
explicit NetworkCookieJar(QObject *parent = nullptr)
|
2015-12-18 14:30:31 +03:00
|
|
|
: QNetworkCookieJar(parent)
|
|
|
|
{
|
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
QList<QNetworkCookie> cookies = Preferences::instance()->getNetworkCookies();
|
2018-11-27 23:15:04 +03:00
|
|
|
for (const QNetworkCookie &cookie : asConst(Preferences::instance()->getNetworkCookies())) {
|
2015-12-18 14:30:31 +03:00
|
|
|
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
|
|
|
|
cookies.removeAll(cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
setAllCookies(cookies);
|
|
|
|
}
|
|
|
|
|
2018-09-07 14:12:38 +03:00
|
|
|
~NetworkCookieJar() override
|
2015-12-18 14:30:31 +03:00
|
|
|
{
|
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
QList<QNetworkCookie> cookies = allCookies();
|
2018-11-27 23:15:04 +03:00
|
|
|
for (const QNetworkCookie &cookie : asConst(allCookies())) {
|
2015-12-18 14:30:31 +03:00
|
|
|
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
|
|
|
|
cookies.removeAll(cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
Preferences::instance()->setNetworkCookies(cookies);
|
|
|
|
}
|
|
|
|
|
2016-01-23 15:12:13 +03:00
|
|
|
using QNetworkCookieJar::allCookies;
|
|
|
|
using QNetworkCookieJar::setAllCookies;
|
|
|
|
|
2015-12-18 14:30:31 +03:00
|
|
|
QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const override
|
|
|
|
{
|
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
QList<QNetworkCookie> cookies = QNetworkCookieJar::cookiesForUrl(url);
|
2018-11-27 23:15:04 +03:00
|
|
|
for (const QNetworkCookie &cookie : asConst(QNetworkCookieJar::cookiesForUrl(url))) {
|
2015-12-18 14:30:31 +03:00
|
|
|
if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now))
|
|
|
|
cookies.removeAll(cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cookies;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url) override
|
|
|
|
{
|
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
QList<QNetworkCookie> cookies = cookieList;
|
2018-11-18 21:40:37 +03:00
|
|
|
for (const QNetworkCookie &cookie : cookieList) {
|
2015-12-18 14:30:31 +03:00
|
|
|
if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now))
|
|
|
|
cookies.removeAll(cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QNetworkCookieJar::setCookiesFromUrl(cookies, url);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
QNetworkRequest createNetworkRequest(const Net::DownloadRequest &downloadRequest)
|
|
|
|
{
|
|
|
|
QNetworkRequest request {downloadRequest.url()};
|
|
|
|
|
|
|
|
if (downloadRequest.userAgent().isEmpty())
|
|
|
|
request.setRawHeader("User-Agent", DEFAULT_USER_AGENT);
|
|
|
|
else
|
|
|
|
request.setRawHeader("User-Agent", downloadRequest.userAgent().toUtf8());
|
|
|
|
|
|
|
|
// Spoof HTTP Referer to allow adding torrent link from Torcache/KickAssTorrents
|
|
|
|
request.setRawHeader("Referer", request.url().toEncoded().data());
|
|
|
|
// Accept gzip
|
|
|
|
request.setRawHeader("Accept-Encoding", "gzip");
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
}
|
2015-03-28 20:22:22 +03:00
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
Net::DownloadManager *Net::DownloadManager::m_instance = nullptr;
|
2015-03-28 20:22:22 +03:00
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
Net::DownloadManager::DownloadManager(QObject *parent)
|
2015-03-28 20:22:22 +03:00
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
#ifndef QT_NO_OPENSSL
|
2018-04-18 16:59:41 +03:00
|
|
|
connect(&m_networkManager, &QNetworkAccessManager::sslErrors, this, &Net::DownloadManager::ignoreSslErrors);
|
2015-03-28 20:22:22 +03:00
|
|
|
#endif
|
2018-06-21 17:50:40 +03:00
|
|
|
connect(&m_networkManager, &QNetworkAccessManager::finished, this, &DownloadManager::handleReplyFinished);
|
|
|
|
connect(ProxyConfigurationManager::instance(), &ProxyConfigurationManager::proxyConfigurationChanged
|
|
|
|
, this, &DownloadManager::applyProxySettings);
|
2015-12-18 14:30:31 +03:00
|
|
|
m_networkManager.setCookieJar(new NetworkCookieJar(this));
|
2018-07-13 12:29:50 +03:00
|
|
|
applyProxySettings();
|
2015-03-28 20:22:22 +03:00
|
|
|
}
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
void Net::DownloadManager::initInstance()
|
2015-03-28 20:22:22 +03:00
|
|
|
{
|
|
|
|
if (!m_instance)
|
|
|
|
m_instance = new DownloadManager;
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
void Net::DownloadManager::freeInstance()
|
2015-03-28 20:22:22 +03:00
|
|
|
{
|
|
|
|
if (m_instance) {
|
|
|
|
delete m_instance;
|
2018-04-15 13:06:31 +03:00
|
|
|
m_instance = nullptr;
|
2015-03-28 20:22:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
Net::DownloadManager *Net::DownloadManager::instance()
|
2015-03-28 20:22:22 +03:00
|
|
|
{
|
|
|
|
return m_instance;
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
Net::DownloadHandler *Net::DownloadManager::download(const DownloadRequest &downloadRequest)
|
2015-03-28 20:22:22 +03:00
|
|
|
{
|
|
|
|
// Process download request
|
2018-06-21 17:50:40 +03:00
|
|
|
const QNetworkRequest request = createNetworkRequest(downloadRequest);
|
|
|
|
const ServiceID id = ServiceID::fromURL(request.url());
|
|
|
|
const bool isSequentialService = m_sequentialServices.contains(id);
|
|
|
|
if (!isSequentialService || !m_busyServices.contains(id)) {
|
|
|
|
qDebug("Downloading %s...", qUtf8Printable(downloadRequest.url()));
|
|
|
|
if (isSequentialService)
|
|
|
|
m_busyServices.insert(id);
|
|
|
|
return new DownloadHandler {
|
|
|
|
m_networkManager.get(request), this, downloadRequest};
|
|
|
|
}
|
2015-03-28 20:22:22 +03:00
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
auto *downloadHandler = new DownloadHandler {nullptr, this, downloadRequest};
|
|
|
|
connect(downloadHandler, &DownloadHandler::destroyed, this, [this, id, downloadHandler]()
|
|
|
|
{
|
|
|
|
m_waitingJobs[id].removeOne(downloadHandler);
|
|
|
|
});
|
|
|
|
m_waitingJobs[id].enqueue(downloadHandler);
|
|
|
|
return downloadHandler;
|
|
|
|
}
|
2015-09-11 09:41:49 +03:00
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
void Net::DownloadManager::registerSequentialService(const Net::ServiceID &serviceID)
|
|
|
|
{
|
|
|
|
m_sequentialServices.insert(serviceID);
|
2015-03-28 20:22:22 +03:00
|
|
|
}
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
QList<QNetworkCookie> Net::DownloadManager::cookiesForUrl(const QUrl &url) const
|
2015-03-28 20:22:22 +03:00
|
|
|
{
|
|
|
|
return m_networkManager.cookieJar()->cookiesForUrl(url);
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
bool Net::DownloadManager::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
|
2015-03-28 20:22:22 +03:00
|
|
|
{
|
|
|
|
return m_networkManager.cookieJar()->setCookiesFromUrl(cookieList, url);
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
QList<QNetworkCookie> Net::DownloadManager::allCookies() const
|
2016-01-23 15:12:13 +03:00
|
|
|
{
|
|
|
|
return static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->allCookies();
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
void Net::DownloadManager::setAllCookies(const QList<QNetworkCookie> &cookieList)
|
2016-01-23 15:12:13 +03:00
|
|
|
{
|
|
|
|
static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->setAllCookies(cookieList);
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
bool Net::DownloadManager::deleteCookie(const QNetworkCookie &cookie)
|
2015-12-18 14:30:31 +03:00
|
|
|
{
|
|
|
|
return static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->deleteCookie(cookie);
|
|
|
|
}
|
|
|
|
|
2018-12-29 15:38:51 +03:00
|
|
|
bool Net::DownloadManager::hasSupportedScheme(const QString &url)
|
|
|
|
{
|
|
|
|
const QStringList schemes = instance()->m_networkManager.supportedSchemes();
|
|
|
|
return std::any_of(schemes.cbegin(), schemes.cend(), [&url](const QString &scheme)
|
|
|
|
{
|
|
|
|
return url.startsWith((scheme + QLatin1Char(':')), Qt::CaseInsensitive);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
void Net::DownloadManager::applyProxySettings()
|
2015-03-28 20:22:22 +03:00
|
|
|
{
|
2016-05-01 11:05:52 +03:00
|
|
|
auto proxyManager = ProxyConfigurationManager::instance();
|
|
|
|
ProxyConfiguration proxyConfig = proxyManager->proxyConfiguration();
|
2015-03-28 20:22:22 +03:00
|
|
|
QNetworkProxy proxy;
|
|
|
|
|
2016-10-30 01:21:46 +03:00
|
|
|
if (!proxyManager->isProxyOnlyForTorrents() && (proxyConfig.type != ProxyType::None)) {
|
2015-03-28 20:22:22 +03:00
|
|
|
// Proxy enabled
|
2016-05-01 11:05:52 +03:00
|
|
|
proxy.setHostName(proxyConfig.ip);
|
|
|
|
proxy.setPort(proxyConfig.port);
|
2015-03-28 20:22:22 +03:00
|
|
|
// Default proxy type is HTTP, we must change if it is SOCKS5
|
2016-05-01 11:05:52 +03:00
|
|
|
if ((proxyConfig.type == ProxyType::SOCKS5) || (proxyConfig.type == ProxyType::SOCKS5_PW)) {
|
2015-03-28 20:22:22 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << "using SOCKS proxy";
|
|
|
|
proxy.setType(QNetworkProxy::Socks5Proxy);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qDebug() << Q_FUNC_INFO << "using HTTP proxy";
|
|
|
|
proxy.setType(QNetworkProxy::HttpProxy);
|
|
|
|
}
|
|
|
|
// Authentication?
|
2016-05-01 11:05:52 +03:00
|
|
|
if (proxyManager->isAuthenticationRequired()) {
|
2018-06-21 17:50:40 +03:00
|
|
|
qDebug("Proxy requires authentication, authenticating...");
|
2016-05-01 11:05:52 +03:00
|
|
|
proxy.setUser(proxyConfig.username);
|
|
|
|
proxy.setPassword(proxyConfig.password);
|
2015-03-28 20:22:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
proxy.setType(QNetworkProxy::NoProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_networkManager.setProxy(proxy);
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:50:40 +03:00
|
|
|
void Net::DownloadManager::handleReplyFinished(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
const ServiceID id = ServiceID::fromURL(reply->url());
|
|
|
|
auto waitingJobsIter = m_waitingJobs.find(id);
|
|
|
|
if ((waitingJobsIter == m_waitingJobs.end()) || waitingJobsIter.value().isEmpty()) {
|
|
|
|
m_busyServices.remove(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadHandler *handler = waitingJobsIter.value().dequeue();
|
|
|
|
qDebug("Downloading %s...", qUtf8Printable(handler->m_downloadRequest.url()));
|
|
|
|
handler->assignNetworkReply(m_networkManager.get(createNetworkRequest(handler->m_downloadRequest)));
|
|
|
|
handler->disconnect(this);
|
|
|
|
}
|
|
|
|
|
2015-03-28 20:22:22 +03:00
|
|
|
#ifndef QT_NO_OPENSSL
|
2018-06-21 17:50:40 +03:00
|
|
|
void Net::DownloadManager::ignoreSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
|
2015-03-28 20:22:22 +03:00
|
|
|
{
|
|
|
|
Q_UNUSED(errors)
|
|
|
|
// Ignore all SSL errors
|
|
|
|
reply->ignoreSslErrors();
|
|
|
|
}
|
|
|
|
#endif
|
2018-06-20 17:07:07 +03:00
|
|
|
|
|
|
|
Net::DownloadRequest::DownloadRequest(const QString &url)
|
|
|
|
: m_url {url}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Net::DownloadRequest::url() const
|
|
|
|
{
|
|
|
|
return m_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
Net::DownloadRequest &Net::DownloadRequest::url(const QString &value)
|
|
|
|
{
|
|
|
|
m_url = value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Net::DownloadRequest::userAgent() const
|
|
|
|
{
|
|
|
|
return m_userAgent;
|
|
|
|
}
|
|
|
|
|
|
|
|
Net::DownloadRequest &Net::DownloadRequest::userAgent(const QString &value)
|
|
|
|
{
|
|
|
|
m_userAgent = value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 Net::DownloadRequest::limit() const
|
|
|
|
{
|
|
|
|
return m_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
Net::DownloadRequest &Net::DownloadRequest::limit(qint64 value)
|
|
|
|
{
|
|
|
|
m_limit = value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Net::DownloadRequest::saveToFile() const
|
|
|
|
{
|
|
|
|
return m_saveToFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
Net::DownloadRequest &Net::DownloadRequest::saveToFile(bool value)
|
|
|
|
{
|
|
|
|
m_saveToFile = value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Net::DownloadRequest::handleRedirectToMagnet() const
|
|
|
|
{
|
|
|
|
return m_handleRedirectToMagnet;
|
|
|
|
}
|
|
|
|
|
|
|
|
Net::DownloadRequest &Net::DownloadRequest::handleRedirectToMagnet(bool value)
|
|
|
|
{
|
|
|
|
m_handleRedirectToMagnet = value;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-06-21 17:50:40 +03:00
|
|
|
|
|
|
|
Net::ServiceID Net::ServiceID::fromURL(const QUrl &url)
|
|
|
|
{
|
|
|
|
return {url.host(), url.port(80)};
|
|
|
|
}
|
|
|
|
|
|
|
|
uint Net::qHash(const ServiceID &serviceID, uint seed)
|
|
|
|
{
|
|
|
|
return ::qHash(serviceID.hostName, seed) ^ serviceID.port;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Net::operator==(const ServiceID &lhs, const ServiceID &rhs)
|
|
|
|
{
|
|
|
|
return ((lhs.hostName == rhs.hostName) && (lhs.port == rhs.port));
|
|
|
|
}
|