initial commit
Some checks failed
BlockStorage/repertory_osx/pipeline/head There was a failure building this commit
BlockStorage/repertory_windows/pipeline/head This commit looks good
BlockStorage/repertory_linux_builds/pipeline/head This commit looks good

This commit is contained in:
2022-03-05 00:30:50 -06:00
commit 3ff46723b8
626 changed files with 178600 additions and 0 deletions

240
src/rpc/client/client.cpp Normal file
View File

@@ -0,0 +1,240 @@
/*
Copyright <2018-2022> <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
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 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;
}
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"];
}
return ret;
}
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"];
}
return ret;
}
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"];
}
return ret;
}
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"];
}
}
return ret;
}
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"];
}
return ret;
}
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"];
}
return ret;
}
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"];
}
return ret;
}
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 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;
}
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 *curl_handle = utils::create_curl();
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();
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]);
}
#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;
}));
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});
}
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"];
}
return ret;
}
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"];
}
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;
}
} // namespace repertory

View File

@@ -0,0 +1,165 @@
/*
Copyright <2018-2022> <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
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 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 "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/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) {}
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());
}
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);
}
return handled;
}
} // namespace repertory

163
src/rpc/server/server.cpp Normal file
View File

@@ -0,0 +1,163 @@
/*
Copyright <2018-2022> <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
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 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"
namespace repertory {
server::rpc_resource::rpc_resource(server &owner) : httpserver::http_resource(), owner_(owner) {
disallow_all();
set_allowing("POST", true);
}
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());
}
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());
}
}
}
}
}
return ret;
}
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::Start() {
mutex_lock l(startStopMutex_);
if (not started_) {
struct sockaddr_in localHost{};
inet_pton(AF_INET, "127.0.0.1", &localHost.sin_addr);
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::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);
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;
}
}
} // namespace repertory