diff options
| author | 2017-08-26 20:15:15 -0400 | |
|---|---|---|
| committer | 2017-08-26 20:15:15 -0400 | |
| commit | 22fc378fe9f3314b08d81ffaaf57fd8688e9e3cc (patch) | |
| tree | d263eaca71ee08c3f9b81441b42bcd0e35977510 /src/web_service | |
| parent | SidebySide Layout (#2859) (diff) | |
| parent | web_backend: Fix CPR bug where Winsock is not properly initializing. (diff) | |
| download | yuzu-22fc378fe9f3314b08d81ffaaf57fd8688e9e3cc.tar.gz yuzu-22fc378fe9f3314b08d81ffaaf57fd8688e9e3cc.tar.xz yuzu-22fc378fe9f3314b08d81ffaaf57fd8688e9e3cc.zip | |
Merge pull request #2897 from bunnei/telemetry-ui
Telemetry UI and final touches
Diffstat (limited to '')
| -rw-r--r-- | src/web_service/telemetry_json.cpp | 3 | ||||
| -rw-r--r-- | src/web_service/telemetry_json.h | 7 | ||||
| -rw-r--r-- | src/web_service/web_backend.cpp | 67 | ||||
| -rw-r--r-- | src/web_service/web_backend.h | 18 |
4 files changed, 51 insertions, 44 deletions
diff --git a/src/web_service/telemetry_json.cpp b/src/web_service/telemetry_json.cpp index a2d007e77..6ad2ffcd4 100644 --- a/src/web_service/telemetry_json.cpp +++ b/src/web_service/telemetry_json.cpp | |||
| @@ -3,7 +3,6 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include "common/assert.h" | 5 | #include "common/assert.h" |
| 6 | #include "core/settings.h" | ||
| 7 | #include "web_service/telemetry_json.h" | 6 | #include "web_service/telemetry_json.h" |
| 8 | #include "web_service/web_backend.h" | 7 | #include "web_service/web_backend.h" |
| 9 | 8 | ||
| @@ -81,7 +80,7 @@ void TelemetryJson::Complete() { | |||
| 81 | SerializeSection(Telemetry::FieldType::UserFeedback, "UserFeedback"); | 80 | SerializeSection(Telemetry::FieldType::UserFeedback, "UserFeedback"); |
| 82 | SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig"); | 81 | SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig"); |
| 83 | SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem"); | 82 | SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem"); |
| 84 | PostJson(Settings::values.telemetry_endpoint_url, TopSection().dump()); | 83 | PostJson(endpoint_url, TopSection().dump(), true, username, token); |
| 85 | } | 84 | } |
| 86 | 85 | ||
| 87 | } // namespace WebService | 86 | } // namespace WebService |
diff --git a/src/web_service/telemetry_json.h b/src/web_service/telemetry_json.h index 39038b4f9..9e78c6803 100644 --- a/src/web_service/telemetry_json.h +++ b/src/web_service/telemetry_json.h | |||
| @@ -17,7 +17,9 @@ namespace WebService { | |||
| 17 | */ | 17 | */ |
| 18 | class TelemetryJson : public Telemetry::VisitorInterface { | 18 | class TelemetryJson : public Telemetry::VisitorInterface { |
| 19 | public: | 19 | public: |
| 20 | TelemetryJson() = default; | 20 | TelemetryJson(const std::string& endpoint_url, const std::string& username, |
| 21 | const std::string& token) | ||
| 22 | : endpoint_url(endpoint_url), username(username), token(token) {} | ||
| 21 | ~TelemetryJson() = default; | 23 | ~TelemetryJson() = default; |
| 22 | 24 | ||
| 23 | void Visit(const Telemetry::Field<bool>& field) override; | 25 | void Visit(const Telemetry::Field<bool>& field) override; |
| @@ -49,6 +51,9 @@ private: | |||
| 49 | 51 | ||
| 50 | nlohmann::json output; | 52 | nlohmann::json output; |
| 51 | std::array<nlohmann::json, 7> sections; | 53 | std::array<nlohmann::json, 7> sections; |
| 54 | std::string endpoint_url; | ||
| 55 | std::string username; | ||
| 56 | std::string token; | ||
| 52 | }; | 57 | }; |
| 53 | 58 | ||
| 54 | } // namespace WebService | 59 | } // namespace WebService |
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index 13e4555ac..d28a3f757 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp | |||
| @@ -2,51 +2,62 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #ifdef _WIN32 | ||
| 6 | #include <winsock.h> | ||
| 7 | #endif | ||
| 8 | |||
| 9 | #include <cstdlib> | ||
| 10 | #include <thread> | ||
| 5 | #include <cpr/cpr.h> | 11 | #include <cpr/cpr.h> |
| 6 | #include <stdlib.h> | ||
| 7 | #include "common/logging/log.h" | 12 | #include "common/logging/log.h" |
| 8 | #include "web_service/web_backend.h" | 13 | #include "web_service/web_backend.h" |
| 9 | 14 | ||
| 10 | namespace WebService { | 15 | namespace WebService { |
| 11 | 16 | ||
| 12 | static constexpr char API_VERSION[]{"1"}; | 17 | static constexpr char API_VERSION[]{"1"}; |
| 13 | static constexpr char ENV_VAR_USERNAME[]{"CITRA_WEB_SERVICES_USERNAME"}; | ||
| 14 | static constexpr char ENV_VAR_TOKEN[]{"CITRA_WEB_SERVICES_TOKEN"}; | ||
| 15 | |||
| 16 | static std::string GetEnvironmentVariable(const char* name) { | ||
| 17 | const char* value{getenv(name)}; | ||
| 18 | if (value) { | ||
| 19 | return value; | ||
| 20 | } | ||
| 21 | return {}; | ||
| 22 | } | ||
| 23 | |||
| 24 | const std::string& GetUsername() { | ||
| 25 | static const std::string username{GetEnvironmentVariable(ENV_VAR_USERNAME)}; | ||
| 26 | return username; | ||
| 27 | } | ||
| 28 | 18 | ||
| 29 | const std::string& GetToken() { | 19 | static std::unique_ptr<cpr::Session> g_session; |
| 30 | static const std::string token{GetEnvironmentVariable(ENV_VAR_TOKEN)}; | ||
| 31 | return token; | ||
| 32 | } | ||
| 33 | 20 | ||
| 34 | void PostJson(const std::string& url, const std::string& data) { | 21 | void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, |
| 22 | const std::string& username, const std::string& token) { | ||
| 35 | if (url.empty()) { | 23 | if (url.empty()) { |
| 36 | LOG_ERROR(WebService, "URL is invalid"); | 24 | LOG_ERROR(WebService, "URL is invalid"); |
| 37 | return; | 25 | return; |
| 38 | } | 26 | } |
| 39 | 27 | ||
| 40 | if (GetUsername().empty() || GetToken().empty()) { | 28 | const bool are_credentials_provided{!token.empty() && !username.empty()}; |
| 41 | LOG_ERROR(WebService, "Environment variables %s and %s must be set to POST JSON", | 29 | if (!allow_anonymous && !are_credentials_provided) { |
| 42 | ENV_VAR_USERNAME, ENV_VAR_TOKEN); | 30 | LOG_ERROR(WebService, "Credentials must be provided for authenticated requests"); |
| 43 | return; | 31 | return; |
| 44 | } | 32 | } |
| 45 | 33 | ||
| 46 | cpr::PostAsync(cpr::Url{url}, cpr::Body{data}, cpr::Header{{"Content-Type", "application/json"}, | 34 | #ifdef _WIN32 |
| 47 | {"x-username", GetUsername()}, | 35 | // On Windows, CPR/libcurl does not properly initialize Winsock. The below code is used to |
| 48 | {"x-token", GetToken()}, | 36 | // initialize Winsock globally, which fixes this problem. Without this, only the first CPR |
| 49 | {"api-version", API_VERSION}}); | 37 | // session will properly be created, and subsequent ones will fail. |
| 38 | WSADATA wsa_data; | ||
| 39 | const int wsa_result{WSAStartup(MAKEWORD(2, 2), &wsa_data)}; | ||
| 40 | if (wsa_result) { | ||
| 41 | LOG_CRITICAL(WebService, "WSAStartup failed: %d", wsa_result); | ||
| 42 | } | ||
| 43 | #endif | ||
| 44 | |||
| 45 | // Built request header | ||
| 46 | cpr::Header header; | ||
| 47 | if (are_credentials_provided) { | ||
| 48 | // Authenticated request if credentials are provided | ||
| 49 | header = {{"Content-Type", "application/json"}, | ||
| 50 | {"x-username", username.c_str()}, | ||
| 51 | {"x-token", token.c_str()}, | ||
| 52 | {"api-version", API_VERSION}}; | ||
| 53 | } else { | ||
| 54 | // Otherwise, anonymous request | ||
| 55 | header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}}; | ||
| 56 | } | ||
| 57 | |||
| 58 | // Post JSON asynchronously | ||
| 59 | static cpr::AsyncResponse future; | ||
| 60 | future = cpr::PostAsync(cpr::Url{url.c_str()}, cpr::Body{data.c_str()}, header); | ||
| 50 | } | 61 | } |
| 51 | 62 | ||
| 52 | } // namespace WebService | 63 | } // namespace WebService |
diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h index 2753d3b68..d17100398 100644 --- a/src/web_service/web_backend.h +++ b/src/web_service/web_backend.h | |||
| @@ -10,22 +10,14 @@ | |||
| 10 | namespace WebService { | 10 | namespace WebService { |
| 11 | 11 | ||
| 12 | /** | 12 | /** |
| 13 | * Gets the current username for accessing services.citra-emu.org. | ||
| 14 | * @returns Username as a string, empty if not set. | ||
| 15 | */ | ||
| 16 | const std::string& GetUsername(); | ||
| 17 | |||
| 18 | /** | ||
| 19 | * Gets the current token for accessing services.citra-emu.org. | ||
| 20 | * @returns Token as a string, empty if not set. | ||
| 21 | */ | ||
| 22 | const std::string& GetToken(); | ||
| 23 | |||
| 24 | /** | ||
| 25 | * Posts JSON to services.citra-emu.org. | 13 | * Posts JSON to services.citra-emu.org. |
| 26 | * @param url URL of the services.citra-emu.org endpoint to post data to. | 14 | * @param url URL of the services.citra-emu.org endpoint to post data to. |
| 27 | * @param data String of JSON data to use for the body of the POST request. | 15 | * @param data String of JSON data to use for the body of the POST request. |
| 16 | * @param allow_anonymous If true, allow anonymous unauthenticated requests. | ||
| 17 | * @param username Citra username to use for authentication. | ||
| 18 | * @param token Citra token to use for authentication. | ||
| 28 | */ | 19 | */ |
| 29 | void PostJson(const std::string& url, const std::string& data); | 20 | void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, |
| 21 | const std::string& username = {}, const std::string& token = {}); | ||
| 30 | 22 | ||
| 31 | } // namespace WebService | 23 | } // namespace WebService |