aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Schumacher <roschuma@microsoft.com>2017-11-13 21:36:29 -0800
committerRobert Schumacher <roschuma@microsoft.com>2017-11-13 21:49:11 -0800
commit415789b42e5fd43ac984902cd72848f6ef6b450a (patch)
tree6262849933cc31b67f0081897fbcad2f800812bf
parent1ea060d6fa9925dd9146f71b8c73a04bfcc5caf4 (diff)
downloadvcpkg-415789b42e5fd43ac984902cd72848f6ef6b450a.tar.gz
vcpkg-415789b42e5fd43ac984902cd72848f6ef6b450a.zip
[vcpkg-metrics] Replace SQM with MAC hash.
-rw-r--r--scripts/SHA256Hash.ps19
-rw-r--r--toolsrc/include/vcpkg/metrics.h4
-rw-r--r--toolsrc/src/vcpkg.cpp59
-rw-r--r--toolsrc/src/vcpkg/metrics.cpp36
4 files changed, 80 insertions, 28 deletions
diff --git a/scripts/SHA256Hash.ps1 b/scripts/SHA256Hash.ps1
new file mode 100644
index 000000000..348d461b7
--- /dev/null
+++ b/scripts/SHA256Hash.ps1
@@ -0,0 +1,9 @@
+[CmdletBinding()]
+Param(
+ [Parameter(Mandatory=$True)]
+ [String]$Value
+)
+
+$sha256 = New-Object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider
+$utf8 = New-Object -TypeName System.Text.UTF8Encoding
+[System.BitConverter]::ToString($sha256.ComputeHash($utf8.GetBytes($Value)))
diff --git a/toolsrc/include/vcpkg/metrics.h b/toolsrc/include/vcpkg/metrics.h
index f73c636cf..ee6f8eb51 100644
--- a/toolsrc/include/vcpkg/metrics.h
+++ b/toolsrc/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();
}
diff --git a/toolsrc/src/vcpkg.cpp b/toolsrc/src/vcpkg.cpp
index 2a73c5b0f..5642f937c 100644
--- a/toolsrc/src/vcpkg.cpp
+++ b/toolsrc/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,39 +174,54 @@ 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())
{
- 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);
+ ::vcpkg::Metrics::Metrics::init_user_information(user_id, user_time);
+ write_config = true;
}
- try
+
+ if (user_mac.empty())
{
- std::error_code ec;
- auto& fs = Files::get_real_filesystem();
- fs.create_directory(localappdata / "vcpkg", ec);
- fs.write_contents(localappdata / "vcpkg" / "config",
- Strings::format("User-Id: %s\n"
- "User-Since: %s\n",
- user_id,
- user_time));
+ user_mac = Metrics::get_MAC_user();
+ write_config = true;
}
- catch (...)
+
{
+ auto locked_metrics = Metrics::g_metrics.lock();
+ 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;
+ auto& fs = Files::get_real_filesystem();
+ fs.create_directory(localappdata / "vcpkg", ec);
+ fs.write_contents(localappdata / "vcpkg" / "config",
+ Strings::format("User-Id: %s\n"
+ "User-Since: %s\n"
+ "Mac-Hash: %s\n",
+ user_id,
+ 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);
diff --git a/toolsrc/src/vcpkg/metrics.cpp b/toolsrc/src/vcpkg/metrics.cpp
index 8f2575886..d7e18a176 100644
--- a/toolsrc/src/vcpkg/metrics.cpp
+++ b/toolsrc/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