diff options
| author | 2018-09-16 20:05:51 +0200 | |
|---|---|---|
| committer | 2018-10-02 15:30:48 +0200 | |
| commit | 4d139943f2407144d5f8e3dc5a673f24850d43d0 (patch) | |
| tree | be24285a32c2b72b9756b69fd614f3d45c70ff41 /src/core/telemetry_session.cpp | |
| parent | Add submodules (diff) | |
| download | yuzu-4d139943f2407144d5f8e3dc5a673f24850d43d0.tar.gz yuzu-4d139943f2407144d5f8e3dc5a673f24850d43d0.tar.xz yuzu-4d139943f2407144d5f8e3dc5a673f24850d43d0.zip | |
Port web_service from Citra
Diffstat (limited to 'src/core/telemetry_session.cpp')
| -rw-r--r-- | src/core/telemetry_session.cpp | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp index b0df154ca..09c85297a 100644 --- a/src/core/telemetry_session.cpp +++ b/src/core/telemetry_session.cpp | |||
| @@ -6,6 +6,8 @@ | |||
| 6 | #include "common/common_types.h" | 6 | #include "common/common_types.h" |
| 7 | #include "common/file_util.h" | 7 | #include "common/file_util.h" |
| 8 | 8 | ||
| 9 | #include <mbedtls/ctr_drbg.h> | ||
| 10 | #include <mbedtls/entropy.h> | ||
| 9 | #include "core/core.h" | 11 | #include "core/core.h" |
| 10 | #include "core/file_sys/control_metadata.h" | 12 | #include "core/file_sys/control_metadata.h" |
| 11 | #include "core/file_sys/patch_manager.h" | 13 | #include "core/file_sys/patch_manager.h" |
| @@ -13,10 +15,30 @@ | |||
| 13 | #include "core/settings.h" | 15 | #include "core/settings.h" |
| 14 | #include "core/telemetry_session.h" | 16 | #include "core/telemetry_session.h" |
| 15 | 17 | ||
| 18 | #ifdef ENABLE_WEB_SERVICE | ||
| 19 | #include "web_service/telemetry_json.h" | ||
| 20 | #include "web_service/verify_login.h" | ||
| 21 | #endif | ||
| 22 | |||
| 16 | namespace Core { | 23 | namespace Core { |
| 17 | 24 | ||
| 18 | static u64 GenerateTelemetryId() { | 25 | static u64 GenerateTelemetryId() { |
| 19 | u64 telemetry_id{}; | 26 | u64 telemetry_id{}; |
| 27 | |||
| 28 | mbedtls_entropy_context entropy; | ||
| 29 | mbedtls_entropy_init(&entropy); | ||
| 30 | mbedtls_ctr_drbg_context ctr_drbg; | ||
| 31 | const char* personalization = "yuzu Telemetry ID"; | ||
| 32 | |||
| 33 | mbedtls_ctr_drbg_init(&ctr_drbg); | ||
| 34 | mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, | ||
| 35 | (const unsigned char*)personalization, strlen(personalization)); | ||
| 36 | ASSERT(mbedtls_ctr_drbg_random(&ctr_drbg, reinterpret_cast<unsigned char*>(&telemetry_id), | ||
| 37 | sizeof(u64)) == 0); | ||
| 38 | |||
| 39 | mbedtls_ctr_drbg_free(&ctr_drbg); | ||
| 40 | mbedtls_entropy_free(&entropy); | ||
| 41 | |||
| 20 | return telemetry_id; | 42 | return telemetry_id; |
| 21 | } | 43 | } |
| 22 | 44 | ||
| @@ -25,14 +47,21 @@ u64 GetTelemetryId() { | |||
| 25 | const std::string filename{FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + | 47 | const std::string filename{FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + |
| 26 | "telemetry_id"}; | 48 | "telemetry_id"}; |
| 27 | 49 | ||
| 28 | if (FileUtil::Exists(filename)) { | 50 | bool generate_new_id = !FileUtil::Exists(filename); |
| 51 | if (!generate_new_id) { | ||
| 29 | FileUtil::IOFile file(filename, "rb"); | 52 | FileUtil::IOFile file(filename, "rb"); |
| 30 | if (!file.IsOpen()) { | 53 | if (!file.IsOpen()) { |
| 31 | LOG_ERROR(Core, "failed to open telemetry_id: {}", filename); | 54 | LOG_ERROR(Core, "failed to open telemetry_id: {}", filename); |
| 32 | return {}; | 55 | return {}; |
| 33 | } | 56 | } |
| 34 | file.ReadBytes(&telemetry_id, sizeof(u64)); | 57 | file.ReadBytes(&telemetry_id, sizeof(u64)); |
| 35 | } else { | 58 | if (telemetry_id == 0) { |
| 59 | LOG_ERROR(Frontend, "telemetry_id is 0. Generating a new one.", telemetry_id); | ||
| 60 | generate_new_id = true; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | if (generate_new_id) { | ||
| 36 | FileUtil::IOFile file(filename, "wb"); | 65 | FileUtil::IOFile file(filename, "wb"); |
| 37 | if (!file.IsOpen()) { | 66 | if (!file.IsOpen()) { |
| 38 | LOG_ERROR(Core, "failed to open telemetry_id: {}", filename); | 67 | LOG_ERROR(Core, "failed to open telemetry_id: {}", filename); |
| @@ -59,23 +88,20 @@ u64 RegenerateTelemetryId() { | |||
| 59 | return new_telemetry_id; | 88 | return new_telemetry_id; |
| 60 | } | 89 | } |
| 61 | 90 | ||
| 62 | std::future<bool> VerifyLogin(std::string username, std::string token, std::function<void()> func) { | 91 | bool VerifyLogin(std::string username, std::string token) { |
| 63 | #ifdef ENABLE_WEB_SERVICE | 92 | #ifdef ENABLE_WEB_SERVICE |
| 64 | return WebService::VerifyLogin(username, token, Settings::values.verify_endpoint_url, func); | 93 | return WebService::VerifyLogin(Settings::values.web_api_url, username, token); |
| 65 | #else | 94 | #else |
| 66 | return std::async(std::launch::async, [func{std::move(func)}]() { | 95 | return false; |
| 67 | func(); | ||
| 68 | return false; | ||
| 69 | }); | ||
| 70 | #endif | 96 | #endif |
| 71 | } | 97 | } |
| 72 | 98 | ||
| 73 | TelemetrySession::TelemetrySession() { | 99 | TelemetrySession::TelemetrySession() { |
| 74 | #ifdef ENABLE_WEB_SERVICE | 100 | #ifdef ENABLE_WEB_SERVICE |
| 75 | if (Settings::values.enable_telemetry) { | 101 | if (Settings::values.enable_telemetry) { |
| 76 | backend = std::make_unique<WebService::TelemetryJson>( | 102 | backend = std::make_unique<WebService::TelemetryJson>(Settings::values.web_api_url, |
| 77 | Settings::values.telemetry_endpoint_url, Settings::values.yuzu_username, | 103 | Settings::values.yuzu_username, |
| 78 | Settings::values.yuzu_token); | 104 | Settings::values.yuzu_token); |
| 79 | } else { | 105 | } else { |
| 80 | backend = std::make_unique<Telemetry::NullVisitor>(); | 106 | backend = std::make_unique<Telemetry::NullVisitor>(); |
| 81 | } | 107 | } |
| @@ -94,7 +120,8 @@ TelemetrySession::TelemetrySession() { | |||
| 94 | u64 program_id{}; | 120 | u64 program_id{}; |
| 95 | const Loader::ResultStatus res{System::GetInstance().GetAppLoader().ReadProgramId(program_id)}; | 121 | const Loader::ResultStatus res{System::GetInstance().GetAppLoader().ReadProgramId(program_id)}; |
| 96 | if (res == Loader::ResultStatus::Success) { | 122 | if (res == Loader::ResultStatus::Success) { |
| 97 | AddField(Telemetry::FieldType::Session, "ProgramId", program_id); | 123 | std::string formatted_program_id{fmt::format("{:016X}", program_id)}; |
| 124 | AddField(Telemetry::FieldType::Session, "ProgramId", formatted_program_id); | ||
| 98 | 125 | ||
| 99 | std::string name; | 126 | std::string name; |
| 100 | System::GetInstance().GetAppLoader().ReadTitle(name); | 127 | System::GetInstance().GetAppLoader().ReadTitle(name); |