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 <QNetworkCookie>
37 #include <QNetworkCookieJar>
38 #include <QNetworkProxy>
39 #include <QNetworkReply>
40 #include <QNetworkRequest>
44 #include "base/global.h"
45 #include "base/logger.h"
46 #include "base/preferences.h"
47 #include "downloadhandlerimpl.h"
48 #include "proxyconfigurationmanager.h"
52 // Disguise as Firefox to avoid web server banning
53 const char DEFAULT_USER_AGENT
[] = "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0";
55 class NetworkCookieJar final
: public QNetworkCookieJar
58 explicit NetworkCookieJar(QObject
*parent
= nullptr)
59 : QNetworkCookieJar(parent
)
61 const QDateTime now
= QDateTime::currentDateTime();
62 QList
<QNetworkCookie
> cookies
= Preferences::instance()->getNetworkCookies();
63 for (const QNetworkCookie
&cookie
: asConst(Preferences::instance()->getNetworkCookies()))
65 if (cookie
.isSessionCookie() || (cookie
.expirationDate() <= now
))
66 cookies
.removeAll(cookie
);
69 setAllCookies(cookies
);
72 ~NetworkCookieJar() override
74 const QDateTime now
= QDateTime::currentDateTime();
75 QList
<QNetworkCookie
> cookies
= allCookies();
76 for (const QNetworkCookie
&cookie
: asConst(allCookies()))
78 if (cookie
.isSessionCookie() || (cookie
.expirationDate() <= now
))
79 cookies
.removeAll(cookie
);
82 Preferences::instance()->setNetworkCookies(cookies
);
85 using QNetworkCookieJar::allCookies
;
86 using QNetworkCookieJar::setAllCookies
;
88 QList
<QNetworkCookie
> cookiesForUrl(const QUrl
&url
) const override
90 const QDateTime now
= QDateTime::currentDateTime();
91 QList
<QNetworkCookie
> cookies
= QNetworkCookieJar::cookiesForUrl(url
);
92 for (const QNetworkCookie
&cookie
: asConst(QNetworkCookieJar::cookiesForUrl(url
)))
94 if (!cookie
.isSessionCookie() && (cookie
.expirationDate() <= now
))
95 cookies
.removeAll(cookie
);
101 bool setCookiesFromUrl(const QList
<QNetworkCookie
> &cookieList
, const QUrl
&url
) override
103 const QDateTime now
= QDateTime::currentDateTime();
104 QList
<QNetworkCookie
> cookies
= cookieList
;
105 for (const QNetworkCookie
&cookie
: cookieList
)
107 if (!cookie
.isSessionCookie() && (cookie
.expirationDate() <= now
))
108 cookies
.removeAll(cookie
);
111 return QNetworkCookieJar::setCookiesFromUrl(cookies
, url
);
115 QNetworkRequest
createNetworkRequest(const Net::DownloadRequest
&downloadRequest
)
117 QNetworkRequest request
{downloadRequest
.url()};
119 if (downloadRequest
.userAgent().isEmpty())
120 request
.setRawHeader("User-Agent", DEFAULT_USER_AGENT
);
122 request
.setRawHeader("User-Agent", downloadRequest
.userAgent().toUtf8());
124 // Spoof HTTP Referer to allow adding torrent link from Torcache/KickAssTorrents
125 request
.setRawHeader("Referer", request
.url().toEncoded().data());
127 request
.setRawHeader("Accept-Encoding", "gzip");
128 // Qt doesn't support Magnet protocol so we need to handle redirections manually
129 request
.setAttribute(QNetworkRequest::RedirectPolicyAttribute
, QNetworkRequest::ManualRedirectPolicy
);
135 Net::DownloadManager
*Net::DownloadManager::m_instance
= nullptr;
137 Net::DownloadManager::DownloadManager(QObject
*parent
)
140 connect(&m_networkManager
, &QNetworkAccessManager::sslErrors
, this, &Net::DownloadManager::ignoreSslErrors
);
141 connect(&m_networkManager
, &QNetworkAccessManager::finished
, this, &DownloadManager::handleReplyFinished
);
142 connect(ProxyConfigurationManager::instance(), &ProxyConfigurationManager::proxyConfigurationChanged
143 , this, &DownloadManager::applyProxySettings
);
144 m_networkManager
.setCookieJar(new NetworkCookieJar(this));
145 applyProxySettings();
148 void Net::DownloadManager::initInstance()
151 m_instance
= new DownloadManager
;
154 void Net::DownloadManager::freeInstance()
157 m_instance
= nullptr;
160 Net::DownloadManager
*Net::DownloadManager::instance()
165 Net::DownloadHandler
*Net::DownloadManager::download(const DownloadRequest
&downloadRequest
)
167 // Process download request
168 const QNetworkRequest request
= createNetworkRequest(downloadRequest
);
169 const ServiceID id
= ServiceID::fromURL(request
.url());
170 const bool isSequentialService
= m_sequentialServices
.contains(id
);
172 auto downloadHandler
= new DownloadHandlerImpl
{this, downloadRequest
};
173 connect(downloadHandler
, &DownloadHandler::finished
, downloadHandler
, &QObject::deleteLater
);
174 connect(downloadHandler
, &QObject::destroyed
, this, [this, id
, downloadHandler
]()
176 m_waitingJobs
[id
].removeOne(downloadHandler
);
179 if (isSequentialService
&& m_busyServices
.contains(id
))
181 m_waitingJobs
[id
].enqueue(downloadHandler
);
185 qDebug("Downloading %s...", qUtf8Printable(downloadRequest
.url()));
186 if (isSequentialService
)
187 m_busyServices
.insert(id
);
188 downloadHandler
->assignNetworkReply(m_networkManager
.get(request
));
191 return downloadHandler
;
194 void Net::DownloadManager::registerSequentialService(const Net::ServiceID
&serviceID
)
196 m_sequentialServices
.insert(serviceID
);
199 QList
<QNetworkCookie
> Net::DownloadManager::cookiesForUrl(const QUrl
&url
) const
201 return m_networkManager
.cookieJar()->cookiesForUrl(url
);
204 bool Net::DownloadManager::setCookiesFromUrl(const QList
<QNetworkCookie
> &cookieList
, const QUrl
&url
)
206 return m_networkManager
.cookieJar()->setCookiesFromUrl(cookieList
, url
);
209 QList
<QNetworkCookie
> Net::DownloadManager::allCookies() const
211 return static_cast<NetworkCookieJar
*>(m_networkManager
.cookieJar())->allCookies();
214 void Net::DownloadManager::setAllCookies(const QList
<QNetworkCookie
> &cookieList
)
216 static_cast<NetworkCookieJar
*>(m_networkManager
.cookieJar())->setAllCookies(cookieList
);
219 bool Net::DownloadManager::deleteCookie(const QNetworkCookie
&cookie
)
221 return static_cast<NetworkCookieJar
*>(m_networkManager
.cookieJar())->deleteCookie(cookie
);
224 bool Net::DownloadManager::hasSupportedScheme(const QString
&url
)
226 const QStringList schemes
= instance()->m_networkManager
.supportedSchemes();
227 return std::any_of(schemes
.cbegin(), schemes
.cend(), [&url
](const QString
&scheme
)
229 return url
.startsWith((scheme
+ QLatin1Char(':')), Qt::CaseInsensitive
);
233 void Net::DownloadManager::applyProxySettings()
235 const auto *proxyManager
= ProxyConfigurationManager::instance();
236 const ProxyConfiguration proxyConfig
= proxyManager
->proxyConfiguration();
239 if (!proxyManager
->isProxyOnlyForTorrents() && (proxyConfig
.type
!= ProxyType::None
))
242 proxy
.setHostName(proxyConfig
.ip
);
243 proxy
.setPort(proxyConfig
.port
);
244 // Default proxy type is HTTP, we must change if it is SOCKS5
245 if ((proxyConfig
.type
== ProxyType::SOCKS5
) || (proxyConfig
.type
== ProxyType::SOCKS5_PW
))
247 qDebug() << Q_FUNC_INFO
<< "using SOCKS proxy";
248 proxy
.setType(QNetworkProxy::Socks5Proxy
);
252 qDebug() << Q_FUNC_INFO
<< "using HTTP proxy";
253 proxy
.setType(QNetworkProxy::HttpProxy
);
256 if (proxyManager
->isAuthenticationRequired())
258 qDebug("Proxy requires authentication, authenticating...");
259 proxy
.setUser(proxyConfig
.username
);
260 proxy
.setPassword(proxyConfig
.password
);
265 proxy
.setType(QNetworkProxy::NoProxy
);
268 m_networkManager
.setProxy(proxy
);
271 void Net::DownloadManager::handleReplyFinished(const QNetworkReply
*reply
)
273 // QNetworkReply::url() may be different from that of the original request
274 // so we need QNetworkRequest::url() to properly process Sequential Services
275 // in the case when the redirection occurred.
276 const ServiceID id
= ServiceID::fromURL(reply
->request().url());
277 const auto waitingJobsIter
= m_waitingJobs
.find(id
);
278 if ((waitingJobsIter
== m_waitingJobs
.end()) || waitingJobsIter
.value().isEmpty())
280 // No more waiting jobs for given ServiceID
281 m_busyServices
.remove(id
);
285 auto handler
= static_cast<DownloadHandlerImpl
*>(waitingJobsIter
.value().dequeue());
286 qDebug("Downloading %s...", qUtf8Printable(handler
->url()));
287 handler
->assignNetworkReply(m_networkManager
.get(createNetworkRequest(handler
->downloadRequest())));
288 handler
->disconnect(this);
291 void Net::DownloadManager::ignoreSslErrors(QNetworkReply
*reply
, const QList
<QSslError
> &errors
)
293 QStringList errorList
;
294 for (const QSslError
&error
: errors
)
295 errorList
+= error
.errorString();
296 LogMsg(tr("Ignoring SSL error, URL: \"%1\", errors: \"%2\"").arg(reply
->url().toString(), errorList
.join(u
". ")), Log::WARNING
);
298 // Ignore all SSL errors
299 reply
->ignoreSslErrors();
302 Net::DownloadRequest::DownloadRequest(const QString
&url
)
307 QString
Net::DownloadRequest::url() const
312 Net::DownloadRequest
&Net::DownloadRequest::url(const QString
&value
)
318 QString
Net::DownloadRequest::userAgent() const
323 Net::DownloadRequest
&Net::DownloadRequest::userAgent(const QString
&value
)
329 qint64
Net::DownloadRequest::limit() const
334 Net::DownloadRequest
&Net::DownloadRequest::limit(const qint64 value
)
340 bool Net::DownloadRequest::saveToFile() const
345 Net::DownloadRequest
&Net::DownloadRequest::saveToFile(const bool value
)
347 m_saveToFile
= value
;
351 Path
Net::DownloadRequest::destFileName() const
353 return m_destFileName
;
356 Net::DownloadRequest
&Net::DownloadRequest::destFileName(const Path
&value
)
358 m_destFileName
= value
;
362 Net::ServiceID
Net::ServiceID::fromURL(const QUrl
&url
)
364 return {url
.host(), url
.port(80)};
367 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
368 std::size_t Net::qHash(const ServiceID
&serviceID
, const std::size_t seed
)
370 return qHashMulti(seed
, serviceID
.hostName
, serviceID
.port
);
373 uint
Net::qHash(const ServiceID
&serviceID
, const uint seed
)
375 return ::qHash(serviceID
.hostName
, seed
) ^ ::qHash(serviceID
.port
);
379 bool Net::operator==(const ServiceID
&lhs
, const ServiceID
&rhs
)
381 return ((lhs
.hostName
== rhs
.hostName
) && (lhs
.port
== rhs
.port
));