Skip to content

Commit

Permalink
Use PolyMC's CurseForge workaround (#47)
Browse files Browse the repository at this point in the history
* Curseforge workarounds

This should allow people to use Curseforge without having to manually
paste a working key into the settings or change the user agent.

Signed-off-by: Lenny McLennington <[email protected]>

* chore: update cf api key api url

Sascha says the domain name we're using is not gonna be renewed, so I'm
switching it to a domain controlled by me instead so that this won't be
a problem in the future.

Signed-off-by: Lenny McLennington <[email protected]>

* feat: add ability to disable cf api key fetching

by setting the cf api key api url to a blank string

Signed-off-by: Lenny McLennington <[email protected]>

* don't ask before fetching key

* change polymc mention to pollymc

Signed-off-by: Lenny McLennington <[email protected]>
Co-authored-by: Lenny McLennington <[email protected]>
  • Loading branch information
fn2006 and LennyMcLennington committed Dec 20, 2022
1 parent 3e8881e commit 689a653
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 10 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ set(Launcher_MSA_CLIENT_ID "" CACHE STRING "Client ID you can get from Microsoft
# https://support.curseforge.com/en/support/solutions/articles/9000207405-curse-forge-3rd-party-api-terms-and-conditions
# NOTE: CurseForge requires you to change this if you make any kind of derivative work.
set(Launcher_CURSEFORGE_API_KEY "" CACHE STRING "API key for the CurseForge platform")
set(Launcher_CURSEFORGE_API_KEY_API_URL "https://cf.polymc.org/api" CACHE STRING "URL to fetch the Curseforge API key from.")


#### Check the current Git commit and branch
Expand Down
2 changes: 2 additions & 0 deletions buildconfig/BuildConfig.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/*
* PolyMC - Minecraft Launcher
* Copyright (C) 2022 Sefa Eyeoglu <[email protected]>
* Copyright (C) 2022 Lenny McLennington <[email protected]>
*
* 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
Expand Down Expand Up @@ -105,6 +106,7 @@ Config::Config()
IMGUR_CLIENT_ID = "@Launcher_IMGUR_CLIENT_ID@";
MSA_CLIENT_ID = "@Launcher_MSA_CLIENT_ID@";
FLAME_API_KEY = "@Launcher_CURSEFORGE_API_KEY@";
FLAME_API_KEY_API_URL = "@Launcher_CURSEFORGE_API_KEY_API_URL@";
META_URL = "@Launcher_META_URL@";

BUG_TRACKER_URL = "@Launcher_BUG_TRACKER_URL@";
Expand Down
6 changes: 6 additions & 0 deletions buildconfig/BuildConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 Jamie Mansfield <[email protected]>
* Copyright (C) 2022 Sefa Eyeoglu <[email protected]>
* Copyright (C) 2022 Lenny McLennington <[email protected]>
*
* 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
Expand Down Expand Up @@ -127,6 +128,11 @@ class Config {
*/
QString FLAME_API_KEY;

/**
* URL to fetch the Client API key for CurseForge from
*/
QString FLAME_API_KEY_API_URL;

/**
* Metadata repository URL prefix
*/
Expand Down
20 changes: 20 additions & 0 deletions launcher/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
#include "icons/IconList.h"
#include "net/HttpMetaCache.h"

#include "ui/GuiUtil.h"

#include "java/JavaUtils.h"

#include "updater/UpdateChecker.h"
Expand Down Expand Up @@ -682,6 +684,7 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv)
m_settings->set("FlameKeyOverride", flameKey);
m_settings->reset("CFKeyOverride");
}
m_settings->registerSetting("FlameKeyShouldBeFetchedOnStartup", true);
m_settings->registerSetting("UserAgentOverride", "");

// Init page provider
Expand Down Expand Up @@ -953,6 +956,7 @@ void Application::setupWizardFinished(int status)
void Application::performMainStartupAction()
{
m_status = Application::Initialized;

if(!m_instanceIdToLaunch.isEmpty())
{
auto inst = instances()->getInstanceById(m_instanceIdToLaunch);
Expand Down Expand Up @@ -992,6 +996,22 @@ void Application::performMainStartupAction()
return;
}
}

{
bool shouldFetch = m_settings->get("FlameKeyShouldBeFetchedOnStartup").toBool();
if (!BuildConfig.FLAME_API_KEY_API_URL.isEmpty() && shouldFetch && !(capabilities() & Capability::SupportsFlame))
{
// don't ask, just fetch
QString apiKey = GuiUtil::fetchFlameKey();
if (!apiKey.isEmpty())
{
m_settings->set("FlameKeyOverride", apiKey);
updateCapabilities();
}
m_settings->set("FlameKeyShouldBeFetchedOnStartup", false);
}
}

