diff options
| -rw-r--r-- | src/core/telemetry_session.cpp | 8 | ||||
| -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 | 31 | ||||
| -rw-r--r-- | src/web_service/web_backend.h | 6 |
5 files changed, 35 insertions, 20 deletions
diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp index d0f257f58..104a16cc9 100644 --- a/src/core/telemetry_session.cpp +++ b/src/core/telemetry_session.cpp | |||
| @@ -77,7 +77,13 @@ u64 RegenerateTelemetryId() { | |||
| 77 | 77 | ||
| 78 | TelemetrySession::TelemetrySession() { | 78 | TelemetrySession::TelemetrySession() { |
| 79 | #ifdef ENABLE_WEB_SERVICE | 79 | #ifdef ENABLE_WEB_SERVICE |
| 80 | backend = std::make_unique<WebService::TelemetryJson>(); | 80 | if (Settings::values.enable_telemetry) { |
| 81 | backend = std::make_unique<WebService::TelemetryJson>( | ||
| 82 | Settings::values.telemetry_endpoint_url, Settings::values.citra_username, | ||
| 83 | Settings::values.citra_token); | ||
| 84 | } else { | ||
| 85 | backend = std::make_unique<Telemetry::NullVisitor>(); | ||
| 86 | } | ||
| 81 | #else | 87 | #else |
| 82 | backend = std::make_unique<Telemetry::NullVisitor>(); | 88 | backend = std::make_unique<Telemetry::NullVisitor>(); |
| 83 | #endif | 89 | #endif |
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 96ddf6c3c..e50c3a301 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp | |||
| @@ -5,36 +5,37 @@ | |||
| 5 | #include <cpr/cpr.h> | 5 | #include <cpr/cpr.h> |
| 6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| 7 | #include "common/logging/log.h" | 7 | #include "common/logging/log.h" |
| 8 | #include "core/settings.h" | ||
| 9 | #include "web_service/web_backend.h" | 8 | #include "web_service/web_backend.h" |
| 10 | 9 | ||
| 11 | namespace WebService { | 10 | namespace WebService { |
| 12 | 11 | ||
| 13 | static constexpr char API_VERSION[]{"1"}; | 12 | static constexpr char API_VERSION[]{"1"}; |
| 14 | 13 | ||
| 15 | void PostJson(const std::string& url, const std::string& data) { | 14 | void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, |
| 16 | if (!Settings::values.enable_telemetry) { | 15 | const std::string& username, const std::string& token) { |
| 17 | // Telemetry disabled by user configuration | 16 | if (url.empty()) { |
| 17 | LOG_ERROR(WebService, "URL is invalid"); | ||
| 18 | return; | 18 | return; |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | if (url.empty()) { | 21 | const bool are_credentials_provided{!token.empty() && !username.empty()}; |
| 22 | LOG_ERROR(WebService, "URL is invalid"); | 22 | if (!allow_anonymous && !are_credentials_provided) { |
| 23 | LOG_ERROR(WebService, "Credentials must be provided for authenticated requests"); | ||
| 23 | return; | 24 | return; |
| 24 | } | 25 | } |
| 25 | 26 | ||
| 26 | if (Settings::values.citra_token.empty() || Settings::values.citra_username.empty()) { | 27 | if (are_credentials_provided) { |
| 27 | // Anonymous request if citra token or username are empty | 28 | // Authenticated request if credentials are provided |
| 28 | cpr::PostAsync( | ||
| 29 | cpr::Url{url}, cpr::Body{data}, | ||
| 30 | cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}}); | ||
| 31 | } else { | ||
| 32 | // We have both, do an authenticated request | ||
| 33 | cpr::PostAsync(cpr::Url{url}, cpr::Body{data}, | 29 | cpr::PostAsync(cpr::Url{url}, cpr::Body{data}, |
| 34 | cpr::Header{{"Content-Type", "application/json"}, | 30 | cpr::Header{{"Content-Type", "application/json"}, |
| 35 | {"x-username", Settings::values.citra_username}, | 31 | {"x-username", username}, |
| 36 | {"x-token", Settings::values.citra_token}, | 32 | {"x-token", token}, |
| 37 | {"api-version", API_VERSION}}); | 33 | {"api-version", API_VERSION}}); |
| 34 | } else { | ||
| 35 | // Otherwise, anonymous request | ||
| 36 | cpr::PostAsync( | ||
| 37 | cpr::Url{url}, cpr::Body{data}, | ||
| 38 | cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}}); | ||
| 38 | } | 39 | } |
| 39 | } | 40 | } |
| 40 | 41 | ||
diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h index 08e384869..d17100398 100644 --- a/src/web_service/web_backend.h +++ b/src/web_service/web_backend.h | |||
| @@ -13,7 +13,11 @@ namespace WebService { | |||
| 13 | * Posts JSON to services.citra-emu.org. | 13 | * Posts JSON to services.citra-emu.org. |
| 14 | * @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. |
| 15 | * @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. | ||
| 16 | */ | 19 | */ |
| 17 | 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 = {}); | ||
| 18 | 22 | ||
| 19 | } // namespace WebService | 23 | } // namespace WebService |