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"
48 #include "proxyconfigurationmanager.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:
NetworkCookieJar(QObject * parent=nullptr)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 
~NetworkCookieJar()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 
82             Preferences::instance()->setNetworkCookies(cookies);
83         }
84 
85         using QNetworkCookieJar::allCookies;
86         using QNetworkCookieJar::setAllCookies;
87 
cookiesForUrl(const QUrl & url) const88         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 
setCookiesFromUrl(const QList<QNetworkCookie> & cookieList,const QUrl & url)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 
createNetworkRequest(const Net::DownloadRequest & downloadRequest)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 
135 Net::DownloadManager *Net::DownloadManager::m_instance = nullptr;
136 
DownloadManager(QObject * parent)137 Net::DownloadManager::DownloadManager(QObject *parent)
138     : QObject(parent)
139 {
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();
146 }
147 
initInstance()148 void Net::DownloadManager::initInstance()
149 {
150     if (!m_instance)
151         m_instance = new DownloadManager;
152 }
153 
freeInstance()154 void Net::DownloadManager::freeInstance()
155 {
156     delete m_instance;
157     m_instance = nullptr;
158 }
159 
instance()160 Net::DownloadManager *Net::DownloadManager::instance()
161 {
162     return m_instance;
163 }
164 
download(const DownloadRequest & downloadRequest)165 Net::DownloadHandler *Net::DownloadManager::download(const DownloadRequest &downloadRequest)
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 
registerSequentialService(const Net::ServiceID & serviceID)194 void Net::DownloadManager::registerSequentialService(const Net::ServiceID &serviceID)
195 {
196     m_sequentialServices.insert(serviceID);
197 }
198 
cookiesForUrl(const QUrl & url) const199 QList<QNetworkCookie> Net::DownloadManager::cookiesForUrl(const QUrl &url) const
200 {
201     return m_networkManager.cookieJar()->cookiesForUrl(url);
202 }
203 
setCookiesFromUrl(const QList<QNetworkCookie> & cookieList,const QUrl & url)204 bool Net::DownloadManager::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
205 {
206     return m_networkManager.cookieJar()->setCookiesFromUrl(cookieList, url);
207 }
208 
allCookies() const209 QList<QNetworkCookie> Net::DownloadManager::allCookies() const
210 {
211     return static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->allCookies();
212 }
213 
setAllCookies(const QList<QNetworkCookie> & cookieList)214 void Net::DownloadManager::setAllCookies(const QList<QNetworkCookie> &cookieList)
215 {
216     static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->setAllCookies(cookieList);
217 }
218 
deleteCookie(const QNetworkCookie & cookie)219 bool Net::DownloadManager::deleteCookie(const QNetworkCookie &cookie)
220 {
221     return static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->deleteCookie(cookie);
222 }
223 
hasSupportedScheme(const QString & url)224 bool Net::DownloadManager::hasSupportedScheme(const QString &url)
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 
applyProxySettings()233 void Net::DownloadManager::applyProxySettings()
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 
handleReplyFinished(const QNetworkReply * reply)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 
ignoreSslErrors(QNetworkReply * reply,const QList<QSslError> & errors)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 
DownloadRequest(const QString & url)302 Net::DownloadRequest::DownloadRequest(const QString &url)
303     : m_url {url}
304 {
305 }
306 
url() const307 QString Net::DownloadRequest::url() const
308 {
309     return m_url;
310 }
311 
url(const QString & value)312 Net::DownloadRequest &Net::DownloadRequest::url(const QString &value)
313 {
314     m_url = value;
315     return *this;
316 }
317 
userAgent() const318 QString Net::DownloadRequest::userAgent() const
319 {
320     return m_userAgent;
321 }
322 
userAgent(const QString & value)323 Net::DownloadRequest &Net::DownloadRequest::userAgent(const QString &value)
324 {
325     m_userAgent = value;
326     return *this;
327 }
328 
limit() const329 qint64 Net::DownloadRequest::limit() const
330 {
331     return m_limit;
332 }
333 
limit(const qint64 value)334 Net::DownloadRequest &Net::DownloadRequest::limit(const qint64 value)
335 {
336     m_limit = value;
337     return *this;
338 }
339 
saveToFile() const340 bool Net::DownloadRequest::saveToFile() const
341 {
342     return m_saveToFile;
343 }
344 
saveToFile(const bool value)345 Net::DownloadRequest &Net::DownloadRequest::saveToFile(const bool value)
346 {
347     m_saveToFile = value;
348     return *this;
349 }
350 
fromURL(const QUrl & url)351 Net::ServiceID Net::ServiceID::fromURL(const QUrl &url)
352 {
353     return {url.host(), url.port(80)};
354 }
355 
qHash(const ServiceID & serviceID,const uint seed)356 uint Net::qHash(const ServiceID &serviceID, const uint seed)
357 {
358     return ::qHash(serviceID.hostName, seed) ^ ::qHash(serviceID.port);
359 }
360 
operator ==(const ServiceID & lhs,const ServiceID & rhs)361 bool Net::operator==(const ServiceID &lhs, const ServiceID &rhs)
362 {
363     return ((lhs.hostName == rhs.hostName) && (lhs.port == rhs.port));
364 }
365