Skip to content

Commit

Permalink
dont use static methods and pinn key in app
Browse files Browse the repository at this point in the history
this "locks" the app to the trle.net server
  • Loading branch information
noisecode3 committed Dec 21, 2024
1 parent dbe5a72 commit 884099a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 72 deletions.
8 changes: 5 additions & 3 deletions src/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ const QString FileManager::calculateMD5(const QString& file, bool lookGameDir) {
QCryptographicHash md5(QCryptographicHash::Md5);

std::array<char, 1024> buffer;
qint64 bytesRead;
const int size = buffer.size();
int bytesRead;

while ((bytesRead = f.read(buffer.data(), buffer.size())) > 0) {
md5.addData(buffer.data(), static_cast<int>(bytesRead));
// flawfinder: ignore
while ((bytesRead = f.read(buffer.data(), size)) > 0) {
md5.addData(buffer.data(), bytesRead);
}

f.close();
Expand Down
100 changes: 49 additions & 51 deletions src/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,62 +74,25 @@ std::string get_ssl_certificate(const std::string& host) {
return cert_buffer;
}

struct WriteData {
Downloader* downloader;
FILE* file;
};

bool Downloader::setUpCamp(const QString& levelDir) {
QFileInfo levelPathInfo(levelDir);
if (levelPathInfo.isDir()) {
levelDir_m.setPath(levelDir);
m_levelDir.setPath(levelDir);
return true;
}
return false;
}

size_t Downloader::write_callback(
void* contents,
size_t size,
size_t nmemb,
void* userData) {
WriteData* data = reinterpret_cast<WriteData*>(userData);
return fwrite(contents, size, nmemb, data->file);
}

int Downloader::progress_callback(
void* clientp,
curl_off_t dltotal,
curl_off_t dlnow,
curl_off_t ultotal,
curl_off_t ulnow) {
if (dltotal > 0) {
double progress = static_cast<double>(dlnow)
/ static_cast<double>(dltotal) * 50.0;

// Emit signal only if progress has increased by at least 1%
static int lastEmittedProgress = 0;
if (static_cast<int>(progress) == 0) lastEmittedProgress = 0;
if (static_cast<int>(progress) > lastEmittedProgress) {
static Downloader& instance = Downloader::getInstance();
emit instance.networkWorkTickSignal();
QCoreApplication::processEvents();
lastEmittedProgress = static_cast<int>(progress);
}
}
return 0;
}

void Downloader::setUrl(QUrl url) {
url_m = url;
m_url = url;
}

void Downloader::setSaveFile(const QString& file) {
file_m = file;
m_file = file;
}

int Downloader::getStatus() {
return status_m;
return m_status;
}

void Downloader::saveToFile(const QByteArray& data, const QString& filePath) {
Expand All @@ -152,15 +115,21 @@ void Downloader::saveToFile(const QByteArray& data, const QString& filePath) {
}

void Downloader::run() {
if (url_m.isEmpty() || file_m.isEmpty() || levelDir_m.isEmpty())
if (m_url.isEmpty() || m_file.isEmpty() || m_levelDir.isEmpty())
return;

QString urlString = url_m.toString();
qDebug() << "m_url: " << m_url.toString();
qDebug() << "m_file: " << m_file;
qDebug() << "m_levelDir: " << m_levelDir.absolutePath();

QString urlString = m_url.toString();
QByteArray byteArray = urlString.toUtf8();
const char* url_cstring = byteArray.constData();

const QString filePath = levelDir_m.absolutePath() +
QDir::separator() + file_m;
const QString filePath = QString("%1%2%3")
.arg(m_levelDir.absolutePath(), QDir::separator(), m_file);

qDebug() << "filePath: " << filePath;

QFileInfo fileInfo(filePath);

Expand All @@ -169,13 +138,13 @@ void Downloader::run() {
return;
}

#pragma cppcheck-suppress fopen_s
FILE* file = fopen(filePath.toUtf8(), "wb"); // flawfinder: ignore
if (!file) {
qDebug() << "Error opening file for writing:" << filePath;
return;
}

WriteData writeData = { this, file };

CURL* curl = curl_easy_init();
if (curl) {
Expand All @@ -189,26 +158,55 @@ void Downloader::run() {

curl_easy_setopt(curl, CURLOPT_URL, url_cstring);
curl_easy_setopt(curl, CURLOPT_CAINFO_BLOB, &blob);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_PINNEDPUBLICKEY,
"sha256//7WRPcNY2QpOjWiQSLbiBu/9Og69JmzccPAdfj2RT5Vw=");

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
Downloader::write_callback);
+[](const void* buf, size_t size, size_t nmemb, void* data) -> size_t {
return fwrite(buf, size, nmemb, static_cast<FILE*>(data));
});

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &writeData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);

// Follow redirects
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

// Enable progress meter
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);

curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION,
Downloader::progress_callback);
+[](void* clientp, curl_off_t dltotal, curl_off_t dlnow,
curl_off_t ultotal, curl_off_t ulnow) -> int {
Downloader& downloader = Downloader::getInstance();

if (dltotal > 0) {
int& lastEmittedProgress =
downloader.m_lastEmittedProgress;

double progress = static_cast<double>(dlnow)
/ static_cast<double>(dltotal) * 50.0;

if (static_cast<int>(progress) == 0) {
lastEmittedProgress = 0;
}
if (static_cast<int>(progress) > lastEmittedProgress) {
emit downloader.networkWorkTickSignal();
QCoreApplication::processEvents();
lastEmittedProgress = static_cast<int>(progress);
}
}
return 0;
});

