Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebAPI/WebUI for managing cookies #21340

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/webui/api/appcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkCookie>
#include <QNetworkInterface>
#include <QRegularExpression>
#include <QStringList>
Expand All @@ -50,6 +51,7 @@
#include "base/bittorrent/session.h"
#include "base/global.h"
#include "base/interfaces/iapplication.h"
#include "base/net/downloadmanager.h"
#include "base/net/portforwarder.h"
#include "base/net/proxyconfigurationmanager.h"
#include "base/path.h"
Expand All @@ -58,6 +60,7 @@
#include "base/rss/rss_session.h"
#include "base/torrentfileguard.h"
#include "base/torrentfileswatcher.h"
#include "base/utils/datetime.h"
#include "base/utils/fs.h"
#include "base/utils/misc.h"
#include "base/utils/net.h"
Expand All @@ -69,6 +72,12 @@

using namespace std::chrono_literals;

const QString KEY_COOKIE_NAME = u"name"_s;
const QString KEY_COOKIE_DOMAIN = u"domain"_s;
const QString KEY_COOKIE_PATH = u"path"_s;
const QString KEY_COOKIE_VALUE = u"value"_s;
const QString KEY_COOKIE_EXPIRATION_DATE = u"expirationDate"_s;

void AppController::webapiVersionAction()
{
setResult(API_VERSION.toString());
Expand Down Expand Up @@ -1187,6 +1196,63 @@ void AppController::getDirectoryContentAction()
setResult(ret);
}

void AppController::cookiesAction()
{
const QList<QNetworkCookie> cookies = Net::DownloadManager::instance()->allCookies();
QJsonArray ret;
for (const QNetworkCookie &cookie : cookies)
{
ret << QJsonObject {
{KEY_COOKIE_NAME, QString::fromLatin1(cookie.name())},
{KEY_COOKIE_DOMAIN, cookie.domain()},
{KEY_COOKIE_PATH, cookie.path()},
{KEY_COOKIE_VALUE, QString::fromLatin1(cookie.value())},
{KEY_COOKIE_EXPIRATION_DATE, Utils::DateTime::toSecsSinceEpoch(cookie.expirationDate())},
};
}

setResult(ret);
}

void AppController::setCookiesAction()
{
requireParams({u"cookies"_s});
const QString cookiesParam {params()[u"cookies"_s].trimmed()};

QJsonParseError jsonError;
const auto cookiesJsonDocument = QJsonDocument::fromJson(cookiesParam.toUtf8(), &jsonError);
if (jsonError.error != QJsonParseError::NoError)
throw APIError(APIErrorType::BadParams, jsonError.errorString());
if (!cookiesJsonDocument.isArray())
throw APIError(APIErrorType::BadParams, tr("cookies must be array"));

const QJsonArray cookiesJsonArr = cookiesJsonDocument.array();
QList<QNetworkCookie> cookies;
cookies.reserve(cookiesJsonArr.size());
for (const QJsonValue &jsonVal : cookiesJsonArr)
{
if (!jsonVal.isObject())
throw APIError(APIErrorType::BadParams);

QNetworkCookie cookie;
const QJsonObject jsonObj = jsonVal.toObject();
if (jsonObj.contains(KEY_COOKIE_NAME))
cookie.setName(jsonObj.value(KEY_COOKIE_NAME).toString().toLatin1());
if (jsonObj.contains(KEY_COOKIE_DOMAIN))
cookie.setDomain(jsonObj.value(KEY_COOKIE_DOMAIN).toString());
if (jsonObj.contains(KEY_COOKIE_PATH))
cookie.setPath(jsonObj.value(KEY_COOKIE_PATH).toString());
if (jsonObj.contains(KEY_COOKIE_VALUE))
cookie.setValue(jsonObj.value(KEY_COOKIE_VALUE).toString().toUtf8());
if (jsonObj.contains(KEY_COOKIE_EXPIRATION_DATE))
cookie.setExpirationDate(QDateTime::fromSecsSinceEpoch(jsonObj.value(KEY_COOKIE_EXPIRATION_DATE).toInteger()));

cookies << cookie;
}

Net::DownloadManager::instance()->setAllCookies(cookies);
}

