Skip to content

Commit 884099a

Browse files
committed
dont use static methods and pinn key in app
this "locks" the app to the trle.net server
1 parent dbe5a72 commit 884099a

File tree

4 files changed

+71
-72
lines changed

4 files changed

+71
-72
lines changed

src/FileManager.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ const QString FileManager::calculateMD5(const QString& file, bool lookGameDir) {
7474
QCryptographicHash md5(QCryptographicHash::Md5);
7575

7676
std::array<char, 1024> buffer;
77-
qint64 bytesRead;
77+
const int size = buffer.size();
78+
int bytesRead;
7879

79-
while ((bytesRead = f.read(buffer.data(), buffer.size())) > 0) {
80-
md5.addData(buffer.data(), static_cast<int>(bytesRead));
80+
// flawfinder: ignore
81+
while ((bytesRead = f.read(buffer.data(), size)) > 0) {
82+
md5.addData(buffer.data(), bytesRead);
8183
}
8284

8385
f.close();

src/Network.cpp

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -74,62 +74,25 @@ std::string get_ssl_certificate(const std::string& host) {
7474
return cert_buffer;
7575
}
7676

77-
struct WriteData {
78-
Downloader* downloader;
79-
FILE* file;
80-
};
81-
8277
bool Downloader::setUpCamp(const QString& levelDir) {
8378
QFileInfo levelPathInfo(levelDir);
8479
if (levelPathInfo.isDir()) {
85-
levelDir_m.setPath(levelDir);
80+
m_levelDir.setPath(levelDir);
8681
return true;
8782
}
8883
return false;
8984
}
9085

91-
size_t Downloader::write_callback(
92-
void* contents,
93-
size_t size,
94-
size_t nmemb,
95-
void* userData) {
96-
WriteData* data = reinterpret_cast<WriteData*>(userData);
97-
return fwrite(contents, size, nmemb, data->file);
98-
}
99-
100-
int Downloader::progress_callback(
101-
void* clientp,
102-
curl_off_t dltotal,
103-
curl_off_t dlnow,
104-
curl_off_t ultotal,
105-
curl_off_t ulnow) {
106-
if (dltotal > 0) {
107-
double progress = static_cast<double>(dlnow)
108-
/ static_cast<double>(dltotal) * 50.0;
109-
110-
// Emit signal only if progress has increased by at least 1%
111-
static int lastEmittedProgress = 0;
112-
if (static_cast<int>(progress) == 0) lastEmittedProgress = 0;
113-
if (static_cast<int>(progress) > lastEmittedProgress) {
114-
static Downloader& instance = Downloader::getInstance();
115-
emit instance.networkWorkTickSignal();
116-
QCoreApplication::processEvents();
117-
lastEmittedProgress = static_cast<int>(progress);
118-
}
119-
}
120-
return 0;
121-
}
122-
12386
void Downloader::setUrl(QUrl url) {
124-
url_m = url;
87+
m_url = url;
12588
}
12689

12790
void Downloader::setSaveFile(const QString& file) {
128-
file_m = file;
91+
m_file = file;
12992
}
13093

13194
int Downloader::getStatus() {
132-
return status_m;
95+
return m_status;
13396
}
13497

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

154117
void Downloader::run() {
155-
if (url_m.isEmpty() || file_m.isEmpty() || levelDir_m.isEmpty())
118+
if (m_url.isEmpty() || m_file.isEmpty() || m_levelDir.isEmpty())
156119
return;
157120

158-
QString urlString = url_m.toString();
121+
qDebug() << "m_url: " << m_url.toString();
122+
qDebug() << "m_file: " << m_file;
123+
qDebug() << "m_levelDir: " << m_levelDir.absolutePath();
124+
125+
QString urlString = m_url.toString();
159126
QByteArray byteArray = urlString.toUtf8();
160127
const char* url_cstring = byteArray.constData();
161128

162-
const QString filePath = levelDir_m.absolutePath() +
163-
QDir::separator() + file_m;
129+
const QString filePath = QString("%1%2%3")
130+
.arg(m_levelDir.absolutePath(), QDir::separator(), m_file);
131+
132+
qDebug() << "filePath: " << filePath;
164133

165134
QFileInfo fileInfo(filePath);
166135

@@ -169,13 +138,13 @@ void Downloader::run() {
169138
return;
170139
}
171140

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

178-
WriteData writeData = { this, file };
179148

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

190159
curl_easy_setopt(curl, CURLOPT_URL, url_cstring);
191160
curl_easy_setopt(curl, CURLOPT_CAINFO_BLOB, &blob);
161+
162+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
163+
curl_easy_setopt(curl, CURLOPT_PINNEDPUBLICKEY,
164+
"sha256//7WRPcNY2QpOjWiQSLbiBu/9Og69JmzccPAdfj2RT5Vw=");
165+
192166
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
193-
Downloader::write_callback);
167+
+[](const void* buf, size_t size, size_t nmemb, void* data) -> size_t {
168+
return fwrite(buf, size, nmemb, static_cast<FILE*>(data));
169+
});
194170

195-
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &writeData);
171+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
196172

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

