Skip to content

Commit

Permalink
merge modautodownloader pr, improve dohworker
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf109909 committed Nov 21, 2024
1 parent 8d5933a commit 4974562
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 37 deletions.
33 changes: 32 additions & 1 deletion primedev/mods/autodownload/moddownloader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "moddownloader.h"
#include "util/dohworker.h"
#include "util/utils.h"
#include <rapidjson/fwd.h>
#include <mz_strm_mem.h>
Expand All @@ -11,9 +12,9 @@
#include <bcrypt.h>
#include <winternl.h>
#include <fstream>

ModDownloader* g_pModDownloader;

//gcdn.thunderstore.io/live/repository/packages/odds-s2space-0.0.5.zip
ModDownloader::ModDownloader()
{
spdlog::info("Mod downloader initialized");
Expand All @@ -39,6 +40,7 @@ ModDownloader::ModDownloader()
}
spdlog::info("Found custom verified mods URL in command line argument: {}", url);
modsListUrl = strdup(url.c_str());

}
else
{
Expand Down Expand Up @@ -72,6 +74,20 @@ void ModDownloader::FetchModsListFromAPI()
easyhandle = curl_easy_init();
std::string readBuffer;

curl_easy_setopt(easyhandle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
const std::string doh_result = g_DohWorker->GetDOHResolve(url);
if (g_DohWorker->m_bDohAvailable)
{
struct curl_slist* host = nullptr;
const std::string addr = g_DohWorker->ExtractDomain(url) + ":443:" + doh_result;
host = curl_slist_append(nullptr, addr.c_str());
curl_easy_setopt(easyhandle, CURLOPT_RESOLVE, host);
}
else
{
// spdlog::warn("[DOH] service is not available. falling back to DNS");
}

// Fetching mods list from GitHub repository
curl_easy_setopt(easyhandle, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(easyhandle, CURLOPT_TIMEOUT, 30L);
Expand Down Expand Up @@ -196,6 +212,21 @@ std::optional<fs::path> ModDownloader::FetchModFromDistantStore(std::string_view
CURL* easyhandle;
easyhandle = curl_easy_init();

curl_easy_setopt(easyhandle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
const std::string doh_result = g_DohWorker->GetDOHResolve(url);
if (g_DohWorker->m_bDohAvailable)
{
struct curl_slist* host = nullptr;
const std::string addr = g_DohWorker->ExtractDomain(url) + ":443:" + doh_result;
host = curl_slist_append(nullptr, addr.c_str());
curl_easy_setopt(easyhandle, CURLOPT_RESOLVE, host);
}
else
{
// spdlog::warn("[DOH] service is not available. falling back to DNS");
}


curl_easy_setopt(easyhandle, CURLOPT_URL, url.data());
curl_easy_setopt(easyhandle, CURLOPT_FAILONERROR, 1L);

Expand Down
2 changes: 1 addition & 1 deletion primedev/mods/autodownload/moddownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ModDownloader
private:
const char* VERIFICATION_FLAG = "-disablemodverification";
const char* CUSTOM_MODS_URL_FLAG = "-customverifiedurl=";
const char* DEFAULT_MODS_LIST_URL = "https://raw.githubusercontent.com/R2Northstar/VerifiedMods/main/verified-mods.json";
const char* DEFAULT_MODS_LIST_URL = "https://gitee.com/R2NorthstarCN/VerifiedMods/raw/master/verified-mods.json";
char* modsListUrl;

enum class VerifiedModPlatform
Expand Down
57 changes: 24 additions & 33 deletions primedev/util/dohworker.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "dohworker.h"
#include "nlohmann/json.hpp"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
Expand All @@ -17,23 +18,23 @@ size_t DOHCurlWriteToStringBufferCallback(char* contents, size_t size, size_t nm
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}


std::string DohWorker::GetDOHResolve(std::string domainname)
std::string DohWorker::ExtractDomain(const std::string& url)
{
bool stripHeaders = false;
if ((domainname.find("https") != std::string::npos))
{
//spdlog::info("[DOH] Found https like headers in {}", domainname);
domainname.erase(0, 8);
stripHeaders = true;
}
if ((domainname.find("http") != std::string::npos) && !stripHeaders)
// Regex to match the domain name
std::regex domainRegex(R"(^(?:https?:\/\/)?([^\/]+))");
std::smatch match;

// Apply regex to the URL
if (std::regex_search(url, match, domainRegex) && match.size() > 1)
{
//spdlog::info("[DOH] Found http like headers in {}", domainname);
domainname.erase(0, 7);
stripHeaders = true;
return match[1].str(); // Extract the first capturing group
}
return ""; // Return empty string if no match
}

std::string DohWorker::GetDOHResolve(std::string url)
{
std::string domainname = ExtractDomain(url);
if (localresolvcache.find(domainname) != localresolvcache.end())
{
//spdlog::info("[DOH] Found cache for {}", domainname);
Expand All @@ -45,29 +46,17 @@ std::string DohWorker::GetDOHResolve(std::string domainname)
}
}

std::string DohWorker::ResolveDomain(std::string domainname)
std::string DohWorker::ResolveDomain(std::string url)
{
while(is_resolving)
Sleep(100);
bool stripHeaders = false;
is_resolving = true;
if ((domainname.find("https") != std::string::npos))
{
//spdlog::info("[DOH] Found https like headers in {}", domainname);
domainname.erase(0, 8);
stripHeaders = true;
}
if ((domainname.find("http") != std::string::npos) && !stripHeaders)
{
//spdlog::info("[DOH] Found http like headers in {}", domainname);
domainname.erase(0, 7);
stripHeaders = true;
}

//spdlog::info("[DOH] Resolving {}", domainname);
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
std::string readBuffer;
std::string domainname = ExtractDomain(url);
char* domainnameescaped = curl_easy_escape(curl, domainname.c_str(), domainname.length());
curl_easy_setopt(
curl,
Expand All @@ -85,17 +74,19 @@ std::string DohWorker::ResolveDomain(std::string domainname)
if (result == CURLcode::CURLE_OK)
{
//spdlog::info(readBuffer);
readBuffer = readBuffer.substr(2, readBuffer.length() - 4);

nlohmann::json resjson = nlohmann::json::parse(readBuffer);

std::string match = resjson[0].get<std::string>();

if (!readBuffer.empty())
{
g_DohWorker->localresolvcache.insert_or_assign(domainname, readBuffer);
spdlog::info("[DOH] Successfully resolved {} , result: {}", domainname, readBuffer);
spdlog::info("[DOH] Successfully resolved {} , result: {}", domainname, match);
curl_easy_cleanup(curl);
m_bDohAvailable = true;
is_resolving = false;
return readBuffer;
return match;
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions primedev/util/dohworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class DohWorker
bool is_resolving = false;
std::map<std::string, std::string> localresolvcache;
void ExecuteDefaults();
std::string ResolveDomain(std::string domainname);
std::string GetDOHResolve(std::string domainname);
std::string ResolveDomain(std::string url);
std::string GetDOHResolve(std::string url);
std::string ExtractDomain(const std::string& url);
};

extern DohWorker* g_DohWorker;

0 comments on commit 4974562

Please sign in to comment.