2017-06-28 05:01:49 +02:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2017-08-25 01:27:13 +02:00
|
|
|
#include <cstdlib>
|
2018-03-24 20:19:35 +01:00
|
|
|
#include <string>
|
2017-08-25 01:27:13 +02:00
|
|
|
#include <thread>
|
2018-03-24 20:19:35 +01:00
|
|
|
#include <LUrlParser.h>
|
2018-10-20 16:47:17 +02:00
|
|
|
#include <httplib.h>
|
2017-10-31 10:02:42 +01:00
|
|
|
#include "common/announce_multiplayer_room.h"
|
2017-06-28 05:18:52 +02:00
|
|
|
#include "common/logging/log.h"
|
2017-06-28 05:01:49 +02:00
|
|
|
#include "web_service/web_backend.h"
|
|
|
|
|
|
|
|
namespace WebService {
|
|
|
|
|
2018-09-29 02:51:28 +02:00
|
|
|
constexpr std::array<const char, 1> API_VERSION{'1'};
|
2017-06-28 05:18:52 +02:00
|
|
|
|
2018-10-13 13:33:04 +02:00
|
|
|
constexpr int HTTP_PORT = 80;
|
|
|
|
constexpr int HTTPS_PORT = 443;
|
2018-03-24 20:19:35 +01:00
|
|
|
|
2018-10-13 13:33:04 +02:00
|
|
|
constexpr std::size_t TIMEOUT_SECONDS = 30;
|
2018-03-24 20:19:35 +01:00
|
|
|
|
2018-09-12 18:22:48 +02:00
|
|
|
Client::JWTCache Client::jwt_cache{};
|
2018-03-24 20:19:35 +01:00
|
|
|
|
2018-09-12 18:22:48 +02:00
|
|
|
Client::Client(const std::string& host, const std::string& username, const std::string& token)
|
|
|
|
: host(host), username(username), token(token) {
|
2018-09-17 20:28:58 +02:00
|
|
|
std::lock_guard<std::mutex> lock(jwt_cache.mutex);
|
2018-09-12 18:22:48 +02:00
|
|
|
if (username == jwt_cache.username && token == jwt_cache.token) {
|
|
|
|
jwt = jwt_cache.jwt;
|
2017-09-19 03:18:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 16:47:17 +02:00
|
|
|
Client::~Client() = default;
|
|
|
|
|
2018-09-12 18:22:48 +02:00
|
|
|
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) {
|
2018-08-25 21:39:23 +02:00
|
|
|
if (cli == nullptr) {
|
2018-09-12 18:22:48 +02:00
|
|
|
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"};
|
|
|
|
}
|
2018-08-25 21:39:23 +02:00
|
|
|
}
|
2018-09-12 18:22:48 +02:00
|
|
|
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},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-29 02:51:28 +02:00
|
|
|
params.emplace(std::string("api-version"), std::string(API_VERSION.begin(), API_VERSION.end()));
|
2018-09-12 18:22:48 +02:00
|
|
|
if (method != "GET") {
|
|
|
|
params.emplace(std::string("Content-Type"), std::string("application/json"));
|
|
|
|
};
|
|
|
|
|
|
|
|
httplib::Request request;
|
|
|
|
request.method = method;
|
|
|
|
request.path = path;
|
2018-08-25 21:39:23 +02:00
|
|
|
request.headers = params;
|
|
|
|
request.body = data;
|
|
|
|
|
2018-09-12 18:22:48 +02:00
|
|
|
httplib::Response response;
|
2018-08-25 21:39:23 +02:00
|
|
|
|
|
|
|
if (!cli->send(request, response)) {
|
2018-09-12 18:22:48 +02:00
|
|
|
LOG_ERROR(WebService, "{} to {} returned null", method, host + path);
|
2018-08-25 21:39:23 +02:00
|
|
|
return Common::WebResult{Common::WebResult::Code::LibError, "Null response"};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.status >= 400) {
|
2018-09-12 18:22:48 +02:00
|
|
|
LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
|
|
|
|
response.status);
|
2018-08-25 21:39:23 +02:00
|
|
|
return Common::WebResult{Common::WebResult::Code::HttpError,
|
|
|
|
std::to_string(response.status)};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto content_type = response.headers.find("content-type");
|
|
|
|
|
2018-09-12 18:22:48 +02:00
|
|
|
if (content_type == response.headers.end()) {
|
|
|
|
LOG_ERROR(WebService, "{} to {} returned no content", method, host + path);
|
2018-08-25 21:39:23 +02:00
|
|
|
return Common::WebResult{Common::WebResult::Code::WrongContent, ""};
|
|
|
|
}
|
|
|
|
|
2018-09-12 18:22:48 +02:00
|
|
|
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"};
|
2017-06-28 05:18:52 +02:00
|
|
|
}
|
2018-09-12 18:22:48 +02:00
|
|
|
return Common::WebResult{Common::WebResult::Code::Success, "", response.body};
|
2018-08-25 21:39:23 +02:00
|
|
|
}
|
2018-03-24 20:19:35 +01:00
|
|
|
|
2018-09-12 18:22:48 +02:00
|
|
|
void Client::UpdateJWT() {
|
|
|
|
if (!username.empty() && !token.empty()) {
|
|
|
|
auto result = GenericJson("POST", "/jwt/internal", "", "", username, token);
|
|
|
|
if (result.result_code != Common::WebResult::Code::Success) {
|
|
|
|
LOG_ERROR(WebService, "UpdateJWT failed");
|
|
|
|
} else {
|
2018-09-17 20:28:58 +02:00
|
|
|
std::lock_guard<std::mutex> lock(jwt_cache.mutex);
|
2018-09-12 18:22:48 +02:00
|
|
|
jwt_cache.username = username;
|
|
|
|
jwt_cache.token = token;
|
|
|
|
jwt_cache.jwt = jwt = result.returned_data;
|
|
|
|
}
|
2018-08-25 21:39:23 +02:00
|
|
|
}
|
2017-09-19 03:18:26 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 18:22:48 +02:00
|
|
|
Common::WebResult Client::GenericJson(const std::string& method, const std::string& path,
|
|
|
|
const std::string& data, bool allow_anonymous) {
|
|
|
|
if (jwt.empty()) {
|
|
|
|
UpdateJWT();
|
2017-09-19 03:18:26 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 18:22:48 +02:00
|
|
|
if (jwt.empty() && !allow_anonymous) {
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
|
2018-09-12 18:22:48 +02:00
|
|
|
return Common::WebResult{Common::WebResult::Code::CredentialsMissing, "Credentials needed"};
|
2017-09-19 03:18:26 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 18:22:48 +02:00
|
|
|
auto result = GenericJson(method, path, data, jwt);
|
|
|
|
if (result.result_string == "401") {
|
|
|
|
// Try again with new JWT
|
|
|
|
UpdateJWT();
|
|
|
|
result = GenericJson(method, path, data, jwt);
|
2017-09-19 03:18:26 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 18:22:48 +02:00
|
|
|
return result;
|
2017-10-31 10:02:42 +01:00
|
|
|
}
|
2017-09-19 03:18:26 +02:00
|
|
|
|
2017-06-28 05:01:49 +02:00
|
|
|
} // namespace WebService
|