-
Notifications
You must be signed in to change notification settings - Fork 54
Contributing back patches to 1DS #1266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
#include <Wincrypt.h> | ||
#include <WinInet.h> | ||
#include <wincred.h> | ||
|
||
#include <algorithm> | ||
#include <memory> | ||
|
@@ -22,6 +23,8 @@ | |
|
||
namespace MAT_NS_BEGIN { | ||
|
||
const std::string kProxyRegKeyPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"; | ||
|
||
class WinInetRequestWrapper | ||
{ | ||
protected: | ||
|
@@ -136,6 +139,105 @@ class WinInetRequestWrapper | |
return true; | ||
} | ||
|
||
DWORD GetDWORDRegKey(HKEY hKey, const std::string& subKey, const std::string& value) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If these arguments are always known constants, don't do unnecessary heap allocations, use const char* rather than std::string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. std::string_view would be ideal, but this repo is still not on C++17 :( |
||
DWORD data; | ||
DWORD dataSize = sizeof(data); | ||
LONG res = RegGetValueA(hKey, subKey.c_str(), value.c_str(), RRF_RT_REG_DWORD, NULL, &data, | ||
&dataSize); | ||
if (res != ERROR_SUCCESS) { | ||
return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does a caller distinguish between a zero in the Registry value and an error? |
||
} | ||
return data; | ||
} | ||
|
||
std::string GetStringRegKey(HKEY hKey, const std::string& subKey, const std::string& value) { | ||
DWORD stringSize; | ||
LONG getSizeResult = RegGetValueA(hKey, subKey.c_str(), value.c_str(), RRF_RT_REG_SZ, NULL, | ||
NULL, &stringSize); | ||
if (getSizeResult != ERROR_SUCCESS) { | ||
return ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use the default string constructor, rather than the c-string constructor: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does a caller distinguish between an empty string in the registry and an error? |
||
} | ||
|
||
std::string data; | ||
data.resize(stringSize); | ||
LONG getStringResult = RegGetValueA(hKey, subKey.c_str(), value.c_str(), RRF_RT_REG_SZ, NULL, | ||
&data[0], &stringSize); | ||
if (getStringResult != ERROR_SUCCESS) { | ||
return ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
} | ||
|
||
// Remove the null terminator from Win32 API. The std::string will already cover this. | ||
data.resize(stringSize - 1); | ||
|
||
return data; | ||
} | ||
|
||
bool ProxyAuthRequired() { | ||
return GetDWORDRegKey(HKEY_CURRENT_USER, kProxyRegKeyPath, "ProxyEnable"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't rely on implicit conversions.
|
||
} | ||
|
||
void SetProxyCredentials() { | ||
// get proxy server from registry | ||
std::string proxyServer = GetStringRegKey(HKEY_CURRENT_USER, kProxyRegKeyPath, "ProxyServer"); | ||
if (proxyServer.empty()) { | ||
return; | ||
} | ||
|
||
std::wstring formattedProxyServer; | ||
|
||
// If there is only one colon, then it is most likely of form IP:PORT and we just need | ||
// to remove the port. If there are two colons, then it is most likely of form SCHEME:IP:PORT | ||
int colonCount = 0; | ||
for (const char& c : proxyServer) { | ||
if (c == ':') { | ||
colonCount++; | ||
} | ||
} | ||
|
||
if (colonCount == 1) { | ||
const auto colonPos = proxyServer.find_first_of(':'); | ||
auto portRemoved = proxyServer.substr(0, colonPos); | ||
formattedProxyServer = std::wstring(portRemoved.begin(), portRemoved.end()); | ||
} else if (colonCount == 2) { | ||
const auto firstColonPos = proxyServer.find_first_of(':'); | ||
auto schemeRemoved = proxyServer.substr(firstColonPos + 3); | ||
|
||
const auto lastColonPos = schemeRemoved.find_last_of(':'); | ||
auto portAndSchemeRemoved = schemeRemoved.substr(0, lastColonPos); | ||
|
||
formattedProxyServer = std::wstring(portAndSchemeRemoved.begin(), | ||
portAndSchemeRemoved.end()); | ||
} else { | ||
formattedProxyServer = std::wstring(proxyServer.begin(), | ||
proxyServer.end()); | ||
} | ||
|
||
// get proxy credentials from credential manager | ||
PCREDENTIALW cred; | ||
if (!::CredReadW(formattedProxyServer.c_str(), CRED_TYPE_GENERIC, 0, &cred)) { | ||
return; | ||
} | ||
wchar_t* proxyUser = cred->UserName; | ||
wchar_t* proxyPass = (wchar_t*)cred->CredentialBlob; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No c-style casts in modern code, use static_cast<wchar_t*> or reinterpret_cast<wchar_t*> |
||
|
||
// set proxy credentials | ||
if (proxyUser) { | ||
::InternetSetOptionW(m_hWinInetRequest, | ||
INTERNET_OPTION_PROXY_USERNAME, | ||
static_cast<void*>(proxyUser), | ||
static_cast<DWORD>(wcslen(proxyUser) + 1)); | ||
} | ||
|
||
if (proxyPass) { | ||
::InternetSetOptionW(m_hWinInetRequest, | ||
INTERNET_OPTION_PROXY_PASSWORD, | ||
static_cast<void*>(proxyPass), | ||
static_cast<DWORD>(wcslen(proxyPass) + 1)); | ||
} | ||
|
||
::CredFree(cred); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should either wrap PCREDENTIALW in a helper type whose destructor calls ::CredFree, or refactor 216-238 into a helper function which is marked There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The former is preferred as it's more resilient. struct CredHolder final
{
PCREDENTIALW cred;
~CredHolder() noexcept
{
::CredFree(cred);
}
}; |
||
} | ||
|
||
// Asynchronously send HTTP request and invoke response callback. | ||
// Ownership semantics: send(...) method self-destroys *this* upon | ||
// receiving WinInet callback. There must be absolutely no methods | ||
|
@@ -209,7 +311,7 @@ class WinInetRequestWrapper | |
PCSTR szAcceptTypes[] = {"*/*", NULL}; | ||
m_hWinInetRequest = ::HttpOpenRequestA( | ||
m_hWinInetSession, m_request->m_method.c_str(), path, NULL, NULL, szAcceptTypes, | ||
INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_AUTH | INTERNET_FLAG_NO_CACHE_WRITE | | ||
INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE | | ||
INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI | INTERNET_FLAG_PRAGMA_NOCACHE | | ||
INTERNET_FLAG_RELOAD | (urlc.nScheme == INTERNET_SCHEME_HTTPS ? INTERNET_FLAG_SECURE : 0), | ||
reinterpret_cast<DWORD_PTR>(this)); | ||
|
@@ -252,6 +354,10 @@ class WinInetRequestWrapper | |
return; | ||
} | ||
|
||
if (ProxyAuthRequired()) { | ||
SetProxyCredentials(); | ||
} | ||
|
||
// Try to send headers and request body to server | ||
DispatchEvent(OnSending); | ||
void *data = static_cast<void *>(m_request->m_body.data()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
#include "ILogger.hpp" | ||
|
||
#include <string> | ||
#include <unordered_set> | ||
#include <vector> | ||
#include <map> | ||
|
||
|
@@ -51,6 +52,10 @@ namespace Microsoft { | |
|
||
// [optional] enabled ECS telemetry | ||
bool enableECSClientTelemetry = false; | ||
|
||
// [optional] Mandatory agents list. If not present in response, the payload | ||
// is determined as bad | ||
std::unordered_set<std::string> mandatoryAgents; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you miss adding any file, as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be used in the EXPCommonClient.cpp file in this PR - Pull 230 - Contributing back patches to 1DS modules. I think this PR will have to be merged first before we merged the private one. |
||
}; | ||
|
||
/// <summary> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't create namespace scoped STL containers as these cause heap allocations which are never released until the containing binary is unloaded, which violates Pay For Play.
static const char* kProxyRegKeyPath = ...