1
0
mirror of synced 2025-02-25 22:08:34 +01:00

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

View File

@ -9,8 +9,6 @@
#include <filesystem> #include <filesystem>
#include <cstdio> #include <cstdio>
#include <mbedtls/ssl.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
@ -52,13 +50,13 @@ namespace hex {
auto *cfg = static_cast<mbedtls_ssl_config *>(sslctx); auto *cfg = static_cast<mbedtls_ssl_config *>(sslctx);
static mbedtls_x509_crt crt; auto crt = static_cast<mbedtls_x509_crt*>(userData);
mbedtls_x509_crt_init(&crt); mbedtls_x509_crt_init(crt);
auto cacert = romfs::get("cacert.pem").string(); 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; 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_CAPATH, nullptr);
curl_easy_setopt(this->m_ctx, CURLOPT_SSLCERTTYPE, "PEM"); 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_FUNCTION, sslCtxFunction);
curl_easy_setopt(this->m_ctx, CURLOPT_SSL_CTX_DATA, &this->m_caCert);
#endif #endif
curl_easy_setopt(this->m_ctx, CURLOPT_PROXY, Net::s_proxyUrl.c_str()); curl_easy_setopt(this->m_ctx, CURLOPT_PROXY, Net::s_proxyUrl.c_str());

View File

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

View File

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

View File

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