From 4d139943f2407144d5f8e3dc5a673f24850d43d0 Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Sun, 16 Sep 2018 20:05:51 +0200 Subject: Port web_service from Citra --- src/core/CMakeLists.txt | 3 +++ src/core/settings.h | 6 +++++ src/core/telemetry_session.cpp | 51 ++++++++++++++++++++++++++++++++---------- src/core/telemetry_session.h | 5 +++-- 4 files changed, 51 insertions(+), 14 deletions(-) (limited to 'src/core') diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 23fd6e920..95f8b5d4a 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -394,6 +394,9 @@ create_target_directory_groups(core) target_link_libraries(core PUBLIC common PRIVATE audio_core video_core) target_link_libraries(core PUBLIC Boost::boost PRIVATE fmt lz4_static mbedtls opus unicorn open_source_archives) +if (ENABLE_WEB_SERVICE) + target_link_libraries(core PUBLIC json-headers web_service) +endif() if (ARCHITECTURE_x86_64) target_sources(core PRIVATE diff --git a/src/core/settings.h b/src/core/settings.h index 0318d019c..1808f5937 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -155,6 +155,12 @@ struct Values { // Debugging bool use_gdbstub; u16 gdbstub_port; + + // WebService + bool enable_telemetry; + std::string web_api_url; + std::string yuzu_username; + std::string yuzu_token; } extern values; void Apply(); 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 @@ #include "common/common_types.h" #include "common/file_util.h" +#include +#include #include "core/core.h" #include "core/file_sys/control_metadata.h" #include "core/file_sys/patch_manager.h" @@ -13,10 +15,30 @@ #include "core/settings.h" #include "core/telemetry_session.h" +#ifdef ENABLE_WEB_SERVICE +#include "web_service/telemetry_json.h" +#include "web_service/verify_login.h" +#endif + namespace Core { static u64 GenerateTelemetryId() { u64 telemetry_id{}; + + mbedtls_entropy_context entropy; + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_context ctr_drbg; + const char* personalization = "yuzu Telemetry ID"; + + mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, + (const unsigned char*)personalization, strlen(personalization)); + ASSERT(mbedtls_ctr_drbg_random(&ctr_drbg, reinterpret_cast(&telemetry_id), + sizeof(u64)) == 0); + + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + return telemetry_id; } @@ -25,14 +47,21 @@ u64 GetTelemetryId() { const std::string filename{FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + "telemetry_id"}; - if (FileUtil::Exists(filename)) { + bool generate_new_id = !FileUtil::Exists(filename); + if (!generate_new_id) { FileUtil::IOFile file(filename, "rb"); if (!file.IsOpen()) { LOG_ERROR(Core, "failed to open telemetry_id: {}", filename); return {}; } file.ReadBytes(&telemetry_id, sizeof(u64)); - } else { + if (telemetry_id == 0) { + LOG_ERROR(Frontend, "telemetry_id is 0. Generating a new one.", telemetry_id); + generate_new_id = true; + } + } + + if (generate_new_id) { FileUtil::IOFile file(filename, "wb"); if (!file.IsOpen()) { LOG_ERROR(Core, "failed to open telemetry_id: {}", filename); @@ -59,23 +88,20 @@ u64 RegenerateTelemetryId() { return new_telemetry_id; } -std::future VerifyLogin(std::string username, std::string token, std::function func) { +bool VerifyLogin(std::string username, std::string token) { #ifdef ENABLE_WEB_SERVICE - return WebService::VerifyLogin(username, token, Settings::values.verify_endpoint_url, func); + return WebService::VerifyLogin(Settings::values.web_api_url, username, token); #else - return std::async(std::launch::async, [func{std::move(func)}]() { - func(); - return false; - }); + return false; #endif } TelemetrySession::TelemetrySession() { #ifdef ENABLE_WEB_SERVICE if (Settings::values.enable_telemetry) { - backend = std::make_unique( - Settings::values.telemetry_endpoint_url, Settings::values.yuzu_username, - Settings::values.yuzu_token); + backend = std::make_unique(Settings::values.web_api_url, + Settings::values.yuzu_username, + Settings::values.yuzu_token); } else { backend = std::make_unique(); } @@ -94,7 +120,8 @@ TelemetrySession::TelemetrySession() { u64 program_id{}; const Loader::ResultStatus res{System::GetInstance().GetAppLoader().ReadProgramId(program_id)}; if (res == Loader::ResultStatus::Success) { - AddField(Telemetry::FieldType::Session, "ProgramId", program_id); + std::string formatted_program_id{fmt::format("{:016X}", program_id)}; + AddField(Telemetry::FieldType::Session, "ProgramId", formatted_program_id); std::string name; System::GetInstance().GetAppLoader().ReadTitle(name); diff --git a/src/core/telemetry_session.h b/src/core/telemetry_session.h index dbc4f8bd4..e6976ad45 100644 --- a/src/core/telemetry_session.h +++ b/src/core/telemetry_session.h @@ -4,7 +4,6 @@ #pragma once -#include #include #include "common/telemetry.h" @@ -31,6 +30,8 @@ public: field_collection.AddField(type, name, std::move(value)); } + static void FinalizeAsyncJob(); + private: Telemetry::FieldCollection field_collection; ///< Tracks all added fields for the session std::unique_ptr backend; ///< Backend interface that logs fields @@ -55,6 +56,6 @@ u64 RegenerateTelemetryId(); * @param func A function that gets exectued when the verification is finished * @returns Future with bool indicating whether the verification succeeded */ -std::future VerifyLogin(std::string username, std::string token, std::function func); +bool VerifyLogin(std::string username, std::string token); } // namespace Core -- cgit v1.2.3 From b4ace6ec6f86079b3bd297f95dfe133240b53e15 Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Mon, 17 Sep 2018 17:16:01 +0200 Subject: Address a bunch of review comments --- src/core/telemetry_session.cpp | 11 ++++++----- src/core/telemetry_session.h | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'src/core') diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp index 09c85297a..c02188adc 100644 --- a/src/core/telemetry_session.cpp +++ b/src/core/telemetry_session.cpp @@ -28,11 +28,12 @@ static u64 GenerateTelemetryId() { mbedtls_entropy_context entropy; mbedtls_entropy_init(&entropy); mbedtls_ctr_drbg_context ctr_drbg; - const char* personalization = "yuzu Telemetry ID"; + std::string personalization = "yuzu Telemetry ID"; mbedtls_ctr_drbg_init(&ctr_drbg); - mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, - (const unsigned char*)personalization, strlen(personalization)); + ASSERT(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, + reinterpret_cast(personalization.c_str()), + personalization.size()) == 0) ASSERT(mbedtls_ctr_drbg_random(&ctr_drbg, reinterpret_cast(&telemetry_id), sizeof(u64)) == 0); @@ -88,7 +89,7 @@ u64 RegenerateTelemetryId() { return new_telemetry_id; } -bool VerifyLogin(std::string username, std::string token) { +bool VerifyLogin(const std::string& username, const std::string& token) { #ifdef ENABLE_WEB_SERVICE return WebService::VerifyLogin(Settings::values.web_api_url, username, token); #else @@ -120,7 +121,7 @@ TelemetrySession::TelemetrySession() { u64 program_id{}; const Loader::ResultStatus res{System::GetInstance().GetAppLoader().ReadProgramId(program_id)}; if (res == Loader::ResultStatus::Success) { - std::string formatted_program_id{fmt::format("{:016X}", program_id)}; + const std::string formatted_program_id{fmt::format("{:016X}", program_id)}; AddField(Telemetry::FieldType::Session, "ProgramId", formatted_program_id); std::string name; diff --git a/src/core/telemetry_session.h b/src/core/telemetry_session.h index e6976ad45..cec271df0 100644 --- a/src/core/telemetry_session.h +++ b/src/core/telemetry_session.h @@ -56,6 +56,6 @@ u64 RegenerateTelemetryId(); * @param func A function that gets exectued when the verification is finished * @returns Future with bool indicating whether the verification succeeded */ -bool VerifyLogin(std::string username, std::string token); +bool VerifyLogin(const std::string& username, const std::string& token); } // namespace Core -- cgit v1.2.3 From 120d8f3bf7951b5fbe84b18338381227cf1452f0 Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Mon, 17 Sep 2018 20:58:24 +0200 Subject: Address more review comments --- src/core/telemetry_session.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core') diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp index c02188adc..f29fff1e7 100644 --- a/src/core/telemetry_session.cpp +++ b/src/core/telemetry_session.cpp @@ -33,7 +33,7 @@ static u64 GenerateTelemetryId() { mbedtls_ctr_drbg_init(&ctr_drbg); ASSERT(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, reinterpret_cast(personalization.c_str()), - personalization.size()) == 0) + personalization.size()) == 0); ASSERT(mbedtls_ctr_drbg_random(&ctr_drbg, reinterpret_cast(&telemetry_id), sizeof(u64)) == 0); -- cgit v1.2.3 From ac06105dfe9c7e5306e1f4cac0ee12dc5f72f14e Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Sat, 29 Sep 2018 02:51:28 +0200 Subject: Review comments -part 4 --- src/core/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core') diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 95f8b5d4a..378d8128d 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -395,6 +395,7 @@ create_target_directory_groups(core) target_link_libraries(core PUBLIC common PRIVATE audio_core video_core) target_link_libraries(core PUBLIC Boost::boost PRIVATE fmt lz4_static mbedtls opus unicorn open_source_archives) if (ENABLE_WEB_SERVICE) + add_definitions(-DENABLE_WEB_SERVICE) target_link_libraries(core PUBLIC json-headers web_service) endif() -- cgit v1.2.3