mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-12-31 04:38:04 -06:00
committed by
GitHub
parent
189514c6de
commit
4471a6377e
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent.
|
||||
* Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
|
||||
* Copyright (C) 2018, 2022 Vladimir Golovnev <glassez@yandex.ru>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
@@ -37,9 +37,8 @@
|
||||
|
||||
#include "apierror.h"
|
||||
|
||||
APIController::APIController(ISessionManager *sessionManager, QObject *parent)
|
||||
APIController::APIController(QObject *parent)
|
||||
: QObject {parent}
|
||||
, m_sessionManager {sessionManager}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -56,11 +55,6 @@ QVariant APIController::run(const QString &action, const StringMap ¶ms, cons
|
||||
return m_result;
|
||||
}
|
||||
|
||||
ISessionManager *APIController::sessionManager() const
|
||||
{
|
||||
return m_sessionManager;
|
||||
}
|
||||
|
||||
const StringMap &APIController::params() const
|
||||
{
|
||||
return m_params;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent.
|
||||
* Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
|
||||
* Copyright (C) 2018, 2022 Vladimir Golovnev <glassez@yandex.ru>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
@@ -28,14 +28,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QtContainerFwd>
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
#include <QtContainerFwd>
|
||||
|
||||
class QString;
|
||||
|
||||
struct ISessionManager;
|
||||
|
||||
using DataMap = QHash<QString, QByteArray>;
|
||||
using StringMap = QHash<QString, QString>;
|
||||
|
||||
@@ -44,18 +42,11 @@ class APIController : public QObject
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY_MOVE(APIController)
|
||||
|
||||
#ifndef Q_MOC_RUN
|
||||
#define WEBAPI_PUBLIC
|
||||
#define WEBAPI_PRIVATE
|
||||
#endif
|
||||
|
||||
public:
|
||||
explicit APIController(ISessionManager *sessionManager, QObject *parent = nullptr);
|
||||
explicit APIController(QObject *parent = nullptr);
|
||||
|
||||
QVariant run(const QString &action, const StringMap ¶ms, const DataMap &data = {});
|
||||
|
||||
ISessionManager *sessionManager() const;
|
||||
|
||||
protected:
|
||||
const StringMap ¶ms() const;
|
||||
const DataMap &data() const;
|
||||
@@ -66,7 +57,6 @@ protected:
|
||||
void setResult(const QJsonObject &result);
|
||||
|
||||
private:
|
||||
ISessionManager *m_sessionManager;
|
||||
StringMap m_params;
|
||||
DataMap m_data;
|
||||
QVariant m_result;
|
||||
|
||||
@@ -37,15 +37,21 @@
|
||||
#include "apierror.h"
|
||||
#include "isessionmanager.h"
|
||||
|
||||
AuthController::AuthController(ISessionManager *sessionManager, QObject *parent)
|
||||
: APIController {parent}
|
||||
, m_sessionManager {sessionManager}
|
||||
{
|
||||
}
|
||||
|
||||
void AuthController::loginAction()
|
||||
{
|
||||
if (sessionManager()->session())
|
||||
if (m_sessionManager->session())
|
||||
{
|
||||
setResult(u"Ok."_qs);
|
||||
return;
|
||||
}
|
||||
|
||||
const QString clientAddr {sessionManager()->clientId()};
|
||||
const QString clientAddr {m_sessionManager->clientId()};
|
||||
const QString usernameFromWeb {params()[u"username"_qs]};
|
||||
const QString passwordFromWeb {params()[u"password"_qs]};
|
||||
|
||||
@@ -69,7 +75,7 @@ void AuthController::loginAction()
|
||||
{
|
||||
m_clientFailedLogins.remove(clientAddr);
|
||||
|
||||
sessionManager()->sessionStart();
|
||||
m_sessionManager->sessionStart();
|
||||
setResult(u"Ok."_qs);
|
||||
LogMsg(tr("WebAPI login success. IP: %1").arg(clientAddr));
|
||||
}
|
||||
@@ -86,12 +92,12 @@ void AuthController::loginAction()
|
||||
|
||||
void AuthController::logoutAction() const
|
||||
{
|
||||
sessionManager()->sessionEnd();
|
||||
m_sessionManager->sessionEnd();
|
||||
}
|
||||
|
||||
bool AuthController::isBanned() const
|
||||
{
|
||||
const auto failedLoginIter = m_clientFailedLogins.find(sessionManager()->clientId());
|
||||
const auto failedLoginIter = m_clientFailedLogins.find(m_sessionManager->clientId());
|
||||
if (failedLoginIter == m_clientFailedLogins.end())
|
||||
return false;
|
||||
|
||||
@@ -107,14 +113,14 @@ bool AuthController::isBanned() const
|
||||
|
||||
int AuthController::failedAttemptsCount() const
|
||||
{
|
||||
return m_clientFailedLogins.value(sessionManager()->clientId()).failedAttemptsCount;
|
||||
return m_clientFailedLogins.value(m_sessionManager->clientId()).failedAttemptsCount;
|
||||
}
|
||||
|
||||
void AuthController::increaseFailedAttempts()
|
||||
{
|
||||
Q_ASSERT(Preferences::instance()->getWebUIMaxAuthFailCount() > 0);
|
||||
|
||||
FailedLogin &failedLogin = m_clientFailedLogins[sessionManager()->clientId()];
|
||||
FailedLogin &failedLogin = m_clientFailedLogins[m_sessionManager->clientId()];
|
||||
++failedLogin.failedAttemptsCount;
|
||||
|
||||
if (failedLogin.failedAttemptsCount >= Preferences::instance()->getWebUIMaxAuthFailCount())
|
||||
|
||||
@@ -35,13 +35,15 @@
|
||||
|
||||
class QString;
|
||||
|
||||
struct ISessionManager;
|
||||
|
||||
class AuthController : public APIController
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY_MOVE(AuthController)
|
||||
|
||||
public:
|
||||
using APIController::APIController;
|
||||
explicit AuthController(ISessionManager *sessionManager, QObject *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void loginAction();
|
||||
@@ -52,6 +54,8 @@ private:
|
||||
int failedAttemptsCount() const;
|
||||
void increaseFailedAttempts();
|
||||
|
||||
ISessionManager *m_sessionManager = nullptr;
|
||||
|
||||
struct FailedLogin
|
||||
{
|
||||
int failedAttemptsCount = 0;
|
||||
|
||||
@@ -36,14 +36,6 @@ struct ISession
|
||||
{
|
||||
virtual ~ISession() = default;
|
||||
virtual QString id() const = 0;
|
||||
virtual QVariant getData(const QString &id) const = 0;
|
||||
virtual void setData(const QString &id, const QVariant &data) = 0;
|
||||
|
||||
template <class T>
|
||||
T getData(const QString &id) const
|
||||
{
|
||||
return this->getData(id).value<T>();
|
||||
}
|
||||
};
|
||||
|
||||
struct ISessionManager
|
||||
|
||||
@@ -45,21 +45,8 @@
|
||||
#include "apierror.h"
|
||||
#include "isessionmanager.h"
|
||||
|
||||
using SearchHandlerPtr = QSharedPointer<SearchHandler>;
|
||||
using SearchHandlerDict = QMap<int, SearchHandlerPtr>;
|
||||
|
||||
namespace
|
||||
{
|
||||
const QString ACTIVE_SEARCHES = u"activeSearches"_qs;
|
||||
const QString SEARCH_HANDLERS = u"searchHandlers"_qs;
|
||||
|
||||
void removeActiveSearch(ISession *session, const int id)
|
||||
{
|
||||
auto activeSearches = session->getData<QSet<int>>(ACTIVE_SEARCHES);
|
||||
if (activeSearches.remove(id))
|
||||
session->setData(ACTIVE_SEARCHES, QVariant::fromValue(activeSearches));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the search categories in JSON format.
|
||||
*
|
||||
@@ -117,22 +104,17 @@ void SearchController::startAction()
|
||||
pluginsToUse << plugins;
|
||||
}
|
||||
|
||||
ISession *const session = sessionManager()->session();
|
||||
auto activeSearches = session->getData<QSet<int>>(ACTIVE_SEARCHES);
|
||||
if (activeSearches.size() >= MAX_CONCURRENT_SEARCHES)
|
||||
if (m_activeSearches.size() >= MAX_CONCURRENT_SEARCHES)
|
||||
throw APIError(APIErrorType::Conflict, tr("Unable to create more than %1 concurrent searches.").arg(MAX_CONCURRENT_SEARCHES));
|
||||
|
||||
const auto id = generateSearchId();
|
||||
const SearchHandlerPtr searchHandler {SearchPluginManager::instance()->startSearch(pattern, category, pluginsToUse)};
|
||||
QObject::connect(searchHandler.data(), &SearchHandler::searchFinished, this, [session, id, this]() { searchFinished(session, id); });
|
||||
QObject::connect(searchHandler.data(), &SearchHandler::searchFailed, this, [session, id, this]() { searchFailed(session, id); });
|
||||
const std::shared_ptr<SearchHandler> searchHandler {SearchPluginManager::instance()->startSearch(pattern, category, pluginsToUse)};
|
||||
QObject::connect(searchHandler.get(), &SearchHandler::searchFinished, this, [id, this]() { m_activeSearches.remove(id); });
|
||||
QObject::connect(searchHandler.get(), &SearchHandler::searchFailed, this, [id, this]() { m_activeSearches.remove(id); });
|
||||
|
||||
auto searchHandlers = session->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
||||
searchHandlers.insert(id, searchHandler);
|
||||
session->setData(SEARCH_HANDLERS, QVariant::fromValue(searchHandlers));
|
||||
m_searchHandlers.insert(id, searchHandler);
|
||||
|
||||
activeSearches.insert(id);
|
||||
session->setData(ACTIVE_SEARCHES, QVariant::fromValue(activeSearches));
|
||||
m_activeSearches.insert(id);
|
||||
|
||||
const QJsonObject result = {{u"id"_qs, id}};
|
||||
setResult(result);
|
||||
@@ -143,18 +125,17 @@ void SearchController::stopAction()
|
||||
requireParams({u"id"_qs});
|
||||
|
||||
const int id = params()[u"id"_qs].toInt();
|
||||
ISession *const session = sessionManager()->session();
|
||||
|
||||
const auto searchHandlers = session->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
||||
if (!searchHandlers.contains(id))
|
||||
const auto iter = m_searchHandlers.find(id);
|
||||
if (iter == m_searchHandlers.end())
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
||||
const SearchHandlerPtr searchHandler = searchHandlers[id];
|
||||
const std::shared_ptr<SearchHandler> &searchHandler = iter.value();
|
||||
|
||||
if (searchHandler->isActive())
|
||||
{
|
||||
searchHandler->cancelSearch();
|
||||
removeActiveSearch(session, id);
|
||||
m_activeSearches.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,16 +143,15 @@ void SearchController::statusAction()
|
||||
{
|
||||
const int id = params()[u"id"_qs].toInt();
|
||||
|
||||
const auto searchHandlers = sessionManager()->session()->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
||||
if ((id != 0) && !searchHandlers.contains(id))
|
||||
if ((id != 0) && !m_searchHandlers.contains(id))
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
||||
QJsonArray statusArray;
|
||||
const QList<int> searchIds {(id == 0) ? searchHandlers.keys() : QList<int> {id}};
|
||||
const QList<int> searchIds {(id == 0) ? m_searchHandlers.keys() : QList<int> {id}};
|
||||
|
||||
for (const int searchId : searchIds)
|
||||
{
|
||||
const SearchHandlerPtr searchHandler = searchHandlers[searchId];
|
||||
const std::shared_ptr<SearchHandler> &searchHandler = m_searchHandlers[searchId];
|
||||
statusArray << QJsonObject
|
||||
{
|
||||
{u"id"_qs, searchId},
|
||||
@@ -191,11 +171,11 @@ void SearchController::resultsAction()
|
||||
int limit = params()[u"limit"_qs].toInt();
|
||||
int offset = params()[u"offset"_qs].toInt();
|
||||
|
||||
const auto searchHandlers = sessionManager()->session()->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
||||
if (!searchHandlers.contains(id))
|
||||
const auto iter = m_searchHandlers.find(id);
|
||||
if (iter == m_searchHandlers.end())
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
||||
const SearchHandlerPtr searchHandler = searchHandlers[id];
|
||||
const std::shared_ptr<SearchHandler> &searchHandler = iter.value();
|
||||
const QList<SearchResult> searchResults = searchHandler->results();
|
||||
const int size = searchResults.size();
|
||||
|
||||
@@ -221,18 +201,15 @@ void SearchController::deleteAction()
|
||||
requireParams({u"id"_qs});
|
||||
|
||||
const int id = params()[u"id"_qs].toInt();
|
||||
ISession *const session = sessionManager()->session();
|
||||
|
||||
auto searchHandlers = session->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
||||
if (!searchHandlers.contains(id))
|
||||
const auto iter = m_searchHandlers.find(id);
|
||||
if (iter == m_searchHandlers.end())
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
||||
const SearchHandlerPtr searchHandler = searchHandlers[id];
|
||||
const std::shared_ptr<SearchHandler> &searchHandler = iter.value();
|
||||
searchHandler->cancelSearch();
|
||||
searchHandlers.remove(id);
|
||||
session->setData(SEARCH_HANDLERS, QVariant::fromValue(searchHandlers));
|
||||
|
||||
removeActiveSearch(session, id);
|
||||
m_activeSearches.remove(id);
|
||||
m_searchHandlers.erase(iter);
|
||||
}
|
||||
|
||||
void SearchController::pluginsAction()
|
||||
@@ -302,24 +279,12 @@ void SearchController::checkForUpdatesFailed(const QString &reason)
|
||||
LogMsg(tr("Failed to check for plugin updates: %1").arg(reason), Log::INFO);
|
||||
}
|
||||
|
||||
void SearchController::searchFinished(ISession *session, const int id)
|
||||
{
|
||||
removeActiveSearch(session, id);
|
||||
}
|
||||
|
||||
void SearchController::searchFailed(ISession *session, const int id)
|
||||
{
|
||||
removeActiveSearch(session, id);
|
||||
}
|
||||
|
||||
int SearchController::generateSearchId() const
|
||||
{
|
||||
const auto searchHandlers = sessionManager()->session()->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
||||
|
||||
while (true)
|
||||
{
|
||||
const int id = Utils::Random::rand(1, std::numeric_limits<int>::max());
|
||||
if (!searchHandlers.contains(id))
|
||||
if (!m_searchHandlers.contains(id))
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <QtContainerFwd>
|
||||
#include <QHash>
|
||||
#include <QSet>
|
||||
|
||||
#include "base/search/searchpluginmanager.h"
|
||||
#include "apicontroller.h"
|
||||
@@ -36,7 +40,6 @@
|
||||
class QJsonArray;
|
||||
class QJsonObject;
|
||||
|
||||
struct ISession;
|
||||
struct SearchResult;
|
||||
|
||||
class SearchController : public APIController
|
||||
@@ -64,9 +67,10 @@ private:
|
||||
|
||||
void checkForUpdatesFinished(const QHash<QString, PluginVersion> &updateInfo);
|
||||
void checkForUpdatesFailed(const QString &reason);
|
||||
void searchFinished(ISession *session, int id);
|
||||
void searchFailed(ISession *session, int id);
|
||||
int generateSearchId() const;
|
||||
QJsonObject getResults(const QList<SearchResult> &searchResults, bool isSearchActive, int totalResults) const;
|
||||
QJsonArray getPluginsInfo(const QStringList &plugins) const;
|
||||
|
||||
QSet<int> m_activeSearches;
|
||||
QHash<int, std::shared_ptr<SearchHandler>> m_searchHandlers;
|
||||
};
|
||||
|
||||
@@ -366,8 +366,8 @@ namespace
|
||||
}
|
||||
}
|
||||
|
||||
SyncController::SyncController(ISessionManager *sessionManager, QObject *parent)
|
||||
: APIController(sessionManager, parent)
|
||||
SyncController::SyncController(QObject *parent)
|
||||
: APIController(parent)
|
||||
{
|
||||
m_freeDiskSpaceThread = new QThread(this);
|
||||
m_freeDiskSpaceChecker = new FreeDiskSpaceChecker();
|
||||
@@ -456,9 +456,6 @@ void SyncController::maindataAction()
|
||||
|
||||
QVariantMap data;
|
||||
|
||||
QVariantMap lastResponse = sessionManager()->session()->getData(u"syncMainDataLastResponse"_qs).toMap();
|
||||
QVariantMap lastAcceptedResponse = sessionManager()->session()->getData(u"syncMainDataLastAcceptedResponse"_qs).toMap();
|
||||
|
||||
QVariantHash torrents;
|
||||
QHash<QString, QStringList> trackers;
|
||||
for (const BitTorrent::Torrent *torrent : asConst(session->torrents()))
|
||||
@@ -470,8 +467,8 @@ void SyncController::maindataAction()
|
||||
|
||||
// Calculated last activity time can differ from actual value by up to 10 seconds (this is a libtorrent issue).
|
||||
// So we don't need unnecessary updates of last activity time in response.
|
||||
const auto iterTorrents = lastResponse.find(u"torrents"_qs);
|
||||
if (iterTorrents != lastResponse.end())
|
||||
const auto iterTorrents = m_lastMaindataResponse.find(u"torrents"_qs);
|
||||
if (iterTorrents != m_lastMaindataResponse.end())
|
||||
{
|
||||
const QVariantHash lastResponseTorrents = iterTorrents->toHash();
|
||||
const auto iterID = lastResponseTorrents.find(torrentID.toString());
|
||||
@@ -529,11 +526,8 @@ void SyncController::maindataAction()
|
||||
serverState[KEY_SYNC_MAINDATA_REFRESH_INTERVAL] = session->refreshInterval();
|
||||
data[u"server_state"_qs] = serverState;
|
||||
|
||||
const int acceptedResponseId {params()[u"rid"_qs].toInt()};
|
||||
setResult(QJsonObject::fromVariantMap(generateSyncData(acceptedResponseId, data, lastAcceptedResponse, lastResponse)));
|
||||
|
||||
sessionManager()->session()->setData(u"syncMainDataLastResponse"_qs, lastResponse);
|
||||
sessionManager()->session()->setData(u"syncMainDataLastAcceptedResponse"_qs, lastAcceptedResponse);
|
||||
const int acceptedResponseId = params()[u"rid"_qs].toInt();
|
||||
setResult(QJsonObject::fromVariantMap(generateSyncData(acceptedResponseId, data, m_lastAcceptedMaindataResponse, m_lastMaindataResponse)));
|
||||
}
|
||||
|
||||
// GET param:
|
||||
@@ -541,9 +535,6 @@ void SyncController::maindataAction()
|
||||
// - rid (int): last response id
|
||||
void SyncController::torrentPeersAction()
|
||||
{
|
||||
auto lastResponse = sessionManager()->session()->getData(u"syncTorrentPeersLastResponse"_qs).toMap();
|
||||
auto lastAcceptedResponse = sessionManager()->session()->getData(u"syncTorrentPeersLastAcceptedResponse"_qs).toMap();
|
||||
|
||||
const auto id = BitTorrent::TorrentID::fromString(params()[u"hash"_qs]);
|
||||
const BitTorrent::Torrent *torrent = BitTorrent::Session::instance()->findTorrent(id);
|
||||
if (!torrent)
|
||||
@@ -598,11 +589,8 @@ void SyncController::torrentPeersAction()
|
||||
}
|
||||
data[u"peers"_qs] = peers;
|
||||
|
||||
const int acceptedResponseId {params()[u"rid"_qs].toInt()};
|
||||
setResult(QJsonObject::fromVariantMap(generateSyncData(acceptedResponseId, data, lastAcceptedResponse, lastResponse)));
|
||||
|
||||
sessionManager()->session()->setData(u"syncTorrentPeersLastResponse"_qs, lastResponse);
|
||||
sessionManager()->session()->setData(u"syncTorrentPeersLastAcceptedResponse"_qs, lastAcceptedResponse);
|
||||
const int acceptedResponseId = params()[u"rid"_qs].toInt();
|
||||
setResult(QJsonObject::fromVariantMap(generateSyncData(acceptedResponseId, data, m_lastAcceptedPeersResponse, m_lastPeersResponse)));
|
||||
}
|
||||
|
||||
qint64 SyncController::getFreeDiskSpace()
|
||||
|
||||
@@ -29,11 +29,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <QElapsedTimer>
|
||||
#include <QVariantMap>
|
||||
|
||||
#include "apicontroller.h"
|
||||
|
||||
struct ISessionManager;
|
||||
|
||||
class QThread;
|
||||
|
||||
class FreeDiskSpaceChecker;
|
||||
@@ -46,7 +45,7 @@ class SyncController : public APIController
|
||||
public:
|
||||
using APIController::APIController;
|
||||
|
||||
explicit SyncController(ISessionManager *sessionManager, QObject *parent = nullptr);
|
||||
explicit SyncController(QObject *parent = nullptr);
|
||||
~SyncController() override;
|
||||
|
||||
private slots:
|
||||
@@ -62,4 +61,9 @@ private:
|
||||
FreeDiskSpaceChecker *m_freeDiskSpaceChecker = nullptr;
|
||||
QThread *m_freeDiskSpaceThread = nullptr;
|
||||
QElapsedTimer m_freeDiskSpaceElapsedTimer;
|
||||
|
||||
QVariantMap m_lastMaindataResponse;
|
||||
QVariantMap m_lastAcceptedMaindataResponse;
|
||||
QVariantMap m_lastPeersResponse;
|
||||
QVariantMap m_lastAcceptedPeersResponse;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user