Skip to content

Commit

Permalink
fix: Add proxy support for web requests within Steam.
Browse files Browse the repository at this point in the history
  • Loading branch information
shdwmtr committed Dec 25, 2024
1 parent 8990389 commit 71dabc6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ set(SOURCE_FILES
)

if (MSVC)
set(SOURCE_FILES "${SOURCE_FILES} version.rc") # conpile version information on msvc
set(SOURCE_FILES "${SOURCE_FILES} version.rc") # compile version information on msvc
endif()

if (WIN32)
Expand Down Expand Up @@ -159,7 +159,7 @@ endif()
target_link_libraries(Millennium CURL::libcurl unofficial::minizip::minizip)

if(WIN32)
target_link_libraries(Millennium Ws2_32.lib wsock32 Iphlpapi)
target_link_libraries(Millennium Ws2_32.lib wsock32 Iphlpapi winhttp)

if (GITHUB_ACTION_BUILD)
target_link_libraries(Millennium "D:/a/Millennium/Millennium/Python-3.11.8/PCbuild/win32/python311.lib")
Expand Down
16 changes: 9 additions & 7 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,27 @@ class Preload

void OnTerminate()
{
std::string errorMessage = "Millennium has a fatal error that it can't recover from, check the logs for more details!";

auto const exceptionPtr = std::current_exception();
if (exceptionPtr) {
try {
std::string errorMessage = "Millennium has a fatal error that it can't recover from, check the logs for more details!";

if (exceptionPtr)
{
try
{
int status;
auto const exceptionType = abi::__cxa_demangle(abi::__cxa_current_exception_type()->name(), 0, 0, &status);
errorMessage.append("\nTerminating with uncaught exception of type `");
errorMessage.append(exceptionType);
errorMessage.append("`");
std::rethrow_exception(exceptionPtr); // rethrow the exception to catch its exception message
}
catch (const std::exception& e) {
catch (const std::exception& e)
{
errorMessage.append(" with `what()` = \"");
errorMessage.append(e.what());
errorMessage.append("\"");
}
catch (...) {
}
catch (...) { }
}

#ifdef _WIN32
Expand Down
39 changes: 39 additions & 0 deletions src/sys/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <thread>
#include <sys/log.h>
#include <curl/curl.h>
#include <winhttp.h>

static size_t write_callback(char* ptr, size_t size, size_t nmemb, std::string* data)
{
Expand All @@ -13,6 +14,41 @@ static size_t write_callback(char* ptr, size_t size, size_t nmemb, std::string*

namespace Http
{
static void AddWindowsProxySettings(CURL* curl)
{
// Function to convert wide strings (wchar_t*) to std::string
const auto wide_to_string = [](const wchar_t* wide_str) -> std::string
{
if (!wide_str)
return "";

int size_needed = WideCharToMultiByte(CP_UTF8, 0, wide_str, -1, nullptr, 0, nullptr, nullptr);
std::string str(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, wide_str, -1, &str[0], size_needed, nullptr, nullptr);
return str;
};

WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyConfig;

if (WinHttpGetIEProxyConfigForCurrentUser(&proxyConfig))
{
if (proxyConfig.lpszProxy)
{
curl_easy_setopt(curl, CURLOPT_PROXY, wide_to_string(proxyConfig.lpszProxy).c_str());
}

// Free memory allocated by WinHttpGetIEProxyConfigForCurrentUser
if (proxyConfig.lpszProxy) GlobalFree(proxyConfig.lpszProxy);
if (proxyConfig.lpszProxyBypass) GlobalFree(proxyConfig.lpszProxyBypass);
if (proxyConfig.lpszAutoConfigUrl) GlobalFree(proxyConfig.lpszAutoConfigUrl);
}
else
{
std::cerr << "Failed to get proxy settings. Error: " << GetLastError() << std::endl;
}

}

static std::string Get(const char* url, bool retry = true)
{
CURL* curl;
Expand All @@ -27,6 +63,7 @@ namespace Http
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_USERAGENT, fmt::format("Millennium/{}", MILLENNIUM_VERSION).c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects
AddWindowsProxySettings(curl);

while (true)
{
Expand Down Expand Up @@ -89,6 +126,7 @@ namespace Http
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headersList);
AddWindowsProxySettings(curl);

curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, +[](char* buffer, size_t size, size_t nitems, std::string* userdata) -> size_t {
userdata->append(buffer, size * nitems);
Expand Down Expand Up @@ -120,6 +158,7 @@ namespace Http
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_USERAGENT, fmt::format("Millennium/{}", MILLENNIUM_VERSION).c_str());
AddWindowsProxySettings(curl);

res = curl_easy_perform(curl);
fclose(file);
Expand Down

0 comments on commit 71dabc6

Please sign in to comment.