summaryrefslogtreecommitdiff
path: root/src/web_service
diff options
context:
space:
mode:
authorGravatar bunnei2017-10-09 23:56:20 -0400
committerGravatar bunnei2017-10-09 23:56:20 -0400
commitb1d5db1cf60344b6b081c9d03cb6ccc3264326cd (patch)
treefde377c4ba3c0f92c032e6f5ec8627aae37270ef /src/web_service
parentloader: Various improvements for NSO/NRO loaders. (diff)
parentMerge pull request #2996 from MerryMage/split-travis (diff)
downloadyuzu-b1d5db1cf60344b6b081c9d03cb6ccc3264326cd.tar.gz
yuzu-b1d5db1cf60344b6b081c9d03cb6ccc3264326cd.tar.xz
yuzu-b1d5db1cf60344b6b081c9d03cb6ccc3264326cd.zip
Merge remote-tracking branch 'upstream/master' into nx
# Conflicts: # src/core/CMakeLists.txt # src/core/arm/dynarmic/arm_dynarmic.cpp # src/core/arm/dyncom/arm_dyncom.cpp # src/core/hle/kernel/process.cpp # src/core/hle/kernel/thread.cpp # src/core/hle/kernel/thread.h # src/core/hle/kernel/vm_manager.cpp # src/core/loader/3dsx.cpp # src/core/loader/elf.cpp # src/core/loader/ncch.cpp # src/core/memory.cpp # src/core/memory.h # src/core/memory_setup.h
Diffstat (limited to 'src/web_service')
-rw-r--r--src/web_service/CMakeLists.txt2
-rw-r--r--src/web_service/telemetry_json.cpp3
-rw-r--r--src/web_service/telemetry_json.h7
-rw-r--r--src/web_service/verify_login.cpp28
-rw-r--r--src/web_service/verify_login.h24
-rw-r--r--src/web_service/web_backend.cpp138
-rw-r--r--src/web_service/web_backend.h32
7 files changed, 194 insertions, 40 deletions
diff --git a/src/web_service/CMakeLists.txt b/src/web_service/CMakeLists.txt
index 334d82a8a..c93811892 100644
--- a/src/web_service/CMakeLists.txt
+++ b/src/web_service/CMakeLists.txt
@@ -1,10 +1,12 @@
1set(SRCS 1set(SRCS
2 telemetry_json.cpp 2 telemetry_json.cpp
3 verify_login.cpp
3 web_backend.cpp 4 web_backend.cpp
4 ) 5 )
5 6
6set(HEADERS 7set(HEADERS
7 telemetry_json.h 8 telemetry_json.h
9 verify_login.h
8 web_backend.h 10 web_backend.h
9 ) 11 )
10 12
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 */
18class TelemetryJson : public Telemetry::VisitorInterface { 18class TelemetryJson : public Telemetry::VisitorInterface {
19public: 19public:
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/verify_login.cpp b/src/web_service/verify_login.cpp
new file mode 100644
index 000000000..1bc3b5afe
--- /dev/null
+++ b/src/web_service/verify_login.cpp
@@ -0,0 +1,28 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <json.hpp>
6#include "web_service/verify_login.h"
7#include "web_service/web_backend.h"
8
9namespace WebService {
10
11std::future<bool> VerifyLogin(std::string& username, std::string& token,
12 const std::string& endpoint_url, std::function<void()> func) {
13 auto get_func = [func, username](const std::string& reply) -> bool {
14 func();
15 if (reply.empty())
16 return false;
17 nlohmann::json json = nlohmann::json::parse(reply);
18 std::string result;
19 try {
20 result = json["username"];
21 } catch (const nlohmann::detail::out_of_range&) {
22 }
23 return result == username;
24 };
25 return GetJson<bool>(get_func, endpoint_url, false, username, token);
26}
27
28} // namespace WebService
diff --git a/src/web_service/verify_login.h b/src/web_service/verify_login.h
new file mode 100644
index 000000000..303f5dbbc
--- /dev/null
+++ b/src/web_service/verify_login.h
@@ -0,0 +1,24 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <functional>
8#include <future>
9#include <string>
10
11namespace WebService {
12
13/**
14 * Checks if username and token is valid
15 * @param username Citra username to use for authentication.
16 * @param token Citra token to use for authentication.
17 * @param endpoint_url URL of the services.citra-emu.org endpoint.
18 * @param func A function that gets exectued when the verification is finished
19 * @returns Future with bool indicating whether the verification succeeded
20 */
21std::future<bool> VerifyLogin(std::string& username, std::string& token,
22 const std::string& endpoint_url, std::function<void()> func);
23
24} // namespace WebService
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp
index 13e4555ac..b17d82f9c 100644
--- a/src/web_service/web_backend.cpp
+++ b/src/web_service/web_backend.cpp
@@ -2,51 +2,139 @@
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
10namespace WebService { 15namespace WebService {
11 16
12static constexpr char API_VERSION[]{"1"}; 17static constexpr char API_VERSION[]{"1"};
13static constexpr char ENV_VAR_USERNAME[]{"CITRA_WEB_SERVICES_USERNAME"};
14static constexpr char ENV_VAR_TOKEN[]{"CITRA_WEB_SERVICES_TOKEN"};
15 18
16static std::string GetEnvironmentVariable(const char* name) { 19static std::unique_ptr<cpr::Session> g_session;
17 const char* value{getenv(name)}; 20
18 if (value) { 21void Win32WSAStartup() {
19 return value; 22#ifdef _WIN32
23 // On Windows, CPR/libcurl does not properly initialize Winsock. The below code is used to
24 // initialize Winsock globally, which fixes this problem. Without this, only the first CPR
25 // session will properly be created, and subsequent ones will fail.
26 WSADATA wsa_data;
27 const int wsa_result{WSAStartup(MAKEWORD(2, 2), &wsa_data)};
28 if (wsa_result) {
29 LOG_CRITICAL(WebService, "WSAStartup failed: %d", wsa_result);
20 } 30 }
21 return {}; 31#endif
22} 32}
23 33
24const std::string& GetUsername() { 34void PostJson(const std::string& url, const std::string& data, bool allow_anonymous,
25 static const std::string username{GetEnvironmentVariable(ENV_VAR_USERNAME)}; 35 const std::string& username, const std::string& token) {
26 return username; 36 if (url.empty()) {
27} 37 LOG_ERROR(WebService, "URL is invalid");
38 return;
39 }
40
41 const bool are_credentials_provided{!token.empty() && !username.empty()};
42 if (!allow_anonymous && !are_credentials_provided) {
43 LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
44 return;
45 }
28 46
29const std::string& GetToken() { 47 Win32WSAStartup();
30 static const std::string token{GetEnvironmentVariable(ENV_VAR_TOKEN)}; 48
31 return token; 49 // Built request header
50 cpr::Header header;
51 if (are_credentials_provided) {
52 // Authenticated request if credentials are provided
53 header = {{"Content-Type", "application/json"},
54 {"x-username", username.c_str()},
55 {"x-token", token.c_str()},
56 {"api-version", API_VERSION}};
57 } else {
58 // Otherwise, anonymous request
59 header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}};
60 }
61
62 // Post JSON asynchronously
63 static std::future<void> future;
64 future = cpr::PostCallback(
65 [](cpr::Response r) {
66 if (r.error) {
67 LOG_ERROR(WebService, "POST returned cpr error: %u:%s",
68 static_cast<u32>(r.error.code), r.error.message.c_str());
69 return;
70 }
71 if (r.status_code >= 400) {
72 LOG_ERROR(WebService, "POST returned error status code: %u", r.status_code);
73 return;
74 }
75 if (r.header["content-type"].find("application/json") == std::string::npos) {
76 LOG_ERROR(WebService, "POST returned wrong content: %s",
77 r.header["content-type"].c_str());
78 return;
79 }
80 },
81 cpr::Url{url}, cpr::Body{data}, header);
32} 82}
33 83
34void PostJson(const std::string& url, const std::string& data) { 84template <typename T>
85std::future<T> GetJson(std::function<T(const std::string&)> func, const std::string& url,
86 bool allow_anonymous, const std::string& username,
87 const std::string& token) {
35 if (url.empty()) { 88 if (url.empty()) {
36 LOG_ERROR(WebService, "URL is invalid"); 89 LOG_ERROR(WebService, "URL is invalid");
37 return; 90 return std::async(std::launch::async, [func{std::move(func)}]() { return func(""); });
38 } 91 }
39 92
40 if (GetUsername().empty() || GetToken().empty()) { 93 const bool are_credentials_provided{!token.empty() && !username.empty()};
41 LOG_ERROR(WebService, "Environment variables %s and %s must be set to POST JSON", 94 if (!allow_anonymous && !are_credentials_provided) {
42 ENV_VAR_USERNAME, ENV_VAR_TOKEN); 95 LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
43 return; 96 return std::async(std::launch::async, [func{std::move(func)}]() { return func(""); });
44 } 97 }
45 98
46 cpr::PostAsync(cpr::Url{url}, cpr::Body{data}, cpr::Header{{"Content-Type", "application/json"}, 99 Win32WSAStartup();
47 {"x-username", GetUsername()}, 100
48 {"x-token", GetToken()}, 101 // Built request header
49 {"api-version", API_VERSION}}); 102 cpr::Header header;
103 if (are_credentials_provided) {
104 // Authenticated request if credentials are provided
105 header = {{"Content-Type", "application/json"},
106 {"x-username", username.c_str()},
107 {"x-token", token.c_str()},
108 {"api-version", API_VERSION}};
109 } else {
110 // Otherwise, anonymous request
111 header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}};
112 }
113
114 // Get JSON asynchronously
115 return cpr::GetCallback(
116 [func{std::move(func)}](cpr::Response r) {
117 if (r.error) {
118 LOG_ERROR(WebService, "GET returned cpr error: %u:%s",
119 static_cast<u32>(r.error.code), r.error.message.c_str());
120 return func("");
121 }
122 if (r.status_code >= 400) {
123 LOG_ERROR(WebService, "GET returned error code: %u", r.status_code);
124 return func("");
125 }
126 if (r.header["content-type"].find("application/json") == std::string::npos) {
127 LOG_ERROR(WebService, "GET returned wrong content: %s",
128 r.header["content-type"].c_str());
129 return func("");
130 }
131 return func(r.text);
132 },
133 cpr::Url{url}, header);
50} 134}
51 135
136template std::future<bool> GetJson(std::function<bool(const std::string&)> func,
137 const std::string& url, bool allow_anonymous,
138 const std::string& username, const std::string& token);
139
52} // namespace WebService 140} // namespace WebService
diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h
index 2753d3b68..a63c75d13 100644
--- a/src/web_service/web_backend.h
+++ b/src/web_service/web_backend.h
@@ -4,28 +4,36 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <functional>
8#include <future>
7#include <string> 9#include <string>
8#include "common/common_types.h" 10#include "common/common_types.h"
9 11
10namespace WebService { 12namespace WebService {
11 13
12/** 14/**
13 * Gets the current username for accessing services.citra-emu.org. 15 * Posts JSON to services.citra-emu.org.
14 * @returns Username as a string, empty if not set. 16 * @param url URL of the services.citra-emu.org endpoint to post data to.
15 */ 17 * @param data String of JSON data to use for the body of the POST request.
16const std::string& GetUsername(); 18 * @param allow_anonymous If true, allow anonymous unauthenticated requests.
17 19 * @param username Citra username to use for authentication.
18/** 20 * @param token Citra token to use for authentication.
19 * Gets the current token for accessing services.citra-emu.org.
20 * @returns Token as a string, empty if not set.
21 */ 21 */
22const std::string& GetToken(); 22void PostJson(const std::string& url, const std::string& data, bool allow_anonymous,
23 const std::string& username = {}, const std::string& token = {});
23 24
24/** 25/**
25 * Posts JSON to services.citra-emu.org. 26 * Gets JSON from services.citra-emu.org.
27 * @param func A function that gets exectued when the json as a string is received
26 * @param url URL of the services.citra-emu.org endpoint to post data to. 28 * @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. 29 * @param allow_anonymous If true, allow anonymous unauthenticated requests.
30 * @param username Citra username to use for authentication.
31 * @param token Citra token to use for authentication.
32 * @return future that holds the return value T of the func
28 */ 33 */
29void PostJson(const std::string& url, const std::string& data); 34template <typename T>
35std::future<T> GetJson(std::function<T(const std::string&)> func, const std::string& url,
36 bool allow_anonymous, const std::string& username = {},
37 const std::string& token = {});
30 38
31} // namespace WebService 39} // namespace WebService