Skip to content

Commit

Permalink
use system proxy when checking for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fwilliamhe committed Nov 1, 2024
1 parent 5ba0441 commit 376800c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ target_link_libraries(trackaudio-afv PRIVATE
sfml-system sfml-graphics sfml-window
${CMAKE_JS_LIB})

if (WIN32)
target_link_libraries(trackaudio-afv PRIVATE
winhttp)
endif ()

# Set the output directory for the built executable
set_target_properties(trackaudio-afv
PROPERTIES
Expand Down
65 changes: 65 additions & 0 deletions backend/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#include "Shared.hpp"
#include "sdk.hpp"

// to obtain proxy settings in Windows
#ifdef WIN32
#include <winhttp.h>
#endif

struct MainThreadShared {
public:
inline static std::unique_ptr<RemoteData> mRemoteDataHandler = nullptr;
Expand Down Expand Up @@ -694,13 +699,73 @@ struct VersionCheckResponse {
bool needUpdate = false;
};

void SetHttpclientProxy(httplib::Client& clt)
{
std::string http_proxy;

if (std::getenv("https_proxy"))
http_proxy = std::string(std::getenv("https_proxy"));
else if (std::getenv("http_proxy"))
http_proxy = std::string(std::getenv("http_proxy"));

#ifdef WIN32
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxy_config;
if (WinHttpGetIEProxyConfigForCurrentUser(&proxy_config)) {
if (proxy_config.lpszProxy) {
auto ConvertLPWSTRToString = [](LPWSTR lpwstr) -> std::string {
int bufferSize = WideCharToMultiByte(CP_UTF8, 0, lpwstr, -1, nullptr, 0, nullptr, nullptr);
if (bufferSize <= 0) {
return "";
}
std::string str(bufferSize, '\0');
WideCharToMultiByte(CP_UTF8, 0, lpwstr, -1, &str[0], bufferSize, nullptr, nullptr);
return str;
};
http_proxy = ConvertLPWSTRToString(proxy_config.lpszProxy);
}
if (proxy_config.lpszAutoConfigUrl)
GlobalFree(proxy_config.lpszAutoConfigUrl);
if (proxy_config.lpszProxy)
GlobalFree(proxy_config.lpszProxy);
if (proxy_config.lpszProxyBypass)
GlobalFree(proxy_config.lpszProxyBypass);
}
#endif

if (http_proxy.empty())
return ;

try
{
std::string proxy_host; int proxy_port = 8080;
// remove prefix
auto pos = http_proxy.find("://");
if (pos != std::string::npos) {
http_proxy = http_proxy.substr(pos + 3); // Strip protocol
}

pos = http_proxy.find(':');
if (pos != std::string::npos) {
proxy_host = http_proxy.substr(0, pos);
proxy_port = std::stoi(http_proxy.substr(pos + 1));
} else {
proxy_host = http_proxy;
}
clt.set_proxy(proxy_host, proxy_port);
}
catch (std::exception &e) {
PLOGE << "Error parsing proxy settings: " << e.what();
}
}

VersionCheckResponse CheckVersionSync(const Napi::CallbackInfo& /*info*/)
{
// We force do a mandatory version check, if an update is needed, the
// programme won't run

try {
httplib::Client client(VERSION_CHECK_BASE_URL);
SetHttpclientProxy(client);
auto res = client.Get(VERSION_CHECK_ENDPOINT);
if (!res || res->status != httplib::StatusCode::OK_200) {
std::string errorDetail;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 376800c

Please sign in to comment.