200176
// Enable progress meter
201177
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
178+
202179
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION,
203-
Downloader::progress_callback);
180+
+[](void* clientp, curl_off_t dltotal, curl_off_t dlnow,
181+
curl_off_t ultotal, curl_off_t ulnow) -> int {
182+
Downloader& downloader = Downloader::getInstance();
183+
184+
if (dltotal > 0) {
185+
int& lastEmittedProgress =
186+
downloader.m_lastEmittedProgress;
187+
188+
double progress = static_cast<double>(dlnow)
189+
/ static_cast<double>(dltotal) * 50.0;
190+
191+
if (static_cast<int>(progress) == 0) {
192+
lastEmittedProgress = 0;
193+
}
194+
if (static_cast<int>(progress) > lastEmittedProgress) {
195+
emit downloader.networkWorkTickSignal();
196+
QCoreApplication::processEvents();
197+
lastEmittedProgress = static_cast<int>(progress);
198+
}
199+
}
200+
return 0;
201+
});
204202

205203
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, nullptr);
206204

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

210208
if (res != CURLE_OK) {
211-
status_m = 1;
209+
m_status = 1;
212210
qDebug() << "CURL failed:" << curl_easy_strerror(res);
213211
// we need to catch any of those that seem inportant here to the GUI
214212
// and reset GUI state
@@ -224,7 +222,7 @@ void Downloader::run() {
224222
QCoreApplication::processEvents();
225223
}
226224
} else {
227-
status_m = 0;
225+
m_status = 0;
228226
qDebug() << "Downloaded successfully";
229227
}
230228
curl_easy_cleanup(curl);

src/Network.hpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,25 @@ class Downloader : public QObject {
3636
void setUrl(QUrl url);
3737
int getStatus();
3838
void setSaveFile(const QString& file);
39-
static size_t write_callback(
40-
void* contents,
41-
size_t size,
42-
size_t nmemb,
43-
void* userData);
44-
static int progress_callback(
45-
void* clientp,
46-
curl_off_t dltotal,
47-
curl_off_t dlnow,
48-
curl_off_t ultotal,
49-
curl_off_t ulnow);
5039

5140
signals:
5241
void networkWorkTickSignal();
5342
void networkWorkErrorSignal(int status);
5443

5544
private:
5645
void saveToFile(const QByteArray& data, const QString& filePath);
57-
QUrl url_m;
58-
QString file_m;
59-
QDir levelDir_m;
60-
qint32 status_m = 0;
46+
QUrl m_url;
47+
QString m_file;
48+
QDir m_levelDir;
49+
qint32 m_status;
50+
int m_lastEmittedProgress;
6151

6252
explicit Downloader(QObject* parent = nullptr) : QObject(parent),
63-
status_m(0) {
53+
m_url(""),
54+
m_file(""),
55+
m_levelDir(""),
56+
m_status(0),
57+
m_lastEmittedProgress(0) {
6458
curl_global_init(CURL_GLOBAL_DEFAULT);
6559
}
6660

src/TombRaiderLinuxLauncher.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,19 @@ void TombRaiderLinuxLauncher::setOptionsClicked() {
419419
}
420420

421421
void TombRaiderLinuxLauncher::linkClicked() {
422+
bool status = false;
422423
QListWidgetItem *selectedItem = ui->listWidgetModds->currentItem();
423424
int id = selectedItem->data(Qt::UserRole).toInt();
424425
if (id) {
425-
qint64 status = controller.link(id);
426+
status = controller.link(id);
426427
} else {
427428
qDebug() << "id error";
428429
}
429-
QApplication::quit();
430+
if (status == true) {
431+
QApplication::quit();
432+
} else {
433+
qDebug() << "link error";
434+
}
430435
}
431436

432437
void TombRaiderLinuxLauncher::downloadClicked() {

0 commit comments

Comments
 (0)