2.0.0-rc (#9)
Some checks failed
BlockStorage/repertory_osx/pipeline/head This commit looks good
BlockStorage/repertory_windows/pipeline/head This commit looks good
BlockStorage/repertory/pipeline/head There was a failure building this commit
BlockStorage/repertory_linux_builds/pipeline/head This commit looks good
BlockStorage/repertory_osx_builds/pipeline/head There was a failure building this commit
Some checks failed
BlockStorage/repertory_osx/pipeline/head This commit looks good
BlockStorage/repertory_windows/pipeline/head This commit looks good
BlockStorage/repertory/pipeline/head There was a failure building this commit
BlockStorage/repertory_linux_builds/pipeline/head This commit looks good
BlockStorage/repertory_osx_builds/pipeline/head There was a failure building this commit
### Issues * \#1 \[bug\] Unable to mount S3 due to 'item_not_found' exception * \#2 Require bucket name for S3 mounts * \#3 \[bug\] File size is not being updated in S3 mount * \#4 Upgrade to libfuse-3.x.x * \#5 Switch to renterd for Sia support * \#6 Switch to cpp-httplib to further reduce dependencies * \#7 Remove global_data and calculate used disk space per provider * \#8 Switch to libcurl for S3 mount support ### Changes from v1.x.x * Added read-only encrypt provider * Pass-through mount point that transparently encrypts source data using `XChaCha20-Poly1305` * Added S3 encryption support via `XChaCha20-Poly1305` * Added replay protection to remote mounts * Added support base64 writes in remote FUSE * Created static linked Linux binaries for `amd64` and `aarch64` using `musl-libc` * Removed legacy Sia renter support * Removed Skynet support * Fixed multiple remote mount WinFSP API issues on \*NIX servers * Implemented chunked read and write * Writes for non-cached files are performed in chunks of 8Mib * Removed `repertory-ui` support * Removed `FreeBSD` support * Switched to `libsodium` over `CryptoPP` * Switched to `XChaCha20-Poly1305` for remote mounts * Updated `GoogleTest` to v1.14.0 * Updated `JSON for Modern C++` to v3.11.2 * Updated `OpenSSL` to v1.1.1w * Updated `RocksDB` to v8.5.3 * Updated `WinFSP` to 2023 * Updated `boost` to v1.78.0 * Updated `cURL` to v8.3.0 * Updated `zlib` to v1.3 * Use `upload_manager` for all providers * Adds a delay to uploads to prevent excessive API calls * Supports re-upload after mount restart for incomplete uploads * NOTE: Uploads for all providers are full file (no resume support) * Multipart upload support is planned for S3 Reviewed-on: #9
This commit is contained in:
@@ -1,240 +1,267 @@
|
||||
/*
|
||||
Copyright <2018-2022> <scott.e.graves@protonmail.com>
|
||||
Copyright <2018-2023> <scott.e.graves@protonmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or
|
||||
substantial portions of the Software.
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
|
||||
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#include "rpc/client/client.hpp"
|
||||
#include "comm/curl/curl_resolver.hpp"
|
||||
|
||||
#include "types/repertory.hpp"
|
||||
#include "utils/Base64.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
|
||||
namespace repertory {
|
||||
client::client(rpc_host_info host_info) : host_info_(std::move(host_info)) { request_id_ = 0u; }
|
||||
|
||||
rpc_response client::export_list(const std::vector<std::string> &paths) {
|
||||
auto ret = make_request(rpc_method::export_links, {paths});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
}
|
||||
|
||||
return ret;
|
||||
client::client(rpc_host_info host_info) : host_info_(std::move(host_info)) {
|
||||
request_id_ = 0u;
|
||||
}
|
||||
|
||||
rpc_response client::export_all() {
|
||||
auto ret = make_request(rpc_method::export_links, {}, 60000u);
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
auto client::get_drive_information() -> rpc_response {
|
||||
const auto base_url =
|
||||
"http://" + host_info_.host + ":" + std::to_string(host_info_.port);
|
||||
|
||||
httplib::Client cli{base_url};
|
||||
cli.set_basic_auth(host_info_.user, host_info_.password);
|
||||
|
||||
auto resp = cli.Get("/api/v1/" + rpc_method::get_drive_information);
|
||||
if (resp.error() != httplib::Error::Success) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", httplib::to_string(resp.error())}}};
|
||||
}
|
||||
if (resp->status != 200) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", std::to_string(resp->status)}}};
|
||||
}
|
||||
|
||||
return ret;
|
||||
return rpc_response{rpc_response_type::success, json::parse(resp->body)};
|
||||
}
|
||||
|
||||
rpc_response client::get_drive_information() {
|
||||
auto ret = make_request(rpc_method::get_drive_information, {});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
auto client::get_config() -> rpc_response {
|
||||
const auto base_url =
|
||||
"http://" + host_info_.host + ":" + std::to_string(host_info_.port);
|
||||
|
||||
httplib::Client cli{base_url};
|
||||
cli.set_basic_auth(host_info_.user, host_info_.password);
|
||||
|
||||
auto resp = cli.Get("/api/v1/" + rpc_method::get_config);
|
||||
if (resp.error() != httplib::Error::Success) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", httplib::to_string(resp.error())}}};
|
||||
}
|
||||
if (resp->status != 200) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", std::to_string(resp->status)}}};
|
||||
}
|
||||
|
||||
return ret;
|
||||
return rpc_response{rpc_response_type::success, json::parse(resp->body)};
|
||||
}
|
||||
|
||||
rpc_response client::get_config() {
|
||||
auto ret = make_request(rpc_method::get_config, {});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
auto client::get_config_value_by_name(const std::string &name) -> rpc_response {
|
||||
const auto base_url =
|
||||
"http://" + host_info_.host + ":" + std::to_string(host_info_.port);
|
||||
|
||||
httplib::Params params{{"name", name}};
|
||||
httplib::Client cli{base_url};
|
||||
cli.set_basic_auth(host_info_.user, host_info_.password);
|
||||
|
||||
auto resp =
|
||||
cli.Get("/api/v1/" + rpc_method::get_config_value_by_name, params, {});
|
||||
if (resp.error() != httplib::Error::Success) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", httplib::to_string(resp.error())}}};
|
||||
}
|
||||
if (resp->status != 200) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", std::to_string(resp->status)}}};
|
||||
}
|
||||
|
||||
return ret;
|
||||
return rpc_response{rpc_response_type::success, json::parse(resp->body)};
|
||||
}
|
||||
|
||||
rpc_response client::get_config_value_by_name(const std::string &name) {
|
||||
auto ret = make_request(rpc_method::get_config_value_by_name, {name});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
if (ret.data["result"]["value"].get<std::string>().empty()) {
|
||||
ret.response_type = rpc_response_type::config_value_not_found;
|
||||
} else {
|
||||
ret.data = ret.data["result"];
|
||||
}
|
||||
auto client::get_directory_items(const std::string &api_path) -> rpc_response {
|
||||
const auto base_url =
|
||||
"http://" + host_info_.host + ":" + std::to_string(host_info_.port);
|
||||
|
||||
httplib::Params params{{"api_path", api_path}};
|
||||
httplib::Client cli{base_url};
|
||||
cli.set_basic_auth(host_info_.user, host_info_.password);
|
||||
|
||||
auto resp = cli.Get("/api/v1/" + rpc_method::get_directory_items, params, {});
|
||||
if (resp.error() != httplib::Error::Success) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", httplib::to_string(resp.error())}}};
|
||||
}
|
||||
if (resp->status != 200) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", std::to_string(resp->status)}}};
|
||||
}
|
||||
|
||||
return ret;
|
||||
return rpc_response{rpc_response_type::success, json::parse(resp->body)};
|
||||
}
|
||||
|
||||
rpc_response client::get_directory_items(const std::string &api_path) {
|
||||
auto ret = make_request(rpc_method::get_directory_items, {api_path});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
auto client::get_open_files() -> rpc_response {
|
||||
const auto base_url =
|
||||
"http://" + host_info_.host + ":" + std::to_string(host_info_.port);
|
||||
|
||||
httplib::Client cli{base_url};
|
||||
cli.set_basic_auth(host_info_.user, host_info_.password);
|
||||
|
||||
auto resp = cli.Get("/api/v1/" + rpc_method::get_open_files);
|
||||
if (resp.error() != httplib::Error::Success) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", httplib::to_string(resp.error())}}};
|
||||
}
|
||||
if (resp->status != 200) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", std::to_string(resp->status)}}};
|
||||
}
|
||||
|
||||
return ret;
|
||||
return rpc_response{rpc_response_type::success, json::parse(resp->body)};
|
||||
}
|
||||
|
||||
rpc_response client::get_open_files() {
|
||||
auto ret = make_request(rpc_method::get_open_files, {});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
auto client::get_pinned_files() -> rpc_response {
|
||||
const auto base_url =
|
||||
"http://" + host_info_.host + ":" + std::to_string(host_info_.port);
|
||||
|
||||
httplib::Client cli{base_url};
|
||||
cli.set_basic_auth(host_info_.user, host_info_.password);
|
||||
|
||||
auto resp = cli.Get("/api/v1/" + rpc_method::get_pinned_files);
|
||||
if (resp.error() != httplib::Error::Success) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", httplib::to_string(resp.error())}}};
|
||||
}
|
||||
if (resp->status != 200) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", std::to_string(resp->status)}}};
|
||||
}
|
||||
|
||||
return ret;
|
||||
return rpc_response{rpc_response_type::success, json::parse(resp->body)};
|
||||
}
|
||||
|
||||
rpc_response client::get_pinned_files() {
|
||||
auto ret = make_request(rpc_method::get_pinned_files, {});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
auto client::pin_file(const std::string &api_path) -> rpc_response {
|
||||
const auto base_url =
|
||||
"http://" + host_info_.host + ":" + std::to_string(host_info_.port);
|
||||
|
||||
httplib::Params params{{"api_path", api_path}};
|
||||
httplib::Client cli{base_url};
|
||||
cli.set_basic_auth(host_info_.user, host_info_.password);
|
||||
|
||||
auto resp = cli.Post("/api/v1/" + rpc_method::pin_file, params);
|
||||
if (resp.error() != httplib::Error::Success) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", httplib::to_string(resp.error())}}};
|
||||
}
|
||||
if (resp->status != 200) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", std::to_string(resp->status)}}};
|
||||
}
|
||||
|
||||
return ret;
|
||||
return rpc_response{rpc_response_type::success, {}};
|
||||
}
|
||||
|
||||
rpc_response client::import_skylink(const skylink_import_list &list) {
|
||||
std::vector<json> json_list;
|
||||
for (const auto &skynet_import : list) {
|
||||
json_list.emplace_back(skynet_import.to_json());
|
||||
auto client::pinned_status(const std::string &api_path) -> rpc_response {
|
||||
const auto base_url =
|
||||
"http://" + host_info_.host + ":" + std::to_string(host_info_.port);
|
||||
|
||||
httplib::Params params{{"api_path", api_path}};
|
||||
httplib::Client cli{base_url};
|
||||
cli.set_basic_auth(host_info_.user, host_info_.password);
|
||||
|
||||
auto resp = cli.Get("/api/v1/" + rpc_method::pinned_status, params, {});
|
||||
if (resp.error() != httplib::Error::Success) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", httplib::to_string(resp.error())}}};
|
||||
}
|
||||
if (resp->status != 200) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", std::to_string(resp->status)}}};
|
||||
}
|
||||
|
||||
auto ret = make_request(rpc_method::import, {json(json_list)}, 60000u);
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
}
|
||||
|
||||
return ret;
|
||||
return rpc_response{rpc_response_type::success, json::parse(resp->body)};
|
||||
}
|
||||
|
||||
rpc_response client::make_request(const std::string &command, const std::vector<json> &args,
|
||||
std::uint32_t timeout_ms) {
|
||||
auto error = rpc_response_type::success;
|
||||
auto client::set_config_value_by_name(const std::string &name,
|
||||
const std::string &value)
|
||||
-> rpc_response {
|
||||
const auto base_url =
|
||||
"http://" + host_info_.host + ":" + std::to_string(host_info_.port);
|
||||
|
||||
auto *curl_handle = utils::create_curl();
|
||||
httplib::Params params{
|
||||
{"name", name},
|
||||
{"value", value},
|
||||
};
|
||||
|
||||
const auto port = static_cast<std::uint16_t>(host_info_.port);
|
||||
const auto url = "http://" + host_info_.host + ":" + std::to_string(port) + "/api";
|
||||
const auto request = json({{"jsonrpc", "2.0"},
|
||||
{"id", std::to_string(++request_id_)},
|
||||
{"method", command},
|
||||
{"params", args}})
|
||||
.dump();
|
||||
httplib::Client cli{base_url};
|
||||
cli.set_basic_auth(host_info_.user, host_info_.password);
|
||||
|
||||
struct curl_slist *hs = nullptr;
|
||||
hs = curl_slist_append(hs, "Content-Type: application/json;");
|
||||
if (not(host_info_.password.empty() && host_info_.user.empty())) {
|
||||
curl_easy_setopt(curl_handle, CURLOPT_USERNAME, &host_info_.user[0]);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_PASSWORD, &host_info_.password[0]);
|
||||
auto resp =
|
||||
cli.Post("/api/v1/" + rpc_method::set_config_value_by_name, params);
|
||||
if (resp.error() != httplib::Error::Success) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", httplib::to_string(resp.error())}}};
|
||||
}
|
||||
#ifndef __APPLE__
|
||||
curl_resolver resolver(curl_handle,
|
||||
{"localhost:" + std::to_string(host_info_.port) + ":127.0.0.1"}, true);
|
||||
#endif
|
||||
if (timeout_ms > 0) {
|
||||
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, timeout_ms);
|
||||
}
|
||||
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, hs);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, request.c_str());
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, request.size());
|
||||
curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 5L);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
|
||||
static_cast<size_t (*)(char *, size_t, size_t, void *)>(
|
||||
[](char *buffer, size_t size, size_t nitems, void *outstream) -> size_t {
|
||||
(*reinterpret_cast<std::string *>(outstream)) +=
|
||||
std::string(buffer, size * nitems);
|
||||
return size * nitems;
|
||||
}));
|
||||
if (resp->status != 200) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", std::to_string(resp->status)}}};
|
||||
};
|
||||
|
||||
std::string response;
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &response);
|
||||
|
||||
json json_data;
|
||||
const auto res = curl_easy_perform(curl_handle);
|
||||
if (res == CURLE_OK) {
|
||||
long httpErrorCode;
|
||||
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &httpErrorCode);
|
||||
if (httpErrorCode == 200) {
|
||||
json_data = json::parse(response.begin(), response.end());
|
||||
} else {
|
||||
json parsed;
|
||||
try {
|
||||
parsed = json::parse(response.begin(), response.end());
|
||||
} catch (...) {
|
||||
}
|
||||
error = rpc_response_type::http_error;
|
||||
json_data = {{"error", {{"code", std::to_string(httpErrorCode)}}}};
|
||||
if (parsed.empty()) {
|
||||
json_data["error"]["response"] = response;
|
||||
} else {
|
||||
json_data["error"]["response"] = parsed;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error = rpc_response_type::curl_error;
|
||||
json_data = {{"error", {{"message", curl_easy_strerror(res)}}}};
|
||||
}
|
||||
curl_easy_cleanup(curl_handle);
|
||||
curl_slist_free_all(hs);
|
||||
|
||||
return rpc_response({error, json_data});
|
||||
return rpc_response{rpc_response_type::success,
|
||||
nlohmann::json::parse(resp->body)};
|
||||
}
|
||||
|
||||
rpc_response client::pin_file(const std::string &api_path) {
|
||||
auto ret = make_request(rpc_method::pin_file, {api_path});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
auto client::unmount() -> rpc_response {
|
||||
const auto base_url =
|
||||
"http://" + host_info_.host + ":" + std::to_string(host_info_.port);
|
||||
|
||||
httplib::Client cli{base_url};
|
||||
cli.set_basic_auth(host_info_.user, host_info_.password);
|
||||
|
||||
auto resp = cli.Post("/api/v1/" + rpc_method::unmount);
|
||||
if (resp.error() != httplib::Error::Success) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", httplib::to_string(resp.error())}}};
|
||||
}
|
||||
if (resp->status != 200) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", std::to_string(resp->status)}}};
|
||||
}
|
||||
|
||||
return ret;
|
||||
return rpc_response{rpc_response_type::success, {}};
|
||||
}
|
||||
|
||||
rpc_response client::pinned_status(const std::string &api_path) {
|
||||
auto ret = make_request(rpc_method::pinned_status, {api_path});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
auto client::unpin_file(const std::string &api_path) -> rpc_response {
|
||||
const auto base_url =
|
||||
"http://" + host_info_.host + ":" + std::to_string(host_info_.port);
|
||||
|
||||
httplib::Params params{{"api_path", api_path}};
|
||||
httplib::Client cli{base_url};
|
||||
cli.set_basic_auth(host_info_.user, host_info_.password);
|
||||
|
||||
auto resp = cli.Post("/api/v1/" + rpc_method::unpin_file, params);
|
||||
if (resp.error() != httplib::Error::Success) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", httplib::to_string(resp.error())}}};
|
||||
}
|
||||
if (resp->status != 200) {
|
||||
return rpc_response{rpc_response_type::http_error,
|
||||
{{"error", std::to_string(resp->status)}}};
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
rpc_response client::set_config_value_by_name(const std::string &name, const std::string &value) {
|
||||
auto ret = make_request(rpc_method::set_config_value_by_name, {name, value});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
if (ret.data["result"]["value"].get<std::string>().empty()) {
|
||||
ret.response_type = rpc_response_type::config_value_not_found;
|
||||
} else {
|
||||
ret.data = ret.data["result"];
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
rpc_response client::unmount() {
|
||||
auto ret = make_request(rpc_method::unmount, {});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
rpc_response client::unpin_file(const std::string &api_path) {
|
||||
auto ret = make_request(rpc_method::unpin_file, {api_path});
|
||||
if (ret.response_type == rpc_response_type::success) {
|
||||
ret.data = ret.data["result"];
|
||||
}
|
||||
|
||||
return ret;
|
||||
return rpc_response{rpc_response_type::success, {}};
|
||||
}
|
||||
} // namespace repertory
|
||||
|
@@ -1,165 +1,204 @@
|
||||
/*
|
||||
Copyright <2018-2022> <scott.e.graves@protonmail.com>
|
||||
Copyright <2018-2023> <scott.e.graves@protonmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or
|
||||
substantial portions of the Software.
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
|
||||
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#include "rpc/server/full_server.hpp"
|
||||
|
||||
#include "app_config.hpp"
|
||||
#include "drives/directory_iterator.hpp"
|
||||
#include "drives/i_open_file_table.hpp"
|
||||
#include "file_manager/i_file_manager.hpp"
|
||||
#include "providers/i_provider.hpp"
|
||||
#include "providers/skynet/skynet_provider.hpp"
|
||||
#include "types/repertory.hpp"
|
||||
#include "types/rpc.hpp"
|
||||
#include "types/skynet.hpp"
|
||||
#include "utils/global_data.hpp"
|
||||
#include "utils/file_utils.hpp"
|
||||
#include "utils/path_utils.hpp"
|
||||
|
||||
namespace repertory {
|
||||
full_server::full_server(app_config &config, i_provider &provider, i_open_file_table &oft)
|
||||
: server(config), provider_(provider), oft_(oft) {}
|
||||
full_server::full_server(app_config &config, i_provider &provider,
|
||||
i_file_manager &fm)
|
||||
: server(config), provider_(provider), fm_(fm) {}
|
||||
|
||||
bool full_server::handle_request(jsonrpcpp::request_ptr &request,
|
||||
std::unique_ptr<jsonrpcpp::Response> &response) {
|
||||
auto handled = true;
|
||||
if (request->method == rpc_method::get_drive_information) {
|
||||
if (request->params.param_array.empty()) {
|
||||
response = std::make_unique<jsonrpcpp::Response>(
|
||||
*request, json({{"cache_space_used", global_data::instance().get_used_cache_space()},
|
||||
{"drive_space_total", provider_.get_total_drive_space()},
|
||||
{"drive_space_used", global_data::instance().get_used_drive_space()},
|
||||
{"item_count", provider_.get_total_item_count()}}));
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else if (request->method == rpc_method::get_pinned_files) {
|
||||
if (request->params.param_array.empty()) {
|
||||
response = std::make_unique<jsonrpcpp::Response>(*request, provider_.get_pinned_files());
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else if (request->method == rpc_method::get_directory_items) {
|
||||
if (request->params.param_array.size() == 1u) {
|
||||
const auto api_path = utils::path::create_api_path(request->params.param_array[0]);
|
||||
auto directoryItems = oft_.get_directory_items(api_path);
|
||||
std::vector<json> items;
|
||||
for (const auto &item : directoryItems) {
|
||||
items.emplace_back(item.to_json());
|
||||
}
|
||||
void full_server::handle_get_directory_items(const httplib::Request &req,
|
||||
httplib::Response &res) {
|
||||
const auto api_path =
|
||||
utils::path::create_api_path(req.get_param_value("api_path"));
|
||||
const auto list = fm_.get_directory_items(api_path);
|
||||
|
||||
response = std::make_unique<jsonrpcpp::Response>(*request, json({{"items", items}}));
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else if (request->method == rpc_method::pin_file) {
|
||||
if (request->params.param_array.size() == 1u) {
|
||||
const auto api_path = utils::path::create_api_path(request->params.param_array[0]);
|
||||
auto success = provider_.is_file(api_path);
|
||||
if (success) {
|
||||
success = api_error::success ==
|
||||
provider_.set_item_meta(api_path, META_PINNED, utils::string::from_bool(true));
|
||||
}
|
||||
|
||||
if (success) {
|
||||
event_system::instance().raise<file_pinned>(api_path);
|
||||
}
|
||||
|
||||
response = std::make_unique<jsonrpcpp::Response>(*request, json({{"success", success}}));
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else if (request->method == rpc_method::pinned_status) {
|
||||
if (request->params.param_array.size() == 1u) {
|
||||
const auto api_path = utils::path::create_api_path(request->params.param_array[0]);
|
||||
std::string pinned;
|
||||
const auto success =
|
||||
api_error::success == provider_.get_item_meta(api_path, META_PINNED, pinned);
|
||||
|
||||
response = std::make_unique<jsonrpcpp::Response>(
|
||||
*request, json({{"success", success},
|
||||
{"pinned", pinned.empty() ? false : utils::string::to_bool(pinned)}}));
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else if (request->method == rpc_method::unpin_file) {
|
||||
if (request->params.param_array.size() == 1u) {
|
||||
const auto api_path = utils::path::create_api_path(request->params.param_array[0]);
|
||||
auto success = provider_.is_file(api_path);
|
||||
if (success) {
|
||||
success = api_error::success ==
|
||||
provider_.set_item_meta(api_path, META_PINNED, utils::string::from_bool(false));
|
||||
}
|
||||
|
||||
if (success) {
|
||||
event_system::instance().raise<file_unpinned>(api_path);
|
||||
}
|
||||
|
||||
response = std::make_unique<jsonrpcpp::Response>(*request, json({{"success", success}}));
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else if (request->method == rpc_method::get_open_files) {
|
||||
if (request->params.param_array.empty()) {
|
||||
const auto list = oft_.get_open_files();
|
||||
json open_files = {{"file_list", std::vector<json>()}};
|
||||
for (const auto &kv : list) {
|
||||
open_files["file_list"].emplace_back(json({{"path", kv.first}, {"count", kv.second}}));
|
||||
}
|
||||
response = std::make_unique<jsonrpcpp::Response>(*request, open_files);
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
#if defined(REPERTORY_ENABLE_SKYNET)
|
||||
} else if (get_config().get_provider_type() == provider_type::skynet) {
|
||||
if (request->method == rpc_method::import) {
|
||||
if (request->params.param_array.size() == 1) {
|
||||
json results = {{"success", std::vector<json>()}, {"failed", std::vector<std::string>()}};
|
||||
for (const auto &link : request->params.param_array[0]) {
|
||||
const auto si = skylink_import::from_json(link);
|
||||
auto &provider = dynamic_cast<skynet_provider &>(provider_);
|
||||
const auto res = provider.import_skylink(si);
|
||||
if (res == api_error::success) {
|
||||
results["success"].emplace_back(link);
|
||||
} else {
|
||||
results["failed"].emplace_back(link["skylink"].get<std::string>());
|
||||
}
|
||||
}
|
||||
response = std::make_unique<jsonrpcpp::Response>(*request, results);
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else if (request->method == rpc_method::export_links) {
|
||||
if (request->params.param_array.empty()) {
|
||||
auto &provider = dynamic_cast<skynet_provider &>(provider_);
|
||||
response = std::make_unique<jsonrpcpp::Response>(*request, provider.export_all());
|
||||
} else if (request->params.param_array.size() == 1) {
|
||||
auto &provider = dynamic_cast<skynet_provider &>(provider_);
|
||||
response = std::make_unique<jsonrpcpp::Response>(
|
||||
*request,
|
||||
provider.export_list(request->params.param_array[0].get<std::vector<std::string>>()));
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else {
|
||||
handled = server::handle_request(request, response);
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
handled = server::handle_request(request, response);
|
||||
json items = {{"items", std::vector<json>()}};
|
||||
for (const auto &item : list) {
|
||||
items["items"].emplace_back(item.to_json());
|
||||
}
|
||||
return handled;
|
||||
res.set_content(items.dump(), "application/json");
|
||||
res.status = 200;
|
||||
}
|
||||
|
||||
void full_server::handle_get_drive_information(const httplib::Request & /*req*/,
|
||||
httplib::Response &res) {
|
||||
res.set_content(
|
||||
json({
|
||||
{"cache_space_used",
|
||||
utils::file::calculate_used_space(
|
||||
get_config().get_cache_directory(), false)},
|
||||
{"drive_space_total", provider_.get_total_drive_space()},
|
||||
{"drive_space_used", provider_.get_used_drive_space()},
|
||||
{"item_count", provider_.get_total_item_count()},
|
||||
})
|
||||
.dump(),
|
||||
"application/json");
|
||||
res.status = 200;
|
||||
}
|
||||
|
||||
void full_server::handle_get_open_files(const httplib::Request & /*req*/,
|
||||
httplib::Response &res) {
|
||||
const auto list = fm_.get_open_files();
|
||||
|
||||
json open_files = {{"items", std::vector<json>()}};
|
||||
for (const auto &kv : list) {
|
||||
open_files["items"].emplace_back(json({
|
||||
{"path", kv.first},
|
||||
{"count", kv.second},
|
||||
}));
|
||||
}
|
||||
res.set_content(open_files.dump(), "application/json");
|
||||
res.status = 200;
|
||||
}
|
||||
|
||||
void full_server::handle_get_pinned_files(const httplib::Request & /*req*/,
|
||||
httplib::Response &res) {
|
||||
res.set_content(json({{"items", provider_.get_pinned_files()}}).dump(),
|
||||
"application/json");
|
||||
res.status = 200;
|
||||
}
|
||||
|
||||
void full_server::handle_get_pinned_status(const httplib::Request &req,
|
||||
httplib::Response &res) {
|
||||
const auto api_path =
|
||||
utils::path::create_api_path(req.get_param_value("api_path"));
|
||||
|
||||
std::string pinned;
|
||||
const auto result = provider_.get_item_meta(api_path, META_PINNED, pinned);
|
||||
if (result != api_error::success) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, result,
|
||||
"failed to get pinned status");
|
||||
res.status = 500;
|
||||
return;
|
||||
}
|
||||
|
||||
res.set_content(
|
||||
json(
|
||||
{{"pinned", pinned.empty() ? false : utils::string::to_bool(pinned)}})
|
||||
.dump(),
|
||||
"application/json");
|
||||
res.status = 200;
|
||||
}
|
||||
|
||||
void full_server::handle_pin_file(const httplib::Request &req,
|
||||
httplib::Response &res) {
|
||||
const auto api_path =
|
||||
utils::path::create_api_path(req.get_param_value("api_path"));
|
||||
|
||||
bool exists{};
|
||||
auto result = provider_.is_file(api_path, exists);
|
||||
if (result != api_error::success) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, result,
|
||||
"failed to pin file");
|
||||
res.status = 500;
|
||||
return;
|
||||
}
|
||||
|
||||
if (exists) {
|
||||
exists = api_error::success ==
|
||||
provider_.set_item_meta(api_path, META_PINNED,
|
||||
utils::string::from_bool(true));
|
||||
}
|
||||
|
||||
if (exists) {
|
||||
event_system::instance().raise<file_pinned>(api_path);
|
||||
}
|
||||
|
||||
res.status = exists ? 200 : 404;
|
||||
}
|
||||
|
||||
void full_server::handle_unpin_file(const httplib::Request &req,
|
||||
httplib::Response &res) {
|
||||
const auto api_path =
|
||||
utils::path::create_api_path(req.get_param_value("api_path"));
|
||||
|
||||
bool exists{};
|
||||
auto result = provider_.is_file(api_path, exists);
|
||||
if (result != api_error::success) {
|
||||
utils::error::raise_api_path_error(__FUNCTION__, api_path, result,
|
||||
"failed to unpin file");
|
||||
res.status = 500;
|
||||
return;
|
||||
}
|
||||
|
||||
if (exists) {
|
||||
exists = api_error::success ==
|
||||
provider_.set_item_meta(api_path, META_PINNED,
|
||||
utils::string::from_bool(false));
|
||||
}
|
||||
|
||||
if (exists) {
|
||||
event_system::instance().raise<file_unpinned>(api_path);
|
||||
}
|
||||
|
||||
res.status = exists ? 200 : 404;
|
||||
}
|
||||
|
||||
void full_server::initialize(httplib::Server &inst) {
|
||||
server::initialize(inst);
|
||||
|
||||
inst.Get("/api/v1/" + rpc_method::get_directory_items,
|
||||
[this](auto &&req, auto &&res) {
|
||||
handle_get_directory_items(std::forward<decltype(req)>(req),
|
||||
std::forward<decltype(res)>(res));
|
||||
});
|
||||
inst.Get("/api/v1/" + rpc_method::get_drive_information,
|
||||
[this](auto &&req, auto &&res) {
|
||||
handle_get_drive_information(std::forward<decltype(req)>(req),
|
||||
std::forward<decltype(res)>(res));
|
||||
});
|
||||
inst.Get("/api/v1/" + rpc_method::get_open_files,
|
||||
[this](auto &&req, auto &&res) {
|
||||
handle_get_open_files(std::forward<decltype(req)>(req),
|
||||
std::forward<decltype(res)>(res));
|
||||
});
|
||||
inst.Get("/api/v1/" + rpc_method::get_pinned_files,
|
||||
[this](auto &&req, auto &&res) {
|
||||
handle_get_pinned_files(std::forward<decltype(req)>(req),
|
||||
std::forward<decltype(res)>(res));
|
||||
});
|
||||
inst.Get("/api/v1/" + rpc_method::pinned_status,
|
||||
[this](auto &&req, auto &&res) {
|
||||
handle_get_pinned_status(std::forward<decltype(req)>(req),
|
||||
std::forward<decltype(res)>(res));
|
||||
});
|
||||
inst.Post("/api/v1/" + rpc_method::pin_file, [this](auto &&req, auto &&res) {
|
||||
handle_pin_file(std::forward<decltype(req)>(req),
|
||||
std::forward<decltype(res)>(res));
|
||||
});
|
||||
inst.Post("/api/v1/" + rpc_method::unpin_file,
|
||||
[this](auto &&req, auto &&res) {
|
||||
handle_unpin_file(std::forward<decltype(req)>(req),
|
||||
std::forward<decltype(res)>(res));
|
||||
});
|
||||
}
|
||||
} // namespace repertory
|
||||
|
@@ -1,163 +1,181 @@
|
||||
/*
|
||||
Copyright <2018-2022> <scott.e.graves@protonmail.com>
|
||||
Copyright <2018-2023> <scott.e.graves@protonmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or
|
||||
substantial portions of the Software.
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
|
||||
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#include "rpc/server/server.hpp"
|
||||
|
||||
#include "app_config.hpp"
|
||||
#include "utils/Base64.hpp"
|
||||
#include "utils/error_utils.hpp"
|
||||
|
||||
namespace repertory {
|
||||
server::rpc_resource::rpc_resource(server &owner) : httpserver::http_resource(), owner_(owner) {
|
||||
disallow_all();
|
||||
set_allowing("POST", true);
|
||||
}
|
||||
server::server(app_config &config) : config_(config) {}
|
||||
|
||||
const std::shared_ptr<httpserver::http_response>
|
||||
server::rpc_resource::render(const httpserver::http_request &request) {
|
||||
std::shared_ptr<json_response> ret;
|
||||
|
||||
try {
|
||||
if (owner_.check_authorization(request)) {
|
||||
std::unique_ptr<jsonrpcpp::Response> response;
|
||||
auto entity = jsonrpcpp::Parser::do_parse(utils::string::to_utf8(request.get_content()));
|
||||
if (entity->is_request()) {
|
||||
auto request = std::dynamic_pointer_cast<jsonrpcpp::Request>(entity);
|
||||
if (not owner_.handle_request(request, response)) {
|
||||
throw jsonrpcpp::MethodNotFoundException(*request);
|
||||
}
|
||||
ret = std::make_shared<json_response>(response->to_json());
|
||||
}
|
||||
} else {
|
||||
ret = std::make_shared<json_response>(json({{"error", "unauthorized"}}),
|
||||
httpserver::http::http_utils::http_unauthorized);
|
||||
}
|
||||
} catch (const jsonrpcpp::RequestException &e) {
|
||||
ret = std::make_shared<json_response>(e.to_json(),
|
||||
httpserver::http::http_utils::http_bad_request);
|
||||
event_system::instance().raise<rpc_server_exception>(e.to_json().dump());
|
||||
} catch (const std::exception &e2) {
|
||||
ret = std::make_shared<json_response>(json({{"exception", e2.what()}}),
|
||||
httpserver::http::http_utils::http_internal_server_error);
|
||||
event_system::instance().raise<rpc_server_exception>(e2.what());
|
||||
auto server::check_authorization(const httplib::Request &req) -> bool {
|
||||
if (config_.get_api_auth().empty() || config_.get_api_user().empty()) {
|
||||
utils::error::raise_error(__FUNCTION__,
|
||||
"authorization user or password is not set");
|
||||
return false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
server::server(app_config &config) : config_(config), resource_(*this) {}
|
||||
|
||||
bool server::check_authorization(const httpserver::http_request &request) {
|
||||
auto ret = (config_.get_api_auth().empty() && config_.get_api_user().empty());
|
||||
if (not ret) {
|
||||
const auto authorization = request.get_header("Authorization");
|
||||
if (not authorization.empty()) {
|
||||
const auto auth_parts = utils::string::split(authorization, ' ');
|
||||
if (not auth_parts.empty()) {
|
||||
const auto auth_type = auth_parts[0];
|
||||
if (auth_type == "Basic") {
|
||||
const auto data = macaron::Base64::Decode(authorization.substr(6));
|
||||
const auto auth = utils::string::split(std::string(data.begin(), data.end()), ':');
|
||||
if (auth.size() == 2) {
|
||||
const auto &user = auth[0];
|
||||
const auto &pwd = auth[1];
|
||||
ret = (user == config_.get_api_user()) && (pwd == config_.get_api_auth());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const auto authorization = req.get_header_value("Authorization");
|
||||
if (authorization.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
const auto auth_parts = utils::string::split(authorization, ' ');
|
||||
if (auth_parts.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto auth_type = auth_parts[0U];
|
||||
if (auth_type != "Basic") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto data = macaron::Base64::Decode(authorization.substr(6U));
|
||||
const auto auth =
|
||||
utils::string::split(std::string(data.begin(), data.end()), ':');
|
||||
if (auth.size() != 2U) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto &user = auth[0U];
|
||||
const auto &pwd = auth[1U];
|
||||
return (user == config_.get_api_user()) && (pwd == config_.get_api_auth());
|
||||
}
|
||||
|
||||
bool server::handle_request(jsonrpcpp::request_ptr &request,
|
||||
std::unique_ptr<jsonrpcpp::Response> &response) {
|
||||
auto handled = true;
|
||||
if (request->method == rpc_method::get_config) {
|
||||
if (request->params.param_array.empty()) {
|
||||
response = std::make_unique<jsonrpcpp::Response>(*request, config_.get_json());
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else if (request->method == rpc_method::get_config_value_by_name) {
|
||||
if (request->params.param_array.size() == 1) {
|
||||
response = std::make_unique<jsonrpcpp::Response>(
|
||||
*request, json({{"value", config_.get_value_by_name(request->params.param_array[0])}}));
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else if (request->method == rpc_method::set_config_value_by_name) {
|
||||
if (request->params.param_array.size() == 2) {
|
||||
response = std::make_unique<jsonrpcpp::Response>(
|
||||
*request, json({{"value", config_.set_value_by_name(request->params.param_array[0],
|
||||
request->params.param_array[1])}}));
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else if (request->method == rpc_method::unmount) {
|
||||
if (request->params.param_array.empty()) {
|
||||
event_system::instance().raise<unmount_requested>();
|
||||
response = std::make_unique<jsonrpcpp::Response>(*request, json({{"success", true}}));
|
||||
} else {
|
||||
throw jsonrpcpp::InvalidParamsException(*request);
|
||||
}
|
||||
} else {
|
||||
handled = false;
|
||||
}
|
||||
return handled;
|
||||
void server::handle_get_config(const httplib::Request & /*req*/,
|
||||
httplib::Response &res) {
|
||||
auto data = config_.get_json();
|
||||
res.set_content(data.dump(), "application/json");
|
||||
res.status = 200;
|
||||
}
|
||||
|
||||
/*void server::Start() {
|
||||
mutex_lock l(startStopMutex_);
|
||||
if (not started_) {
|
||||
struct sockaddr_in localHost{};
|
||||
inet_pton(AF_INET, "127.0.0.1", &localHost.sin_addr);
|
||||
void server::handle_get_config_value_by_name(const httplib::Request &req,
|
||||
httplib::Response &res) {
|
||||
auto name = req.get_param_value("name");
|
||||
auto data = json({{"value", config_.get_value_by_name(name)}});
|
||||
res.set_content(data.dump(), "application/json");
|
||||
res.status = 200;
|
||||
}
|
||||
|
||||
ws_ = std::make_unique<httpserver::webserver>(
|
||||
httpserver::create_webserver(config_.GetAPIPort())
|
||||
.bind_address((sockaddr*)&localHost)
|
||||
.default_policy(httpserver::http::http_utils::REJECT));
|
||||
ws_->allow_ip("127.0.0.1");
|
||||
ws_->register_resource("/api", &rpcResource_);
|
||||
ws_->start(false);
|
||||
started_ = true;
|
||||
}
|
||||
}*/
|
||||
void server::handle_set_config_value_by_name(const httplib::Request &req,
|
||||
httplib::Response &res) {
|
||||
auto name = req.get_param_value("name");
|
||||
auto value = req.get_param_value("value");
|
||||
|
||||
json data = {{"value", config_.set_value_by_name(name, value)}};
|
||||
res.set_content(data.dump(), "application/json");
|
||||
res.status = 200;
|
||||
}
|
||||
|
||||
void server::handle_unmount(const httplib::Request & /*req*/,
|
||||
httplib::Response &res) {
|
||||
event_system::instance().raise<unmount_requested>();
|
||||
res.status = 200;
|
||||
}
|
||||
|
||||
void server::initialize(httplib::Server &inst) {
|
||||
inst.Get("/api/v1/" + rpc_method::get_config, [this](auto &&req, auto &&res) {
|
||||
handle_get_config(std::forward<decltype(req)>(req),
|
||||
std::forward<decltype(res)>(res));
|
||||
});
|
||||
|
||||
inst.Get("/api/v1/" + rpc_method::get_config_value_by_name,
|
||||
[this](auto &&req, auto &&res) {
|
||||
handle_get_config_value_by_name(std::forward<decltype(req)>(req),
|
||||
std::forward<decltype(res)>(res));
|
||||
});
|
||||
|
||||
inst.Post("/api/v1/" + rpc_method::set_config_value_by_name,
|
||||
[this](auto &&req, auto &&res) {
|
||||
handle_set_config_value_by_name(std::forward<decltype(req)>(req),
|
||||
std::forward<decltype(res)>(res));
|
||||
});
|
||||
|
||||
inst.Post("/api/v1/" + rpc_method::unmount, [this](auto &&req, auto &&res) {
|
||||
handle_unmount(std::forward<decltype(req)>(req),
|
||||
std::forward<decltype(res)>(res));
|
||||
});
|
||||
}
|
||||
|
||||
void server::start() {
|
||||
mutex_lock l(start_stop_mutex_);
|
||||
if (not started_) {
|
||||
ws_ = std::make_unique<httpserver::webserver>(
|
||||
httpserver::create_webserver(config_.get_api_port())
|
||||
.default_policy(httpserver::http::http_utils::REJECT));
|
||||
ws_->allow_ip("127.0.0.1");
|
||||
ws_->register_resource("/api", &resource_);
|
||||
ws_->start(false);
|
||||
event_system::instance().raise<service_started>("server");
|
||||
server_ = std::make_unique<httplib::Server>();
|
||||
|
||||
server_->set_exception_handler([](const httplib::Request &req,
|
||||
httplib::Response &res,
|
||||
std::exception_ptr ep) {
|
||||
json data = {{"path", req.path}};
|
||||
|
||||
try {
|
||||
std::rethrow_exception(ep);
|
||||
} catch (std::exception &e) {
|
||||
data["error"] = e.what() ? e.what() : "unknown error";
|
||||
utils::error::raise_error(__FUNCTION__, e,
|
||||
"failed request: " + req.path);
|
||||
} catch (...) {
|
||||
data["error"] = "unknown error";
|
||||
utils::error::raise_error(__FUNCTION__, "unknown error",
|
||||
"failed request: " + req.path);
|
||||
}
|
||||
|
||||
res.set_content(data.dump(), "application/json");
|
||||
res.status = 500;
|
||||
});
|
||||
|
||||
server_->set_pre_routing_handler(
|
||||
[this](auto &&req, auto &&res) -> httplib::Server::HandlerResponse {
|
||||
if (check_authorization(req)) {
|
||||
return httplib::Server::HandlerResponse::Unhandled;
|
||||
}
|
||||
|
||||
res.status = 401;
|
||||
return httplib::Server::HandlerResponse::Handled;
|
||||
});
|
||||
|
||||
initialize(*server_);
|
||||
|
||||
server_thread_ = std::make_unique<std::thread>(
|
||||
[this]() { server_->listen("127.0.0.1", config_.get_api_port()); });
|
||||
|
||||
started_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void server::stop() {
|
||||
mutex_lock l(start_stop_mutex_);
|
||||
if (started_) {
|
||||
event_system::instance().raise<service_shutdown>("server");
|
||||
ws_->stop();
|
||||
ws_.reset();
|
||||
started_ = false;
|
||||
mutex_lock l(start_stop_mutex_);
|
||||
if (started_) {
|
||||
event_system::instance().raise<service_shutdown_begin>("server");
|
||||
|
||||
server_->stop();
|
||||
server_thread_->join();
|
||||
server_thread_.reset();
|
||||
|
||||
started_ = false;
|
||||
event_system::instance().raise<service_shutdown_end>("server");
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace repertory
|
||||
|
Reference in New Issue
Block a user