Sandboxie/SandboxiePlus/SandMan/OnlineUpdater.cpp

482 lines
16 KiB
C++
Raw Normal View History

2022-08-09 17:19:46 +01:00
#include "stdafx.h"
#include "OnlineUpdater.h"
#include "../MiscHelpers/Common/Common.h"
#include "SandMan.h"
#include "Windows/SettingsWindow.h"
#include <QUrlQuery>
#include <QJsonDocument>
#include "../MiscHelpers/Common/CheckableMessageBox.h"
#include <QMessageBox>
2022-06-08 16:23:19 +01:00
2022-08-09 17:19:46 +01:00
#include <windows.h>
COnlineUpdater* g_pUpdater = NULL;
COnlineUpdater::COnlineUpdater(QObject *parent) : QObject(parent)
{
m_RequestManager = NULL;
}
COnlineUpdater* COnlineUpdater::Instance()
{
if (!g_pUpdater)
g_pUpdater = new COnlineUpdater(theGUI);
return g_pUpdater;
}
void COnlineUpdater::Process()
{
if (g_pUpdater && !g_pUpdater->m_pUpdateProgress.isNull() && g_pUpdater->m_RequestManager != NULL) {
if (g_pUpdater->m_pUpdateProgress->IsCanceled()) {
g_pUpdater->m_pUpdateProgress->Finish(SB_OK);
g_pUpdater->m_pUpdateProgress.clear();
g_pUpdater->m_RequestManager->AbortAll();
}
}
}
2022-08-11 23:09:35 +01:00
void COnlineUpdater::GetUpdates(QObject* receiver, const char* member, const QVariantMap& Params)
2022-06-08 16:23:19 +01:00
{
2022-08-11 23:09:35 +01:00
CGetUpdatesJob* pJob = new CGetUpdatesJob(Params, this);
QObject::connect(pJob, SIGNAL(UpdateData(const QVariantMap&, const QVariantMap&)), receiver, member, Qt::QueuedConnection);
2022-06-08 16:23:19 +01:00
if (m_RequestManager == NULL)
m_RequestManager = new CNetworkAccessManager(30 * 1000, this);
QUrlQuery Query;
Query.addQueryItem("software", "sandboxie-plus");
//QString Branche = theConf->GetString("Options/ReleaseBranche");
//if (!Branche.isEmpty())
// Query.addQueryItem("branche", Branche);
//Query.addQueryItem("version", GetVersion());
Query.addQueryItem("version", QString::number(VERSION_MJR) + "." + QString::number(VERSION_MIN) + "." + QString::number(VERSION_REV) + "." + QString::number(VERSION_UPD));
Query.addQueryItem("system", "windows-" + QSysInfo::kernelVersion() + "-" + QSysInfo::currentCpuArchitecture());
2022-08-09 17:19:46 +01:00
Query.addQueryItem("language", QString::number(theGUI->m_LanguageId));
2022-06-08 16:23:19 +01:00
QString UpdateKey = GetArguments(g_Certificate, L'\n', L':').value("UPDATEKEY");
if (UpdateKey.isEmpty())
UpdateKey = theAPI->GetGlobalSettings()->GetText("UpdateKey"); // theConf->GetString("Options/UpdateKey");
if (!UpdateKey.isEmpty())
Query.addQueryItem("update_key", UpdateKey);
2022-08-11 23:09:35 +01:00
if (Params.contains("channel"))
Query.addQueryItem("channel", Params["channel"].toString());
else {
int UpdateChannel = theConf->GetInt("Options/UpdateChannel", 0);
Query.addQueryItem("channel", QString::number(UpdateChannel));
}
if(Params.contains("manual")) Query.addQueryItem("auto", Params["manual"].toBool() ? "0" : "1");
//QString Test = Query.toString();
2022-06-08 16:23:19 +01:00
QUrl Url("https://sandboxie-plus.com/update.php");
Url.setQuery(Query);
QNetworkRequest Request = QNetworkRequest(Url);
2022-08-09 17:19:46 +01:00
//Request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
Request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
2022-06-08 16:23:19 +01:00
//Request.setRawHeader("Accept-Encoding", "gzip");
QNetworkReply* pReply = m_RequestManager->get(Request);
2022-08-11 23:09:35 +01:00
//pReply->setProperty("manual", bManual);
2022-06-08 16:23:19 +01:00
connect(pReply, SIGNAL(finished()), this, SLOT(OnUpdateCheck()));
2022-08-11 23:09:35 +01:00
m_JobQueue.insert(pReply, pJob);
2022-06-08 16:23:19 +01:00
}
2022-08-22 20:06:43 +01:00
void COnlineUpdater::CheckForUpdates(bool bManual, bool bDownload)
2022-06-08 16:23:19 +01:00
{
2022-08-11 23:09:35 +01:00
if (!m_pUpdateProgress.isNull())
2022-06-08 16:23:19 +01:00
return;
2022-08-11 23:09:35 +01:00
m_pUpdateProgress = CSbieProgressPtr(new CSbieProgress());
theGUI->AddAsyncOp(m_pUpdateProgress);
m_pUpdateProgress->ShowMessage(tr("Checking for updates..."));
QVariantMap Params;
Params["manual"] = bManual;
2022-08-22 20:06:43 +01:00
Params["download"] = bDownload;
2022-08-11 23:09:35 +01:00
GetUpdates(this, SLOT(OnUpdateData(const QVariantMap&, const QVariantMap&)), Params);
}
void COnlineUpdater::OnUpdateCheck()
{
2022-06-08 16:23:19 +01:00
QNetworkReply* pReply = qobject_cast<QNetworkReply*>(sender());
2022-08-11 23:09:35 +01:00
//bool bManual = pReply->property("manual").toBool();
2022-06-08 16:23:19 +01:00
QByteArray Reply = pReply->readAll();
pReply->deleteLater();
2022-08-11 23:09:35 +01:00
CGetUpdatesJob* pJob = m_JobQueue.take(pReply);
if (!pJob)
return;
2022-06-08 16:23:19 +01:00
QVariantMap Data = QJsonDocument::fromJson(Reply).toVariant().toMap();
2022-08-11 23:09:35 +01:00
emit pJob->UpdateData(Data, pJob->m_Params);
pJob->deleteLater();
}
void COnlineUpdater::OnUpdateData(const QVariantMap& Data, const QVariantMap& Params)
{
bool bManual = Params["manual"].toBool();
2022-08-22 20:06:43 +01:00
bool bDownload = Params["download"].toBool();
2022-08-11 23:09:35 +01:00
if (!m_pUpdateProgress.isNull()) {
m_pUpdateProgress->Finish(SB_OK);
m_pUpdateProgress.clear();
}
2022-06-08 16:23:19 +01:00
if (Data.isEmpty() || Data["error"].toBool())
{
QString Error = Data.isEmpty() ? tr("server not reachable") : Data["errorMsg"].toString();
2022-08-09 17:19:46 +01:00
theGUI->OnLogMessage(tr("Failed to check for updates, error: %1").arg(Error), !bManual);
2022-06-08 16:23:19 +01:00
if (bManual)
2022-08-09 17:19:46 +01:00
QMessageBox::critical(theGUI, "Sandboxie-Plus", tr("Failed to check for updates, error: %1").arg(Error));
2022-06-08 16:23:19 +01:00
return;
}
bool bNothing = true;
QStringList IgnoredUpdates = theConf->GetStringList("Options/IgnoredUpdates");
QString UserMsg = Data["userMsg"].toString();
if (!UserMsg.isEmpty())
{
QString MsgHash = QCryptographicHash::hash(Data["userMsg"].toByteArray(), QCryptographicHash::Md5).toHex().left(8);
if (!IgnoredUpdates.contains(MsgHash))
{
QString FullMessage = UserMsg;
QString InfoUrl = Data["infoUrl"].toString();
if (!InfoUrl.isEmpty())
FullMessage += tr("<p>Do you want to go to the <a href=\"%1\">info page</a>?</p>").arg(InfoUrl);
2022-08-09 17:19:46 +01:00
CCheckableMessageBox mb(theGUI);
2022-06-08 16:23:19 +01:00
mb.setWindowTitle("Sandboxie-Plus");
QIcon ico(QLatin1String(":/SandMan.png"));
mb.setIconPixmap(ico.pixmap(64, 64));
//mb.setTextFormat(Qt::RichText);
mb.setText(UserMsg);
mb.setCheckBoxText(tr("Don't show this announcement in the future."));
if (!InfoUrl.isEmpty()) {
mb.setStandardButtons(QDialogButtonBox::Yes | QDialogButtonBox::No);
mb.setDefaultButton(QDialogButtonBox::Yes);
}
else
mb.setStandardButtons(QDialogButtonBox::Ok);
mb.exec();
if (mb.isChecked())
theConf->SetValue("Options/IgnoredUpdates", IgnoredUpdates << MsgHash);
if (mb.clickedStandardButton() == QDialogButtonBox::Yes)
{
QDesktopServices::openUrl(InfoUrl);
}
bNothing = false;
}
}
2022-08-09 17:19:46 +01:00
//int TemplateVersion = CSbieIni("DefaultTemplates", theAPI, this).GetNum("TemplateVersion", 0, false, true);
2022-06-08 16:23:19 +01:00
QString VersionStr = Data["version"].toString();
if (!VersionStr.isEmpty()) //&& VersionStr != GetVersion())
{
2022-08-22 20:06:43 +01:00
if (IsVersionNewer(VersionStr))
2022-06-08 16:23:19 +01:00
if (bManual || !IgnoredUpdates.contains(VersionStr)) // when checked manually always show result
{
bNothing = false;
2022-08-09 17:19:46 +01:00
//QDateTime Updated = QDateTime::fromSecsSinceEpoch(Data["updated"].toULongLong());
2022-06-08 16:23:19 +01:00
QString DownloadUrl = Data["downloadUrl"].toString();
// 'sha256'
// 'signature'
2022-08-22 20:06:43 +01:00
if (!DownloadUrl.isEmpty() && (bDownload || theConf->GetInt("Options/DownloadUpdates", 0) == 1))
{
theConf->SetValue("Options/PendingUpdateVersion", VersionStr);
2022-06-08 16:23:19 +01:00
DownloadUpdates(DownloadUrl, bManual);
2022-08-22 20:06:43 +01:00
}
2022-06-08 16:23:19 +01:00
else
{
QString UpdateMsg = Data["updateMsg"].toString();
QString UpdateUrl = Data["updateUrl"].toString();
2022-08-22 20:56:19 +01:00
QString FullMessage = UpdateMsg.isEmpty() ? tr("<p>There is a new version of Sandboxie-Plus available.<br /><font color='red'><b>New version:</b></font> <b>%1</b></p>").arg(VersionStr) : UpdateMsg;
2022-06-08 16:23:19 +01:00
if (!DownloadUrl.isEmpty())
FullMessage += tr("<p>Do you want to download the latest version?</p>");
else if (!UpdateUrl.isEmpty())
FullMessage += tr("<p>Do you want to go to the <a href=\"%1\">download page</a>?</p>").arg(UpdateUrl);
2022-08-09 17:19:46 +01:00
CCheckableMessageBox mb(theGUI);
2022-06-08 16:23:19 +01:00
mb.setWindowTitle("Sandboxie-Plus");
QIcon ico(QLatin1String(":/SandMan.png"));
mb.setIconPixmap(ico.pixmap(64, 64));
//mb.setTextFormat(Qt::RichText);
mb.setText(FullMessage);
mb.setCheckBoxText(tr("Don't show this message anymore."));
mb.setCheckBoxVisible(!bManual);
if (!UpdateUrl.isEmpty() || !DownloadUrl.isEmpty()) {
mb.setStandardButtons(QDialogButtonBox::Yes | QDialogButtonBox::No);
mb.setDefaultButton(QDialogButtonBox::Yes);
}
else
mb.setStandardButtons(QDialogButtonBox::Ok);
mb.exec();
2022-08-22 20:06:43 +01:00
if (mb.isChecked()) {
theConf->DelValue("Options/PendingUpdateVersion");
theGUI->UpdateLabel();
2022-06-08 16:23:19 +01:00
theConf->SetValue("Options/IgnoredUpdates", IgnoredUpdates << VersionStr);
2022-08-22 20:06:43 +01:00
}
2022-06-08 16:23:19 +01:00
if (mb.clickedStandardButton() == QDialogButtonBox::Yes)
{
2022-08-22 20:06:43 +01:00
if (!DownloadUrl.isEmpty())
{
theConf->SetValue("Options/PendingUpdateVersion", VersionStr);
2022-06-08 16:23:19 +01:00
DownloadUpdates(DownloadUrl, bManual);
2022-08-22 20:06:43 +01:00
}
2022-06-08 16:23:19 +01:00
else
QDesktopServices::openUrl(UpdateUrl);
}
2022-08-22 20:06:43 +01:00
else if (!mb.isChecked())
{
theConf->SetValue("Options/PendingUpdateVersion", VersionStr);
theGUI->UpdateLabel();
}
2022-06-08 16:23:19 +01:00
}
}
}
if (bNothing)
{
2022-08-22 20:06:43 +01:00
theConf->DelValue("Options/PendingUpdateVersion");
theGUI->UpdateLabel();
2022-08-09 17:19:46 +01:00
theConf->SetValue("Options/NextCheckForUpdates", QDateTime::currentDateTime().addDays(7).toSecsSinceEpoch());
2022-06-08 16:23:19 +01:00
if (bManual) {
2022-08-09 17:19:46 +01:00
QMessageBox::information(theGUI, "Sandboxie-Plus", tr("No new updates found, your Sandboxie-Plus is up-to-date.\n"
2022-06-08 16:23:19 +01:00
"\nNote: The update check is often behind the latest GitHub release to ensure that only tested updates are offered."));
}
}
}
2022-08-09 17:19:46 +01:00
void COnlineUpdater::DownloadUpdates(const QString& DownloadUrl, bool bManual)
2022-06-08 16:23:19 +01:00
{
QNetworkRequest Request = QNetworkRequest(DownloadUrl);
2022-08-09 17:19:46 +01:00
//Request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
Request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
2022-06-08 16:23:19 +01:00
//Request.setRawHeader("Accept-Encoding", "gzip");
QNetworkReply* pReply = m_RequestManager->get(Request);
pReply->setProperty("manual", bManual);
connect(pReply, SIGNAL(finished()), this, SLOT(OnUpdateDownload()));
connect(pReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(OnUpdateProgress(qint64, qint64)));
m_pUpdateProgress = CSbieProgressPtr(new CSbieProgress());
2022-08-09 17:19:46 +01:00
theGUI->AddAsyncOp(m_pUpdateProgress);
2022-06-08 16:23:19 +01:00
m_pUpdateProgress->ShowMessage(tr("Downloading new version..."));
}
2022-08-09 17:19:46 +01:00
void COnlineUpdater::OnUpdateProgress(qint64 bytes, qint64 bytesTotal)
2022-06-08 16:23:19 +01:00
{
if (bytesTotal != 0 && !m_pUpdateProgress.isNull())
m_pUpdateProgress->Progress(100 * bytes / bytesTotal);
}
2022-08-09 17:19:46 +01:00
void COnlineUpdater::OnUpdateDownload()
2022-06-08 16:23:19 +01:00
{
if (m_pUpdateProgress.isNull())
return;
QString TempDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
if (TempDir.right(1) != "/")
TempDir += "/";
m_pUpdateProgress->Progress(-1);
QNetworkReply* pReply = qobject_cast<QNetworkReply*>(sender());
bool bManual = pReply->property("manual").toBool();
quint64 Size = pReply->bytesAvailable();
QString Name = pReply->request().url().fileName();
if (Name.isEmpty() || Name.right(4).compare(".exe", Qt::CaseInsensitive) != 0)
Name = "Sandboxie-Plus-Install.exe";
QString FilePath = TempDir + Name;
QFile File(FilePath);
if (File.open(QFile::WriteOnly)) {
while (pReply->bytesAvailable() > 0)
File.write(pReply->read(4096));
File.close();
}
pReply->deleteLater();
m_pUpdateProgress->Finish(SB_OK);
m_pUpdateProgress.clear();
if (File.size() != Size) {
2022-08-09 17:19:46 +01:00
QMessageBox::critical(theGUI, "Sandboxie-Plus", tr("Failed to download update from: %1").arg(pReply->request().url().toString()));
2022-06-08 16:23:19 +01:00
return;
}
theConf->SetValue("Options/PendingUpdatePackage", FilePath);
2022-08-09 17:19:46 +01:00
theGUI->UpdateLabel();
2022-06-08 16:23:19 +01:00
if (bManual)
InstallUpdate();
}
2022-08-22 20:06:43 +01:00
bool COnlineUpdater::IsVersionNewer(const QString& VersionStr)
{
#ifdef _DEBUG
if (QApplication::keyboardModifiers() & Qt::ControlModifier)
return true;
#endif
quint8 myVersion[4] = { VERSION_UPD, VERSION_REV, VERSION_MIN, VERSION_MJR }; // ntohl
quint32 MyVersion = *(quint32*)&myVersion;
quint32 Version = 0;
QStringList Nums = VersionStr.split(".");
for (int i = 0, Bits = 24; i < Nums.count() && Bits >= 0; i++, Bits -= 8)
Version |= (Nums[i].toInt() & 0xFF) << Bits;
return (Version > MyVersion);
}
void COnlineUpdater::CheckPendingUpdate()
{
QString VersionStr = theConf->GetString("Options/PendingUpdateVersion");
2022-08-22 20:17:55 +01:00
bool bIsNewVersion = IsVersionNewer(VersionStr);
if (!bIsNewVersion)
2022-08-22 20:06:43 +01:00
theConf->DelValue("Options/PendingUpdateVersion");
2022-08-22 20:17:55 +01:00
QString FilePath = theConf->GetString("Options/PendingUpdatePackage");
if (!bIsNewVersion || !QFile::exists(FilePath))
2022-08-22 20:06:43 +01:00
theConf->DelValue("Options/PendingUpdatePackage");
}
void COnlineUpdater::DownloadUpdate()
{
CheckForUpdates(true, true);
}
2022-08-09 17:19:46 +01:00
void COnlineUpdater::InstallUpdate()
2022-06-08 16:23:19 +01:00
{
QString FilePath = theConf->GetString("Options/PendingUpdatePackage");
if (FilePath.isEmpty())
return;
QString Message = tr("<p>A Sandboxie-Plus update has been downloaded to the following location:</p><p><a href=\"%2\">%1</a></p><p>Do you want to begin the installation? If any programs are running sandboxed, they will be terminated.</p>")
.arg(FilePath).arg("File:///" + Split2(FilePath, "/", true).first);
2022-08-09 17:19:46 +01:00
int Ret = QMessageBox("Sandboxie-Plus", Message, QMessageBox::Information, QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape, QMessageBox::Cancel, theGUI).exec();
2022-06-08 16:23:19 +01:00
if (Ret == QMessageBox::Cancel) {
theConf->DelValue("Options/PendingUpdatePackage");
2022-08-09 17:19:46 +01:00
theGUI->UpdateLabel();
2022-06-08 16:23:19 +01:00
}
if (Ret != QMessageBox::Yes)
return;
theAPI->TerminateAll();
2022-08-09 17:19:46 +01:00
std::wstring wFile = FilePath.toStdWString();
2022-06-08 16:23:19 +01:00
SHELLEXECUTEINFO si = { 0 };
si.cbSize = sizeof(SHELLEXECUTEINFO);
si.fMask = SEE_MASK_NOCLOSEPROCESS;
si.hwnd = NULL;
si.lpVerb = L"runas";
si.lpFile = wFile.c_str();
si.lpParameters = L"/SILENT";
si.lpDirectory = NULL;
si.nShow = SW_SHOW;
si.hInstApp = NULL;
if (ShellExecuteEx(&si)) {
theConf->DelValue("Options/PendingUpdatePackage");
QApplication::quit();
}
}
2022-08-09 17:19:46 +01:00
void COnlineUpdater::UpdateCert()
2022-06-08 16:23:19 +01:00
{
QString UpdateKey; // for now only patreons can update the cert automatically
2022-08-09 17:19:46 +01:00
TArguments args = GetArguments(g_Certificate, L'\n', L':');
if(args.value("TYPE").indexOf("PATREON") == 0)
UpdateKey = args.value("UPDATEKEY");
2022-06-08 16:23:19 +01:00
if (UpdateKey.isEmpty()) {
2022-08-09 17:19:46 +01:00
theGUI->OpenUrl("https://sandboxie-plus.com/go.php?to=sbie-get-cert");
2022-06-08 16:23:19 +01:00
return;
}
if (!m_pUpdateProgress.isNull())
return;
m_pUpdateProgress = CSbieProgressPtr(new CSbieProgress());
2022-08-09 17:19:46 +01:00
theGUI->AddAsyncOp(m_pUpdateProgress);
2022-06-08 16:23:19 +01:00
m_pUpdateProgress->ShowMessage(tr("Checking for certificate..."));
if (m_RequestManager == NULL)
m_RequestManager = new CNetworkAccessManager(30 * 1000, this);
QUrlQuery Query;
Query.addQueryItem("UpdateKey", UpdateKey);
QUrl Url("https://sandboxie-plus.com/get_cert.php");
Url.setQuery(Query);
QNetworkRequest Request = QNetworkRequest(Url);
2022-08-09 17:19:46 +01:00
//Request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
Request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
2022-06-08 16:23:19 +01:00
//Request.setRawHeader("Accept-Encoding", "gzip");
QNetworkReply* pReply = m_RequestManager->get(Request);
connect(pReply, SIGNAL(finished()), this, SLOT(OnCertCheck()));
}
2022-08-09 17:19:46 +01:00
void COnlineUpdater::OnCertCheck()
2022-06-08 16:23:19 +01:00
{
if (m_pUpdateProgress.isNull())
return;
QNetworkReply* pReply = qobject_cast<QNetworkReply*>(sender());
QByteArray Reply = pReply->readAll();
int Code = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
pReply->deleteLater();
m_pUpdateProgress->Finish(SB_OK);
m_pUpdateProgress.clear();
if (Code > 299 || Code < 200) {
2022-08-09 17:19:46 +01:00
QMessageBox::critical(theGUI, "Sandboxie-Plus", tr("No certificate found on server!"));
2022-06-08 16:23:19 +01:00
return;
}
if (Reply.replace("\r\n","\n").compare(g_Certificate.replace("\r\n","\n"), Qt::CaseInsensitive) == 0){
2022-08-09 17:19:46 +01:00
QMessageBox::information(theGUI, "Sandboxie-Plus", tr("There is no updated certificate available."));
2022-06-08 16:23:19 +01:00
return;
}
QString CertPath = theAPI->GetSbiePath() + "\\Certificate.dat";
QString TempPath = QDir::tempPath() + "/Sbie+Certificate.dat";
QFile CertFile(TempPath);
if (CertFile.open(QFile::WriteOnly)) {
CertFile.write(Reply);
CertFile.close();
}
WindowsMoveFile(TempPath.replace("/", "\\"), CertPath.replace("/", "\\"));
if (!theAPI->ReloadCert().IsError()) {
CSettingsWindow::LoadCertificate();
2022-08-09 17:19:46 +01:00
theGUI->UpdateCertState();
2022-06-08 16:23:19 +01:00
}
else { // this should not happen
g_Certificate.clear();
g_CertInfo.State = 0;
}
2022-08-09 17:19:46 +01:00
}