2014-08-22 23:08:44 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
2022-04-12 13:39:35 +03:00
|
|
|
* Copyright (C) 2014, 2017, 2022 Vladimir Golovnev <glassez@yandex.ru>
|
2014-08-22 23:08:44 +04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*/
|
|
|
|
|
2017-10-14 16:27:21 +03:00
|
|
|
#pragma once
|
2014-08-22 23:08:44 +04:00
|
|
|
|
2022-04-12 13:39:35 +03:00
|
|
|
#include <type_traits>
|
2022-08-15 06:56:59 +03:00
|
|
|
#include <utility>
|
2022-04-12 13:39:35 +03:00
|
|
|
|
2017-10-14 16:27:21 +03:00
|
|
|
#include <QDateTime>
|
2019-06-13 17:43:34 +03:00
|
|
|
#include <QElapsedTimer>
|
2017-10-14 16:27:21 +03:00
|
|
|
#include <QHash>
|
2022-09-07 08:29:46 +03:00
|
|
|
#include <QHostAddress>
|
2022-04-12 13:39:35 +03:00
|
|
|
#include <QMap>
|
2017-10-14 16:27:21 +03:00
|
|
|
#include <QObject>
|
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QSet>
|
2018-06-07 20:07:28 +03:00
|
|
|
#include <QTranslator>
|
2022-09-07 08:29:46 +03:00
|
|
|
#include <QVector>
|
2014-08-22 23:08:44 +04:00
|
|
|
|
2022-06-25 15:46:55 +03:00
|
|
|
#include "base/applicationcomponent.h"
|
2022-03-26 06:53:50 +03:00
|
|
|
#include "base/global.h"
|
2017-10-14 16:27:21 +03:00
|
|
|
#include "base/http/irequesthandler.h"
|
|
|
|
#include "base/http/responsebuilder.h"
|
|
|
|
#include "base/http/types.h"
|
2022-02-08 06:03:48 +03:00
|
|
|
#include "base/path.h"
|
2018-07-14 10:47:34 +03:00
|
|
|
#include "base/utils/net.h"
|
2017-10-14 16:27:21 +03:00
|
|
|
#include "base/utils/version.h"
|
2022-03-26 06:53:50 +03:00
|
|
|
#include "api/isessionmanager.h"
|
2017-10-14 16:27:21 +03:00
|
|
|
|
2023-04-05 12:33:45 +03:00
|
|
|
inline const Utils::Version<3, 2> API_VERSION {2, 9, 1};
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
class APIController;
|
2022-04-12 13:39:35 +03:00
|
|
|
class AuthController;
|
2017-10-14 16:27:21 +03:00
|
|
|
class WebApplication;
|
|
|
|
|
2022-06-25 15:46:55 +03:00
|
|
|
class WebSession final : public QObject, public ApplicationComponent, public ISession
|
2017-10-14 16:27:21 +03:00
|
|
|
{
|
|
|
|
public:
|
2022-06-25 15:46:55 +03:00
|
|
|
explicit WebSession(const QString &sid, IApplication *app);
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
QString id() const override;
|
2019-06-13 17:43:34 +03:00
|
|
|
|
|
|
|
bool hasExpired(qint64 seconds) const;
|
|
|
|
void updateTimestamp();
|
2017-10-14 16:27:21 +03:00
|
|
|
|
2022-04-12 13:39:35 +03:00
|
|
|
template <typename T>
|
|
|
|
void registerAPIController(const QString &scope)
|
|
|
|
{
|
|
|
|
static_assert(std::is_base_of_v<APIController, T>, "Class should be derived from APIController.");
|
2022-06-25 15:46:55 +03:00
|
|
|
m_apiControllers[scope] = new T(app(), this);
|
2022-04-12 13:39:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
APIController *getAPIController(const QString &scope) const;
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
const QString m_sid;
|
2019-06-13 17:43:34 +03:00
|
|
|
QElapsedTimer m_timer; // timestamp
|
2022-04-12 13:39:35 +03:00
|
|
|
QMap<QString, APIController *> m_apiControllers;
|
2017-10-14 16:27:21 +03:00
|
|
|
};
|
|
|
|
|
2020-04-17 07:37:53 +03:00
|
|
|
class WebApplication final
|
2022-06-25 15:46:55 +03:00
|
|
|
: public QObject, public ApplicationComponent
|
|
|
|
, public Http::IRequestHandler, public ISessionManager
|
2017-10-14 16:27:21 +03:00
|
|
|
, private Http::ResponseBuilder
|
2014-08-22 23:08:44 +04:00
|
|
|
{
|
2017-10-14 16:27:21 +03:00
|
|
|
Q_OBJECT
|
2021-06-29 09:45:23 +03:00
|
|
|
Q_DISABLE_COPY_MOVE(WebApplication)
|
2014-08-22 23:08:44 +04:00
|
|
|
|
|
|
|
public:
|
2022-06-25 15:46:55 +03:00
|
|
|
explicit WebApplication(IApplication *app, QObject *parent = nullptr);
|
2017-10-14 16:27:21 +03:00
|
|
|
~WebApplication() override;
|
|
|
|
|
2018-03-06 18:41:18 +03:00
|
|
|
Http::Response processRequest(const Http::Request &request, const Http::Environment &env) override;
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
QString clientId() const override;
|
|
|
|
WebSession *session() override;
|
|
|
|
void sessionStart() override;
|
|
|
|
void sessionEnd() override;
|
|
|
|
|
|
|
|
const Http::Request &request() const;
|
|
|
|
const Http::Environment &env() const;
|
2014-08-22 23:08:44 +04:00
|
|
|
|
|
|
|
private:
|
2017-10-14 16:27:21 +03:00
|
|
|
void doProcessRequest();
|
|
|
|
void configure();
|
|
|
|
|
|
|
|
void declarePublicAPI(const QString &apiPath);
|
|
|
|
|
2022-02-08 06:03:48 +03:00
|
|
|
void sendFile(const Path &path);
|
2017-10-14 16:27:21 +03:00
|
|
|
void sendWebUIFile();
|
2014-08-22 23:08:44 +04:00
|
|
|
|
2019-08-14 15:27:06 +03:00
|
|
|
void translateDocument(QString &data) const;
|
2018-06-07 20:07:28 +03:00
|
|
|
|
2017-10-14 16:27:21 +03:00
|
|
|
// Session management
|
|
|
|
QString generateSid() const;
|
|
|
|
void sessionInitialize();
|
|
|
|
bool isAuthNeeded();
|
|
|
|
bool isPublicAPI(const QString &scope, const QString &action) const;
|
|
|
|
|
|
|
|
bool isCrossSiteRequest(const Http::Request &request) const;
|
|
|
|
bool validateHostHeader(const QStringList &domains) const;
|
|
|
|
|
2021-06-23 09:01:36 +03:00
|
|
|
QHostAddress resolveClientAddress() const;
|
|
|
|
|
2017-10-14 16:27:21 +03:00
|
|
|
// Persistent data
|
2019-04-25 21:50:40 +03:00
|
|
|
QHash<QString, WebSession *> m_sessions;
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
// Current data
|
|
|
|
WebSession *m_currentSession = nullptr;
|
|
|
|
Http::Request m_request;
|
|
|
|
Http::Environment m_env;
|
2019-08-04 12:22:28 +03:00
|
|
|
QHash<QString, QString> m_params;
|
2019-04-17 15:09:03 +03:00
|
|
|
const QString m_cacheID;
|
2017-10-14 16:27:21 +03:00
|
|
|
|
2023-06-17 21:02:02 +03:00
|
|
|
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]*)$"_s};
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
QSet<QString> m_publicAPIs;
|
2022-08-15 06:56:59 +03:00
|
|
|
const QHash<std::pair<QString, QString>, QString> m_allowedMethod =
|
|
|
|
{
|
|
|
|
// <<controller name, action name>, HTTP method>
|
2023-06-17 21:02:02 +03:00
|
|
|
{{u"app"_s, u"setPreferences"_s}, Http::METHOD_POST},
|
|
|
|
{{u"app"_s, u"shutdown"_s}, Http::METHOD_POST},
|
|
|
|
{{u"auth"_s, u"login"_s}, Http::METHOD_POST},
|
|
|
|
{{u"auth"_s, u"logout"_s}, Http::METHOD_POST},
|
|
|
|
{{u"rss"_s, u"addFeed"_s}, Http::METHOD_POST},
|
|
|
|
{{u"rss"_s, u"setFeedURL"_s}, Http::METHOD_POST},
|
|
|
|
{{u"rss"_s, u"addFolder"_s}, Http::METHOD_POST},
|
|
|
|
{{u"rss"_s, u"markAsRead"_s}, Http::METHOD_POST},
|
|
|
|
{{u"rss"_s, u"moveItem"_s}, Http::METHOD_POST},
|
|
|
|
{{u"rss"_s, u"refreshItem"_s}, Http::METHOD_POST},
|
|
|
|
{{u"rss"_s, u"removeItem"_s}, Http::METHOD_POST},
|
|
|
|
{{u"rss"_s, u"removeRule"_s}, Http::METHOD_POST},
|
|
|
|
{{u"rss"_s, u"renameRule"_s}, Http::METHOD_POST},
|
|
|
|
{{u"rss"_s, u"setRule"_s}, Http::METHOD_POST},
|
|
|
|
{{u"search"_s, u"delete"_s}, Http::METHOD_POST},
|
|
|
|
{{u"search"_s, u"enablePlugin"_s}, Http::METHOD_POST},
|
|
|
|
{{u"search"_s, u"installPlugin"_s}, Http::METHOD_POST},
|
|
|
|
{{u"search"_s, u"start"_s}, Http::METHOD_POST},
|
|
|
|
{{u"search"_s, u"stop"_s}, Http::METHOD_POST},
|
|
|
|
{{u"search"_s, u"uninstallPlugin"_s}, Http::METHOD_POST},
|
|
|
|
{{u"search"_s, u"updatePlugins"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"add"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"addPeers"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"addTags"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"addTrackers"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"bottomPrio"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"createCategory"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"createTags"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"decreasePrio"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"delete"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"deleteTags"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"editCategory"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"editTracker"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"filePrio"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"increasePrio"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"pause"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"reannounce"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"recheck"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"removeCategories"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"removeTags"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"removeTrackers"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"rename"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"renameFile"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"renameFolder"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"resume"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"setAutoManagement"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"setCategory"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"setDownloadLimit"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"setDownloadPath"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"setForceStart"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"setLocation"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"setSavePath"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"setShareLimits"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"setSuperSeeding"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"setUploadLimit"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"toggleFirstLastPiecePrio"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"toggleSequentialDownload"_s}, Http::METHOD_POST},
|
|
|
|
{{u"torrents"_s, u"topPrio"_s}, Http::METHOD_POST},
|
|
|
|
{{u"transfer"_s, u"banPeers"_s}, Http::METHOD_POST},
|
|
|
|
{{u"transfer"_s, u"setDownloadLimit"_s}, Http::METHOD_POST},
|
|
|
|
{{u"transfer"_s, u"setSpeedLimitsMode"_s}, Http::METHOD_POST},
|
|
|
|
{{u"transfer"_s, u"setUploadLimit"_s}, Http::METHOD_POST},
|
|
|
|
{{u"transfer"_s, u"toggleSpeedLimitsMode"_s}, Http::METHOD_POST},
|
2022-08-15 06:56:59 +03:00
|
|
|
};
|
2017-10-14 16:27:21 +03:00
|
|
|
bool m_isAltUIUsed = false;
|
2022-02-08 06:03:48 +03:00
|
|
|
Path m_rootFolder;
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
struct TranslatedFile
|
|
|
|
{
|
|
|
|
QByteArray data;
|
2019-08-14 12:48:40 +03:00
|
|
|
QString mimeType;
|
2017-10-14 16:27:21 +03:00
|
|
|
QDateTime lastModified;
|
|
|
|
};
|
2022-02-08 06:03:48 +03:00
|
|
|
QHash<Path, TranslatedFile> m_translatedFiles;
|
2018-05-11 15:45:00 +03:00
|
|
|
QString m_currentLocale;
|
2018-06-07 20:07:28 +03:00
|
|
|
QTranslator m_translator;
|
2018-12-08 07:03:43 +03:00
|
|
|
bool m_translationFileLoaded = false;
|
2018-05-21 18:33:44 +03:00
|
|
|
|
2022-04-12 13:39:35 +03:00
|
|
|
AuthController *m_authController = nullptr;
|
2023-05-07 10:16:32 +03:00
|
|
|
bool m_isLocalAuthEnabled = false;
|
|
|
|
bool m_isAuthSubnetWhitelistEnabled = false;
|
2019-08-02 07:55:06 +03:00
|
|
|
QVector<Utils::Net::Subnet> m_authSubnetWhitelist;
|
2023-05-07 10:16:32 +03:00
|
|
|
int m_sessionTimeout = 0;
|
2023-01-17 09:31:17 +03:00
|
|
|
QString m_sessionCookieName;
|
2018-07-14 10:47:34 +03:00
|
|
|
|
2018-05-21 18:33:44 +03:00
|
|
|
// security related
|
2018-07-14 10:47:34 +03:00
|
|
|
QStringList m_domainList;
|
2023-05-07 10:16:32 +03:00
|
|
|
bool m_isCSRFProtectionEnabled = true;
|
|
|
|
bool m_isSecureCookieEnabled = true;
|
|
|
|
bool m_isHostHeaderValidationEnabled = true;
|
|
|
|
bool m_isHttpsEnabled = false;
|
2020-04-22 12:15:12 +03:00
|
|
|
|
2021-06-23 09:01:36 +03:00
|
|
|
// Reverse proxy
|
2023-05-07 10:16:32 +03:00
|
|
|
bool m_isReverseProxySupportEnabled = false;
|
2022-09-07 08:29:46 +03:00
|
|
|
QVector<Utils::Net::Subnet> m_trustedReverseProxyList;
|
2021-06-23 09:01:36 +03:00
|
|
|
QHostAddress m_clientAddress;
|
|
|
|
|
2020-05-09 21:48:21 +03:00
|
|
|
QVector<Http::Header> m_prebuiltHeaders;
|
2017-10-14 16:27:21 +03:00
|
|
|
};
|