2023-08-05 22:14:05 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2021-11-01 09:54:58 -04:00
|
|
|
#include "squarelauncher.h"
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
#include <KLocalizedString>
|
2022-08-15 11:14:37 -04:00
|
|
|
#include <QDesktopServices>
|
2021-11-01 09:54:58 -04:00
|
|
|
#include <QFile>
|
2022-08-15 11:14:37 -04:00
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QRegularExpressionMatch>
|
|
|
|
#include <QUrlQuery>
|
2021-11-01 09:54:58 -04:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
#include "account.h"
|
2021-11-23 15:34:23 -05:00
|
|
|
#include "launchercore.h"
|
2022-01-27 09:25:23 -05:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
SquareLauncher::SquareLauncher(LauncherCore &window, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, window(window)
|
|
|
|
{
|
|
|
|
}
|
2021-11-01 09:54:58 -04:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
QString getFileHash(const QString &file)
|
|
|
|
{
|
2021-11-01 09:54:58 -04:00
|
|
|
auto f = QFile(file);
|
|
|
|
if (!f.open(QIODevice::ReadOnly))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha1);
|
|
|
|
hash.addData(&f);
|
|
|
|
|
|
|
|
return QString("%1/%2").arg(QString::number(f.size()), hash.result().toHex());
|
|
|
|
}
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
void SquareLauncher::getStored(const LoginInformation &info)
|
|
|
|
{
|
|
|
|
Q_EMIT window.stageChanged(i18n("Logging in..."));
|
|
|
|
|
2021-11-01 09:54:58 -04:00
|
|
|
QUrlQuery query;
|
2022-03-16 14:33:04 -04:00
|
|
|
// en is always used to the top url
|
2021-11-01 09:54:58 -04:00
|
|
|
query.addQueryItem("lng", "en");
|
2022-03-16 14:33:04 -04:00
|
|
|
// for some reason, we always use region 3. the actual region is acquired later
|
2021-11-01 09:54:58 -04:00
|
|
|
query.addQueryItem("rgn", "3");
|
2023-07-30 08:49:34 -04:00
|
|
|
query.addQueryItem("isft", info.profile->account()->isFreeTrial() ? "1" : "0");
|
2021-11-01 09:54:58 -04:00
|
|
|
query.addQueryItem("cssmode", "1");
|
|
|
|
query.addQueryItem("isnew", "1");
|
2022-03-16 14:33:04 -04:00
|
|
|
query.addQueryItem("launchver", "3");
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
if (info.profile->account()->license() == Account::GameLicense::WindowsSteam) {
|
2022-03-16 14:33:04 -04:00
|
|
|
query.addQueryItem("issteam", "1");
|
|
|
|
|
|
|
|
// TODO: get steam ticket information from steam api
|
|
|
|
query.addQueryItem("session_ticket", "1");
|
|
|
|
query.addQueryItem("ticket_size", "1");
|
|
|
|
}
|
2021-11-01 09:54:58 -04:00
|
|
|
|
2023-08-18 21:36:29 -04:00
|
|
|
QUrl url;
|
|
|
|
url.setScheme("https");
|
|
|
|
url.setHost(QStringLiteral("ffxiv-login.%1").arg(window.squareEnixLoginServer()));
|
|
|
|
url.setPath("/oauth/ffxivarr/login/top");
|
2021-11-01 09:54:58 -04:00
|
|
|
url.setQuery(query);
|
|
|
|
|
|
|
|
auto request = QNetworkRequest(url);
|
2023-07-30 08:49:34 -04:00
|
|
|
window.buildRequest(*info.profile, request);
|
2021-11-01 09:54:58 -04:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
QNetworkReply *reply = window.mgr->get(request);
|
2021-11-01 09:54:58 -04:00
|
|
|
|
2023-07-30 10:11:14 -04:00
|
|
|
connect(reply, &QNetworkReply::finished, [this, &info, reply, url] {
|
2021-11-01 09:54:58 -04:00
|
|
|
auto str = QString(reply->readAll());
|
|
|
|
|
2022-03-16 14:33:04 -04:00
|
|
|
// fetches Steam username
|
2023-07-30 08:49:34 -04:00
|
|
|
if (info.profile->account()->license() == Account::GameLicense::WindowsSteam) {
|
2022-03-16 14:33:04 -04:00
|
|
|
QRegularExpression re(R"lit(<input name=""sqexid"" type=""hidden"" value=""(?<sqexid>.*)""\/>)lit");
|
|
|
|
QRegularExpressionMatch match = re.match(str);
|
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
if (match.hasMatch()) {
|
2022-03-16 14:33:04 -04:00
|
|
|
username = match.captured(1);
|
|
|
|
} else {
|
2023-07-30 08:49:34 -04:00
|
|
|
Q_EMIT window.loginError(i18n("Could not get Steam username, have you attached your account?"));
|
2022-03-16 14:33:04 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
username = info.username;
|
|
|
|
}
|
|
|
|
|
2021-11-01 09:54:58 -04:00
|
|
|
QRegularExpression re(R"lit(\t<\s*input .* name="_STORED_" value="(?<stored>.*)">)lit");
|
|
|
|
QRegularExpressionMatch match = re.match(str);
|
|
|
|
if (match.hasMatch()) {
|
|
|
|
stored = match.captured(1);
|
|
|
|
login(info, url);
|
|
|
|
} else {
|
2023-07-30 08:49:34 -04:00
|
|
|
Q_EMIT window.loginError(
|
|
|
|
i18n("Square Enix servers refused to confirm session information. The game may be under maintenance, try the official launcher."));
|
2021-11-01 09:54:58 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
void SquareLauncher::login(const LoginInformation &info, const QUrl &referer)
|
|
|
|
{
|
2021-11-01 09:54:58 -04:00
|
|
|
QUrlQuery postData;
|
|
|
|
postData.addQueryItem("_STORED_", stored);
|
|
|
|
postData.addQueryItem("sqexid", info.username);
|
|
|
|
postData.addQueryItem("password", info.password);
|
|
|
|
postData.addQueryItem("otppw", info.oneTimePassword);
|
|
|
|
|
2023-08-18 21:36:29 -04:00
|
|
|
QUrl url;
|
|
|
|
url.setScheme("https");
|
|
|
|
url.setHost(QStringLiteral("ffxiv-login.%1").arg(window.squareEnixLoginServer()));
|
|
|
|
url.setPath("/oauth/ffxivarr/login/login.send");
|
|
|
|
|
|
|
|
QNetworkRequest request(url);
|
2023-07-30 08:49:34 -04:00
|
|
|
window.buildRequest(*info.profile, request);
|
2022-08-15 11:14:37 -04:00
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
2021-11-01 09:54:58 -04:00
|
|
|
request.setRawHeader("Referer", referer.toEncoded());
|
|
|
|
request.setRawHeader("Cache-Control", "no-cache");
|
|
|
|
|
|
|
|
auto reply = window.mgr->post(request, postData.toString(QUrl::FullyEncoded).toUtf8());
|
2023-07-30 10:11:14 -04:00
|
|
|
connect(reply, &QNetworkReply::finished, [this, &info, reply] {
|
2021-11-01 09:54:58 -04:00
|
|
|
auto str = QString(reply->readAll());
|
|
|
|
|
|
|
|
QRegularExpression re(R"lit(window.external.user\("login=auth,ok,(?<launchParams>.*)\);)lit");
|
|
|
|
QRegularExpressionMatch match = re.match(str);
|
2022-08-15 11:14:37 -04:00
|
|
|
if (match.hasMatch()) {
|
2021-11-01 09:54:58 -04:00
|
|
|
const auto parts = match.captured(1).split(',');
|
|
|
|
|
|
|
|
const bool terms = parts[3] == "1";
|
|
|
|
const bool playable = parts[9] == "1";
|
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
if (!playable) {
|
2023-07-30 08:49:34 -04:00
|
|
|
Q_EMIT window.loginError(i18n("Your account is unplayable. Check that you have the correct license, and a valid subscription."));
|
2022-03-16 14:33:04 -04:00
|
|
|
return;
|
2021-11-01 09:54:58 -04:00
|
|
|
}
|
2022-03-16 14:33:04 -04:00
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
if (!terms) {
|
2023-07-30 08:49:34 -04:00
|
|
|
Q_EMIT window.loginError(i18n("Your account is unplayable. You need to accept the terms of service from the official launcher first."));
|
2022-03-16 14:33:04 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SID = parts[1];
|
|
|
|
auth.region = parts[5].toInt();
|
|
|
|
auth.maxExpansion = parts[13].toInt();
|
|
|
|
|
|
|
|
registerSession(info);
|
2021-11-01 09:54:58 -04:00
|
|
|
} else {
|
2022-03-16 14:51:16 -04:00
|
|
|
QRegularExpression re(R"lit(window.external.user\("login=auth,ng,err,(?<launchParams>.*)\);)lit");
|
|
|
|
QRegularExpressionMatch match = re.match(str);
|
|
|
|
|
|
|
|
const auto parts = match.captured(1).split(',');
|
|
|
|
|
|
|
|
// there's a stray quote at the end of the error string, so let's remove that
|
|
|
|
QString errorStr = match.captured(1).chopped(1);
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
Q_EMIT window.loginError(errorStr);
|
2021-11-01 09:54:58 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
void SquareLauncher::registerSession(const LoginInformation &info)
|
|
|
|
{
|
2021-11-01 09:54:58 -04:00
|
|
|
QUrl url;
|
|
|
|
url.setScheme("https");
|
2023-08-18 21:36:29 -04:00
|
|
|
url.setHost(QStringLiteral("patch-gamever.%1").arg(window.squareEnixServer()));
|
2023-07-30 08:49:34 -04:00
|
|
|
url.setPath(QString("/http/win32/ffxivneo_release_game/%1/%2").arg(info.profile->repositories.repositories[0].version, SID));
|
2021-11-01 09:54:58 -04:00
|
|
|
|
|
|
|
auto request = QNetworkRequest(url);
|
|
|
|
window.setSSL(request);
|
|
|
|
request.setRawHeader("X-Hash-Check", "enabled");
|
|
|
|
request.setRawHeader("User-Agent", "FFXIV PATCH CLIENT");
|
2022-08-15 11:14:37 -04:00
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
2021-11-01 09:54:58 -04:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
QString report = QString("%1=%2").arg(info.profile->bootVersion, getBootHash(info));
|
2021-11-01 09:54:58 -04:00
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
for (int i = 1; i < auth.maxExpansion + 1; i++) {
|
2023-07-30 08:49:34 -04:00
|
|
|
if (i <= static_cast<int>(info.profile->repositories.repositories_count)) {
|
|
|
|
report += QString("\nex%1\t%2").arg(QString::number(i), info.profile->repositories.repositories[i].version);
|
2022-07-20 17:58:21 -04:00
|
|
|
} else {
|
|
|
|
report += QString("\nex%1\t2012.01.01.0000.0000").arg(QString::number(i));
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 09:54:58 -04:00
|
|
|
|
|
|
|
auto reply = window.mgr->post(request, report.toUtf8());
|
2023-07-30 10:11:14 -04:00
|
|
|
connect(reply, &QNetworkReply::finished, [this, &info, reply] {
|
2023-04-25 10:31:38 -04:00
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
|
|
|
if (reply->rawHeaderList().contains("X-Patch-Unique-Id")) {
|
|
|
|
QString body = reply->readAll();
|
2022-07-20 18:00:42 -04:00
|
|
|
|
2023-07-30 14:56:24 -04:00
|
|
|
patcher = new Patcher(window, info.profile->gamePath() + "/game", info.profile->gameData, this);
|
2023-07-30 10:11:14 -04:00
|
|
|
connect(patcher, &Patcher::done, [this, &info, reply] {
|
2023-07-30 08:49:34 -04:00
|
|
|
info.profile->readGameVersion();
|
2022-07-20 18:00:42 -04:00
|
|
|
|
2023-04-25 10:31:38 -04:00
|
|
|
auth.SID = reply->rawHeader("X-Patch-Unique-Id");
|
2021-11-01 09:54:58 -04:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
window.launchGame(*info.profile, auth);
|
2023-04-25 10:31:38 -04:00
|
|
|
});
|
2022-07-20 18:00:42 -04:00
|
|
|
|
2023-04-25 10:31:38 -04:00
|
|
|
patcher->processPatchList(*window.mgr, body);
|
|
|
|
} else {
|
2023-07-30 08:49:34 -04:00
|
|
|
Q_EMIT window.loginError(i18n("Fatal error, request was successful but X-Patch-Unique-Id was not recieved."));
|
2023-04-25 10:31:38 -04:00
|
|
|
}
|
2021-11-01 09:54:58 -04:00
|
|
|
} else {
|
2023-04-25 10:31:38 -04:00
|
|
|
if (reply->error() == QNetworkReply::SslHandshakeFailedError) {
|
2023-07-30 08:49:34 -04:00
|
|
|
Q_EMIT window.loginError(
|
|
|
|
i18n("SSL handshake error detected. If you are using OpenSUSE or Fedora, try running `update-crypto-policies --set LEGACY`."));
|
2023-04-25 10:31:38 -04:00
|
|
|
} else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 405) {
|
2023-07-30 08:49:34 -04:00
|
|
|
Q_EMIT window.loginError(i18n("The game failed the anti-tamper check. Restore the game to the original state and try updating again."));
|
2023-04-25 10:31:38 -04:00
|
|
|
} else {
|
2023-07-30 08:49:34 -04:00
|
|
|
Q_EMIT window.loginError(i18n("Unknown error when registering the session."));
|
2023-04-25 10:31:38 -04:00
|
|
|
}
|
2021-11-01 09:54:58 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
QString SquareLauncher::getBootHash(const LoginInformation &info)
|
|
|
|
{
|
|
|
|
const QList<QString> fileList = {"ffxivboot.exe", "ffxivboot64.exe", "ffxivlauncher.exe", "ffxivlauncher64.exe", "ffxivupdater.exe", "ffxivupdater64.exe"};
|
2021-11-01 09:54:58 -04:00
|
|
|
|
|
|
|
QString result;
|
|
|
|
for (int i = 0; i < fileList.count(); i++) {
|
2023-07-30 08:49:34 -04:00
|
|
|
result += fileList[i] + "/" + getFileHash(info.profile->gamePath() + "/boot/" + fileList[i]);
|
2021-11-01 09:54:58 -04:00
|
|
|
|
|
|
|
if (i != fileList.length() - 1)
|
|
|
|
result += ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2022-05-09 15:53:17 -04:00
|
|
|
}
|