if(!m_mainWindow)
{
// normal main window
Expand Down
2 changes: 2 additions & 0 deletions launcher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ set(NET_SOURCES
net/ChecksumValidator.h
net/Download.cpp
net/Download.h
net/FetchFlameAPIKey.cpp
net/FetchFlameAPIKey.h
net/FileSink.cpp
net/FileSink.h
net/HttpMetaCache.cpp
Expand Down
6 changes: 4 additions & 2 deletions launcher/net/Download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ void Download::executeTask()
return;
}

request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgent().toUtf8());
if (APPLICATION->capabilities() & Application::SupportsFlame
&& request.url().host().contains("api.curseforge.com")) {
request.setRawHeader("x-api-key", APPLICATION->getFlameAPIKey().toUtf8());
};
}
else {
request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgent().toUtf8());
}

QNetworkReply* rep = m_network->get(request);

Expand Down
81 changes: 81 additions & 0 deletions launcher/net/FetchFlameAPIKey.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (C) 2022 Lenny McLennington <[email protected]>
*
* 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, version 3.
*
* 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, see <https://www.gnu.org/licenses/>.
*/

#include "FetchFlameAPIKey.h"
#include "Application.h"
#include <BuildConfig.h>
#include <Json.h>

#include <ui/dialogs/ProgressDialog.h>
#include <ui/dialogs/CustomMessageBox.h>

FetchFlameAPIKey::FetchFlameAPIKey(QObject *parent)
: Task{parent}
{

}

void FetchFlameAPIKey::executeTask()
{
QNetworkRequest req(BuildConfig.FLAME_API_KEY_API_URL);
m_reply.reset(APPLICATION->network()->get(req));
connect(m_reply.get(), &QNetworkReply::downloadProgress, this, &Task::setProgress);
connect(m_reply.get(), &QNetworkReply::finished, this, &FetchFlameAPIKey::downloadFinished);
connect(m_reply.get(),
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
&QNetworkReply::errorOccurred,
#else
qOverload<QNetworkReply::NetworkError>(&QNetworkReply::error),
#endif
this,
[this] (QNetworkReply::NetworkError error) {
qCritical() << "Network error: " << error;
emitFailed(m_reply->errorString());
});

setStatus(tr("Fetching Curseforge core API key"));
}

void FetchFlameAPIKey::downloadFinished()
{
auto res = m_reply->readAll();
auto doc = QJsonDocument::fromJson(res);

qDebug() << doc;

try {
auto obj = Json::requireObject(doc);

auto success = Json::requireBoolean(obj, "ok");

if (success)
{
m_result = Json::requireString(obj, "token");
emitSucceeded();
}
else
{
emitFailed("The API returned an output indicating failure.");
}
}
catch (Json::JsonException&)
{
qCritical() << "Output: " << res;
emitFailed("The API returned an unexpected JSON output.");
}
}
44 changes: 44 additions & 0 deletions launcher/net/FetchFlameAPIKey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (C) 2022 Lenny McLennington <[email protected]>
*
* 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, version 3.
*
* 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, see <https://www.gnu.org/licenses/>.
*/

#ifndef FETCHFLAMEAPIKEY_H
#define FETCHFLAMEAPIKEY_H

#include <QObject>
#include <QNetworkReply>
#include <tasks/Task.h>

class FetchFlameAPIKey : public Task
{
Q_OBJECT
public:
explicit FetchFlameAPIKey(QObject *parent = nullptr);

QString m_result;

public slots:
void downloadFinished();

protected:
virtual void executeTask();


std::shared_ptr<QNetworkReply> m_reply;
};

#endif // FETCHFLAMEAPIKEY_H
4 changes: 3 additions & 1 deletion launcher/net/Upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,13 @@ namespace Net {
return;
}

request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgent().toUtf8());
if (APPLICATION->capabilities() & Application::SupportsFlame
&& request.url().host().contains("api.curseforge.com")) {
request.setRawHeader("x-api-key", APPLICATION->getFlameAPIKey().toUtf8());
}
else {
request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgent().toUtf8());
}
//TODO other types of post requests ?
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QNetworkReply* rep = m_network->post(request, m_post_data);
Expand Down
26 changes: 26 additions & 0 deletions launcher/ui/GuiUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <QApplication>
#include <QFileDialog>

