2021-04-18 20:24:42 +02:00
|
|
|
#include <hex/helpers/net.hpp>
|
|
|
|
|
|
|
|
#include <hex/helpers/utils.hpp>
|
2021-09-08 15:18:24 +02:00
|
|
|
#include <hex/helpers/file.hpp>
|
2022-01-13 09:05:16 +01:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2022-07-01 14:05:32 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
|
2021-04-18 20:24:42 +02:00
|
|
|
#include <filesystem>
|
2021-08-28 00:45:59 +02:00
|
|
|
#include <cstdio>
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2021-08-22 20:22:46 +02:00
|
|
|
#include <mbedtls/ssl.h>
|
2021-07-31 17:10:19 +02:00
|
|
|
|
2021-12-17 09:52:58 +01:00
|
|
|
#include <curl/curl.h>
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2021-12-22 15:06:16 +01:00
|
|
|
#include <romfs/romfs.hpp>
|
2021-07-31 17:10:19 +02:00
|
|
|
|
2021-04-18 20:24:42 +02:00
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
Net::Net() {
|
2021-08-28 00:45:59 +02:00
|
|
|
FIRST_TIME {
|
|
|
|
curl_global_sslset(CURLSSLBACKEND_MBEDTLS, nullptr, nullptr);
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
};
|
|
|
|
|
|
|
|
FINAL_CLEANUP {
|
|
|
|
curl_global_cleanup();
|
|
|
|
};
|
|
|
|
|
2021-04-18 20:24:42 +02:00
|
|
|
this->m_ctx = curl_easy_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Net::~Net() {
|
|
|
|
curl_easy_cleanup(this->m_ctx);
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
static size_t writeToString(void *contents, size_t size, size_t nmemb, void *userdata) {
|
|
|
|
static_cast<std::string *>(userdata)->append((char *)contents, size * nmemb);
|
2021-04-18 20:24:42 +02:00
|
|
|
return size * nmemb;
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
static size_t writeToFile(void *contents, size_t size, size_t nmemb, void *userdata) {
|
2022-01-24 20:53:17 +01:00
|
|
|
FILE *file = static_cast<FILE *>(userdata);
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
return fwrite(contents, size, nmemb, file);
|
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
[[maybe_unused]]
|
|
|
|
static CURLcode sslCtxFunction(CURL *ctx, void *sslctx, void *userData) {
|
|
|
|
hex::unused(ctx, userData);
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
auto *cfg = static_cast<mbedtls_ssl_config *>(sslctx);
|
2021-08-22 20:22:46 +02:00
|
|
|
|
|
|
|
static mbedtls_x509_crt crt;
|
|
|
|
mbedtls_x509_crt_init(&crt);
|
2021-12-22 15:06:16 +01:00
|
|
|
|
2022-01-15 02:46:24 +01:00
|
|
|
auto cacert = romfs::get("cacert.pem").string();
|
2022-01-24 20:53:17 +01:00
|
|
|
mbedtls_x509_crt_parse(&crt, reinterpret_cast<const u8 *>(cacert.data()), cacert.size());
|
2021-08-22 20:22:46 +02:00
|
|
|
|
|
|
|
mbedtls_ssl_conf_ca_chain(cfg, &crt, nullptr);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
int progressCallback(void *contents, curl_off_t dlTotal, curl_off_t dlNow, curl_off_t ulTotal, curl_off_t ulNow) {
|
2022-01-24 20:53:17 +01:00
|
|
|
auto &net = *static_cast<Net *>(contents);
|
2021-08-28 00:45:59 +02:00
|
|
|
|
|
|
|
if (dlTotal > 0)
|
|
|
|
net.m_progress = float(dlNow) / dlTotal;
|
|
|
|
else if (ulTotal > 0)
|
|
|
|
net.m_progress = float(ulNow) / ulTotal;
|
|
|
|
else
|
|
|
|
net.m_progress = 0.0F;
|
|
|
|
|
|
|
|
return net.m_shouldCancel ? CURLE_ABORTED_BY_CALLBACK : CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:52:58 +01:00
|
|
|
void Net::setCommonSettings(std::string &response, const std::string &url, u32 timeout, const std::map<std::string, std::string> &extraHeaders, const std::string &body) {
|
2021-08-28 00:45:59 +02:00
|
|
|
this->m_headers = curl_slist_append(this->m_headers, "Cache-Control: no-cache");
|
2021-04-18 20:24:42 +02:00
|
|
|
|
|
|
|
if (!extraHeaders.empty())
|
|
|
|
for (const auto &[key, value] : extraHeaders) {
|
|
|
|
std::string entry = key;
|
|
|
|
entry += ": ";
|
|
|
|
entry += value;
|
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
this->m_headers = curl_slist_append(this->m_headers, entry.c_str());
|
2021-04-18 20:24:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!body.empty())
|
2021-08-28 00:45:59 +02:00
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_POSTFIELDS, body.c_str());
|
|
|
|
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_URL, url.c_str());
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_HTTPHEADER, this->m_headers);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_USERAGENT, "ImHex/1.0");
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_DEFAULT_PROTOCOL, "https");
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_WRITEFUNCTION, writeToString);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_SSL_VERIFYPEER, 1L);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_SSL_VERIFYHOST, 2L);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_WRITEDATA, &response);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_TIMEOUT_MS, 0L);
|
2021-12-17 09:52:58 +01:00
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_CONNECTTIMEOUT_MS, timeout);
|
2021-08-28 00:45:59 +02:00
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_XFERINFODATA, this);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_XFERINFOFUNCTION, progressCallback);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_NOSIGNAL, 1L);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_NOPROGRESS, 0L);
|
2022-01-29 13:40:17 +01:00
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_CAINFO, 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_SSL_CTX_FUNCTION, sslCtxFunction);
|
|
|
|
#endif
|
2022-07-01 14:05:32 +02:00
|
|
|
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_PROXY, Net::s_proxyUrl.c_str());
|
2021-04-18 20:24:42 +02:00
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
std::optional<i32> Net::execute() {
|
2021-04-18 20:24:42 +02:00
|
|
|
CURLcode result = curl_easy_perform(this->m_ctx);
|
2022-01-13 09:05:16 +01:00
|
|
|
if (result != CURLE_OK)
|
2022-07-06 10:17:44 +02:00
|
|
|
log::error("Net request failed with error {0}: '{1}'", u32(result), curl_easy_strerror(result));
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
i32 responseCode = 0;
|
2021-04-18 20:24:42 +02:00
|
|
|
curl_easy_getinfo(this->m_ctx, CURLINFO_RESPONSE_CODE, &responseCode);
|
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
curl_slist_free_all(this->m_headers);
|
2022-02-01 22:09:44 +01:00
|
|
|
this->m_headers = nullptr;
|
|
|
|
this->m_progress = 0.0F;
|
2021-08-28 00:45:59 +02:00
|
|
|
this->m_shouldCancel = false;
|
|
|
|
|
2021-04-18 20:24:42 +02:00
|
|
|
if (result != CURLE_OK)
|
2022-01-30 12:43:43 +01:00
|
|
|
return std::nullopt;
|
2021-04-18 20:24:42 +02:00
|
|
|
else
|
2021-08-28 00:45:59 +02:00
|
|
|
return responseCode;
|
2021-04-18 20:24:42 +02:00
|
|
|
}
|
|
|
|
|
2021-12-17 09:52:58 +01:00
|
|
|
std::future<Response<std::string>> Net::getString(const std::string &url, u32 timeout) {
|
2021-08-28 00:45:59 +02:00
|
|
|
this->m_transmissionActive.lock();
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
return std::async(std::launch::async, [=, this] {
|
2021-08-28 00:45:59 +02:00
|
|
|
std::string response;
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); };
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "GET");
|
2021-12-17 09:52:58 +01:00
|
|
|
setCommonSettings(response, url, timeout);
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2021-08-28 00:45:59 +02:00
|
|
|
auto responseCode = execute();
|
|
|
|
|
|
|
|
return Response<std::string> { responseCode.value_or(0), response };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:52:58 +01:00
|
|
|
std::future<Response<nlohmann::json>> Net::getJson(const std::string &url, u32 timeout) {
|
2021-08-28 00:45:59 +02:00
|
|
|
this->m_transmissionActive.lock();
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
return std::async(std::launch::async, [=, this] {
|
2021-08-28 00:45:59 +02:00
|
|
|
std::string response;
|
|
|
|
|
|
|
|
ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); };
|
|
|
|
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "GET");
|
2021-12-17 09:52:58 +01:00
|
|
|
setCommonSettings(response, url, timeout);
|
2021-08-28 00:45:59 +02:00
|
|
|
|
|
|
|
auto responseCode = execute();
|
|
|
|
|
|
|
|
return Response<nlohmann::json> { responseCode.value_or(0), nlohmann::json::parse(response) };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
std::future<Response<std::string>> Net::uploadFile(const std::string &url, const std::fs::path &filePath, u32 timeout) {
|
2021-08-28 00:45:59 +02:00
|
|
|
this->m_transmissionActive.lock();
|
|
|
|
|
|
|
|
return std::async(std::launch::async, [=, this] {
|
|
|
|
std::string response;
|
|
|
|
|
|
|
|
ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); };
|
|
|
|
|
2022-07-05 00:00:00 +02:00
|
|
|
fs::File file(filePath, fs::File::Mode::Read);
|
2021-09-08 15:18:24 +02:00
|
|
|
if (!file.isValid())
|
2022-01-24 20:53:17 +01:00
|
|
|
return Response<std::string> { 400, {} };
|
2021-08-28 00:45:59 +02:00
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
curl_mime *mime = curl_mime_init(this->m_ctx);
|
2021-08-28 00:45:59 +02:00
|
|
|
curl_mimepart *part = curl_mime_addpart(mime);
|
|
|
|
|
|
|
|
auto fileName = filePath.filename().string();
|
2022-01-24 20:53:17 +01:00
|
|
|
curl_mime_data_cb(
|
|
|
|
part, file.getSize(), [](char *buffer, size_t size, size_t nitems, void *arg) -> size_t {
|
2021-08-28 00:45:59 +02:00
|
|
|
auto file = static_cast<FILE*>(arg);
|
2022-01-24 20:53:17 +01:00
|
|
|
return fread(buffer, size, nitems, file); }, [](void *arg, curl_off_t offset, int origin) -> int {
|
2021-08-28 00:45:59 +02:00
|
|
|
auto file = static_cast<FILE*>(arg);
|
|
|
|
fseek(file, offset, origin);
|
2022-01-24 20:53:17 +01:00
|
|
|
return CURL_SEEKFUNC_OK; }, [](void *arg) {
|
2021-08-28 00:45:59 +02:00
|
|
|
auto file = static_cast<FILE*>(arg);
|
2022-01-24 20:53:17 +01:00
|
|
|
fclose(file); }, file.getHandle());
|
2021-08-28 00:45:59 +02:00
|
|
|
curl_mime_filename(part, fileName.c_str());
|
|
|
|
curl_mime_name(part, "file");
|
|
|
|
|
2021-12-17 09:52:58 +01:00
|
|
|
setCommonSettings(response, url, timeout);
|
2021-08-28 00:45:59 +02:00
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_MIMEPOST, mime);
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "POST");
|
|
|
|
|
|
|
|
auto responseCode = execute();
|
|
|
|
|
|
|
|
return Response<std::string> { responseCode.value_or(0), response };
|
|
|
|
});
|
2021-04-18 20:24:42 +02:00
|
|
|
}
|
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
std::future<Response<void>> Net::downloadFile(const std::string &url, const std::fs::path &filePath, u32 timeout) {
|
2021-09-03 02:34:40 +02:00
|
|
|
this->m_transmissionActive.lock();
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
return std::async(std::launch::async, [=, this] {
|
2021-09-03 02:34:40 +02:00
|
|
|
std::string response;
|
|
|
|
|
|
|
|
ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); };
|
|
|
|
|
2022-07-05 00:00:00 +02:00
|
|
|
fs::File file(filePath, fs::File::Mode::Create);
|
2021-09-08 15:18:24 +02:00
|
|
|
if (!file.isValid())
|
2021-09-03 02:34:40 +02:00
|
|
|
return Response<void> { 400 };
|
|
|
|
|
2021-12-17 09:52:58 +01:00
|
|
|
setCommonSettings(response, url, timeout);
|
2021-09-03 02:34:40 +02:00
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_CUSTOMREQUEST, "GET");
|
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_WRITEFUNCTION, writeToFile);
|
2021-09-08 15:18:24 +02:00
|
|
|
curl_easy_setopt(this->m_ctx, CURLOPT_WRITEDATA, file.getHandle());
|
2021-09-03 02:34:40 +02:00
|
|
|
auto responseCode = execute();
|
|
|
|
|
|
|
|
return Response<void> { responseCode.value_or(0) };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:52:58 +01:00
|
|
|
std::string Net::encode(const std::string &input) {
|
|
|
|
auto escapedString = curl_easy_escape(this->m_ctx, input.c_str(), std::strlen(input.c_str()));
|
|
|
|
|
|
|
|
if (escapedString != nullptr) {
|
|
|
|
std::string output = escapedString;
|
|
|
|
curl_free(escapedString);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2021-12-17 09:52:58 +01:00
|
|
|
}
|
|
|
|
|
2022-07-06 11:29:24 +02:00
|
|
|
std::string Net::decode(const std::string &input) {
|
|
|
|
auto unescapedString = curl_easy_unescape(this->m_ctx, input.c_str(), std::strlen(input.c_str()), nullptr);
|
|
|
|
|
|
|
|
if (unescapedString != nullptr) {
|
|
|
|
std::string output = unescapedString;
|
|
|
|
curl_free(unescapedString);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-01 14:05:32 +02:00
|
|
|
std::string Net::s_proxyUrl;
|
|
|
|
|
|
|
|
void Net::setProxy(const std::string &url) {
|
|
|
|
Net::s_proxyUrl = url;
|
|
|
|
}
|
|
|
|
|
2021-04-18 20:24:42 +02:00
|
|
|
}
|