mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-11 07:18:08 +00:00
parent
189514c6de
commit
4471a6377e
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Bittorrent Client using Qt and libtorrent.
|
* 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
|
* This program is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public License
|
* modify it under the terms of the GNU General Public License
|
||||||
@ -37,9 +37,8 @@
|
|||||||
|
|
||||||
#include "apierror.h"
|
#include "apierror.h"
|
||||||
|
|
||||||
APIController::APIController(ISessionManager *sessionManager, QObject *parent)
|
APIController::APIController(QObject *parent)
|
||||||
: QObject {parent}
|
: QObject {parent}
|
||||||
, m_sessionManager {sessionManager}
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,11 +55,6 @@ QVariant APIController::run(const QString &action, const StringMap ¶ms, cons
|
|||||||
return m_result;
|
return m_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
ISessionManager *APIController::sessionManager() const
|
|
||||||
{
|
|
||||||
return m_sessionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
const StringMap &APIController::params() const
|
const StringMap &APIController::params() const
|
||||||
{
|
{
|
||||||
return m_params;
|
return m_params;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Bittorrent Client using Qt and libtorrent.
|
* 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
|
* This program is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public License
|
* modify it under the terms of the GNU General Public License
|
||||||
@ -28,14 +28,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QtContainerFwd>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
#include <QtContainerFwd>
|
|
||||||
|
|
||||||
class QString;
|
class QString;
|
||||||
|
|
||||||
struct ISessionManager;
|
|
||||||
|
|
||||||
using DataMap = QHash<QString, QByteArray>;
|
using DataMap = QHash<QString, QByteArray>;
|
||||||
using StringMap = QHash<QString, QString>;
|
using StringMap = QHash<QString, QString>;
|
||||||
|
|
||||||
@ -44,18 +42,11 @@ class APIController : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_DISABLE_COPY_MOVE(APIController)
|
Q_DISABLE_COPY_MOVE(APIController)
|
||||||
|
|
||||||
#ifndef Q_MOC_RUN
|
|
||||||
#define WEBAPI_PUBLIC
|
|
||||||
#define WEBAPI_PRIVATE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit APIController(ISessionManager *sessionManager, QObject *parent = nullptr);
|
explicit APIController(QObject *parent = nullptr);
|
||||||
|
|
||||||
QVariant run(const QString &action, const StringMap ¶ms, const DataMap &data = {});
|
QVariant run(const QString &action, const StringMap ¶ms, const DataMap &data = {});
|
||||||
|
|
||||||
ISessionManager *sessionManager() const;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const StringMap ¶ms() const;
|
const StringMap ¶ms() const;
|
||||||
const DataMap &data() const;
|
const DataMap &data() const;
|
||||||
@ -66,7 +57,6 @@ protected:
|
|||||||
void setResult(const QJsonObject &result);
|
void setResult(const QJsonObject &result);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ISessionManager *m_sessionManager;
|
|
||||||
StringMap m_params;
|
StringMap m_params;
|
||||||
DataMap m_data;
|
DataMap m_data;
|
||||||
QVariant m_result;
|
QVariant m_result;
|
||||||
|
@ -37,15 +37,21 @@
|
|||||||
#include "apierror.h"
|
#include "apierror.h"
|
||||||
#include "isessionmanager.h"
|
#include "isessionmanager.h"
|
||||||
|
|
||||||
|
AuthController::AuthController(ISessionManager *sessionManager, QObject *parent)
|
||||||
|
: APIController {parent}
|
||||||
|
, m_sessionManager {sessionManager}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void AuthController::loginAction()
|
void AuthController::loginAction()
|
||||||
{
|
{
|
||||||
if (sessionManager()->session())
|
if (m_sessionManager->session())
|
||||||
{
|
{
|
||||||
setResult(u"Ok."_qs);
|
setResult(u"Ok."_qs);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString clientAddr {sessionManager()->clientId()};
|
const QString clientAddr {m_sessionManager->clientId()};
|
||||||
const QString usernameFromWeb {params()[u"username"_qs]};
|
const QString usernameFromWeb {params()[u"username"_qs]};
|
||||||
const QString passwordFromWeb {params()[u"password"_qs]};
|
const QString passwordFromWeb {params()[u"password"_qs]};
|
||||||
|
|
||||||
@ -69,7 +75,7 @@ void AuthController::loginAction()
|
|||||||
{
|
{
|
||||||
m_clientFailedLogins.remove(clientAddr);
|
m_clientFailedLogins.remove(clientAddr);
|
||||||
|
|
||||||
sessionManager()->sessionStart();
|
m_sessionManager->sessionStart();
|
||||||
setResult(u"Ok."_qs);
|
setResult(u"Ok."_qs);
|
||||||
LogMsg(tr("WebAPI login success. IP: %1").arg(clientAddr));
|
LogMsg(tr("WebAPI login success. IP: %1").arg(clientAddr));
|
||||||
}
|
}
|
||||||
@ -86,12 +92,12 @@ void AuthController::loginAction()
|
|||||||
|
|
||||||
void AuthController::logoutAction() const
|
void AuthController::logoutAction() const
|
||||||
{
|
{
|
||||||
sessionManager()->sessionEnd();
|
m_sessionManager->sessionEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AuthController::isBanned() const
|
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())
|
if (failedLoginIter == m_clientFailedLogins.end())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -107,14 +113,14 @@ bool AuthController::isBanned() const
|
|||||||
|
|
||||||
int AuthController::failedAttemptsCount() const
|
int AuthController::failedAttemptsCount() const
|
||||||
{
|
{
|
||||||
return m_clientFailedLogins.value(sessionManager()->clientId()).failedAttemptsCount;
|
return m_clientFailedLogins.value(m_sessionManager->clientId()).failedAttemptsCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AuthController::increaseFailedAttempts()
|
void AuthController::increaseFailedAttempts()
|
||||||
{
|
{
|
||||||
Q_ASSERT(Preferences::instance()->getWebUIMaxAuthFailCount() > 0);
|
Q_ASSERT(Preferences::instance()->getWebUIMaxAuthFailCount() > 0);
|
||||||
|
|
||||||
FailedLogin &failedLogin = m_clientFailedLogins[sessionManager()->clientId()];
|
FailedLogin &failedLogin = m_clientFailedLogins[m_sessionManager->clientId()];
|
||||||
++failedLogin.failedAttemptsCount;
|
++failedLogin.failedAttemptsCount;
|
||||||
|
|
||||||
if (failedLogin.failedAttemptsCount >= Preferences::instance()->getWebUIMaxAuthFailCount())
|
if (failedLogin.failedAttemptsCount >= Preferences::instance()->getWebUIMaxAuthFailCount())
|
||||||
|
@ -35,13 +35,15 @@
|
|||||||
|
|
||||||
class QString;
|
class QString;
|
||||||
|
|
||||||
|
struct ISessionManager;
|
||||||
|
|
||||||
class AuthController : public APIController
|
class AuthController : public APIController
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_DISABLE_COPY_MOVE(AuthController)
|
Q_DISABLE_COPY_MOVE(AuthController)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using APIController::APIController;
|
explicit AuthController(ISessionManager *sessionManager, QObject *parent = nullptr);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void loginAction();
|
void loginAction();
|
||||||
@ -52,6 +54,8 @@ private:
|
|||||||
int failedAttemptsCount() const;
|
int failedAttemptsCount() const;
|
||||||
void increaseFailedAttempts();
|
void increaseFailedAttempts();
|
||||||
|
|
||||||
|
ISessionManager *m_sessionManager = nullptr;
|
||||||
|
|
||||||
struct FailedLogin
|
struct FailedLogin
|
||||||
{
|
{
|
||||||
int failedAttemptsCount = 0;
|
int failedAttemptsCount = 0;
|
||||||
|
@ -36,14 +36,6 @@ struct ISession
|
|||||||
{
|
{
|
||||||
virtual ~ISession() = default;
|
virtual ~ISession() = default;
|
||||||
virtual QString id() const = 0;
|
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
|
struct ISessionManager
|
||||||
|
@ -45,21 +45,8 @@
|
|||||||
#include "apierror.h"
|
#include "apierror.h"
|
||||||
#include "isessionmanager.h"
|
#include "isessionmanager.h"
|
||||||
|
|
||||||
using SearchHandlerPtr = QSharedPointer<SearchHandler>;
|
|
||||||
using SearchHandlerDict = QMap<int, SearchHandlerPtr>;
|
|
||||||
|
|
||||||
namespace
|
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.
|
* Returns the search categories in JSON format.
|
||||||
*
|
*
|
||||||
@ -117,22 +104,17 @@ void SearchController::startAction()
|
|||||||
pluginsToUse << plugins;
|
pluginsToUse << plugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
ISession *const session = sessionManager()->session();
|
if (m_activeSearches.size() >= MAX_CONCURRENT_SEARCHES)
|
||||||
auto activeSearches = session->getData<QSet<int>>(ACTIVE_SEARCHES);
|
|
||||||
if (activeSearches.size() >= MAX_CONCURRENT_SEARCHES)
|
|
||||||
throw APIError(APIErrorType::Conflict, tr("Unable to create more than %1 concurrent searches.").arg(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 auto id = generateSearchId();
|
||||||
const SearchHandlerPtr searchHandler {SearchPluginManager::instance()->startSearch(pattern, category, pluginsToUse)};
|
const std::shared_ptr<SearchHandler> searchHandler {SearchPluginManager::instance()->startSearch(pattern, category, pluginsToUse)};
|
||||||
QObject::connect(searchHandler.data(), &SearchHandler::searchFinished, this, [session, id, this]() { searchFinished(session, id); });
|
QObject::connect(searchHandler.get(), &SearchHandler::searchFinished, this, [id, this]() { m_activeSearches.remove(id); });
|
||||||
QObject::connect(searchHandler.data(), &SearchHandler::searchFailed, this, [session, id, this]() { searchFailed(session, id); });
|
QObject::connect(searchHandler.get(), &SearchHandler::searchFailed, this, [id, this]() { m_activeSearches.remove(id); });
|
||||||
|
|
||||||
auto searchHandlers = session->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
m_searchHandlers.insert(id, searchHandler);
|
||||||
searchHandlers.insert(id, searchHandler);
|
|
||||||
session->setData(SEARCH_HANDLERS, QVariant::fromValue(searchHandlers));
|
|
||||||
|
|
||||||
activeSearches.insert(id);
|
m_activeSearches.insert(id);
|
||||||
session->setData(ACTIVE_SEARCHES, QVariant::fromValue(activeSearches));
|
|
||||||
|
|
||||||
const QJsonObject result = {{u"id"_qs, id}};
|
const QJsonObject result = {{u"id"_qs, id}};
|
||||||
setResult(result);
|
setResult(result);
|
||||||
@ -143,18 +125,17 @@ void SearchController::stopAction()
|
|||||||
requireParams({u"id"_qs});
|
requireParams({u"id"_qs});
|
||||||
|
|
||||||
const int id = params()[u"id"_qs].toInt();
|
const int id = params()[u"id"_qs].toInt();
|
||||||
ISession *const session = sessionManager()->session();
|
|
||||||
|
|
||||||
const auto searchHandlers = session->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
const auto iter = m_searchHandlers.find(id);
|
||||||
if (!searchHandlers.contains(id))
|
if (iter == m_searchHandlers.end())
|
||||||
throw APIError(APIErrorType::NotFound);
|
throw APIError(APIErrorType::NotFound);
|
||||||
|
|
||||||
const SearchHandlerPtr searchHandler = searchHandlers[id];
|
const std::shared_ptr<SearchHandler> &searchHandler = iter.value();
|
||||||
|
|
||||||
if (searchHandler->isActive())
|
if (searchHandler->isActive())
|
||||||
{
|
{
|
||||||
searchHandler->cancelSearch();
|
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 int id = params()[u"id"_qs].toInt();
|
||||||
|
|
||||||
const auto searchHandlers = sessionManager()->session()->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
if ((id != 0) && !m_searchHandlers.contains(id))
|
||||||
if ((id != 0) && !searchHandlers.contains(id))
|
|
||||||
throw APIError(APIErrorType::NotFound);
|
throw APIError(APIErrorType::NotFound);
|
||||||
|
|
||||||
QJsonArray statusArray;
|
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)
|
for (const int searchId : searchIds)
|
||||||
{
|
{
|
||||||
const SearchHandlerPtr searchHandler = searchHandlers[searchId];
|
const std::shared_ptr<SearchHandler> &searchHandler = m_searchHandlers[searchId];
|
||||||
statusArray << QJsonObject
|
statusArray << QJsonObject
|
||||||
{
|
{
|
||||||
{u"id"_qs, searchId},
|
{u"id"_qs, searchId},
|
||||||
@ -191,11 +171,11 @@ void SearchController::resultsAction()
|
|||||||
int limit = params()[u"limit"_qs].toInt();
|
int limit = params()[u"limit"_qs].toInt();
|
||||||
int offset = params()[u"offset"_qs].toInt();
|
int offset = params()[u"offset"_qs].toInt();
|
||||||
|
|
||||||
const auto searchHandlers = sessionManager()->session()->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
const auto iter = m_searchHandlers.find(id);
|
||||||
if (!searchHandlers.contains(id))
|
if (iter == m_searchHandlers.end())
|
||||||
throw APIError(APIErrorType::NotFound);
|
throw APIError(APIErrorType::NotFound);
|
||||||
|
|
||||||
const SearchHandlerPtr searchHandler = searchHandlers[id];
|
const std::shared_ptr<SearchHandler> &searchHandler = iter.value();
|
||||||
const QList<SearchResult> searchResults = searchHandler->results();
|
const QList<SearchResult> searchResults = searchHandler->results();
|
||||||
const int size = searchResults.size();
|
const int size = searchResults.size();
|
||||||
|
|
||||||
@ -221,18 +201,15 @@ void SearchController::deleteAction()
|
|||||||
requireParams({u"id"_qs});
|
requireParams({u"id"_qs});
|
||||||
|
|
||||||
const int id = params()[u"id"_qs].toInt();
|
const int id = params()[u"id"_qs].toInt();
|
||||||
ISession *const session = sessionManager()->session();
|
|
||||||
|
|
||||||
auto searchHandlers = session->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
const auto iter = m_searchHandlers.find(id);
|
||||||
if (!searchHandlers.contains(id))
|
if (iter == m_searchHandlers.end())
|
||||||
throw APIError(APIErrorType::NotFound);
|
throw APIError(APIErrorType::NotFound);
|
||||||
|
|
||||||
const SearchHandlerPtr searchHandler = searchHandlers[id];
|
const std::shared_ptr<SearchHandler> &searchHandler = iter.value();
|
||||||
searchHandler->cancelSearch();
|
searchHandler->cancelSearch();
|
||||||
searchHandlers.remove(id);
|
m_activeSearches.remove(id);
|
||||||
session->setData(SEARCH_HANDLERS, QVariant::fromValue(searchHandlers));
|
m_searchHandlers.erase(iter);
|
||||||
|
|
||||||
removeActiveSearch(session, id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SearchController::pluginsAction()
|
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);
|
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
|
int SearchController::generateSearchId() const
|
||||||
{
|
{
|
||||||
const auto searchHandlers = sessionManager()->session()->getData<SearchHandlerDict>(SEARCH_HANDLERS);
|
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
const int id = Utils::Random::rand(1, std::numeric_limits<int>::max());
|
const int id = Utils::Random::rand(1, std::numeric_limits<int>::max());
|
||||||
if (!searchHandlers.contains(id))
|
if (!m_searchHandlers.contains(id))
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <QtContainerFwd>
|
#include <QtContainerFwd>
|
||||||
|
#include <QHash>
|
||||||
|
#include <QSet>
|
||||||
|
|
||||||
#include "base/search/searchpluginmanager.h"
|
#include "base/search/searchpluginmanager.h"
|
||||||
#include "apicontroller.h"
|
#include "apicontroller.h"
|
||||||
@ -36,7 +40,6 @@
|
|||||||
class QJsonArray;
|
class QJsonArray;
|
||||||
class QJsonObject;
|
class QJsonObject;
|
||||||
|
|
||||||
struct ISession;
|
|
||||||
struct SearchResult;
|
struct SearchResult;
|
||||||
|
|
||||||
class SearchController : public APIController
|
class SearchController : public APIController
|
||||||
@ -64,9 +67,10 @@ private:
|
|||||||
|
|
||||||
void checkForUpdatesFinished(const QHash<QString, PluginVersion> &updateInfo);
|
void checkForUpdatesFinished(const QHash<QString, PluginVersion> &updateInfo);
|
||||||
void checkForUpdatesFailed(const QString &reason);
|
void checkForUpdatesFailed(const QString &reason);
|
||||||
void searchFinished(ISession *session, int id);
|
|
||||||
void searchFailed(ISession *session, int id);
|
|
||||||
int generateSearchId() const;
|
int generateSearchId() const;
|
||||||
QJsonObject getResults(const QList<SearchResult> &searchResults, bool isSearchActive, int totalResults) const;
|
QJsonObject getResults(const QList<SearchResult> &searchResults, bool isSearchActive, int totalResults) const;
|
||||||
QJsonArray getPluginsInfo(const QStringList &plugins) 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)
|
SyncController::SyncController(QObject *parent)
|
||||||
: APIController(sessionManager, parent)
|
: APIController(parent)
|
||||||
{
|
{
|
||||||
m_freeDiskSpaceThread = new QThread(this);
|
m_freeDiskSpaceThread = new QThread(this);
|
||||||
m_freeDiskSpaceChecker = new FreeDiskSpaceChecker();
|
m_freeDiskSpaceChecker = new FreeDiskSpaceChecker();
|
||||||
@ -456,9 +456,6 @@ void SyncController::maindataAction()
|
|||||||
|
|
||||||
QVariantMap data;
|
QVariantMap data;
|
||||||
|
|
||||||
QVariantMap lastResponse = sessionManager()->session()->getData(u"syncMainDataLastResponse"_qs).toMap();
|
|
||||||
QVariantMap lastAcceptedResponse = sessionManager()->session()->getData(u"syncMainDataLastAcceptedResponse"_qs).toMap();
|
|
||||||
|
|
||||||
QVariantHash torrents;
|
QVariantHash torrents;
|
||||||
QHash<QString, QStringList> trackers;
|
QHash<QString, QStringList> trackers;
|
||||||
for (const BitTorrent::Torrent *torrent : asConst(session->torrents()))
|
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).
|
// 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.
|
// So we don't need unnecessary updates of last activity time in response.
|
||||||
const auto iterTorrents = lastResponse.find(u"torrents"_qs);
|
const auto iterTorrents = m_lastMaindataResponse.find(u"torrents"_qs);
|
||||||
if (iterTorrents != lastResponse.end())
|
if (iterTorrents != m_lastMaindataResponse.end())
|
||||||
{
|
{
|
||||||
const QVariantHash lastResponseTorrents = iterTorrents->toHash();
|
const QVariantHash lastResponseTorrents = iterTorrents->toHash();
|
||||||
const auto iterID = lastResponseTorrents.find(torrentID.toString());
|
const auto iterID = lastResponseTorrents.find(torrentID.toString());
|
||||||
@ -529,11 +526,8 @@ void SyncController::maindataAction()
|
|||||||
serverState[KEY_SYNC_MAINDATA_REFRESH_INTERVAL] = session->refreshInterval();
|
serverState[KEY_SYNC_MAINDATA_REFRESH_INTERVAL] = session->refreshInterval();
|
||||||
data[u"server_state"_qs] = serverState;
|
data[u"server_state"_qs] = serverState;
|
||||||
|
|
||||||
const int acceptedResponseId {params()[u"rid"_qs].toInt()};
|
const int acceptedResponseId = params()[u"rid"_qs].toInt();
|
||||||
setResult(QJsonObject::fromVariantMap(generateSyncData(acceptedResponseId, data, lastAcceptedResponse, lastResponse)));
|
setResult(QJsonObject::fromVariantMap(generateSyncData(acceptedResponseId, data, m_lastAcceptedMaindataResponse, m_lastMaindataResponse)));
|
||||||
|
|
||||||
sessionManager()->session()->setData(u"syncMainDataLastResponse"_qs, lastResponse);
|
|
||||||
sessionManager()->session()->setData(u"syncMainDataLastAcceptedResponse"_qs, lastAcceptedResponse);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET param:
|
// GET param:
|
||||||
@ -541,9 +535,6 @@ void SyncController::maindataAction()
|
|||||||
// - rid (int): last response id
|
// - rid (int): last response id
|
||||||
void SyncController::torrentPeersAction()
|
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 auto id = BitTorrent::TorrentID::fromString(params()[u"hash"_qs]);
|
||||||
const BitTorrent::Torrent *torrent = BitTorrent::Session::instance()->findTorrent(id);
|
const BitTorrent::Torrent *torrent = BitTorrent::Session::instance()->findTorrent(id);
|
||||||
if (!torrent)
|
if (!torrent)
|
||||||
@ -598,11 +589,8 @@ void SyncController::torrentPeersAction()
|
|||||||
}
|
}
|
||||||
data[u"peers"_qs] = peers;
|
data[u"peers"_qs] = peers;
|
||||||
|
|
||||||
const int acceptedResponseId {params()[u"rid"_qs].toInt()};
|
const int acceptedResponseId = params()[u"rid"_qs].toInt();
|
||||||
setResult(QJsonObject::fromVariantMap(generateSyncData(acceptedResponseId, data, lastAcceptedResponse, lastResponse)));
|
setResult(QJsonObject::fromVariantMap(generateSyncData(acceptedResponseId, data, m_lastAcceptedPeersResponse, m_lastPeersResponse)));
|
||||||
|
|
||||||
sessionManager()->session()->setData(u"syncTorrentPeersLastResponse"_qs, lastResponse);
|
|
||||||
sessionManager()->session()->setData(u"syncTorrentPeersLastAcceptedResponse"_qs, lastAcceptedResponse);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qint64 SyncController::getFreeDiskSpace()
|
qint64 SyncController::getFreeDiskSpace()
|
||||||
|
@ -29,11 +29,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QElapsedTimer>
|
#include <QElapsedTimer>
|
||||||
|
#include <QVariantMap>
|
||||||
|
|
||||||
#include "apicontroller.h"
|
#include "apicontroller.h"
|
||||||
|
|
||||||
struct ISessionManager;
|
|
||||||
|
|
||||||
class QThread;
|
class QThread;
|
||||||
|
|
||||||
class FreeDiskSpaceChecker;
|
class FreeDiskSpaceChecker;
|
||||||
@ -46,7 +45,7 @@ class SyncController : public APIController
|
|||||||
public:
|
public:
|
||||||
using APIController::APIController;
|
using APIController::APIController;
|
||||||
|
|
||||||
explicit SyncController(ISessionManager *sessionManager, QObject *parent = nullptr);
|
explicit SyncController(QObject *parent = nullptr);
|
||||||
~SyncController() override;
|
~SyncController() override;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
@ -62,4 +61,9 @@ private:
|
|||||||
FreeDiskSpaceChecker *m_freeDiskSpaceChecker = nullptr;
|
FreeDiskSpaceChecker *m_freeDiskSpaceChecker = nullptr;
|
||||||
QThread *m_freeDiskSpaceThread = nullptr;
|
QThread *m_freeDiskSpaceThread = nullptr;
|
||||||
QElapsedTimer m_freeDiskSpaceElapsedTimer;
|
QElapsedTimer m_freeDiskSpaceElapsedTimer;
|
||||||
|
|
||||||
|
QVariantMap m_lastMaindataResponse;
|
||||||
|
QVariantMap m_lastAcceptedMaindataResponse;
|
||||||
|
QVariantMap m_lastPeersResponse;
|
||||||
|
QVariantMap m_lastAcceptedPeersResponse;
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Bittorrent Client using Qt and libtorrent.
|
* Bittorrent Client using Qt and libtorrent.
|
||||||
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
|
* Copyright (C) 2014, 2022 Vladimir Golovnev <glassez@yandex.ru>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or
|
* This program is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public License
|
* modify it under the terms of the GNU General Public License
|
||||||
@ -119,16 +119,8 @@ namespace
|
|||||||
WebApplication::WebApplication(QObject *parent)
|
WebApplication::WebApplication(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, m_cacheID {QString::number(Utils::Random::rand(), 36)}
|
, m_cacheID {QString::number(Utils::Random::rand(), 36)}
|
||||||
|
, m_authController {new AuthController(this, this)}
|
||||||
{
|
{
|
||||||
registerAPIController(u"app"_qs, new AppController(this, this));
|
|
||||||
registerAPIController(u"auth"_qs, new AuthController(this, this));
|
|
||||||
registerAPIController(u"log"_qs, new LogController(this, this));
|
|
||||||
registerAPIController(u"rss"_qs, new RSSController(this, this));
|
|
||||||
registerAPIController(u"search"_qs, new SearchController(this, this));
|
|
||||||
registerAPIController(u"sync"_qs, new SyncController(this, this));
|
|
||||||
registerAPIController(u"torrents"_qs, new TorrentsController(this, this));
|
|
||||||
registerAPIController(u"transfer"_qs, new TransferController(this, this));
|
|
||||||
|
|
||||||
declarePublicAPI(u"auth/login"_qs);
|
declarePublicAPI(u"auth/login"_qs);
|
||||||
|
|
||||||
configure();
|
configure();
|
||||||
@ -260,9 +252,16 @@ void WebApplication::doProcessRequest()
|
|||||||
const QString action = match.captured(u"action"_qs);
|
const QString action = match.captured(u"action"_qs);
|
||||||
const QString scope = match.captured(u"scope"_qs);
|
const QString scope = match.captured(u"scope"_qs);
|
||||||
|
|
||||||
APIController *controller = m_apiControllers.value(scope);
|
APIController *controller = nullptr;
|
||||||
|
if (session())
|
||||||
|
controller = session()->getAPIController(scope);
|
||||||
if (!controller)
|
if (!controller)
|
||||||
throw NotFoundHTTPError();
|
{
|
||||||
|
if (scope == u"auth")
|
||||||
|
controller = m_authController;
|
||||||
|
else
|
||||||
|
throw NotFoundHTTPError();
|
||||||
|
}
|
||||||
|
|
||||||
if (!session() && !isPublicAPI(scope, action))
|
if (!session() && !isPublicAPI(scope, action))
|
||||||
throw ForbiddenHTTPError();
|
throw ForbiddenHTTPError();
|
||||||
@ -414,14 +413,6 @@ void WebApplication::configure()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebApplication::registerAPIController(const QString &scope, APIController *controller)
|
|
||||||
{
|
|
||||||
Q_ASSERT(controller);
|
|
||||||
Q_ASSERT(!m_apiControllers.value(scope));
|
|
||||||
|
|
||||||
m_apiControllers[scope] = controller;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WebApplication::declarePublicAPI(const QString &apiPath)
|
void WebApplication::declarePublicAPI(const QString &apiPath)
|
||||||
{
|
{
|
||||||
m_publicAPIs << apiPath;
|
m_publicAPIs << apiPath;
|
||||||
@ -607,6 +598,13 @@ void WebApplication::sessionStart()
|
|||||||
});
|
});
|
||||||
|
|
||||||
m_currentSession = new WebSession(generateSid());
|
m_currentSession = new WebSession(generateSid());
|
||||||
|
m_currentSession->registerAPIController<AppController>(u"app"_qs);
|
||||||
|
m_currentSession->registerAPIController<LogController>(u"log"_qs);
|
||||||
|
m_currentSession->registerAPIController<RSSController>(u"rss"_qs);
|
||||||
|
m_currentSession->registerAPIController<SearchController>(u"search"_qs);
|
||||||
|
m_currentSession->registerAPIController<SyncController>(u"sync"_qs);
|
||||||
|
m_currentSession->registerAPIController<TorrentsController>(u"torrents"_qs);
|
||||||
|
m_currentSession->registerAPIController<TransferController>(u"transfer"_qs);
|
||||||
m_sessions[m_currentSession->id()] = m_currentSession;
|
m_sessions[m_currentSession->id()] = m_currentSession;
|
||||||
|
|
||||||
QNetworkCookie cookie(C_SID, m_currentSession->id().toUtf8());
|
QNetworkCookie cookie(C_SID, m_currentSession->id().toUtf8());
|
||||||
@ -775,12 +773,7 @@ void WebSession::updateTimestamp()
|
|||||||
m_timer.start();
|
m_timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant WebSession::getData(const QString &id) const
|
APIController *WebSession::getAPIController(const QString &scope) const
|
||||||
{
|
{
|
||||||
return m_data.value(id);
|
return m_apiControllers.value(scope);
|
||||||
}
|
|
||||||
|
|
||||||
void WebSession::setData(const QString &id, const QVariant &data)
|
|
||||||
{
|
|
||||||
m_data[id] = data;
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Bittorrent Client using Qt and libtorrent.
|
* Bittorrent Client using Qt and libtorrent.
|
||||||
* Copyright (C) 2014, 2017 Vladimir Golovnev <glassez@yandex.ru>
|
* Copyright (C) 2014, 2017, 2022 Vladimir Golovnev <glassez@yandex.ru>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or
|
* This program is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public License
|
* modify it under the terms of the GNU General Public License
|
||||||
@ -28,9 +28,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QElapsedTimer>
|
#include <QElapsedTimer>
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
|
#include <QMap>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
@ -48,9 +51,10 @@
|
|||||||
inline const Utils::Version<int, 3, 2> API_VERSION {2, 8, 9};
|
inline const Utils::Version<int, 3, 2> API_VERSION {2, 8, 9};
|
||||||
|
|
||||||
class APIController;
|
class APIController;
|
||||||
|
class AuthController;
|
||||||
class WebApplication;
|
class WebApplication;
|
||||||
|
|
||||||
class WebSession final : public ISession
|
class WebSession final : public QObject, public ISession
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit WebSession(const QString &sid);
|
explicit WebSession(const QString &sid);
|
||||||
@ -60,13 +64,19 @@ public:
|
|||||||
bool hasExpired(qint64 seconds) const;
|
bool hasExpired(qint64 seconds) const;
|
||||||
void updateTimestamp();
|
void updateTimestamp();
|
||||||
|
|
||||||
QVariant getData(const QString &id) const override;
|
template <typename T>
|
||||||
void setData(const QString &id, const QVariant &data) override;
|
void registerAPIController(const QString &scope)
|
||||||
|
{
|
||||||
|
static_assert(std::is_base_of_v<APIController, T>, "Class should be derived from APIController.");
|
||||||
|
m_apiControllers[scope] = new T(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
APIController *getAPIController(const QString &scope) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const QString m_sid;
|
const QString m_sid;
|
||||||
QElapsedTimer m_timer; // timestamp
|
QElapsedTimer m_timer; // timestamp
|
||||||
QVariantHash m_data;
|
QMap<QString, APIController *> m_apiControllers;
|
||||||
};
|
};
|
||||||
|
|
||||||
class WebApplication final
|
class WebApplication final
|
||||||
@ -76,11 +86,6 @@ class WebApplication final
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_DISABLE_COPY_MOVE(WebApplication)
|
Q_DISABLE_COPY_MOVE(WebApplication)
|
||||||
|
|
||||||
#ifndef Q_MOC_RUN
|
|
||||||
#define WEBAPI_PUBLIC
|
|
||||||
#define WEBAPI_PRIVATE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit WebApplication(QObject *parent = nullptr);
|
explicit WebApplication(QObject *parent = nullptr);
|
||||||
~WebApplication() override;
|
~WebApplication() override;
|
||||||
@ -99,7 +104,6 @@ private:
|
|||||||
void doProcessRequest();
|
void doProcessRequest();
|
||||||
void configure();
|
void configure();
|
||||||
|
|
||||||
void registerAPIController(const QString &scope, APIController *controller);
|
|
||||||
void declarePublicAPI(const QString &apiPath);
|
void declarePublicAPI(const QString &apiPath);
|
||||||
|
|
||||||
void sendFile(const Path &path);
|
void sendFile(const Path &path);
|
||||||
@ -130,7 +134,6 @@ private:
|
|||||||
|
|
||||||
const QRegularExpression m_apiPathPattern {u"^/api/v2/(?<scope>[A-Za-z_][A-Za-z_0-9]*)/(?<action>[A-Za-z_][A-Za-z_0-9]*)$"_qs};
|
const QRegularExpression m_apiPathPattern {u"^/api/v2/(?<scope>[A-Za-z_][A-Za-z_0-9]*)/(?<action>[A-Za-z_][A-Za-z_0-9]*)$"_qs};
|
||||||
|
|
||||||
QHash<QString, APIController *> m_apiControllers;
|
|
||||||
QSet<QString> m_publicAPIs;
|
QSet<QString> m_publicAPIs;
|
||||||
bool m_isAltUIUsed = false;
|
bool m_isAltUIUsed = false;
|
||||||
Path m_rootFolder;
|
Path m_rootFolder;
|
||||||
@ -146,6 +149,7 @@ private:
|
|||||||
QTranslator m_translator;
|
QTranslator m_translator;
|
||||||
bool m_translationFileLoaded = false;
|
bool m_translationFileLoaded = false;
|
||||||
|
|
||||||
|
AuthController *m_authController = nullptr;
|
||||||
bool m_isLocalAuthEnabled;
|
bool m_isLocalAuthEnabled;
|
||||||
bool m_isAuthSubnetWhitelistEnabled;
|
bool m_isAuthSubnetWhitelistEnabled;
|
||||||
QVector<Utils::Net::Subnet> m_authSubnetWhitelist;
|
QVector<Utils::Net::Subnet> m_authSubnetWhitelist;
|
||||||
|
Loading…
Reference in New Issue
Block a user