feat: custom user-agent

This commit is contained in:
Ryan Cao 2022-06-04 15:33:17 +08:00
parent 04a3669fc4
commit 25ab121e42
No known key found for this signature in database
GPG Key ID: 528A2C1B6656B97F
9 changed files with 60 additions and 7 deletions

View File

@ -708,6 +708,7 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
// Custom MSA credentials // Custom MSA credentials
m_settings->registerSetting("MSAClientIDOverride", ""); m_settings->registerSetting("MSAClientIDOverride", "");
m_settings->registerSetting("CFKeyOverride", ""); m_settings->registerSetting("CFKeyOverride", "");
m_settings->registerSetting("UserAgentOverride", "");
// Init page provider // Init page provider
{ {
@ -1553,3 +1554,23 @@ QString Application::getCurseKey()
return BuildConfig.CURSEFORGE_API_KEY; return BuildConfig.CURSEFORGE_API_KEY;
} }
QString Application::getUserAgent()
{
QString keyOverride = m_settings->get("UserAgentOverride").toString();
if (!keyOverride.isEmpty()) {
return keyOverride;
}
return BuildConfig.USER_AGENT;
}
QString Application::getUserAgentUncached()
{
QString keyOverride = m_settings->get("UserAgentOverride").toString();
if (!keyOverride.isEmpty()) {
return keyOverride;
}
return BuildConfig.USER_AGENT_UNCACHED;
}

View File

@ -156,6 +156,8 @@ public:
QString getMSAClientID(); QString getMSAClientID();
QString getCurseKey(); QString getCurseKey();
QString getUserAgent();
QString getUserAgentUncached();
/// this is the root of the 'installation'. Used for automatic updates /// this is the root of the 'installation'. Used for automatic updates
const QString &root() { const QString &root() {

View File

@ -116,7 +116,7 @@ void Download::executeTask()
return; return;
} }
request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT); request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgent().toUtf8());
if (request.url().host().contains("api.curseforge.com")) { if (request.url().host().contains("api.curseforge.com")) {
request.setRawHeader("x-api-key", APPLICATION->getCurseKey().toUtf8()); request.setRawHeader("x-api-key", APPLICATION->getCurseKey().toUtf8());
}; };

View File

@ -71,7 +71,7 @@ void PasteUpload::executeTask()
QNetworkRequest request{QUrl(m_uploadUrl)}; QNetworkRequest request{QUrl(m_uploadUrl)};
QNetworkReply *rep{}; QNetworkReply *rep{};
request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT_UNCACHED); request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgentUncached().toUtf8());
switch (m_pasteType) { switch (m_pasteType) {
case NullPointer: { case NullPointer: {
@ -91,7 +91,7 @@ void PasteUpload::executeTask()
break; break;
} }
case Hastebin: { case Hastebin: {
request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT_UNCACHED); request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgentUncached().toUtf8());
rep = APPLICATION->network()->post(request, m_text); rep = APPLICATION->network()->post(request, m_text);
break; break;
} }

View File

@ -173,7 +173,7 @@ namespace Net {
return; return;
} }
request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT); request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgent().toUtf8());
if (request.url().host().contains("api.curseforge.com")) { if (request.url().host().contains("api.curseforge.com")) {
request.setRawHeader("x-api-key", APPLICATION->getCurseKey().toUtf8()); request.setRawHeader("x-api-key", APPLICATION->getCurseKey().toUtf8());
} }

View File

@ -55,7 +55,7 @@ void ImgurAlbumCreation::executeTask()
{ {
m_state = State::Running; m_state = State::Running;
QNetworkRequest request(m_url); QNetworkRequest request(m_url);
request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT_UNCACHED); request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgentUncached().toUtf8());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setRawHeader("Authorization", QString("Client-ID %1").arg(BuildConfig.IMGUR_CLIENT_ID).toStdString().c_str()); request.setRawHeader("Authorization", QString("Client-ID %1").arg(BuildConfig.IMGUR_CLIENT_ID).toStdString().c_str());
request.setRawHeader("Accept", "application/json"); request.setRawHeader("Accept", "application/json");

View File

@ -35,6 +35,7 @@
#include "ImgurUpload.h" #include "ImgurUpload.h"
#include "BuildConfig.h" #include "BuildConfig.h"
#include "Application.h"
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QHttpMultiPart> #include <QHttpMultiPart>
@ -56,7 +57,7 @@ void ImgurUpload::executeTask()
finished = false; finished = false;
m_state = Task::State::Running; m_state = Task::State::Running;
QNetworkRequest request(m_url); QNetworkRequest request(m_url);
request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT_UNCACHED); request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgentUncached().toUtf8());
request.setRawHeader("Authorization", QString("Client-ID %1").arg(BuildConfig.IMGUR_CLIENT_ID).toStdString().c_str()); request.setRawHeader("Authorization", QString("Client-ID %1").arg(BuildConfig.IMGUR_CLIENT_ID).toStdString().c_str());
request.setRawHeader("Accept", "application/json"); request.setRawHeader("Accept", "application/json");

View File

@ -78,6 +78,7 @@ APIPage::APIPage(QWidget *parent) :
ui->tabWidget->tabBar()->hide(); ui->tabWidget->tabBar()->hide();
ui->metaURL->setPlaceholderText(BuildConfig.META_URL); ui->metaURL->setPlaceholderText(BuildConfig.META_URL);
ui->userAgentLineEdit->setPlaceholderText(BuildConfig.USER_AGENT);
loadSettings(); loadSettings();
@ -139,6 +140,8 @@ void APIPage::loadSettings()
ui->metaURL->setText(metaURL); ui->metaURL->setText(metaURL);
QString curseKey = s->get("CFKeyOverride").toString(); QString curseKey = s->get("CFKeyOverride").toString();
ui->curseKey->setText(curseKey); ui->curseKey->setText(curseKey);
QString customUserAgent = s->get("UserAgentOverride").toString();
ui->userAgentLineEdit->setText(customUserAgent);
} }
void APIPage::applySettings() void APIPage::applySettings()
@ -167,6 +170,7 @@ void APIPage::applySettings()
s->set("MetaURLOverride", metaURL); s->set("MetaURLOverride", metaURL);
QString curseKey = ui->curseKey->text(); QString curseKey = ui->curseKey->text();
s->set("CFKeyOverride", curseKey); s->set("CFKeyOverride", curseKey);
s->set("UserAgentOverride", ui->userAgentLineEdit->text());
} }
bool APIPage::apply() bool APIPage::apply()

View File

@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>800</width> <width>800</width>
<height>600</height> <height>702</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
@ -220,6 +220,31 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item>
<widget class="QGroupBox" name="groupBox_ua">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="title">
<string>User Agent</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_7">
<item>
<widget class="QLineEdit" name="userAgentLineEdit"/>
</item>
<item>
<widget class="QLabel" name="userAgentLabel">
<property name="text">
<string>Enter a custom User Agent here. The special string ${launcher_version} will be replaced with the version of the launcher.</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item> <item>
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">