curl_easy_setopt(curl, CURLOPT_XFERINFODATA, nullptr);

// Perform the download
CURLcode res = curl_easy_perform(curl);

if (res != CURLE_OK) {
status_m = 1;
m_status = 1;
qDebug() << "CURL failed:" << curl_easy_strerror(res);
// we need to catch any of those that seem inportant here to the GUI
// and reset GUI state
Expand All @@ -224,7 +222,7 @@ void Downloader::run() {
QCoreApplication::processEvents();
}
} else {
status_m = 0;
m_status = 0;
qDebug() << "Downloaded successfully";
}
curl_easy_cleanup(curl);
Expand Down
26 changes: 10 additions & 16 deletions src/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,25 @@ class Downloader : public QObject {
void setUrl(QUrl url);
int getStatus();
void setSaveFile(const QString& file);
static size_t write_callback(
void* contents,
size_t size,
size_t nmemb,
void* userData);
static int progress_callback(
void* clientp,
curl_off_t dltotal,
curl_off_t dlnow,
curl_off_t ultotal,
curl_off_t ulnow);

signals:
void networkWorkTickSignal();
void networkWorkErrorSignal(int status);

private:
void saveToFile(const QByteArray& data, const QString& filePath);
QUrl url_m;
QString file_m;
QDir levelDir_m;
qint32 status_m = 0;
QUrl m_url;
QString m_file;
QDir m_levelDir;
qint32 m_status;
int m_lastEmittedProgress;

explicit Downloader(QObject* parent = nullptr) : QObject(parent),
status_m(0) {
m_url(""),
m_file(""),
m_levelDir(""),
m_status(0),
m_lastEmittedProgress(0) {
curl_global_init(CURL_GLOBAL_DEFAULT);
}

Expand Down
9 changes: 7 additions & 2 deletions src/TombRaiderLinuxLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,19 @@ void TombRaiderLinuxLauncher::setOptionsClicked() {
}

void TombRaiderLinuxLauncher::linkClicked() {
bool status = false;
QListWidgetItem *selectedItem = ui->listWidgetModds->currentItem();
int id = selectedItem->data(Qt::UserRole).toInt();
if (id) {
qint64 status = controller.link(id);
status = controller.link(id);
} else {
qDebug() << "id error";
}
QApplication::quit();
if (status == true) {
QApplication::quit();
} else {
qDebug() << "link error";
}
}

void TombRaiderLinuxLauncher::downloadClicked() {
Expand Down

0 comments on commit 884099a

Please sign in to comment.