Merge pull request #4359 from FearlessTobi/port-webstuff
web_service: Port yuzu-emu/yuzu#1465 and Port yuzu-emu/yuzu#1473 (Many self-contained changes)
This commit is contained in:
commit
59cb0ec570
11 changed files with 251 additions and 229 deletions
|
@ -90,6 +90,7 @@ add_library(common STATIC
|
||||||
timer.cpp
|
timer.cpp
|
||||||
timer.h
|
timer.h
|
||||||
vector_math.h
|
vector_math.h
|
||||||
|
web_result.h
|
||||||
)
|
)
|
||||||
|
|
||||||
if(ARCHITECTURE_x86_64)
|
if(ARCHITECTURE_x86_64)
|
||||||
|
|
|
@ -9,23 +9,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
|
#include "common/web_result.h"
|
||||||
namespace Common {
|
|
||||||
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 Common
|
|
||||||
|
|
||||||
namespace AnnounceMultiplayerRoom {
|
namespace AnnounceMultiplayerRoom {
|
||||||
|
|
||||||
|
|
25
src/common/web_result.h
Normal file
25
src/common/web_result.h
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
// Copyright 2018 Citra 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 Common {
|
||||||
|
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 Common
|
|
@ -446,7 +446,7 @@ target_link_libraries(core PUBLIC common PRIVATE audio_core network video_core)
|
||||||
target_link_libraries(core PUBLIC Boost::boost PRIVATE cryptopp fmt open_source_archives)
|
target_link_libraries(core PUBLIC Boost::boost PRIVATE cryptopp fmt open_source_archives)
|
||||||
if (ENABLE_WEB_SERVICE)
|
if (ENABLE_WEB_SERVICE)
|
||||||
target_compile_definitions(core PRIVATE -DENABLE_WEB_SERVICE)
|
target_compile_definitions(core PRIVATE -DENABLE_WEB_SERVICE)
|
||||||
target_link_libraries(core PRIVATE json-headers web_service)
|
target_link_libraries(core PRIVATE web_service)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (ARCHITECTURE_x86_64)
|
if (ARCHITECTURE_x86_64)
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include "common/assert.h"
|
#include "common/assert.h"
|
||||||
#include "common/file_util.h"
|
#include "common/file_util.h"
|
||||||
|
#include "common/logging/log.h"
|
||||||
#include "common/scm_rev.h"
|
#include "common/scm_rev.h"
|
||||||
#ifdef ARCHITECTURE_x86_64
|
#ifdef ARCHITECTURE_x86_64
|
||||||
#include "common/x64/cpu_detect.h"
|
#include "common/x64/cpu_detect.h"
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
#include "common/telemetry.h"
|
#include "common/telemetry.h"
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
@ -30,8 +31,6 @@ public:
|
||||||
field_collection.AddField(type, name, std::move(value));
|
field_collection.AddField(type, name, std::move(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void FinalizeAsyncJob();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Telemetry::FieldCollection field_collection; ///< Tracks all added fields for the session
|
Telemetry::FieldCollection field_collection; ///< Tracks all added fields for the session
|
||||||
std::unique_ptr<Telemetry::VisitorInterface> backend; ///< Backend interface that logs fields
|
std::unique_ptr<Telemetry::VisitorInterface> backend; ///< Backend interface that logs fields
|
||||||
|
@ -53,7 +52,6 @@ u64 RegenerateTelemetryId();
|
||||||
* Verifies the username and token.
|
* Verifies the username and token.
|
||||||
* @param username Citra username to use for authentication.
|
* @param username Citra username to use for authentication.
|
||||||
* @param token Citra token to use for authentication.
|
* @param token Citra token to use for authentication.
|
||||||
* @param func A function that gets exectued when the verification is finished
|
|
||||||
* @returns Future with bool indicating whether the verification succeeded
|
* @returns Future with bool indicating whether the verification succeeded
|
||||||
*/
|
*/
|
||||||
bool VerifyLogin(const std::string& username, const std::string& token);
|
bool VerifyLogin(const std::string& username, const std::string& token);
|
||||||
|
|
|
@ -2,96 +2,114 @@
|
||||||
// Licensed under GPLv2 or any later version
|
// Licensed under GPLv2 or any later version
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
#include <thread>
|
#include <json.hpp>
|
||||||
#include "common/assert.h"
|
|
||||||
#include "common/detached_tasks.h"
|
#include "common/detached_tasks.h"
|
||||||
|
#include "common/web_result.h"
|
||||||
#include "web_service/telemetry_json.h"
|
#include "web_service/telemetry_json.h"
|
||||||
#include "web_service/web_backend.h"
|
#include "web_service/web_backend.h"
|
||||||
|
|
||||||
namespace WebService {
|
namespace WebService {
|
||||||
|
|
||||||
TelemetryJson::TelemetryJson(const std::string& host, const std::string& username,
|
struct TelemetryJson::Impl {
|
||||||
const std::string& token)
|
Impl(std::string host, std::string username, std::string token)
|
||||||
: host(std::move(host)), username(std::move(username)), token(std::move(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;
|
TelemetryJson::~TelemetryJson() = default;
|
||||||
|
|
||||||
template <class T>
|
|
||||||
void TelemetryJson::Serialize(Telemetry::FieldType type, const std::string& name, T value) {
|
|
||||||
sections[static_cast<u8>(type)][name] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TelemetryJson::SerializeSection(Telemetry::FieldType type, const std::string& name) {
|
|
||||||
TopSection()[name] = sections[static_cast<unsigned>(type)];
|
|
||||||
}
|
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<bool>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<bool>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<double>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<double>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<float>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<float>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<u8>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<u8>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<u16>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<u16>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<u32>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<u32>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<u64>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<u64>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<s8>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<s8>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<s16>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<s16>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<s32>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<s32>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<s64>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<s64>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<std::string>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<std::string>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<const char*>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<const char*>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), std::string(field.GetValue()));
|
impl->Serialize(field.GetType(), field.GetName(), std::string(field.GetValue()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Visit(const Telemetry::Field<std::chrono::microseconds>& field) {
|
void TelemetryJson::Visit(const Telemetry::Field<std::chrono::microseconds>& field) {
|
||||||
Serialize(field.GetType(), field.GetName(), field.GetValue().count());
|
impl->Serialize(field.GetType(), field.GetName(), field.GetValue().count());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelemetryJson::Complete() {
|
void TelemetryJson::Complete() {
|
||||||
SerializeSection(Telemetry::FieldType::App, "App");
|
impl->SerializeSection(Telemetry::FieldType::App, "App");
|
||||||
SerializeSection(Telemetry::FieldType::Session, "Session");
|
impl->SerializeSection(Telemetry::FieldType::Session, "Session");
|
||||||
SerializeSection(Telemetry::FieldType::Performance, "Performance");
|
impl->SerializeSection(Telemetry::FieldType::Performance, "Performance");
|
||||||
SerializeSection(Telemetry::FieldType::UserFeedback, "UserFeedback");
|
impl->SerializeSection(Telemetry::FieldType::UserFeedback, "UserFeedback");
|
||||||
SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
|
impl->SerializeSection(Telemetry::FieldType::UserConfig, "UserConfig");
|
||||||
SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
|
impl->SerializeSection(Telemetry::FieldType::UserSystem, "UserSystem");
|
||||||
|
|
||||||
auto content = TopSection().dump();
|
auto content = impl->TopSection().dump();
|
||||||
// Send the telemetry async but don't handle the errors since they were written to the log
|
// Send the telemetry async but don't handle the errors since they were written to the log
|
||||||
Common::DetachedTasks::AddTask(
|
Common::DetachedTasks::AddTask(
|
||||||
[host{this->host}, username{this->username}, token{this->token}, content]() {
|
[host{impl->host}, username{impl->username}, token{impl->token}, content]() {
|
||||||
Client{host, username, token}.PostJson("/telemetry", content, true);
|
Client{host, username, token}.PostJson("/telemetry", content, true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,8 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <array>
|
#include <chrono>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <json.hpp>
|
|
||||||
#include "common/announce_multiplayer_room.h"
|
#include "common/announce_multiplayer_room.h"
|
||||||
#include "common/telemetry.h"
|
#include "common/telemetry.h"
|
||||||
|
|
||||||
|
@ -18,8 +17,8 @@ namespace WebService {
|
||||||
*/
|
*/
|
||||||
class TelemetryJson : public Telemetry::VisitorInterface {
|
class TelemetryJson : public Telemetry::VisitorInterface {
|
||||||
public:
|
public:
|
||||||
TelemetryJson(const std::string& host, const std::string& username, const std::string& token);
|
TelemetryJson(std::string host, std::string username, std::string token);
|
||||||
~TelemetryJson();
|
~TelemetryJson() override;
|
||||||
|
|
||||||
void Visit(const Telemetry::Field<bool>& field) override;
|
void Visit(const Telemetry::Field<bool>& field) override;
|
||||||
void Visit(const Telemetry::Field<double>& field) override;
|
void Visit(const Telemetry::Field<double>& field) override;
|
||||||
|
@ -39,20 +38,8 @@ public:
|
||||||
void Complete() override;
|
void Complete() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
nlohmann::json& TopSection() {
|
struct Impl;
|
||||||
return sections[static_cast<u8>(Telemetry::FieldType::None)];
|
std::unique_ptr<Impl> impl;
|
||||||
}
|
|
||||||
|
|
||||||
template <class T>
|
|
||||||
void Serialize(Telemetry::FieldType type, const std::string& name, T value);
|
|
||||||
|
|
||||||
void SerializeSection(Telemetry::FieldType type, const std::string& name);
|
|
||||||
|
|
||||||
nlohmann::json output;
|
|
||||||
std::array<nlohmann::json, 7> sections;
|
|
||||||
std::string host;
|
|
||||||
std::string username;
|
|
||||||
std::string token;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace WebService
|
} // namespace WebService
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
#include <json.hpp>
|
#include <json.hpp>
|
||||||
|
#include "common/web_result.h"
|
||||||
#include "web_service/verify_login.h"
|
#include "web_service/verify_login.h"
|
||||||
#include "web_service/web_backend.h"
|
#include "web_service/web_backend.h"
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,15 @@
|
||||||
// Licensed under GPLv2 or any later version
|
// Licensed under GPLv2 or any later version
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
|
||||||
#include <LUrlParser.h>
|
#include <LUrlParser.h>
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
#include "common/announce_multiplayer_room.h"
|
#include "common/common_types.h"
|
||||||
#include "common/logging/log.h"
|
#include "common/logging/log.h"
|
||||||
|
#include "common/web_result.h"
|
||||||
#include "web_service/web_backend.h"
|
#include "web_service/web_backend.h"
|
||||||
|
|
||||||
namespace WebService {
|
namespace WebService {
|
||||||
|
@ -20,101 +22,132 @@ constexpr int HTTPS_PORT = 443;
|
||||||
|
|
||||||
constexpr std::size_t TIMEOUT_SECONDS = 30;
|
constexpr std::size_t TIMEOUT_SECONDS = 30;
|
||||||
|
|
||||||
Client::JWTCache Client::jwt_cache{};
|
struct Client::Impl {
|
||||||
|
Impl(std::string host, std::string username, std::string token)
|
||||||
Client::Client(const std::string& host, const std::string& username, const std::string& token)
|
: host{std::move(host)}, username{std::move(username)}, token{std::move(token)} {
|
||||||
: host(host), username(username), token(token) {
|
std::lock_guard<std::mutex> lock(jwt_cache.mutex);
|
||||||
std::lock_guard<std::mutex> lock(jwt_cache.mutex);
|
if (this->username == jwt_cache.username && this->token == jwt_cache.token) {
|
||||||
if (username == jwt_cache.username && token == jwt_cache.token) {
|
jwt = jwt_cache.jwt;
|
||||||
jwt = jwt_cache.jwt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Client::~Client() = default;
|
|
||||||
|
|
||||||
Common::WebResult Client::GenericJson(const std::string& method, const std::string& path,
|
|
||||||
const std::string& data, const std::string& jwt,
|
|
||||||
const std::string& username, const std::string& token) {
|
|
||||||
if (cli == nullptr) {
|
|
||||||
auto parsedUrl = LUrlParser::clParseURL::ParseURL(host);
|
|
||||||
int port;
|
|
||||||
if (parsedUrl.m_Scheme == "http") {
|
|
||||||
if (!parsedUrl.GetPort(&port)) {
|
|
||||||
port = HTTP_PORT;
|
|
||||||
}
|
|
||||||
cli =
|
|
||||||
std::make_unique<httplib::Client>(parsedUrl.m_Host.c_str(), port, TIMEOUT_SECONDS);
|
|
||||||
} else if (parsedUrl.m_Scheme == "https") {
|
|
||||||
if (!parsedUrl.GetPort(&port)) {
|
|
||||||
port = HTTPS_PORT;
|
|
||||||
}
|
|
||||||
cli = std::make_unique<httplib::SSLClient>(parsedUrl.m_Host.c_str(), port,
|
|
||||||
TIMEOUT_SECONDS);
|
|
||||||
} else {
|
|
||||||
LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme);
|
|
||||||
return Common::WebResult{Common::WebResult::Code::InvalidURL, "Bad URL scheme"};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (cli == nullptr) {
|
|
||||||
LOG_ERROR(WebService, "Invalid URL {}", host + path);
|
/// A generic function handles POST, GET and DELETE request together
|
||||||
return Common::WebResult{Common::WebResult::Code::InvalidURL, "Invalid URL"};
|
Common::WebResult GenericJson(const std::string& method, const std::string& path,
|
||||||
|
const std::string& data, bool allow_anonymous) {
|
||||||
|
if (jwt.empty()) {
|
||||||
|
UpdateJWT();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jwt.empty() && !allow_anonymous) {
|
||||||
|
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
|
||||||
|
return Common::WebResult{Common::WebResult::Code::CredentialsMissing,
|
||||||
|
"Credentials needed"};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto result = GenericJson(method, path, data, jwt);
|
||||||
|
if (result.result_string == "401") {
|
||||||
|
// Try again with new JWT
|
||||||
|
UpdateJWT();
|
||||||
|
result = GenericJson(method, path, data, jwt);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
httplib::Headers params;
|
/**
|
||||||
if (!jwt.empty()) {
|
* A generic function with explicit authentication method specified
|
||||||
params = {
|
* JWT is used if the jwt parameter is not empty
|
||||||
{std::string("Authorization"), fmt::format("Bearer {}", jwt)},
|
* 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
|
||||||
} else if (!username.empty()) {
|
*/
|
||||||
params = {
|
Common::WebResult GenericJson(const std::string& method, const std::string& path,
|
||||||
{std::string("x-username"), username},
|
const std::string& data, const std::string& jwt = "",
|
||||||
{std::string("x-token"), token},
|
const std::string& username = "", const std::string& token = "") {
|
||||||
|
if (cli == nullptr) {
|
||||||
|
auto parsedUrl = LUrlParser::clParseURL::ParseURL(host);
|
||||||
|
int port;
|
||||||
|
if (parsedUrl.m_Scheme == "http") {
|
||||||
|
if (!parsedUrl.GetPort(&port)) {
|
||||||
|
port = HTTP_PORT;
|
||||||
|
}
|
||||||
|
cli = std::make_unique<httplib::Client>(parsedUrl.m_Host.c_str(), port,
|
||||||
|
TIMEOUT_SECONDS);
|
||||||
|
} else if (parsedUrl.m_Scheme == "https") {
|
||||||
|
if (!parsedUrl.GetPort(&port)) {
|
||||||
|
port = HTTPS_PORT;
|
||||||
|
}
|
||||||
|
cli = std::make_unique<httplib::SSLClient>(parsedUrl.m_Host.c_str(), port,
|
||||||
|
TIMEOUT_SECONDS);
|
||||||
|
} else {
|
||||||
|
LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme);
|
||||||
|
return Common::WebResult{Common::WebResult::Code::InvalidURL, "Bad URL scheme"};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cli == nullptr) {
|
||||||
|
LOG_ERROR(WebService, "Invalid URL {}", host + path);
|
||||||
|
return Common::WebResult{Common::WebResult::Code::InvalidURL, "Invalid URL"};
|
||||||
|
}
|
||||||
|
|
||||||
|
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 Common::WebResult{Common::WebResult::Code::LibError, "Null response"};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.status >= 400) {
|
||||||
|
LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
|
||||||
|
response.status);
|
||||||
|
return Common::WebResult{Common::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 Common::WebResult{Common::WebResult::Code::WrongContent, ""};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (content_type->second.find("application/json") == std::string::npos &&
|
||||||
|
content_type->second.find("text/html; charset=utf-8") == std::string::npos) {
|
||||||
|
LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
|
||||||
|
content_type->second);
|
||||||
|
return Common::WebResult{Common::WebResult::Code::WrongContent, "Wrong content"};
|
||||||
|
}
|
||||||
|
return Common::WebResult{Common::WebResult::Code::Success, "", response.body};
|
||||||
}
|
}
|
||||||
|
|
||||||
params.emplace(std::string("api-version"), std::string(API_VERSION.begin(), API_VERSION.end()));
|
// Retrieve a new JWT from given username and token
|
||||||
if (method != "GET") {
|
void UpdateJWT() {
|
||||||
params.emplace(std::string("Content-Type"), std::string("application/json"));
|
if (username.empty() || token.empty()) {
|
||||||
};
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
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 Common::WebResult{Common::WebResult::Code::LibError, "Null response"};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.status >= 400) {
|
|
||||||
LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
|
|
||||||
response.status);
|
|
||||||
return Common::WebResult{Common::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 Common::WebResult{Common::WebResult::Code::WrongContent, ""};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (content_type->second.find("application/json") == std::string::npos &&
|
|
||||||
content_type->second.find("text/html; charset=utf-8") == std::string::npos) {
|
|
||||||
LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
|
|
||||||
content_type->second);
|
|
||||||
return Common::WebResult{Common::WebResult::Code::WrongContent, "Wrong content"};
|
|
||||||
}
|
|
||||||
return Common::WebResult{Common::WebResult::Code::Success, "", response.body};
|
|
||||||
}
|
|
||||||
|
|
||||||
void Client::UpdateJWT() {
|
|
||||||
if (!username.empty() && !token.empty()) {
|
|
||||||
auto result = GenericJson("POST", "/jwt/internal", "", "", username, token);
|
auto result = GenericJson("POST", "/jwt/internal", "", "", username, token);
|
||||||
if (result.result_code != Common::WebResult::Code::Success) {
|
if (result.result_code != Common::WebResult::Code::Success) {
|
||||||
LOG_ERROR(WebService, "UpdateJWT failed");
|
LOG_ERROR(WebService, "UpdateJWT failed");
|
||||||
|
@ -125,27 +158,39 @@ void Client::UpdateJWT() {
|
||||||
jwt_cache.jwt = jwt = result.returned_data;
|
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;
|
||||||
|
|
||||||
|
Common::WebResult Client::PostJson(const std::string& path, const std::string& data,
|
||||||
|
bool allow_anonymous) {
|
||||||
|
return impl->GenericJson("POST", path, data, allow_anonymous);
|
||||||
}
|
}
|
||||||
|
|
||||||
Common::WebResult Client::GenericJson(const std::string& method, const std::string& path,
|
Common::WebResult Client::GetJson(const std::string& path, bool allow_anonymous) {
|
||||||
const std::string& data, bool allow_anonymous) {
|
return impl->GenericJson("GET", path, "", allow_anonymous);
|
||||||
if (jwt.empty()) {
|
}
|
||||||
UpdateJWT();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (jwt.empty() && !allow_anonymous) {
|
Common::WebResult Client::DeleteJson(const std::string& path, const std::string& data,
|
||||||
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
|
bool allow_anonymous) {
|
||||||
return Common::WebResult{Common::WebResult::Code::CredentialsMissing, "Credentials needed"};
|
return impl->GenericJson("DELETE", path, data, allow_anonymous);
|
||||||
}
|
|
||||||
|
|
||||||
auto result = GenericJson(method, path, data, jwt);
|
|
||||||
if (result.result_string == "401") {
|
|
||||||
// Try again with new JWT
|
|
||||||
UpdateJWT();
|
|
||||||
result = GenericJson(method, path, data, jwt);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace WebService
|
} // namespace WebService
|
||||||
|
|
|
@ -4,22 +4,18 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
#include <memory>
|
||||||
#include <mutex>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <tuple>
|
|
||||||
#include "common/announce_multiplayer_room.h"
|
|
||||||
#include "common/common_types.h"
|
|
||||||
|
|
||||||
namespace httplib {
|
namespace Common {
|
||||||
class Client;
|
struct WebResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace WebService {
|
namespace WebService {
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
public:
|
public:
|
||||||
Client(const std::string& host, const std::string& username, const std::string& token);
|
Client(std::string host, std::string username, std::string token);
|
||||||
~Client();
|
~Client();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,9 +26,7 @@ public:
|
||||||
* @return the result of the request.
|
* @return the result of the request.
|
||||||
*/
|
*/
|
||||||
Common::WebResult PostJson(const std::string& path, const std::string& data,
|
Common::WebResult PostJson(const std::string& path, const std::string& data,
|
||||||
bool allow_anonymous) {
|
bool allow_anonymous);
|
||||||
return GenericJson("POST", path, data, allow_anonymous);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets JSON from the specified path.
|
* Gets JSON from the specified path.
|
||||||
|
@ -40,9 +34,7 @@ public:
|
||||||
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
* @param allow_anonymous If true, allow anonymous unauthenticated requests.
|
||||||
* @return the result of the request.
|
* @return the result of the request.
|
||||||
*/
|
*/
|
||||||
Common::WebResult GetJson(const std::string& path, bool allow_anonymous) {
|
Common::WebResult GetJson(const std::string& path, bool allow_anonymous);
|
||||||
return GenericJson("GET", path, "", allow_anonymous);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes JSON to the specified path.
|
* Deletes JSON to the specified path.
|
||||||
|
@ -52,41 +44,11 @@ public:
|
||||||
* @return the result of the request.
|
* @return the result of the request.
|
||||||
*/
|
*/
|
||||||
Common::WebResult DeleteJson(const std::string& path, const std::string& data,
|
Common::WebResult DeleteJson(const std::string& path, const std::string& data,
|
||||||
bool allow_anonymous) {
|
bool allow_anonymous);
|
||||||
return GenericJson("DELETE", path, data, allow_anonymous);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// A generic function handles POST, GET and DELETE request together
|
struct Impl;
|
||||||
Common::WebResult GenericJson(const std::string& method, const std::string& path,
|
std::unique_ptr<Impl> impl;
|
||||||
const std::string& data, bool allow_anonymous);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
Common::WebResult GenericJson(const std::string& method, const std::string& path,
|
|
||||||
const std::string& data, const std::string& jwt = "",
|
|
||||||
const std::string& username = "", const std::string& token = "");
|
|
||||||
|
|
||||||
// Retrieve a new JWT from given username and token
|
|
||||||
void UpdateJWT();
|
|
||||||
|
|
||||||
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 JWTCache jwt_cache;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace WebService
|
} // namespace WebService
|
||||||
|
|
Loading…
Reference in a new issue