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/common/web_result.h | 2 +- src/core/telemetry_session.cpp | 11 ++++++----- src/core/telemetry_session.h | 2 +- src/web_service/telemetry_json.cpp | 5 +++++ src/web_service/telemetry_json.h | 5 ++--- src/web_service/web_backend.cpp | 8 ++++---- src/yuzu/compatdb.cpp | 6 +++++- src/yuzu/compatdb.h | 1 - src/yuzu/configuration/configure_web.cpp | 2 +- src/yuzu/discord_impl.h | 2 +- src/yuzu/main.cpp | 2 +- 11 files changed, 27 insertions(+), 19 deletions(-) (limited to 'src/core') diff --git a/src/common/web_result.h b/src/common/web_result.h index 13610a7ea..969926674 100644 --- a/src/common/web_result.h +++ b/src/common/web_result.h @@ -21,4 +21,4 @@ struct WebResult { std::string result_string; std::string returned_data; }; -} // namespace Commo \ No newline at end of file +} // namespace Common 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 diff --git a/src/web_service/telemetry_json.cpp b/src/web_service/telemetry_json.cpp index a0b7f9c4e..033ea1ea4 100644 --- a/src/web_service/telemetry_json.cpp +++ b/src/web_service/telemetry_json.cpp @@ -10,6 +10,11 @@ namespace WebService { +TelemetryJson::TelemetryJson(const std::string& host, const std::string& username, + const std::string& token) + : host(std::move(host)), username(std::move(username)), token(std::move(token)) {} +TelemetryJson::~TelemetryJson() = default; + template void TelemetryJson::Serialize(Telemetry::FieldType type, const std::string& name, T value) { sections[static_cast(type)][name] = value; diff --git a/src/web_service/telemetry_json.h b/src/web_service/telemetry_json.h index 9bc886538..29d565964 100644 --- a/src/web_service/telemetry_json.h +++ b/src/web_service/telemetry_json.h @@ -18,9 +18,8 @@ namespace WebService { */ class TelemetryJson : public Telemetry::VisitorInterface { public: - TelemetryJson(const std::string& host, const std::string& username, const std::string& token) - : host(host), username(username), token(token) {} - ~TelemetryJson() = default; + TelemetryJson(const std::string& host, const std::string& username, const std::string& token); + ~TelemetryJson(); void Visit(const Telemetry::Field& field) override; void Visit(const Telemetry::Field& field) override; diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index a726fb8eb..3a3f44dc2 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp @@ -13,12 +13,12 @@ namespace WebService { -static constexpr char API_VERSION[]{"1"}; +constexpr char API_VERSION[]{"1"}; -constexpr int HTTP_PORT = 80; -constexpr int HTTPS_PORT = 443; +constexpr u32 HTTP_PORT = 80; +constexpr u32 HTTPS_PORT = 443; -constexpr int TIMEOUT_SECONDS = 30; +constexpr u32 TIMEOUT_SECONDS = 30; Client::JWTCache Client::jwt_cache{}; diff --git a/src/yuzu/compatdb.cpp b/src/yuzu/compatdb.cpp index 45f8b4461..91e754274 100644 --- a/src/yuzu/compatdb.cpp +++ b/src/yuzu/compatdb.cpp @@ -27,7 +27,11 @@ CompatDB::CompatDB(QWidget* parent) CompatDB::~CompatDB() = default; -enum class CompatDBPage { Intro = 0, Selection = 1, Final = 2 }; +enum class CompatDBPage { + Intro = 0, + Selection = 1, + Final = 2, +}; void CompatDB::Submit() { QButtonGroup* compatibility = new QButtonGroup(this); diff --git a/src/yuzu/compatdb.h b/src/yuzu/compatdb.h index 0a0f27cca..ca0dd11d6 100644 --- a/src/yuzu/compatdb.h +++ b/src/yuzu/compatdb.h @@ -21,7 +21,6 @@ public: private: std::unique_ptr ui; -private slots: void Submit(); void EnableNext(); }; diff --git a/src/yuzu/configuration/configure_web.cpp b/src/yuzu/configuration/configure_web.cpp index cfca08014..4b5c39e26 100644 --- a/src/yuzu/configuration/configure_web.cpp +++ b/src/yuzu/configuration/configure_web.cpp @@ -25,7 +25,7 @@ ConfigureWeb::ConfigureWeb(QWidget* parent) this->setConfiguration(); } -ConfigureWeb::~ConfigureWeb() {} +ConfigureWeb::~ConfigureWeb() = default; void ConfigureWeb::setConfiguration() { ui->web_credentials_disclaimer->setWordWrap(true); diff --git a/src/yuzu/discord_impl.h b/src/yuzu/discord_impl.h index d71428c10..4bfda8cdf 100644 --- a/src/yuzu/discord_impl.h +++ b/src/yuzu/discord_impl.h @@ -11,7 +11,7 @@ namespace DiscordRPC { class DiscordImpl : public DiscordInterface { public: DiscordImpl(); - ~DiscordImpl(); + ~DiscordImpl() override; void Pause() override; void Update() override; diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 2d6e0d4fc..f236c63c5 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -115,7 +115,7 @@ void GMainWindow::ShowTelemetryCallout() { } UISettings::values.callout_flags |= static_cast(CalloutFlag::Telemetry); - static const QString telemetry_message = + const QString telemetry_message = tr("Anonymous " "data is collected to help improve yuzu. " "

