diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/settings.h | 3 | ||||
-rw-r--r-- | src/core/telemetry_session.cpp | 57 | ||||
-rw-r--r-- | src/core/telemetry_session.h | 12 |
3 files changed, 71 insertions, 1 deletions
diff --git a/src/core/settings.h b/src/core/settings.h index ca657719a..bf8014c5a 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -130,7 +130,10 @@ struct Values { u16 gdbstub_port; // WebService + bool enable_telemetry; std::string telemetry_endpoint_url; + std::string citra_username; + std::string citra_token; } extern values; // a special value for Values::region_value indicating that citra will automatically select a region diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp index 94483f385..104a16cc9 100644 --- a/src/core/telemetry_session.cpp +++ b/src/core/telemetry_session.cpp @@ -3,8 +3,10 @@ // Refer to the license.txt file included. #include <cstring> +#include <cryptopp/osrng.h> #include "common/assert.h" +#include "common/file_util.h" #include "common/scm_rev.h" #include "common/x64/cpu_detect.h" #include "core/core.h" @@ -29,12 +31,65 @@ static const char* CpuVendorToStr(Common::CPUVendor vendor) { UNREACHABLE(); } +static u64 GenerateTelemetryId() { + u64 telemetry_id{}; + CryptoPP::AutoSeededRandomPool rng; + rng.GenerateBlock(reinterpret_cast<CryptoPP::byte*>(&telemetry_id), sizeof(u64)); + return telemetry_id; +} + +u64 GetTelemetryId() { + u64 telemetry_id{}; + static const std::string& filename{FileUtil::GetUserPath(D_CONFIG_IDX) + "telemetry_id"}; + + if (FileUtil::Exists(filename)) { + FileUtil::IOFile file(filename, "rb"); + if (!file.IsOpen()) { + LOG_ERROR(Core, "failed to open telemetry_id: %s", filename.c_str()); + return {}; + } + file.ReadBytes(&telemetry_id, sizeof(u64)); + } else { + FileUtil::IOFile file(filename, "wb"); + if (!file.IsOpen()) { + LOG_ERROR(Core, "failed to open telemetry_id: %s", filename.c_str()); + return {}; + } + telemetry_id = GenerateTelemetryId(); + file.WriteBytes(&telemetry_id, sizeof(u64)); + } + + return telemetry_id; +} + +u64 RegenerateTelemetryId() { + const u64 new_telemetry_id{GenerateTelemetryId()}; + static const std::string& filename{FileUtil::GetUserPath(D_CONFIG_IDX) + "telemetry_id"}; + + FileUtil::IOFile file(filename, "wb"); + if (!file.IsOpen()) { + LOG_ERROR(Core, "failed to open telemetry_id: %s", filename.c_str()); + return {}; + } + file.WriteBytes(&new_telemetry_id, sizeof(u64)); + return new_telemetry_id; +} + TelemetrySession::TelemetrySession() { #ifdef ENABLE_WEB_SERVICE - backend = std::make_unique<WebService::TelemetryJson>(); + if (Settings::values.enable_telemetry) { + backend = std::make_unique<WebService::TelemetryJson>( + Settings::values.telemetry_endpoint_url, Settings::values.citra_username, + Settings::values.citra_token); + } else { + backend = std::make_unique<Telemetry::NullVisitor>(); + } #else backend = std::make_unique<Telemetry::NullVisitor>(); #endif + // Log one-time top-level information + AddField(Telemetry::FieldType::None, "TelemetryId", GetTelemetryId()); + // Log one-time session start information const s64 init_time{std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now().time_since_epoch()) diff --git a/src/core/telemetry_session.h b/src/core/telemetry_session.h index cf53835c3..65613daae 100644 --- a/src/core/telemetry_session.h +++ b/src/core/telemetry_session.h @@ -35,4 +35,16 @@ private: std::unique_ptr<Telemetry::VisitorInterface> backend; ///< Backend interface that logs fields }; +/** + * Gets TelemetryId, a unique identifier used for the user's telemetry sessions. + * @returns The current TelemetryId for the session. + */ +u64 GetTelemetryId(); + +/** + * Regenerates TelemetryId, a unique identifier used for the user's telemetry sessions. + * @returns The new TelemetryId that was generated. + */ +u64 RegenerateTelemetryId(); + } // namespace Core |