mirror of
https://github.com/UltimMC/Launcher.git
synced 2026-01-04 14:28:09 +00:00
Authlib injector, local auth server and ely by accounts support (#31)
* Add injector * Add uuid generation for profile * Add auth server emulator * Start auth server on random port and bypass it to injector * Run injector only when account type is dummy * Clean authlib injector * Add ely by authentication * Remove old comments * Add response status text to auth server * Fix json value access as done by @maximmasterr
This commit is contained in:
161
api/logic/minecraft/launch/InjectAuthlib.cpp
Normal file
161
api/logic/minecraft/launch/InjectAuthlib.cpp
Normal file
@@ -0,0 +1,161 @@
|
||||
/* Copyright 2013-2021 MultiMC Contributors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "InjectAuthlib.h"
|
||||
#include <launch/LaunchTask.h>
|
||||
#include <minecraft/MinecraftInstance.h>
|
||||
#include <FileSystem.h>
|
||||
#include <Env.h>
|
||||
#include <Json.h>
|
||||
|
||||
InjectAuthlib::InjectAuthlib(LaunchTask *parent, AuthlibInjectorPtr* injector) : LaunchStep(parent)
|
||||
{
|
||||
m_injector = injector;
|
||||
}
|
||||
|
||||
void InjectAuthlib::executeTask()
|
||||
{
|
||||
|
||||
if (m_aborted)
|
||||
{
|
||||
emitFailed(tr("Task aborted."));
|
||||
return;
|
||||
}
|
||||
|
||||
auto latestVersionInfo = QString("https://authlib-injector.yushi.moe/artifact/latest.json");
|
||||
auto netJob = new NetJob("Injector versions info download");
|
||||
MetaEntryPtr entry = ENV.metacache()->resolveEntry("injectors", "version.json");
|
||||
entry->setStale(true);
|
||||
auto task = Net::Download::makeCached(QUrl(latestVersionInfo), entry);
|
||||
netJob->addNetAction(task);
|
||||
|
||||
jobPtr.reset(netJob);
|
||||
QObject::connect(netJob, &NetJob::succeeded, this, &InjectAuthlib::onVersionDownloadSucceeded);
|
||||
QObject::connect(netJob, &NetJob::failed, this, &InjectAuthlib::onDownloadFailed);
|
||||
jobPtr->start();
|
||||
}
|
||||
|
||||
void InjectAuthlib::onVersionDownloadSucceeded()
|
||||
{
|
||||
|
||||
QByteArray data;
|
||||
try
|
||||
{
|
||||
data = FS::read(QDir("injectors").absoluteFilePath("version.json"));
|
||||
}
|
||||
catch (const Exception &e)
|
||||
{
|
||||
qCritical() << "Translations Download Failed: index file not readable";
|
||||
jobPtr.reset();
|
||||
emitFailed("Error while parsing JSON response from InjectorEndpoint");
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonParseError parse_error;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(data, &parse_error);
|
||||
if (parse_error.error != QJsonParseError::NoError)
|
||||
{
|
||||
qCritical() << "Error while parsing JSON response from InjectorEndpoint at " << parse_error.offset << " reason: " << parse_error.errorString();
|
||||
qCritical() << data;
|
||||
jobPtr.reset();
|
||||
emitFailed("Error while parsing JSON response from InjectorEndpoint");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!doc.isObject())
|
||||
{
|
||||
qCritical() << "Error while parsing JSON response from InjectorEndpoint root is not object";
|
||||
qCritical() << data;
|
||||
jobPtr.reset();
|
||||
emitFailed("Error while parsing JSON response from InjectorEndpoint");
|
||||
return;
|
||||
}
|
||||
|
||||
QString downloadUrl;
|
||||
try
|
||||
{
|
||||
downloadUrl = Json::requireString(doc.object(), "download_url");
|
||||
}
|
||||
catch (const JSONValidationError &e)
|
||||
{
|
||||
qCritical() << "Error while parsing JSON response from InjectorEndpoint download url is not string";
|
||||
qCritical() << e.cause();
|
||||
qCritical() << data;
|
||||
jobPtr.reset();
|
||||
emitFailed("Error while parsing JSON response from InjectorEndpoint");
|
||||
return;
|
||||
}
|
||||
|
||||
QFileInfo fi(downloadUrl);
|
||||
m_versionName = fi.fileName();
|
||||
|
||||
qDebug() << "Authlib injector version:" << m_versionName;
|
||||
|
||||
auto netJob = new NetJob("Injector download");
|
||||
MetaEntryPtr entry = ENV.metacache()->resolveEntry("injectors", m_versionName);
|
||||
entry->setStale(true);
|
||||
auto task = Net::Download::makeCached(QUrl(downloadUrl), entry);
|
||||
netJob->addNetAction(task);
|
||||
|
||||
jobPtr.reset(netJob);
|
||||
QObject::connect(netJob, &NetJob::succeeded, this, &InjectAuthlib::onDownloadSucceeded);
|
||||
QObject::connect(netJob, &NetJob::failed, this, &InjectAuthlib::onDownloadFailed);
|
||||
jobPtr->start();
|
||||
}
|
||||
|
||||
void InjectAuthlib::onDownloadSucceeded()
|
||||
{
|
||||
QString injector = QString("-javaagent:%1=%2").arg(QDir("injectors").absoluteFilePath(m_versionName)).arg(m_authServer);
|
||||
|
||||
qDebug()
|
||||
<< "Injecting " << injector;
|
||||
auto inj = new AuthlibInjector(injector);
|
||||
m_injector->reset(inj);
|
||||
|
||||
jobPtr.reset();
|
||||
emitSucceeded();
|
||||
}
|
||||
|
||||
void InjectAuthlib::onDownloadFailed(QString reason)
|
||||
{
|
||||
jobPtr.reset();
|
||||
emitFailed(reason);
|
||||
}
|
||||
|
||||
void InjectAuthlib::proceed()
|
||||
{
|
||||
}
|
||||
|
||||
bool InjectAuthlib::canAbort() const
|
||||
{
|
||||
if (jobPtr)
|
||||
{
|
||||
return jobPtr->canAbort();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InjectAuthlib::abort()
|
||||
{
|
||||
m_aborted = true;
|
||||
if (jobPtr)
|
||||
{
|
||||
if (jobPtr->canAbort())
|
||||
{
|
||||
return jobPtr->abort();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
70
api/logic/minecraft/launch/InjectAuthlib.h
Normal file
70
api/logic/minecraft/launch/InjectAuthlib.h
Normal file
@@ -0,0 +1,70 @@
|
||||
/* Copyright 2013-2021 MultiMC Contributors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <launch/LaunchStep.h>
|
||||
#include <QObjectPtr.h>
|
||||
#include <LoggedProcess.h>
|
||||
#include <java/JavaChecker.h>
|
||||
#include <net/Mode.h>
|
||||
#include <net/NetJob.h>
|
||||
|
||||
struct AuthlibInjector
|
||||
{
|
||||
QString javaArg;
|
||||
|
||||
AuthlibInjector(const QString arg)
|
||||
{
|
||||
javaArg = std::move(arg);
|
||||
qDebug() << "NEW INJECTOR" << javaArg;
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<AuthlibInjector> AuthlibInjectorPtr;
|
||||
|
||||
// FIXME: stupid. should be defined by the instance type? or even completely abstracted away...
|
||||
class InjectAuthlib : public LaunchStep
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
InjectAuthlib(LaunchTask *parent, AuthlibInjectorPtr *injector);
|
||||
virtual ~InjectAuthlib(){};
|
||||
|
||||
void executeTask() override;
|
||||
bool canAbort() const override;
|
||||
void proceed() override;
|
||||
|
||||
void setAuthServer(QString server)
|
||||
{
|
||||
m_authServer = server;
|
||||
};
|
||||
|
||||
public slots:
|
||||
bool abort() override;
|
||||
|
||||
private slots:
|
||||
void onVersionDownloadSucceeded();
|
||||
void onDownloadSucceeded();
|
||||
void onDownloadFailed(QString reason);
|
||||
|
||||
private:
|
||||
shared_qobject_ptr<Task> jobPtr;
|
||||
bool m_aborted = false;
|
||||
|
||||
QString m_versionName;
|
||||
QString m_authServer;
|
||||
AuthlibInjectorPtr *m_injector;
|
||||
};
|
||||
Reference in New Issue
Block a user