[vcpkg-metrics] Replace SQM with MAC hash.
MODIFY
include/vcpkg/metrics.h
include/vcpkg/metrics.h
@@ -11,7 +11,7 @@ namespace vcpkg::Metrics
void set_send_metrics(bool should_send_metrics);
void set_print_metrics(bool should_print_metrics);
void set_user_information(const std::string& user_id, const std::string& first_use_time);
- void init_user_information(std::string& user_id, std::string& first_use_time);
+ static void init_user_information(std::string& user_id, std::string& first_use_time);
void track_metric(const std::string& name, double value);
void track_property(const std::string& name, const std::string& value);
@@ -22,6 +22,6 @@ namespace vcpkg::Metrics
extern Util::LockGuarded<Metrics> g_metrics;
- std::string get_SQM_user();
+ std::string get_MAC_user();
bool get_compiled_metrics_enabled();
}

MODIFY
src/vcpkg.cpp
src/vcpkg.cpp
@@ -157,6 +157,7 @@ static void load_config()
CoTaskMemFree(localappdatapath);
}
+ std::string user_id, user_time, user_mac;
try
{
auto maybe_pghs = Paragraphs::get_paragraphs(Files::get_real_filesystem(), localappdata / "vcpkg" / "config");
@@ -173,26 +174,38 @@ static void load_config()
keys.insert(p);
}
- auto user_id = keys["User-Id"];
- auto user_time = keys["User-Since"];
- if (!user_id.empty() && !user_time.empty())
- {
- Metrics::g_metrics.lock()->set_user_information(user_id, user_time);
- return;
- }
+ user_id = keys["User-Id"];
+ user_time = keys["User-Since"];
+ user_mac = keys["Mac-Hash"];
}
}
catch (...)
{
}
+ bool write_config = false;
+
// config file not found, could not be read, or invalid
- std::string user_id, user_time;
+ if (user_id.empty() || user_time.empty())
+ {
+ ::vcpkg::Metrics::Metrics::init_user_information(user_id, user_time);
+ write_config = true;
+ }
+
+ if (user_mac.empty())
+ {
+ user_mac = Metrics::get_MAC_user();
+ write_config = true;
+ }
+
{
auto locked_metrics = Metrics::g_metrics.lock();
- locked_metrics->init_user_information(user_id, user_time);
locked_metrics->set_user_information(user_id, user_time);
+ locked_metrics->track_property("user_mac", user_mac);
}
+
+ if (write_config)
+ {
try
{
std::error_code ec;
@@ -200,13 +213,16 @@ static void load_config()
fs.create_directory(localappdata / "vcpkg", ec);
fs.write_contents(localappdata / "vcpkg" / "config",
Strings::format("User-Id: %s\n"
- "User-Since: %s\n",
+ "User-Since: %s\n"
+ "Mac-Hash: %s\n",
user_id,
- user_time));
+ user_time,
+ user_mac));
}
catch (...)
{
}
+ }
#endif
}
@@ -259,7 +275,6 @@ int main(const int argc, const char* const* const argv)
#endif
}
load_config();
- Metrics::g_metrics.lock()->track_property("sqmuser", Metrics::get_SQM_user());
const VcpkgCmdArguments args = VcpkgCmdArguments::create_from_command_line(argc, argv);

MODIFY
src/vcpkg/metrics.cpp
src/vcpkg/metrics.cpp
@@ -233,12 +233,40 @@ namespace vcpkg::Metrics
bool get_compiled_metrics_enabled() { return DISABLE_METRICS == 0; }
- std::string get_SQM_user()
+ static fs::path get_vcpkg_root()
+ {
+ return Files::get_real_filesystem().find_file_recursively_up(
+ fs::stdfs::absolute(System::get_exe_path_of_current_process()), ".vcpkg-root");
+ }
+
+ std::string get_MAC_user()
{
#if defined(_WIN32)
- auto hkcu_sqmclient =
- System::get_registry_string(HKEY_CURRENT_USER, R"(Software\Microsoft\SQMClient)", "UserId");
- return hkcu_sqmclient.value_or("{}");
+ auto getmac = System::cmd_execute_and_capture_output("getmac");
+
+ if (getmac.exit_code != 0) return "0";
+
+ std::regex mac_regex("([a-fA-F0-9]{2}(-[a-fA-F0-9]{2}){5})");
+ std::sregex_iterator next(getmac.output.begin(), getmac.output.end(), mac_regex);
+ std::sregex_iterator last;
+
+ while (next != last)
+ {
+ auto match = *next;
+ if (match[0] != "00-00-00-00-00-00")
+ {
+ std::string matchstr = match[0];
+ auto hash_result = System::powershell_execute_and_capture_output(
+ "SHA256Hash", get_vcpkg_root() / "SHA256Hash.ps1", Strings::format("-Value \"%s\"", matchstr));
+ Util::erase_remove_if(hash_result,
+ [](char ch) { return !(ch >= 'A' && ch <= 'F') && !(ch >= '0' && ch <= '9'); });
+ hash_result = Strings::ascii_to_lowercase(hash_result);
+ return hash_result;
+ }
+ ++next;
+ }
+
+ return "0";
#else
return "{}";
#endif

[vcpkg-metrics] Replace SQM with MAC hash.