void AppController::networkInterfaceListAction()
{
QJsonArray ifaceList;
Expand Down
2 changes: 2 additions & 0 deletions src/webui/api/appcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ private slots:
void defaultSavePathAction();
void sendTestEmailAction();
void getDirectoryContentAction();
void cookiesAction();
void setCookiesAction();

void networkInterfaceListAction();
void networkInterfaceAddressListAction();
Expand Down
21 changes: 0 additions & 21 deletions src/webui/api/torrentscontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <QJsonArray>
#include <QJsonObject>
#include <QList>
#include <QNetworkCookie>
#include <QRegularExpression>
#include <QUrl>

Expand All @@ -53,7 +52,6 @@
#include "base/interfaces/iapplication.h"
#include "base/global.h"
#include "base/logger.h"
#include "base/net/downloadmanager.h"
#include "base/torrentfilter.h"
#include "base/utils/datetime.h"
#include "base/utils/fs.h"
Expand Down Expand Up @@ -787,7 +785,6 @@ void TorrentsController::pieceStatesAction()
void TorrentsController::addAction()
{
const QString urls = params()[u"urls"_s];
const QString cookie = params()[u"cookie"_s];
Copy link
Member Author

@Piccirello Piccirello Sep 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a WebAPI bump yet in 5.1? It'd be nice to get this in 5.0 if possible but otherwise can bump the WebAPI in 5.1 too.


const bool skipChecking = parseBool(params()[u"skip_checking"_s]).value_or(false);
const bool seqDownload = parseBool(params()[u"sequentialDownload"_s]).value_or(false);
Expand Down Expand Up @@ -818,23 +815,6 @@ void TorrentsController::addAction()
? Utils::String::toEnum(contentLayoutParam, BitTorrent::TorrentContentLayout::Original)
: std::optional<BitTorrent::TorrentContentLayout> {});

QList<QNetworkCookie> cookies;
if (!cookie.isEmpty())
{
const QStringList cookiesStr = cookie.split(u"; "_s);
for (QString cookieStr : cookiesStr)
{
cookieStr = cookieStr.trimmed();
int index = cookieStr.indexOf(u'=');
if (index > 1)
{
QByteArray name = cookieStr.left(index).toLatin1();
QByteArray value = cookieStr.right(cookieStr.length() - index - 1).toLatin1();
cookies += QNetworkCookie(name, value);
}
}
}

const BitTorrent::AddTorrentParams addTorrentParams
{
// TODO: Check if destination actually exists
Expand Down Expand Up @@ -875,7 +855,6 @@ void TorrentsController::addAction()
url = url.trimmed();
if (!url.isEmpty())
{
Net::DownloadManager::instance()->setCookiesFromUrl(cookies, QUrl::fromEncoded(url.toUtf8()));
partialSuccess |= app()->addTorrentManager()->addTorrent(url, addTorrentParams);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/webui/webapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class WebApplication final : public ApplicationComponent<QObject>
{
// <<controller name, action name>, HTTP method>
{{u"app"_s, u"sendTestEmail"_s}, Http::METHOD_POST},
{{u"app"_s, u"setCookies"_s}, Http::METHOD_POST},
{{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},
Expand Down
8 changes: 0 additions & 8 deletions src/webui/www/private/download.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ <h2><label for="urls">QBT_TR(Add torrent links)QBT_TR[CONTEXT=AddNewTorrentDialo
<input type="text" id="savepath" name="savepath" class="pathDirectory" style="width: 16em;">
</td>
</tr>
<tr>
<td>
<label for="cookie">QBT_TR(Cookie:)QBT_TR[CONTEXT=HttpServer]</label>
</td>
<td>
<input type="text" id="cookie" name="cookie" style="width: 16em;">
</td>
</tr>
<tr>
<td>
<label for="rename">QBT_TR(Rename torrent)QBT_TR[CONTEXT=HttpServer]</label>
Expand Down
1 change: 1 addition & 0 deletions src/webui/www/private/images/browser-cookies.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/webui/www/private/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ <h1 class="applicationTitle">qBittorrent Web User Interface <span class="version
<ul>
<li><a id="preferencesLink"><img class="MyMenuIcon" src="images/configure.svg" alt="QBT_TR(Options...)QBT_TR[CONTEXT=MainWindow]" width="16" height="16">QBT_TR(Options...)QBT_TR[CONTEXT=MainWindow]</a></li>
<li><a id="registerMagnetHandlerLink"><img class="MyMenuIcon" src="images/torrent-magnet.svg" alt="QBT_TR(Register to handle magnet links...)QBT_TR[CONTEXT=HttpServer]" width="16" height="16">QBT_TR(Register to handle magnet links...)QBT_TR[CONTEXT=HttpServer]</a></li>
<li><a id="manageCookiesLink"><img class="MyMenuIcon" src="images/browser-cookies.svg" alt="QBT_TR(Manage Cookies...)QBT_TR[CONTEXT=MainWindow]" width="16" height="16">QBT_TR(Manage Cookies...)QBT_TR[CONTEXT=MainWindow]</a></li>
</ul>
</li>
<li>
Expand Down
21 changes: 21 additions & 0 deletions src/webui/www/private/scripts/mocha-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,27 @@ const initializeWindows = function() {
});
});

addClickEvent("manageCookies", (e) => {
e.preventDefault();
e.stopPropagation();

const id = "cookiesPage";
new MochaUI.Window({
id: id,
title: "QBT_TR(Manage Cookies)QBT_TR[CONTEXT=CookiesDialog]",
loadMethod: "xhr",
contentURL: new URI("views/cookies.html").toString(),
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
width: loadWindowWidth(id, 900),
height: loadWindowHeight(id, 400),
onResize: function() {
saveWindowSize(id);
}
});
});

addClickEvent("upload", (e) => {
e.preventDefault();
e.stopPropagation();
Expand Down
Loading
Loading