fix: Curl SSL context not being thread safe in the slightest

This commit is contained in:
WerWolv 2022-09-19 21:56:43 +02:00
parent 4c01a749de
commit a55177edfa
5 changed files with 22 additions and 23 deletions

View file

@ -11,6 +11,7 @@
#include <nlohmann/json_fwd.hpp>
#include <curl/system.h>
#include <mbedtls/ssl.h>
#include <hex/helpers/fs.hpp>
@ -60,6 +61,7 @@ namespace hex {
private:
CURL *m_ctx;
mbedtls_x509_crt m_caCert;
curl_slist *m_headers = nullptr;
std::mutex m_transmissionActive;

View file

@ -9,8 +9,6 @@
#include <filesystem>
#include <cstdio>
#include <mbedtls/ssl.h>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
@ -52,13 +50,13 @@ namespace hex {
auto *cfg = static_cast<mbedtls_ssl_config *>(sslctx);
static mbedtls_x509_crt crt;
mbedtls_x509_crt_init(&crt);
auto crt = static_cast<mbedtls_x509_crt*>(userData);
mbedtls_x509_crt_init(crt);
auto cacert = romfs::get("cacert.pem").string();
mbedtls_x509_crt_parse(&crt, reinterpret_cast<const u8 *>(cacert.data()), cacert.size());
mbedtls_x509_crt_parse(crt, reinterpret_cast<const u8 *>(cacert.data()), cacert.size());
mbedtls_ssl_conf_ca_chain(cfg, &crt, nullptr);
mbedtls_ssl_conf_ca_chain(cfg, crt, nullptr);
return CURLE_OK;
}
@ -114,6 +112,7 @@ namespace hex {
curl_easy_setopt(this->m_ctx, CURLOPT_CAPATH, nullptr);
curl_easy_setopt(this->m_ctx, CURLOPT_SSLCERTTYPE, "PEM");
curl_easy_setopt(this->m_ctx, CURLOPT_SSL_CTX_FUNCTION, sslCtxFunction);
curl_easy_setopt(this->m_ctx, CURLOPT_SSL_CTX_DATA, &this->m_caCert);
#endif
curl_easy_setopt(this->m_ctx, CURLOPT_PROXY, Net::s_proxyUrl.c_str());

View file

@ -26,7 +26,7 @@ namespace hex::init {
private:
GLFWwindow *m_window;
std::mutex m_progressMutex;
float m_progress = 0;
std::atomic<float> m_progress = 0;
std::string m_currTaskName;
void initGLFW();

View file

@ -43,18 +43,20 @@ namespace hex::init {
return std::async(std::launch::async, [this] {
bool status = true;
u32 tasksCompleted = 0;
std::atomic<u32> tasksCompleted = 0;
for (const auto &[name, task, async] : this->m_tasks) {
if (!async) {
auto runTask = [&, task = task, name = name] {
{
std::lock_guard guard(this->m_progressMutex);
this->m_currTaskName = name;
}
auto runTask = [&, task = task] {
if (!task())
status = false;
tasksCompleted++;
this->m_progress = float(tasksCompleted) / this->m_tasks.size();
};
try {
@ -68,18 +70,14 @@ namespace hex::init {
log::error("Init task '{}' threw an exception: {}", name, e.what());
status = false;
}
{
std::lock_guard guard(this->m_progressMutex);
this->m_progress += 1.0F / this->m_tasks.size();
}
}
while (tasksCompleted < this->m_tasks.size())
while (tasksCompleted < this->m_tasks.size()) {
std::this_thread::sleep_for(100ms);
}
// Small extra delay so the last progress step is visible
std::this_thread::sleep_for(200ms);
std::this_thread::sleep_for(100ms);
return status;
});

View file

@ -324,7 +324,7 @@ namespace hex::init {
std::vector<Task> getInitTasks() {
return {
{ "Checking for updates...", checkForUpdates, false },
{ "Checking for updates...", checkForUpdates, true },
{ "Downloading information...", downloadInformation, true },
{ "Loading fonts...", loadFonts, true },
{ "Creating directories...", createDirectories, false },