mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-11-20 03:23:35 +09:00
483 lines
15 KiB
C++
483 lines
15 KiB
C++
#include "updateController.h"
|
|
|
|
#include <QCryptographicHash>
|
|
#include <QDesktopServices>
|
|
#include <QDir>
|
|
#include <QFileInfo>
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QNetworkReply>
|
|
#include <QTimer>
|
|
#include <QUrl>
|
|
#include <QStandardPaths>
|
|
|
|
#include "amneziaApplication.h"
|
|
#include "logger.h"
|
|
#include "core/controllers/gatewayController.h"
|
|
#include "core/utils/api/gatewayPayloadBuilder.h"
|
|
#include "core/utils/appUiConfig.h"
|
|
#include "core/utils/constants/apiConstants.h"
|
|
#include "core/utils/constants/apiKeys.h"
|
|
#include "core/utils/selfhosted/scriptsRegistry.h"
|
|
|
|
#if defined(Q_OS_ANDROID)
|
|
#include <QOperatingSystemVersion>
|
|
|
|
#include "platforms/android/android_controller.h"
|
|
#endif
|
|
|
|
namespace
|
|
{
|
|
Logger logger("UpdateController");
|
|
|
|
#if defined(Q_OS_WINDOWS)
|
|
QString installerRemoteFileName(const QString &version)
|
|
{
|
|
return QStringLiteral("AmneziaVPN_%1_windows_x64.exe").arg(version);
|
|
}
|
|
|
|
QString installerLocalPath()
|
|
{
|
|
return QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/AmneziaVPN_installer.exe";
|
|
}
|
|
#elif defined(Q_OS_MACOS) && !defined(MACOS_NE)
|
|
QString installerRemoteFileName(const QString &version)
|
|
{
|
|
return QStringLiteral("AmneziaVPN_%1_macos_x64.pkg").arg(version);
|
|
}
|
|
|
|
QString installerLocalPath()
|
|
{
|
|
return QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/AmneziaVPN.pkg";
|
|
}
|
|
#elif defined(Q_OS_ANDROID)
|
|
#if defined(Q_PROCESSOR_ARM_64)
|
|
const QLatin1String kAndroidAbi("arm64-v8a");
|
|
#elif defined(Q_PROCESSOR_ARM_32)
|
|
const QLatin1String kAndroidAbi("armeabi-v7a");
|
|
#elif defined(Q_PROCESSOR_X86_64)
|
|
const QLatin1String kAndroidAbi("x86_64");
|
|
#elif defined(Q_PROCESSOR_X86_32)
|
|
const QLatin1String kAndroidAbi("x86");
|
|
#endif
|
|
|
|
QString installerRemoteFileName(const QString &version)
|
|
{
|
|
const QLatin1String osLabel = QOperatingSystemVersion::current() >= QOperatingSystemVersion::Android11
|
|
? QLatin1String("android11+")
|
|
: QLatin1String("android9-10");
|
|
return QStringLiteral("AmneziaVPN_%1_%2_%3.apk").arg(version, osLabel, kAndroidAbi);
|
|
}
|
|
|
|
QString installerLocalPath()
|
|
{
|
|
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/update/AmneziaVPN.apk";
|
|
}
|
|
|
|
bool isSha256Hex(const QByteArray &hash)
|
|
{
|
|
return hash.size() == 64 && QByteArray::fromHex(hash).toHex() == hash;
|
|
}
|
|
#elif defined(Q_OS_LINUX)
|
|
QString installerRemoteFileName(const QString &version)
|
|
{
|
|
return QStringLiteral("AmneziaVPN_%1_linux_x64.run").arg(version);
|
|
}
|
|
|
|
QString installerLocalPath()
|
|
{
|
|
return QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/AmneziaVPN.run";
|
|
}
|
|
#endif
|
|
|
|
#if !defined(Q_OS_IOS) && !defined(MACOS_NE)
|
|
QNetworkReply *requestDownload(const QString &url)
|
|
{
|
|
QNetworkRequest request;
|
|
request.setTransferTimeout(30000);
|
|
request.setUrl(QUrl(url));
|
|
return amnApp->networkManager()->get(request);
|
|
}
|
|
#endif
|
|
|
|
QStringList stringListFromJsonValue(const QJsonValue &value)
|
|
{
|
|
QStringList result;
|
|
for (const QJsonValue &item : value.toArray()) {
|
|
const QString text = item.toString();
|
|
if (!text.isEmpty()) {
|
|
result.append(text);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
UpdateController::UpdateController(SecureAppSettingsRepository* appSettingsRepository, QObject *parent)
|
|
: QObject(parent), m_appSettingsRepository(appSettingsRepository)
|
|
{
|
|
}
|
|
|
|
QString UpdateController::getVersion() const
|
|
{
|
|
return m_version;
|
|
}
|
|
|
|
QString UpdateController::getReleaseDate() const
|
|
{
|
|
return m_releaseDate;
|
|
}
|
|
|
|
QString UpdateController::getDescription() const
|
|
{
|
|
return m_description;
|
|
}
|
|
|
|
QStringList UpdateController::getTags() const
|
|
{
|
|
return m_tags;
|
|
}
|
|
|
|
QStringList UpdateController::getNewFeatures() const
|
|
{
|
|
return m_newFeatures;
|
|
}
|
|
|
|
QStringList UpdateController::getImprovements() const
|
|
{
|
|
return m_improvements;
|
|
}
|
|
|
|
QStringList UpdateController::getBugFixes() const
|
|
{
|
|
return m_bugFixes;
|
|
}
|
|
|
|
UpdateState::State UpdateController::getUpdateState() const
|
|
{
|
|
return m_updateState;
|
|
}
|
|
|
|
bool UpdateController::isStoreUpdate() const
|
|
{
|
|
#if defined(Q_OS_IOS) || defined(MACOS_NE)
|
|
return true;
|
|
#elif defined(Q_OS_ANDROID)
|
|
return AndroidController::instance()->isPlay();
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
void UpdateController::setUpdateState(UpdateState::State state)
|
|
{
|
|
if (m_updateState == state) {
|
|
return;
|
|
}
|
|
m_updateState = state;
|
|
emit updateStateChanged();
|
|
}
|
|
|
|
bool UpdateController::isUpdateCheckRunning() const
|
|
{
|
|
return m_updateCheckRunning;
|
|
}
|
|
|
|
void UpdateController::setUpdateCheckRunning(bool running)
|
|
{
|
|
if (m_updateCheckRunning == running) {
|
|
return;
|
|
}
|
|
m_updateCheckRunning = running;
|
|
emit updateCheckRunningChanged();
|
|
}
|
|
|
|
void UpdateController::checkForUpdates()
|
|
{
|
|
#if !CLIENT_ENABLE_APP_UPDATES
|
|
return;
|
|
#endif
|
|
|
|
if (m_updateCheckRunning || !m_appSettingsRepository) {
|
|
return;
|
|
}
|
|
setUpdateCheckRunning(true);
|
|
|
|
auto gatewayController = QSharedPointer<GatewayController>::create(
|
|
m_appSettingsRepository->gatewayEnvironment(), apiDefs::requestTimeoutMsecs,
|
|
m_appSettingsRepository->isStrictKillSwitchEnabled(), m_appSettingsRepository);
|
|
|
|
QJsonObject apiPayload = GatewayPayloadBuilder(m_appSettingsRepository).build();
|
|
|
|
// Workaround: wait before contacting gateway to avoid rate limit triggered by other requests (news etc.)
|
|
QTimer::singleShot(1000, this, [this, gatewayController, apiPayload]() {
|
|
gatewayController->postAsync(QStringLiteral("%1v1/app_update"), apiPayload)
|
|
.then(this, [this, gatewayController](QPair<ErrorCode, QByteArray> result) {
|
|
setUpdateCheckRunning(false);
|
|
|
|
auto [err, gatewayResponse] = result;
|
|
if (err != ErrorCode::NoError) {
|
|
logger.error() << "Gateway request failed, error code:" << static_cast<int>(err);
|
|
emit updateCheckFailed();
|
|
return;
|
|
}
|
|
|
|
QJsonObject gatewayData = QJsonDocument::fromJson(gatewayResponse).object();
|
|
if (!gatewayData.value(apiDefs::key::updateAvailable).toBool()) {
|
|
logger.info() << "No application update available";
|
|
emit updateNotFound();
|
|
return;
|
|
}
|
|
|
|
m_version = gatewayData.value(apiDefs::key::updateVersion).toString();
|
|
m_releaseDate = gatewayData.value(apiDefs::key::releaseDate).toString();
|
|
m_description = gatewayData.value(apiDefs::key::updateDescription).toString();
|
|
m_tags = stringListFromJsonValue(gatewayData.value(apiDefs::key::updateTags));
|
|
m_newFeatures = stringListFromJsonValue(gatewayData.value(apiDefs::key::newFeatures));
|
|
m_improvements = stringListFromJsonValue(gatewayData.value(apiDefs::key::improvements));
|
|
m_bugFixes = stringListFromJsonValue(gatewayData.value(apiDefs::key::bugFixes));
|
|
m_downloadBaseUrl = gatewayData.value(apiDefs::key::downloadBaseUrl).toString();
|
|
if (m_downloadBaseUrl.endsWith('/')) {
|
|
m_downloadBaseUrl.chop(1);
|
|
}
|
|
#if defined(Q_OS_ANDROID)
|
|
m_installerSha256 = gatewayData.value(apiDefs::key::checksums)
|
|
.toObject()
|
|
.value(installerRemoteFileName(m_version))
|
|
.toString()
|
|
.toLower()
|
|
.toLatin1();
|
|
#endif
|
|
|
|
if (m_version.isEmpty()) {
|
|
logger.error() << "Update response has no version, ignoring";
|
|
emit updateCheckFailed();
|
|
return;
|
|
}
|
|
|
|
logger.info() << "Application update available:" << m_version;
|
|
setUpdateState(UpdateState::State::Idle);
|
|
emit updateFound();
|
|
});
|
|
});
|
|
}
|
|
|
|
void UpdateController::openStorePage() const
|
|
{
|
|
#if defined(Q_OS_IOS)
|
|
QDesktopServices::openUrl(QUrl(QLatin1String(APP_IOS_STORE_URL_FALLBACK)));
|
|
#elif defined(MACOS_NE)
|
|
QDesktopServices::openUrl(QUrl(QStringLiteral("https://apps.apple.com/app/id1600529900")));
|
|
#elif defined(Q_OS_ANDROID)
|
|
QDesktopServices::openUrl(
|
|
QUrl(QStringLiteral("https://play.google.com/store/apps/details?id=%1").arg(QLatin1String(APP_ANDROID_PACKAGE))));
|
|
#endif
|
|
}
|
|
|
|
void UpdateController::startUpdate()
|
|
{
|
|
if (isStoreUpdate()) {
|
|
openStorePage();
|
|
return;
|
|
}
|
|
|
|
#if !defined(Q_OS_IOS) && !defined(MACOS_NE)
|
|
downloadInstaller();
|
|
#endif
|
|
}
|
|
|
|
QString UpdateController::composeDownloadUrl() const
|
|
{
|
|
#if !defined(Q_OS_IOS) && !defined(MACOS_NE)
|
|
if (m_downloadBaseUrl.isEmpty() || m_version.isEmpty()) {
|
|
return QString();
|
|
}
|
|
return m_downloadBaseUrl + "/" + QString::fromLatin1(QUrl::toPercentEncoding(installerRemoteFileName(m_version)));
|
|
#else
|
|
return QString();
|
|
#endif
|
|
}
|
|
|
|
void UpdateController::downloadInstaller()
|
|
{
|
|
#if !defined(Q_OS_IOS) && !defined(MACOS_NE)
|
|
if (m_updateState == UpdateState::State::Downloading) {
|
|
return;
|
|
}
|
|
|
|
const QString downloadUrl = composeDownloadUrl();
|
|
if (downloadUrl.isEmpty()) {
|
|
logger.error() << "Download URL is empty";
|
|
setUpdateState(UpdateState::State::DownloadError);
|
|
return;
|
|
}
|
|
|
|
#if defined(Q_OS_ANDROID)
|
|
if (!isSha256Hex(m_installerSha256)) {
|
|
logger.error() << "Update response has no valid checksum for the installer";
|
|
setUpdateState(UpdateState::State::DownloadError);
|
|
return;
|
|
}
|
|
#endif
|
|
|
|
setUpdateState(UpdateState::State::Downloading);
|
|
downloadInstallerFile(downloadUrl, m_installerSha256);
|
|
#endif
|
|
}
|
|
|
|
void UpdateController::downloadInstallerFile(const QString &downloadUrl, const QByteArray &expectedSha256)
|
|
{
|
|
#if !defined(Q_OS_IOS) && !defined(MACOS_NE)
|
|
QNetworkReply *reply = requestDownload(downloadUrl);
|
|
|
|
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply, expectedSha256]() {
|
|
reply->deleteLater();
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
logger.error() << "Installer download failed, network error:" << static_cast<int>(reply->error())
|
|
<< reply->errorString();
|
|
logger.error() << "HTTP status:" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
setUpdateState(UpdateState::State::DownloadError);
|
|
return;
|
|
}
|
|
|
|
const QByteArray data = reply->readAll();
|
|
if (!expectedSha256.isEmpty() && QCryptographicHash::hash(data, QCryptographicHash::Sha256).toHex() != expectedSha256) {
|
|
logger.error() << "Installer checksum mismatch";
|
|
setUpdateState(UpdateState::State::DownloadError);
|
|
return;
|
|
}
|
|
|
|
const QString localPath = installerLocalPath();
|
|
QDir().mkpath(QFileInfo(localPath).absolutePath());
|
|
|
|
QFile file(localPath);
|
|
if (!file.open(QIODevice::WriteOnly)) {
|
|
logger.error() << "Failed to open installer file for writing:" << localPath
|
|
<< "Error:" << file.errorString();
|
|
setUpdateState(UpdateState::State::DownloadError);
|
|
return;
|
|
}
|
|
|
|
if (file.write(data) == -1) {
|
|
logger.error() << "Failed to write installer data to file:" << localPath
|
|
<< "Error:" << file.errorString();
|
|
file.close();
|
|
setUpdateState(UpdateState::State::DownloadError);
|
|
return;
|
|
}
|
|
|
|
file.close();
|
|
setUpdateState(UpdateState::State::ReadyToInstall);
|
|
});
|
|
#endif
|
|
}
|
|
|
|
void UpdateController::installUpdate()
|
|
{
|
|
#if !defined(Q_OS_IOS) && !defined(MACOS_NE)
|
|
if (m_updateState != UpdateState::State::ReadyToInstall) {
|
|
logger.error() << "Installer is not downloaded yet";
|
|
return;
|
|
}
|
|
|
|
#if defined(Q_OS_WINDOWS)
|
|
runWindowsInstaller(installerLocalPath());
|
|
#elif defined(Q_OS_MACOS) && !defined(MACOS_NE)
|
|
runMacInstaller(installerLocalPath());
|
|
#elif defined(Q_OS_ANDROID)
|
|
AndroidController::instance()->installApk(installerLocalPath());
|
|
#elif defined(Q_OS_LINUX)
|
|
runLinuxInstaller(installerLocalPath());
|
|
#endif
|
|
#endif
|
|
}
|
|
|
|
#if defined(Q_OS_WINDOWS)
|
|
int UpdateController::runWindowsInstaller(const QString &installerPath)
|
|
{
|
|
qint64 pid;
|
|
bool success = QProcess::startDetached(installerPath, QStringList(), QString(), &pid);
|
|
|
|
if (success) {
|
|
logger.info() << "Installation process started with PID:" << pid;
|
|
} else {
|
|
logger.error() << "Failed to start installation process";
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
#if defined(Q_OS_MACOS) && !defined(MACOS_NE)
|
|
int UpdateController::runMacInstaller(const QString &installerPath)
|
|
{
|
|
// Create temporary directory for extraction
|
|
QTemporaryDir extractDir;
|
|
extractDir.setAutoRemove(false);
|
|
if (!extractDir.isValid()) {
|
|
logger.error() << "Failed to create temporary directory";
|
|
return -1;
|
|
}
|
|
logger.info() << "Temporary directory created:" << extractDir.path();
|
|
|
|
// Create script file in the temporary directory
|
|
QString scriptPath = extractDir.path() + "/mac_installer.sh";
|
|
QFile scriptFile(scriptPath);
|
|
if (!scriptFile.open(QIODevice::WriteOnly)) {
|
|
logger.error() << "Failed to create script file";
|
|
return -1;
|
|
}
|
|
|
|
// Get script content from registry
|
|
QString scriptContent = amnezia::scriptData(amnezia::ClientScriptType::mac_installer);
|
|
if (scriptContent.isEmpty()) {
|
|
logger.error() << "macOS installer script content is empty";
|
|
scriptFile.close();
|
|
return -1;
|
|
}
|
|
|
|
scriptFile.write(scriptContent.toUtf8());
|
|
scriptFile.close();
|
|
logger.info() << "Script file created:" << scriptPath;
|
|
|
|
// Make script executable
|
|
QFile::setPermissions(scriptPath, QFile::permissions(scriptPath) | QFile::ExeUser);
|
|
|
|
// Start detached process
|
|
qint64 pid;
|
|
bool success =
|
|
QProcess::startDetached("/bin/bash", QStringList() << scriptPath << extractDir.path() << installerPath, extractDir.path(), &pid);
|
|
|
|
if (success) {
|
|
logger.info() << "Installation process started with PID:" << pid;
|
|
} else {
|
|
logger.error() << "Failed to start installation process";
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
|
|
int UpdateController::runLinuxInstaller(const QString &installerPath)
|
|
{
|
|
QFile::setPermissions(installerPath, QFile::permissions(installerPath) | QFile::ExeUser);
|
|
|
|
qint64 pid;
|
|
bool success = QProcess::startDetached(installerPath, QStringList(), QString(), &pid);
|
|
|
|
if (success) {
|
|
logger.info() << "Installation process started with PID:" << pid;
|
|
} else {
|
|
logger.error() << "Failed to start installation process";
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
#endif
|