early-access version 1255
This commit is contained in:
12
src/web_service/CMakeLists.txt
Executable file
12
src/web_service/CMakeLists.txt
Executable file
@@ -0,0 +1,12 @@
|
||||
add_library(web_service STATIC
|
||||
telemetry_json.cpp
|
||||
telemetry_json.h
|
||||
verify_login.cpp
|
||||
verify_login.h
|
||||
web_backend.cpp
|
||||
web_backend.h
|
||||
web_result.h
|
||||
)
|
||||
|
||||
create_target_directory_groups(web_service)
|
||||
target_link_libraries(web_service PRIVATE common nlohmann_json::nlohmann_json httplib)
|
131
src/web_service/telemetry_json.cpp
Executable file
131
src/web_service/telemetry_json.cpp
Executable file
@@ -0,0 +1,131 @@
|
||||
// Copyright 2017 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "common/detached_tasks.h"
|
||||
#include "web_service/telemetry_json.h"
|
||||
#include "web_service/web_backend.h"
|
||||
#include "web_service/web_result.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
namespace Telemetry = Common::Telemetry;
|
||||
|
||||
struct TelemetryJson::Impl {
|
||||
Impl(std::string host, std::string username, std::string token)
|
||||
: host{std::move(host)}, username{std::move(username)}, token{std::move(token)} {}
|
||||
|
||||
nlohmann::json& TopSection() {
|
||||
return sections[static_cast<u8>(Telemetry::FieldType::None)];
|
||||
}
|
||||
|
||||
const nlohmann::json& TopSection() const {
|
||||
return sections[static_cast<u8>(Telemetry::FieldType::None)];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Serialize(Telemetry::FieldType type, const std::string& name, T value) {
|
||||
sections[static_cast<u8>(type)][name] = value;
|
||||
}
|
||||
|
||||
void SerializeSection(Telemetry::FieldType type, const std::string& name) {
|
||||
TopSection()[name] = sections[static_cast<unsigned>(type)];
|
||||
}
|
||||
|
||||
nlohmann::json output;
|
||||
std::array<nlohmann::json, 7> sections;
|
||||
std::string host;
|
||||
std::string username;
|
||||
std::string token;
|
||||
};
|
||||
|
||||
TelemetryJson::TelemetryJson(std::string host, std::string username, std::string token)
|
||||
: impl{std::make_unique<Impl>(std::move(host), std::move(username), std::move(token))} {}
|
||||
TelemetryJson::~TelemetryJson() = default;
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<bool>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<double>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<float>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<u8>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<u16>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<u32>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<u64>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<s8>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<s16>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<s32>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<s64>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<std::string>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<const char*>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), std::string(field.GetValue()));
|
||||
}
|
||||
|
||||
void TelemetryJson::Visit(const Telemetry::Field<std::chrono::microseconds>& field) {
|
||||
impl->Serialize(field.GetType(), field.GetName(), field.GetValue().count());
|
||||
}
|
||||
|
||||
void TelemetryJson::Complete() {
|
||||
impl->SerializeSection(Telemetry::FieldType::App, "App");
|
||||
impl->SerializeSection(Telemetry::FieldType::Session, "Session");
|
||||
impl->SerializeSection(Telemetry::FieldType::Performance, "Performance");
|
||||
impl->SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
|
||||
impl->SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
|
||||
|
||||
auto content = impl->TopSection().dump();
|
||||
// Send the telemetry async but don't handle the errors since they were written to the log
|
||||
Common::DetachedTasks::AddTask([host{impl->host}, content]() {
|
||||
Client{host, "", ""}.PostJson("/telemetry", content, true);
|
||||
});
|
||||
}
|
||||
|
||||
bool TelemetryJson::SubmitTestcase() {
|
||||
impl->SerializeSection(Telemetry::FieldType::App, "App");
|
||||
impl->SerializeSection(Telemetry::FieldType::Session, "Session");
|
||||
impl->SerializeSection(Telemetry::FieldType::UserFeedback, "UserFeedback");
|
||||
impl->SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
|
||||
impl->SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
|
||||
|
||||
auto content = impl->TopSection().dump();
|
||||
Client client(impl->host, impl->username, impl->token);
|
||||
auto value = client.PostJson("/gamedb/testcase", content, false);
|
||||
|
||||
return value.result_code == WebResult::Code::Success;
|
||||
}
|
||||
|
||||
} // namespace WebService
|
45
src/web_service/telemetry_json.h
Executable file
45
src/web_service/telemetry_json.h
Executable file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2017 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include "common/telemetry.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
/**
|
||||
* Implementation of VisitorInterface that serialized telemetry into JSON, and submits it to the
|
||||
* yuzu web service
|
||||
*/
|
||||
class TelemetryJson : public Common::Telemetry::VisitorInterface {
|
||||
public:
|
||||
TelemetryJson(std::string host, std::string username, std::string token);
|
||||
~TelemetryJson() override;
|
||||
|
||||
void Visit(const Common::Telemetry::Field<bool>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<double>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<float>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<u8>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<u16>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<u32>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<u64>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<s8>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<s16>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<s32>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<s64>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<std::string>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<const char*>& field) override;
|
||||
void Visit(const Common::Telemetry::Field<std::chrono::microseconds>& field) override;
|
||||
|
||||
void Complete() override;
|
||||
bool SubmitTestcase() override;
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl;
|
||||
};
|
||||
|
||||
} // namespace WebService
|
28
src/web_service/verify_login.cpp
Executable file
28
src/web_service/verify_login.cpp
Executable file
@@ -0,0 +1,28 @@
|
||||
// Copyright 2017 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "web_service/verify_login.h"
|
||||
#include "web_service/web_backend.h"
|
||||
#include "web_service/web_result.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
bool VerifyLogin(const std::string& host, const std::string& username, const std::string& token) {
|
||||
Client client(host, username, token);
|
||||
auto reply = client.GetJson("/profile", false).returned_data;
|
||||
if (reply.empty()) {
|
||||
return false;
|
||||
}
|
||||
nlohmann::json json = nlohmann::json::parse(reply);
|
||||
const auto iter = json.find("username");
|
||||
|
||||
if (iter == json.end()) {
|
||||
return username.empty();
|
||||
}
|
||||
|
||||
return username == *iter;
|
||||
}
|
||||
|
||||
} // namespace WebService
|
20
src/web_service/verify_login.h
Executable file
20
src/web_service/verify_login.h
Executable file
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace WebService {
|
||||
|
||||
/**
|
||||
* Checks if username and token is valid
|
||||
* @param host the web API URL
|
||||
* @param username yuzu username to use for authentication.
|
||||
* @param token yuzu token to use for authentication.
|
||||
* @returns a bool indicating whether the verification succeeded
|
||||
*/
|
||||
bool VerifyLogin(const std::string& host, const std::string& username, const std::string& token);
|
||||
|
||||
} // namespace WebService
|
193
src/web_service/web_backend.cpp
Executable file
193
src/web_service/web_backend.cpp
Executable file
@@ -0,0 +1,193 @@
|
||||
// Copyright 2017 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <array>
|
||||
#include <cstdlib>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <httplib.h>
|
||||
|
||||
#include "common/logging/log.h"
|
||||
#include "web_service/web_backend.h"
|
||||
#include "web_service/web_result.h"
|
||||
|
||||
namespace WebService {
|
||||
|
||||
constexpr std::array<const char, 1> API_VERSION{'1'};
|
||||
|
||||
constexpr std::size_t TIMEOUT_SECONDS = 30;
|
||||
|
||||
struct Client::Impl {
|
||||
Impl(std::string host, std::string username, std::string token)
|
||||
: host{std::move(host)}, username{std::move(username)}, token{std::move(token)} {
|
||||
std::lock_guard lock{jwt_cache.mutex};
|
||||
if (this->username == jwt_cache.username && this->token == jwt_cache.token) {
|
||||
jwt = jwt_cache.jwt;
|
||||
}
|
||||
}
|
||||
|
||||
/// A generic function handles POST, GET and DELETE request together
|
||||
WebResult GenericRequest(const std::string& method, const std::string& path,
|
||||
const std::string& data, bool allow_anonymous,
|
||||
const std::string& accept) {
|
||||
if (jwt.empty()) {
|
||||
UpdateJWT();
|
||||
}
|
||||
|
||||
if (jwt.empty() && !allow_anonymous) {
|
||||
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
|
||||
return WebResult{WebResult::Code::CredentialsMissing, "Credentials needed", ""};
|
||||
}
|
||||
|
||||
auto result = GenericRequest(method, path, data, accept, jwt);
|
||||
if (result.result_string == "401") {
|
||||
// Try again with new JWT
|
||||
UpdateJWT();
|
||||
result = GenericRequest(method, path, data, accept, jwt);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* A generic function with explicit authentication method specified
|
||||
* JWT is used if the jwt parameter is not empty
|
||||
* username + token is used if jwt is empty but username and token are
|
||||
* not empty anonymous if all of jwt, username and token are empty
|
||||
*/
|
||||
WebResult GenericRequest(const std::string& method, const std::string& path,
|
||||
const std::string& data, const std::string& accept,
|
||||
const std::string& jwt = "", const std::string& username = "",
|
||||
const std::string& token = "") {
|
||||
if (cli == nullptr) {
|
||||
cli = std::make_unique<httplib::Client>(host.c_str());
|
||||
}
|
||||
|
||||
if (!cli->is_valid()) {
|
||||
LOG_ERROR(WebService, "Client is invalid, skipping request!");
|
||||
return {};
|
||||
}
|
||||
|
||||
cli->set_connection_timeout(TIMEOUT_SECONDS);
|
||||
cli->set_read_timeout(TIMEOUT_SECONDS);
|
||||
cli->set_write_timeout(TIMEOUT_SECONDS);
|
||||
|
||||
httplib::Headers params;
|
||||
if (!jwt.empty()) {
|
||||
params = {
|
||||
{std::string("Authorization"), fmt::format("Bearer {}", jwt)},
|
||||
};
|
||||
} else if (!username.empty()) {
|
||||
params = {
|
||||
{std::string("x-username"), username},
|
||||
{std::string("x-token"), token},
|
||||
};
|
||||
}
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
httplib::Request request;
|
||||
request.method = method;
|
||||
request.path = path;
|
||||
request.headers = params;
|
||||
request.body = data;
|
||||
|
||||
httplib::Response response;
|
||||
|
||||
if (!cli->send(request, response)) {
|
||||
LOG_ERROR(WebService, "{} to {} returned null", method, host + path);
|
||||
return WebResult{WebResult::Code::LibError, "Null response", ""};
|
||||
}
|
||||
|
||||
if (response.status >= 400) {
|
||||
LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
|
||||
response.status);
|
||||
return WebResult{WebResult::Code::HttpError, std::to_string(response.status), ""};
|
||||
}
|
||||
|
||||
auto content_type = response.headers.find("content-type");
|
||||
|
||||
if (content_type == response.headers.end()) {
|
||||
LOG_ERROR(WebService, "{} to {} returned no content", method, host + path);
|
||||
return WebResult{WebResult::Code::WrongContent, "", ""};
|
||||
}
|
||||
|
||||
if (content_type->second.find(accept) == std::string::npos) {
|
||||
LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
|
||||
content_type->second);
|
||||
return WebResult{WebResult::Code::WrongContent, "Wrong content", ""};
|
||||
}
|
||||
return WebResult{WebResult::Code::Success, "", response.body};
|
||||
}
|
||||
|
||||
// Retrieve a new JWT from given username and token
|
||||
void UpdateJWT() {
|
||||
if (username.empty() || token.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto result = GenericRequest("POST", "/jwt/internal", "", "text/html", "", username, token);
|
||||
if (result.result_code != WebResult::Code::Success) {
|
||||
LOG_ERROR(WebService, "UpdateJWT failed");
|
||||
} else {
|
||||
std::lock_guard lock{jwt_cache.mutex};
|
||||
jwt_cache.username = username;
|
||||
jwt_cache.token = token;
|
||||
jwt_cache.jwt = jwt = result.returned_data;
|
||||
}
|
||||
}
|
||||
|
||||
std::string host;
|
||||
std::string username;
|
||||
std::string token;
|
||||
std::string jwt;
|
||||
std::unique_ptr<httplib::Client> cli;
|
||||
|
||||
struct JWTCache {
|
||||
std::mutex mutex;
|
||||
std::string username;
|
||||
std::string token;
|
||||
std::string jwt;
|
||||
};
|
||||
static inline JWTCache jwt_cache;
|
||||
};
|
||||
|
||||
Client::Client(std::string host, std::string username, std::string token)
|
||||
: impl{std::make_unique<Impl>(std::move(host), std::move(username), std::move(token))} {}
|
||||
|
||||
Client::~Client() = default;
|
||||
|
||||
WebResult Client::PostJson(const std::string& path, const std::string& data, bool allow_anonymous) {
|
||||
return impl->GenericRequest("POST", path, data, allow_anonymous, "application/json");
|
||||
}
|
||||
|
||||
WebResult Client::GetJson(const std::string& path, bool allow_anonymous) {
|
||||
return impl->GenericRequest("GET", path, "", allow_anonymous, "application/json");
|
||||
}
|
||||
|
||||
WebResult Client::DeleteJson(const std::string& path, const std::string& data,
|
||||
bool allow_anonymous) {
|
||||
return impl->GenericRequest("DELETE", path, data, allow_anonymous, "application/json");
|
||||
}
|
||||
|
||||
WebResult Client::GetPlain(const std::string& path, bool allow_anonymous) {
|
||||
return impl->GenericRequest("GET", path, "", allow_anonymous, "text/plain");
|
||||
}
|
||||
|
||||
WebResult Client::GetImage(const std::string& path, bool allow_anonymous) {
|
||||
return impl->GenericRequest("GET", path, "", allow_anonymous, "image/png");
|
||||
}
|
||||
|
||||
WebResult Client::GetExternalJWT(const std::string& audience) {
|
||||
return impl->GenericRequest("POST", fmt::format("/jwt/external/{}", audience), "", false,
|
||||
"text/html");
|
||||
}
|
||||
|
||||
} // namespace WebService
|
73
src/web_service/web_backend.h
Executable file
73
src/web_service/web_backend.h
Executable file
@@ -0,0 +1,73 @@
|
||||
// Copyright 2017 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace WebService {
|
||||
|
||||
struct WebResult;
|
||||
|
||||
class Client {
|
||||
public:
|
||||
Client(std::string host, std::string username, std::string token);
|
||||
~Client();
|
||||
|
||||
/**
|
||||
* Posts JSON to the specified path.
|
||||
* @param path the URL segment after the host address.
|
||||
* @param data String of JSON data to use for the body of the POST request.
|
||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult PostJson(const std::string& path, const std::string& data, bool allow_anonymous);
|
||||
|
||||
/**
|
||||
* Gets JSON from the specified path.
|
||||
* @param path the URL segment after the host address.
|
||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult GetJson(const std::string& path, bool allow_anonymous);
|
||||
|
||||
/**
|
||||
* Deletes JSON to the specified path.
|
||||
* @param path the URL segment after the host address.
|
||||
* @param data String of JSON data to use for the body of the DELETE request.
|
||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult DeleteJson(const std::string& path, const std::string& data, bool allow_anonymous);
|
||||
|
||||
/**
|
||||
* Gets a plain string from the specified path.
|
||||
* @param path the URL segment after the host address.
|
||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult GetPlain(const std::string& path, bool allow_anonymous);
|
||||
|
||||
/**
|
||||
* Gets an PNG image from the specified path.
|
||||
* @param path the URL segment after the host address.
|
||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult GetImage(const std::string& path, bool allow_anonymous);
|
||||
|
||||
/**
|
||||
* Requests an external JWT for the specific audience provided.
|
||||
* @param audience the audience of the JWT requested.
|
||||
* @return the result of the request.
|
||||
*/
|
||||
WebResult GetExternalJWT(const std::string& audience);
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl;
|
||||
};
|
||||
|
||||
} // namespace WebService
|
25
src/web_service/web_result.h
Executable file
25
src/web_service/web_result.h
Executable file
@@ -0,0 +1,25 @@
|
||||
// Copyright 2018 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace WebService {
|
||||
struct WebResult {
|
||||
enum class Code : u32 {
|
||||
Success,
|
||||
InvalidURL,
|
||||
CredentialsMissing,
|
||||
LibError,
|
||||
HttpError,
|
||||
WrongContent,
|
||||
NoWebservice,
|
||||
};
|
||||
Code result_code;
|
||||
std::string result_string;
|
||||
std::string returned_data;
|
||||
};
|
||||
} // namespace WebService
|
Reference in New Issue
Block a user