Would you like to share your usage data with us?"); -- 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 --- externals/CMakeLists.txt | 8 ++++---- src/core/telemetry_session.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/core') diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index c6b175147..6a573881d 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -73,7 +73,7 @@ endif() # DiscordRPC if (USE_DISCORD_PRESENCE) - add_subdirectory(discord-rpc) + add_subdirectory(discord-rpc EXCLUDE_FROM_ALL) target_include_directories(discord-rpc INTERFACE ./discord-rpc/include) endif() @@ -81,11 +81,11 @@ if (ENABLE_WEB_SERVICE) # LibreSSL set(LIBRESSL_SKIP_INSTALL ON CACHE BOOL "") add_definitions(-DHAVE_INET_NTOP) - add_subdirectory(libressl) + add_subdirectory(libressl EXCLUDE_FROM_ALL) target_include_directories(ssl INTERFACE ./libressl/include) # lurlparser - add_subdirectory(lurlparser) + add_subdirectory(lurlparser EXCLUDE_FROM_ALL) # httplib add_library(httplib INTERFACE) @@ -94,4 +94,4 @@ if (ENABLE_WEB_SERVICE) # JSON add_library(json-headers INTERFACE) target_include_directories(json-headers INTERFACE ./json) -endif() \ No newline at end of file +endif() 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 --- CMakeLists.txt | 7 ------- src/core/CMakeLists.txt | 1 + src/web_service/web_backend.cpp | 4 ++-- src/yuzu/CMakeLists.txt | 4 ++++ 4 files changed, 7 insertions(+), 9 deletions(-) (limited to 'src/core') diff --git a/CMakeLists.txt b/CMakeLists.txt index 047443704..5e94e57ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -325,13 +325,6 @@ if (ENABLE_QT) find_package(Qt5 REQUIRED COMPONENTS Widgets OpenGL ${QT_PREFIX_HINT}) endif() -if (ENABLE_WEB_SERVICE) - add_definitions(-DENABLE_WEB_SERVICE) -endif() -if (YUZU_ENABLE_COMPATIBILITY_REPORTING) - add_definitions(-DYUZU_ENABLE_COMPATIBILITY_REPORTING) -endif() - # Platform-specific library requirements # ====================================== 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() diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index 5df4df5eb..787b0fbcb 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp @@ -13,7 +13,7 @@ namespace WebService { -constexpr char API_VERSION[]{"1"}; +constexpr std::array API_VERSION{'1'}; constexpr u32 HTTP_PORT = 80; constexpr u32 HTTPS_PORT = 443; @@ -70,7 +70,7 @@ Common::WebResult Client::GenericJson(const std::string& method, const std::stri }; } - params.emplace(std::string("api-version"), std::string(API_VERSION)); + params.emplace(std::string("api-version"), std::string(API_VERSION.begin(), API_VERSION.end())); if (method != "GET") { params.emplace(std::string("Content-Type"), std::string("application/json")); }; diff --git a/src/yuzu/CMakeLists.txt b/src/yuzu/CMakeLists.txt index f93ba2569..04464ad5e 100644 --- a/src/yuzu/CMakeLists.txt +++ b/src/yuzu/CMakeLists.txt @@ -120,6 +120,10 @@ target_link_libraries(yuzu PRIVATE common core input_common video_core) target_link_libraries(yuzu PRIVATE Boost::boost glad Qt5::OpenGL Qt5::Widgets) target_link_libraries(yuzu PRIVATE ${PLATFORM_LIBRARIES} Threads::Threads) +if (YUZU_ENABLE_COMPATIBILITY_REPORTING) + add_definitions(-DYUZU_ENABLE_COMPATIBILITY_REPORTING) +endif() + if (USE_DISCORD_PRESENCE) target_sources(yuzu PUBLIC discord_impl.cpp -- cgit v1.2.3