qBittorrent
downloadmanager.cpp
Go to the documentation of this file.
1 /*
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>
5  *
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.
10  *
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.
15  *
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.
19  *
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.
28  */
29 
30 #include "downloadmanager.h"
31 
32 #include <algorithm>
33 
34 #include <QDateTime>
35 #include <QDebug>
36 #include <QNetworkCookie>
37 #include <QNetworkCookieJar>
38 #include <QNetworkProxy>
39 #include <QNetworkReply>
40 #include <QNetworkRequest>
41 #include <QSslError>
42 #include <QUrl>
43 
44 #include "base/global.h"
45 #include "base/logger.h"
46 #include "base/preferences.h"
47 #include "downloadhandlerimpl.h"
49 
50 namespace
51 {
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";
54 
55  class NetworkCookieJar final : public QNetworkCookieJar
56  {
57  public:
58  explicit NetworkCookieJar(QObject *parent = nullptr)
59  : QNetworkCookieJar(parent)
60  {
61  const QDateTime now = QDateTime::currentDateTime();
62  QList<QNetworkCookie> cookies = Preferences::instance()->getNetworkCookies();
63  for (const QNetworkCookie &cookie : asConst(Preferences::instance()->getNetworkCookies()))
64  {
65  if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
66  cookies.removeAll(cookie);
67  }
68 
69  setAllCookies(cookies);
70  }
71 
72  ~NetworkCookieJar() override
73  {
74  const QDateTime now = QDateTime::currentDateTime();
75  QList<QNetworkCookie> cookies = allCookies();
76  for (const QNetworkCookie &cookie : asConst(allCookies()))
77  {
78  if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
79  cookies.removeAll(cookie);
80  }
81 
83  }
84 
85  using QNetworkCookieJar::allCookies;
86  using QNetworkCookieJar::setAllCookies;
87 
88  QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const override
89  {
90  const QDateTime now = QDateTime::currentDateTime();
91  QList<QNetworkCookie> cookies = QNetworkCookieJar::cookiesForUrl(url);
92  for (const QNetworkCookie &cookie : asConst(QNetworkCookieJar::cookiesForUrl(url)))
93  {
94  if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now))
95  cookies.removeAll(cookie);
96  }
97 
98  return cookies;
99  }
100 
101  bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url) override
102  {
103  const QDateTime now = QDateTime::currentDateTime();
104  QList<QNetworkCookie> cookies = cookieList;
105  for (const QNetworkCookie &cookie : cookieList)
106  {
107  if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now))
108  cookies.removeAll(cookie);
109  }
110 
111  return QNetworkCookieJar::setCookiesFromUrl(cookies, url);
112  }
113  };
114 
115  QNetworkRequest createNetworkRequest(const Net::DownloadRequest &downloadRequest)
116  {
117  QNetworkRequest request {downloadRequest.url()};
118 
119  if (downloadRequest.userAgent().isEmpty())
120  request.setRawHeader("User-Agent", DEFAULT_USER_AGENT);
121  else
122  request.setRawHeader("User-Agent", downloadRequest.userAgent().toUtf8());
123 
124  // Spoof HTTP Referer to allow adding torrent link from Torcache/KickAssTorrents
125  request.setRawHeader("Referer", request.url().toEncoded().data());
126  // Accept gzip
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);
130 
131  return request;
132  }
133 }
134 
136 
138  : QObject(parent)
139 {
140  connect(&m_networkManager, &QNetworkAccessManager::sslErrors, this, &Net::DownloadManager::ignoreSslErrors);
141  connect(&m_networkManager, &QNetworkAccessManager::finished, this, &DownloadManager::handleReplyFinished);
144  m_networkManager.setCookieJar(new NetworkCookieJar(this));
146 }
147 
149 {
150  if (!m_instance)
151  m_instance = new DownloadManager;
152 }
153 
155 {
156  delete m_instance;
157  m_instance = nullptr;
158 }
159 
161 {
162  return m_instance;
163 }
164 
166 {
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);
171 
172  auto downloadHandler = new DownloadHandlerImpl {this, downloadRequest};
173  connect(downloadHandler, &DownloadHandler::finished, downloadHandler, &QObject::deleteLater);
174  connect(downloadHandler, &QObject::destroyed, this, [this, id, downloadHandler]()
175  {
176  m_waitingJobs[id].removeOne(downloadHandler);
177  });
178 
179  if (isSequentialService && m_busyServices.contains(id))
180  {
181  m_waitingJobs[id].enqueue(downloadHandler);
182  }
183  else
184  {
185  qDebug("Downloading %s...", qUtf8Printable(downloadRequest.url()));
186  if (isSequentialService)
187  m_busyServices.insert(id);
188  downloadHandler->assignNetworkReply(m_networkManager.get(request));
189  }
190 
191  return downloadHandler;
192 }
193 
195 {
196  m_sequentialServices.insert(serviceID);
197 }
198 
199 QList<QNetworkCookie> Net::DownloadManager::cookiesForUrl(const QUrl &url) const
200 {
201  return m_networkManager.cookieJar()->cookiesForUrl(url);
202 }
203 
204 bool Net::DownloadManager::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
205 {
206  return m_networkManager.cookieJar()->setCookiesFromUrl(cookieList, url);
207 }
208 
209 QList<QNetworkCookie> Net::DownloadManager::allCookies() const
210 {
211  return static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->allCookies();
212 }
213 
214 void Net::DownloadManager::setAllCookies(const QList<QNetworkCookie> &cookieList)
215 {
216  static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->setAllCookies(cookieList);
217 }
218 
219 bool Net::DownloadManager::deleteCookie(const QNetworkCookie &cookie)
220 {
221  return static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->deleteCookie(cookie);
222 }
223 
225 {
226  const QStringList schemes = instance()->m_networkManager.supportedSchemes();
227  return std::any_of(schemes.cbegin(), schemes.cend(), [&url](const QString &scheme)
228  {
229  return url.startsWith((scheme + QLatin1Char(':')), Qt::CaseInsensitive);
230  });
231 }
232 
234 {
235  const auto *proxyManager = ProxyConfigurationManager::instance();
236  const ProxyConfiguration proxyConfig = proxyManager->proxyConfiguration();
237  QNetworkProxy proxy;
238 
239  if (!proxyManager->isProxyOnlyForTorrents() && (proxyConfig.type != ProxyType::None))
240  {
241  // Proxy enabled
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))
246  {
247  qDebug() << Q_FUNC_INFO << "using SOCKS proxy";
248  proxy.setType(QNetworkProxy::Socks5Proxy);
249  }
250  else
251  {
252  qDebug() << Q_FUNC_INFO << "using HTTP proxy";
253  proxy.setType(QNetworkProxy::HttpProxy);
254  }
255  // Authentication?
256  if (proxyManager->isAuthenticationRequired())
257  {
258  qDebug("Proxy requires authentication, authenticating...");
259  proxy.setUser(proxyConfig.username);
260  proxy.setPassword(proxyConfig.password);
261  }
262  }
263  else
264  {
265  proxy.setType(QNetworkProxy::NoProxy);
266  }
267 
268  m_networkManager.setProxy(proxy);
269 }
270 
271 void Net::DownloadManager::handleReplyFinished(const QNetworkReply *reply)
272 {
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())
279  {
280  // No more waiting jobs for given ServiceID
281  m_busyServices.remove(id);
282  return;
283  }
284 
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);
289 }
290 
291 void Net::DownloadManager::ignoreSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
292 {
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(". ")), Log::WARNING);
297 
298  // Ignore all SSL errors
299  reply->ignoreSslErrors();
300 }
301 
303  : m_url {url}
304 {
305 }
306 
308 {
309  return m_url;
310 }
311 
313 {
314  m_url = value;
315  return *this;
316 }
317 
319 {
320  return m_userAgent;
321 }
322 
324 {
325  m_userAgent = value;
326  return *this;
327 }
328 
330 {
331  return m_limit;
332 }
333 
335 {
336  m_limit = value;
337  return *this;
338 }
339 
341 {
342  return m_saveToFile;
343 }
344 
346 {
347  m_saveToFile = value;
348  return *this;
349 }
350 
352 {
353  return m_destFileName;
354 }
355 
357 {
358  m_destFileName = value;
359  return *this;
360 }
361 
363 {
364  return {url.host(), url.port(80)};
365 }
366 
367 uint Net::qHash(const ServiceID &serviceID, const uint seed)
368 {
369  return ::qHash(serviceID.hostName, seed) ^ ::qHash(serviceID.port);
370 }
371 
372 bool Net::operator==(const ServiceID &lhs, const ServiceID &rhs)
373 {
374  return ((lhs.hostName == rhs.hostName) && (lhs.port == rhs.port));
375 }
void finished(const DownloadResult &result)
DownloadManager(QObject *parent=nullptr)
void handleReplyFinished(const QNetworkReply *reply)
void ignoreSslErrors(QNetworkReply *, const QList< QSslError > &)
void setAllCookies(const QList< QNetworkCookie > &cookieList)
bool setCookiesFromUrl(const QList< QNetworkCookie > &cookieList, const QUrl &url)
QList< QNetworkCookie > allCookies() const
void registerSequentialService(const ServiceID &serviceID)
QNetworkAccessManager m_networkManager
bool deleteCookie(const QNetworkCookie &cookie)
static void freeInstance()
static bool hasSupportedScheme(const QString &url)
DownloadHandler * download(const DownloadRequest &downloadRequest)
static DownloadManager * instance()
QList< QNetworkCookie > cookiesForUrl(const QUrl &url) const
static void initInstance()
static DownloadManager * m_instance
QString userAgent() const
DownloadRequest(const QString &url)
QString destFileName() const
static ProxyConfigurationManager * instance()
static Preferences * instance()
void setNetworkCookies(const QList< QNetworkCookie > &cookies)
QList< QNetworkCookie > getNetworkCookies() const
constexpr std::add_const_t< T > & asConst(T &t) noexcept
Definition: global.h:42
void LogMsg(const QString &message, const Log::MsgType &type)
Definition: logger.cpp:125
uint qHash(const TorrentID &key, uint seed)
Definition: infohash.cpp:86
@ WARNING
Definition: logger.h:47
bool operator==(const ServiceID &lhs, const ServiceID &rhs)
uint qHash(const ServiceID &serviceID, uint seed)
QNetworkRequest createNetworkRequest(const Net::DownloadRequest &downloadRequest)
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64
static ServiceID fromURL(const QUrl &url)