#include "net/FetchFlameAPIKey.h"
#include "ui/dialogs/ProgressDialog.h"
#include "ui/dialogs/CustomMessageBox.h"
#include "net/PasteUpload.h"
Expand All @@ -49,6 +50,31 @@
#include <DesktopServices.h>
#include <BuildConfig.h>

QString GuiUtil::fetchFlameKey(QWidget *parentWidget)
{
if (BuildConfig.FLAME_API_KEY_API_URL.isEmpty())
return "";

ProgressDialog prog(parentWidget);
auto flameKeyTask = std::make_unique<FetchFlameAPIKey>();
prog.execWithTask(flameKeyTask.get());

if (!flameKeyTask->wasSuccessful())
{
auto message = QObject::tr("Fetching the Curseforge API key failed. Reason: %1").arg(flameKeyTask->failReason());
if (!(APPLICATION->capabilities() & Application::SupportsFlame))
{
message += "\n\n" + QObject::tr("Downloading Curseforge modpacks will not work unless you manually set a valid Curseforge Core API key in the settings.");
}

CustomMessageBox::selectable(parentWidget,
QObject::tr("Failed to fetch Curseforge API key."),
message, QMessageBox::Critical)->exec();
}

return flameKeyTask->m_result;
}

QString GuiUtil::uploadPaste(const QString &text, QWidget *parentWidget)
{
ProgressDialog dialog(parentWidget);
Expand Down
1 change: 1 addition & 0 deletions launcher/ui/GuiUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GuiUtil
{
QString fetchFlameKey(QWidget *parentWidget = nullptr);
QString uploadPaste(const QString &text, QWidget *parentWidget);
void setClipboardText(const QString &text);
QStringList BrowseForFiles(QString context, QString caption, QString filter, QString defaultPath, QWidget *parentWidget);
Expand Down
15 changes: 15 additions & 0 deletions launcher/ui/pages/global/APIPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#include "net/PasteUpload.h"
#include "BuildConfig.h"

#include "ui/GuiUtil.h"

APIPage::APIPage(QWidget *parent) :
QWidget(parent),
ui(new Ui::APIPage)
Expand Down Expand Up @@ -87,11 +89,16 @@ APIPage::APIPage(QWidget *parent) :
ui->metaURL->setPlaceholderText(BuildConfig.META_URL);
ui->userAgentLineEdit->setPlaceholderText(BuildConfig.USER_AGENT);

if (BuildConfig.FLAME_API_KEY_API_URL.isEmpty())
ui->fetchKeyButton->hide();

loadSettings();

resetBaseURLNote();
connect(ui->pasteTypeComboBox, currentIndexChangedSignal, this, &APIPage::updateBaseURLNote);
connect(ui->baseURLEntry, &QLineEdit::textEdited, this, &APIPage::resetBaseURLNote);

connect(ui->fetchKeyButton, &QPushButton::clicked, this, &APIPage::fetchKeyButtonPressed);
}

APIPage::~APIPage()
Expand Down Expand Up @@ -180,6 +187,14 @@ void APIPage::applySettings()
s->set("UserAgentOverride", ui->userAgentLineEdit->text());
}

void APIPage::fetchKeyButtonPressed()
{
QString apiKey = GuiUtil::fetchFlameKey(parentWidget());

if (!apiKey.isEmpty())
ui->flameKey->setText(apiKey);
}

bool APIPage::apply()
{
applySettings();
Expand Down
1 change: 1 addition & 0 deletions launcher/ui/pages/global/APIPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class APIPage : public QWidget, public BasePage
void updateBaseURLPlaceholder(int index);
void loadSettings();
void applySettings();
void fetchKeyButtonPressed();

private:
Ui::APIPage *ui;
Expand Down
30 changes: 23 additions & 7 deletions launcher/ui/pages/global/APIPage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,6 @@
<string>&amp;CurseForge Core API</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Note: you probably don't need to set this if CurseForge already works.</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
Expand All @@ -237,6 +230,29 @@
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="fetchKeyButton">
<property name="text">
<string>Fetch Official Launcher's Key</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label_8">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Note: you probably don't need to set this if CurseForge already works.&lt;/p&gt;&lt;p&gt;&lt;span style=&quot; font-weight:700;&quot;&gt;Using the Official Curseforge Launcher's key may break Curseforge's Terms of service, but should allow PollyMC to download all mods in a modpack without you needing to download any of them manually.&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit 689a653

Please sign in to comment.