From cfea1fe3e902d3f2bd48657d07a0f8d1dce29e1f Mon Sep 17 00:00:00 2001 From: Yevhen Viter Date: Mon, 18 Oct 2021 14:42:52 +0300 Subject: [PATCH 01/15] Initial commit --- .../peerplays_sidechain/CMakeLists.txt | 1 + .../peerplays_sidechain/common/https_call.cpp | 247 ++++++++++++++++++ .../peerplays_sidechain/common/https_call.h | 84 ++++++ .../peerplays_sidechain/common/rpc_client.cpp | 29 ++ 4 files changed, 361 insertions(+) create mode 100644 libraries/plugins/peerplays_sidechain/common/https_call.cpp create mode 100644 libraries/plugins/peerplays_sidechain/common/https_call.h diff --git a/libraries/plugins/peerplays_sidechain/CMakeLists.txt b/libraries/plugins/peerplays_sidechain/CMakeLists.txt index 48dd44f1..70e25460 100755 --- a/libraries/plugins/peerplays_sidechain/CMakeLists.txt +++ b/libraries/plugins/peerplays_sidechain/CMakeLists.txt @@ -15,6 +15,7 @@ add_library( peerplays_sidechain bitcoin/utils.cpp bitcoin/sign_bitcoin_transaction.cpp common/rpc_client.cpp + common/https_call.cpp common/utils.cpp hive/asset.cpp hive/operations.cpp diff --git a/libraries/plugins/peerplays_sidechain/common/https_call.cpp b/libraries/plugins/peerplays_sidechain/common/https_call.cpp new file mode 100644 index 00000000..40078501 --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/common/https_call.cpp @@ -0,0 +1,247 @@ +#include "https_call.h" + +#include +#include +#include + +#include +#include + + +namespace peerplays { +namespace net { + +namespace detail { + +static const char Cr = 0x0D; +static const char Lf = 0x0A; +static const char * CrLf = "\x0D\x0A"; +static const char * CrLfCrLf = "\x0D\x0A\x0D\x0A"; + +using namespace boost::asio; + +[[noreturn]] static void throwError(const std::string & msg) { + throw std::runtime_error(msg); +} + +class Impl { +public: + + Impl(const HttpsCall & call, const HttpRequest & request, HttpResponse & response) : + m_Call(call), + m_Request(request), + m_Response(response), + m_Service(), + m_Context(ssl::context::tlsv12), + m_Socket(m_Service, m_Context), + m_Endpoint(), + m_ResponseBuf(call.responseSizeLimitBytes()), + m_ContentLength(0) + {} + + void exec() { + resolve(); + connect(); + sendRequest(); + processResponse(); + } + +private: + + const HttpsCall & m_Call; + const HttpRequest & m_Request; + HttpResponse & m_Response; + + io_service m_Service; + ssl::context m_Context; + ssl::stream m_Socket; + ip::tcp::endpoint m_Endpoint; + streambuf m_ResponseBuf; + uint32_t m_ContentLength; + + static uint16_t u16Swap(uint16_t x) { + return ((x >> 8) & 0x00FF) | ((x << 8) & 0xFF00); + } + + void resolve() { + + // resolve TCP endpoint for host name + + ip::tcp::resolver resolver(m_Service); + auto query = ip::tcp::resolver::query(m_Call.host(), "https"); + auto iter = resolver.resolve(query); + m_Endpoint = *iter; + + if (m_Call.port() != 0) // if port was specified + m_Endpoint.port(u16Swap(m_Call.port())); // force set port + } + + void connect() { + + // TCP connect + + m_Socket.lowest_layer().connect(m_Endpoint); + + // SSL connect + + m_Socket.set_verify_mode(ssl::verify_none); + m_Socket.handshake(ssl::stream_base::client); + } + + void sendRequest() { + + streambuf requestBuf; + std::ostream stream(&requestBuf); + + // start string: method path HTTP/1.0 + + stream << m_Request.method << " " << m_Request.path << " HTTP/1.0" << CrLf; + + // host + + stream << "Host: " << m_Call.host() << ":" << u16Swap(m_Endpoint.port()) << CrLf; + + // content + + if (!m_Request.body.empty()) { + stream << "Content-Type: " << m_Request.contentType << CrLf; + stream << "Content-Length: " << m_Request.body.size() << CrLf; + } + + // additional headers + + const auto & h = m_Request.headers; + + if (!h.empty()) { + if (h.size() < 2) + throw 1; + stream << h; + if ((h.substr(h.size() - 2) != CrLf)) + stream << CrLf; + } + + // other + + stream << "Accept: *\x2F*" << CrLf; + stream << "Connection: close" << CrLf; + + // end + + stream << CrLf; + + // content + + if (!m_Request.body.empty()) + stream << m_Request.body; + + // send + + write(m_Socket, requestBuf); + } + + void helper1() { + + std::istream stream(&m_ResponseBuf); + + std::string httpVersion; + stream >> httpVersion; + stream >> m_Response.statusCode; + + if (!stream || httpVersion.substr(0, 5) != "HTTP/") { + throw "invalid response"; + } + + // read/skip headers + + for(;;) { + std::string header; + if (!std::getline(stream, header, Lf) || (header.size() == 1 && header[0] == Cr)) + break; + if (m_ContentLength) // if content length is already known + continue; // continue skipping headers + auto pos = header.find(':'); + if (pos == std::string::npos) + continue; + auto name = header.substr(0, pos); + boost::algorithm::trim(name); + boost::algorithm::to_lower(name); + if (name != "content-length") + continue; + auto value = header.substr(pos + 1); + boost::algorithm::trim(value); + m_ContentLength = std::stol(value); + } + + } + + void processResponse() { + + auto & socket = m_Socket; + auto & buf = m_ResponseBuf; + auto & contentLength = m_ContentLength; + auto & body = m_Response.body; + + read_until(socket, buf, CrLfCrLf); + + helper1(); + + if (contentLength < 2) // minimum content is "{}" + throwError("invalid response body (too short)"); + + if (contentLength > m_Call.responseSizeLimitBytes()) + throwError("response body size limit exceeded"); + + auto avail = buf.size(); + + if (avail > contentLength) + throwError("invalid response body (content length mismatch)"); + + body.resize(contentLength); + + if (avail) { + if (avail != buf.sgetn(&body[0], avail)) { + throwError("stream read failed"); + } + } + + auto rest = contentLength - avail; + + boost::system::error_code errorCode; + + read(socket, buffer(&body[avail], rest), errorCode); + + socket.shutdown(errorCode); + + } + +}; + +} // detail + +// HttpsCall + +HttpsCall::HttpsCall(const std::string & host, uint16_t port) : + m_Host(host), + m_Port(port) +{} + +bool HttpsCall::exec(const HttpRequest & request, HttpResponse * response) { + +// ASSERT(response); + auto & resp = *response; + + detail::Impl impl(*this, request, resp); + + try { + resp.clear(); + impl.exec(); + } catch (...) { + resp.clear(); + return false; + } + + return true; +} + +} // net +} // peerplays diff --git a/libraries/plugins/peerplays_sidechain/common/https_call.h b/libraries/plugins/peerplays_sidechain/common/https_call.h new file mode 100644 index 00000000..5a96d481 --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/common/https_call.h @@ -0,0 +1,84 @@ +#pragma once + +#include + +namespace peerplays { +namespace net { + +struct HttpRequest { + + std::string method; // ex: "POST" + std::string path; // ex: "/" + std::string headers; + std::string body; + std::string contentType; // ex: "application/json" + + HttpRequest() {} + + HttpRequest(const std::string & method_, const std::string & path_, const std::string & headers_, const std::string & body_, const std::string & contentType_) : + method(method_), + path(path_), + headers(headers_), + body(body_), + contentType(contentType_) + {} + + HttpRequest(const std::string & method_, const std::string & path_, const std::string & headers_, const std::string & body_ = std::string()) : + method(method_), + path(path_), + headers(headers_), + body(body_), + contentType("application/json") + {} + + void clear() { + method.clear(); + path.clear(); + headers.clear(); + body.clear(); + contentType.clear(); + } + +}; + +struct HttpResponse { + + uint16_t statusCode; + std::string body; + + void clear() { + statusCode = 0; + body = decltype(body)(); + } + +}; + +class HttpsCall { +public: + + static constexpr auto ResponseSizeLimitBytes = 1024 * 1024; + + HttpsCall(const std::string & host, uint16_t port = 0); + + bool exec(const HttpRequest & request, HttpResponse * response); + + const std::string host() const { + return m_Host; + } + + uint16_t port() const { + return m_Port; + } + + uint32_t responseSizeLimitBytes() const { + return ResponseSizeLimitBytes; + } + +private: + std::string m_Host; + uint16_t m_Port; +}; + + +} // net +} // peerplays diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index cb7c5251..b3d9c4e5 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -9,6 +9,8 @@ #include #include +#include "https_call.h" + namespace graphene { namespace peerplays_sidechain { rpc_client::rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password, bool _debug_rpc_calls) : @@ -100,6 +102,33 @@ std::string rpc_client::send_post_request(std::string method, std::string params } fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { + + using namespace peerplays::net; + + HttpRequest request("POST", "/", authorization.key + ":" + authorization.val, body); + + HttpsCall call(ip, port); + + HttpRequest response; + + fc::http::reply reply; + + if (call.exec(request, &response)) { + reply.status = response.statusCode; + reply.body.resize(response.body.size()); + memcpy(&reply.body[0], &response.body[0], response.body.size()); + } + + if (show_log) { + std::string url = "http://" + ip + ":" + std::to_string(port); + ilog("### Request URL: ${url}", ("url", url)); + ilog("### Request: ${body}", ("body", body)); + std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); + ilog("### Response: ${ss}", ("ss", ss.str())); + } + + return reply; + fc::http::connection conn; conn.connect_to(fc::ip::endpoint(fc::ip::address(ip), port)); -- 2.45.2 From 6618745cf749bc58c6bdbe9507baec449a42259e Mon Sep 17 00:00:00 2001 From: moss9001 Date: Wed, 20 Oct 2021 00:29:30 +0300 Subject: [PATCH 02/15] Fixed compile fail Minor error was --- .gitignore | 1 + libraries/plugins/peerplays_sidechain/common/rpc_client.cpp | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 39b23163..1a84b559 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ data CMakeDoxyfile.in build +build__* libraries/utilities/git_revision.cpp diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index b3d9c4e5..86a35844 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -109,7 +109,7 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { HttpsCall call(ip, port); - HttpRequest response; + HttpResponse response; fc::http::reply reply; @@ -128,7 +128,7 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { } return reply; - +/* fc::http::connection conn; conn.connect_to(fc::ip::endpoint(fc::ip::address(ip), port)); @@ -148,6 +148,7 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { } return reply; + */ } }} // namespace graphene::peerplays_sidechain -- 2.45.2 From ebb9ebb8c220f42d5f456988882c84f1db395ae0 Mon Sep 17 00:00:00 2001 From: moss9001 Date: Wed, 20 Oct 2021 18:44:14 +0300 Subject: [PATCH 03/15] Update https_call.cpp Minor changes --- .../plugins/peerplays_sidechain/common/https_call.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/common/https_call.cpp b/libraries/plugins/peerplays_sidechain/common/https_call.cpp index 40078501..4d8be9c3 100644 --- a/libraries/plugins/peerplays_sidechain/common/https_call.cpp +++ b/libraries/plugins/peerplays_sidechain/common/https_call.cpp @@ -32,12 +32,14 @@ public: m_Request(request), m_Response(response), m_Service(), - m_Context(ssl::context::tlsv12), + m_Context(ssl::context::tlsv12_client), m_Socket(m_Service, m_Context), m_Endpoint(), m_ResponseBuf(call.responseSizeLimitBytes()), m_ContentLength(0) - {} + { + m_Context.set_default_verify_paths(); + } void exec() { resolve(); @@ -84,7 +86,7 @@ private: // SSL connect - m_Socket.set_verify_mode(ssl::verify_none); + m_Socket.set_verify_mode(ssl::verify_peer); m_Socket.handshake(ssl::stream_base::client); } -- 2.45.2 From dc72582fb6b168dd3309711182e8fca1f4fbc726 Mon Sep 17 00:00:00 2001 From: moss9001 Date: Thu, 21 Oct 2021 00:39:43 +0300 Subject: [PATCH 04/15] Fixed port issue --- .../plugins/peerplays_sidechain/common/https_call.cpp | 10 ++++------ .../plugins/peerplays_sidechain/common/rpc_client.cpp | 6 ++++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/common/https_call.cpp b/libraries/plugins/peerplays_sidechain/common/https_call.cpp index 4d8be9c3..4e2b922a 100644 --- a/libraries/plugins/peerplays_sidechain/common/https_call.cpp +++ b/libraries/plugins/peerplays_sidechain/common/https_call.cpp @@ -7,6 +7,7 @@ #include #include +//#include namespace peerplays { namespace net { @@ -61,10 +62,6 @@ private: streambuf m_ResponseBuf; uint32_t m_ContentLength; - static uint16_t u16Swap(uint16_t x) { - return ((x >> 8) & 0x00FF) | ((x << 8) & 0xFF00); - } - void resolve() { // resolve TCP endpoint for host name @@ -75,7 +72,8 @@ private: m_Endpoint = *iter; if (m_Call.port() != 0) // if port was specified - m_Endpoint.port(u16Swap(m_Call.port())); // force set port + m_Endpoint.port(m_Call.port()); // force set port + } void connect() { @@ -101,7 +99,7 @@ private: // host - stream << "Host: " << m_Call.host() << ":" << u16Swap(m_Endpoint.port()) << CrLf; + stream << "Host: " << m_Call.host() << ":" << m_Endpoint.port() << CrLf; // content diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index 86a35844..34dda09b 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -120,11 +120,13 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { } if (show_log) { - std::string url = "http://" + ip + ":" + std::to_string(port); + std::string url = "https://" + ip + ":" + std::to_string(port); ilog("### Request URL: ${url}", ("url", url)); ilog("### Request: ${body}", ("body", body)); + ilog("### Response code: ${code}", ("code", response.statusCode)); + ilog("### Response len: ${len}", ("len", response.body.size())); std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); - ilog("### Response: ${ss}", ("ss", ss.str())); + ilog("### Response body: ${ss}", ("ss", ss.str())); } return reply; -- 2.45.2 From f59d34c0010248f85a08a63292513ab7766098f6 Mon Sep 17 00:00:00 2001 From: moss9001 Date: Thu, 21 Oct 2021 17:47:46 +0300 Subject: [PATCH 05/15] Update https_call.cpp --- .../plugins/peerplays_sidechain/common/https_call.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libraries/plugins/peerplays_sidechain/common/https_call.cpp b/libraries/plugins/peerplays_sidechain/common/https_call.cpp index 4e2b922a..796296ac 100644 --- a/libraries/plugins/peerplays_sidechain/common/https_call.cpp +++ b/libraries/plugins/peerplays_sidechain/common/https_call.cpp @@ -99,7 +99,14 @@ private: // host - stream << "Host: " << m_Call.host() << ":" << m_Endpoint.port() << CrLf; + stream << "Host: " << m_Call.host(); + + if (m_Call.port() != 0) { + //ASSERT(m_Endpoint.port() == m_Call.port()); + stream << ":" << m_Call.port(); + } + + stream << CrLf; // content -- 2.45.2 From 7237ac0e718abcbe2ea7fa7e3d7e261e4d9f4046 Mon Sep 17 00:00:00 2001 From: moss9001 Date: Thu, 21 Oct 2021 19:54:25 +0300 Subject: [PATCH 06/15] Intermediate --- .../peerplays_sidechain/CMakeLists.txt | 1 + .../peerplays_sidechain/common/net_utl.cpp | 22 ++++ .../peerplays_sidechain/common/net_utl.h | 11 ++ .../peerplays_sidechain/common/rpc_client.cpp | 103 ++++++++++++------ .../sidechain_net_handler_hive.cpp | 5 +- 5 files changed, 104 insertions(+), 38 deletions(-) create mode 100644 libraries/plugins/peerplays_sidechain/common/net_utl.cpp create mode 100644 libraries/plugins/peerplays_sidechain/common/net_utl.h diff --git a/libraries/plugins/peerplays_sidechain/CMakeLists.txt b/libraries/plugins/peerplays_sidechain/CMakeLists.txt index 70e25460..425be8fa 100755 --- a/libraries/plugins/peerplays_sidechain/CMakeLists.txt +++ b/libraries/plugins/peerplays_sidechain/CMakeLists.txt @@ -16,6 +16,7 @@ add_library( peerplays_sidechain bitcoin/sign_bitcoin_transaction.cpp common/rpc_client.cpp common/https_call.cpp + common/net_utl.cpp common/utils.cpp hive/asset.cpp hive/operations.cpp diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp new file mode 100644 index 00000000..81fe6673 --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp @@ -0,0 +1,22 @@ +#include "https_call.h" + +#include + + +namespace peerplays { +namespace net { + +std::string resolveHostAddr(const std::string & hostName) { + using namespace boost::asio; + io_service service; + ip::tcp::resolver resolver(service); + auto query = ip::tcp::resolver::query(hostName, ""); + auto iter = resolver.resolve(query); + auto endpoint = *iter; + auto addr = endpoint.address(); + return addr.to_string(); +} + + +} // net +} // peerplays diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.h b/libraries/plugins/peerplays_sidechain/common/net_utl.h new file mode 100644 index 00000000..96c00dd5 --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace peerplays { +namespace net { + +std::string resolveHostAddr(const std::string & hostName); + +} // net +} // peerplays diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index 34dda09b..6f7a7b26 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -10,6 +10,7 @@ #include #include "https_call.h" +#include "net_utl.h" namespace graphene { namespace peerplays_sidechain { @@ -103,54 +104,84 @@ std::string rpc_client::send_post_request(std::string method, std::string params fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { - using namespace peerplays::net; - - HttpRequest request("POST", "/", authorization.key + ":" + authorization.val, body); - - HttpsCall call(ip, port); - - HttpResponse response; - fc::http::reply reply; + auto temp = ip.substr(0, 6); + boost::algorithm::to_lower(temp); + + if (temp == "https:") { + + auto host = ip.substr(8); + + using namespace peerplays::net; + + HttpsCall call(host, port); + HttpRequest request("POST", "/", authorization.key + ":" + authorization.val, body); + HttpResponse response; + + if (call.exec(request, &response)) { + reply.status = response.statusCode; + reply.body.resize(response.body.size()); + memcpy(&reply.body[0], &response.body[0], response.body.size()); + } + + if (show_log) { + std::string url = ip + ":" + std::to_string(port); + ilog("### Request URL: ${url}", ("url", url)); + ilog("### Request: ${body}", ("body", body)); + ilog("### Response code: ${code}", ("code", response.statusCode)); + ilog("### Response len: ${len}", ("len", response.body.size())); + std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); + ilog("### Response body: ${ss}", ("ss", ss.str())); + } + + return reply; - if (call.exec(request, &response)) { - reply.status = response.statusCode; - reply.body.resize(response.body.size()); - memcpy(&reply.body[0], &response.body[0], response.body.size()); } + std::string host; + + if (temp == "http:/") + host = ip.substr(7); + else + host = ip; + + fc::ip::endpoint endpoint; + + try { + endpoint = fc::ip::endpoint(fc::ip::address(host), port)); + } catch (...) { + try { + endpoint = fc::ip::endpoint(fc::ip::address(peerplays::net::resolveHostIp(host)), port)); + } catch (...) { + if (show_log) { + std::string url = ip + ":" + std::to_string(port); + ilog("### Request URL: ${url}", ("url", url)); + ilog("### Request: ${body}", ("body", body)); + ilog("### Request: error: host address resolve failed"); + } + return reply; + } + } + + fc::http::connection conn; + conn.connect_to(endpoint); + std::string url = "http://" + host + ":" + std::to_string(port); + + //if (wallet.length() > 0) { + // url = url + "/wallet/" + wallet; + //} + + reply = conn.request("POST", url, body, fc::http::headers{authorization}); + if (show_log) { - std::string url = "https://" + ip + ":" + std::to_string(port); ilog("### Request URL: ${url}", ("url", url)); ilog("### Request: ${body}", ("body", body)); - ilog("### Response code: ${code}", ("code", response.statusCode)); - ilog("### Response len: ${len}", ("len", response.body.size())); std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); - ilog("### Response body: ${ss}", ("ss", ss.str())); + ilog("### Response: ${ss}", ("ss", ss.str())); } return reply; -/* - fc::http::connection conn; - conn.connect_to(fc::ip::endpoint(fc::ip::address(ip), port)); - std::string url = "http://" + ip + ":" + std::to_string(port); - - //if (wallet.length() > 0) { - // url = url + "/wallet/" + wallet; - //} - - fc::http::reply reply = conn.request("POST", url, body, fc::http::headers{authorization}); - - if (show_log) { - ilog("### Request URL: ${url}", ("url", url)); - ilog("### Request: ${body}", ("body", body)); - std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); - ilog("### Response: ${ss}", ("ss", ss.str())); - } - - return reply; - */ } }} // namespace graphene::peerplays_sidechain diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp index b1945588..ac96da16 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp @@ -144,15 +144,16 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi private_keys[key_pair.first] = key_pair.second; } } - +/* fc::http::connection conn; + try { conn.connect_to(fc::ip::endpoint(fc::ip::address(node_ip), node_rpc_port)); } catch (fc::exception &e) { elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port)); FC_ASSERT(false); } - +*/ node_rpc_client = new hive_node_rpc_client(node_ip, node_rpc_port, node_rpc_user, node_rpc_password, debug_rpc_calls); std::string chain_id_str = node_rpc_client->get_chain_id(); -- 2.45.2 From 2826f2e83f2cf916d4d4b9f3e54ccfdcbe407c4d Mon Sep 17 00:00:00 2001 From: moss9001 Date: Thu, 21 Oct 2021 21:34:42 +0300 Subject: [PATCH 07/15] net utl added --- .../peerplays_sidechain/common/net_utl.cpp | 11 +++++- .../peerplays_sidechain/common/net_utl.h | 2 + .../peerplays_sidechain/common/rpc_client.cpp | 37 +++++++++++-------- .../sidechain_net_handler_hive.cpp | 20 ++++++++-- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp index 81fe6673..d895193d 100644 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp @@ -1,4 +1,4 @@ -#include "https_call.h" +#include "net_utl.h" #include @@ -13,10 +13,17 @@ std::string resolveHostAddr(const std::string & hostName) { auto query = ip::tcp::resolver::query(hostName, ""); auto iter = resolver.resolve(query); auto endpoint = *iter; - auto addr = endpoint.address(); + auto addr = ((ip::tcp::endpoint)endpoint).address(); return addr.to_string(); } +std::string stripProtoName(const std::string & url) { + auto index = url.find("://"); + if (index == url::npos) + return url; + return url.substr(index + 3); +} + } // net } // peerplays diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.h b/libraries/plugins/peerplays_sidechain/common/net_utl.h index 96c00dd5..d549fef3 100644 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.h +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.h @@ -6,6 +6,8 @@ namespace peerplays { namespace net { std::string resolveHostAddr(const std::string & hostName); +std::string stripProtoName(const std::string & utl); + } // net } // peerplays diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index 6f7a7b26..32edfc2d 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -105,12 +105,12 @@ std::string rpc_client::send_post_request(std::string method, std::string params fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { fc::http::reply reply; - auto temp = ip.substr(0, 6); - boost::algorithm::to_lower(temp); + auto start = ip.substr(0, 6); + boost::algorithm::to_lower(start); - if (temp == "https:") { + if (start == "https:") { - auto host = ip.substr(8); + auto host = ip.substr(8); // skip "https://" using namespace peerplays::net; @@ -140,18 +140,19 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { std::string host; - if (temp == "http:/") - host = ip.substr(7); + if (start == "http:/") + host = ip.substr(7); // skip "http://" else host = ip; - fc::ip::endpoint endpoint; + std::string url = "http://" + host + ":" + std::to_string(port); + fc::ip::address addr; try { - endpoint = fc::ip::endpoint(fc::ip::address(host), port)); + addr = fc::ip::address(host); } catch (...) { try { - endpoint = fc::ip::endpoint(fc::ip::address(peerplays::net::resolveHostIp(host)), port)); + addr = fc::ip::address(peerplays::net::resolveHostIp(host)); } catch (...) { if (show_log) { std::string url = ip + ":" + std::to_string(port); @@ -163,15 +164,19 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { } } - fc::http::connection conn; - conn.connect_to(endpoint); - std::string url = "http://" + host + ":" + std::to_string(port); + try { - //if (wallet.length() > 0) { - // url = url + "/wallet/" + wallet; - //} + fc::http::connection conn; + conn.connect_to(fc::ip::endpoint(addr, port)); - reply = conn.request("POST", url, body, fc::http::headers{authorization}); + //if (wallet.length() > 0) { + // url = url + "/wallet/" + wallet; + //} + + reply = conn.request("POST", url, body, fc::http::headers{authorization}); + + } catch (...) { + } if (show_log) { ilog("### Request URL: ${url}", ("url", url)); diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp index ac96da16..8e270c4c 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp @@ -28,6 +28,8 @@ #include #include +#include "common/net_utl.h" + namespace graphene { namespace peerplays_sidechain { hive_node_rpc_client::hive_node_rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password, bool _debug_rpc_calls) : @@ -144,16 +146,28 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi private_keys[key_pair.first] = key_pair.second; } } -/* + fc::http::connection conn; try { - conn.connect_to(fc::ip::endpoint(fc::ip::address(node_ip), node_rpc_port)); + auto host = peerplays::net::stripProtoName(node_ip); + fc::ip::address addr; + try { + addr = fc::ip::address(host); + } catch (...) { + try { + addr = fc::ip::address(peerplays::net::resolveHostIp(host)); + } catch (...) { + elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); + FC_ASSERT(false); + } + } + conn.connect_to(fc::ip::endpoint(addr, node_rpc_port)); } catch (fc::exception &e) { elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port)); FC_ASSERT(false); } -*/ + node_rpc_client = new hive_node_rpc_client(node_ip, node_rpc_port, node_rpc_user, node_rpc_password, debug_rpc_calls); std::string chain_id_str = node_rpc_client->get_chain_id(); -- 2.45.2 From 7ddad07df2f5e5865986cbc98f0110bd063847b8 Mon Sep 17 00:00:00 2001 From: moss9001 Date: Fri, 22 Oct 2021 00:20:37 +0300 Subject: [PATCH 08/15] Tested IP of api.hive.blog = 51.79.10.214 Not work: hive-node-ip = 51.79.10.214 hive-node-rpc-port = 443 - bad request! Work: hive-node-ip = https://51.79.10.214 hive-node-rpc-port = 443 Work: hive-node-ip = https://api.hive.blog hive-node-rpc-port = 443 Work: hive-node-ip = 10.11.12.202 hive-node-rpc-port = 28090 Work: hive-node-ip = http://10.11.12.202 hive-node-rpc-port = 28090 Work: hive-node-ip = http://localhost hive-node-rpc-port = 28090 Work: hive-node-ip = localhost hive-node-rpc-port = 28090 --- libraries/plugins/peerplays_sidechain/common/net_utl.cpp | 3 +-- libraries/plugins/peerplays_sidechain/common/net_utl.h | 2 +- libraries/plugins/peerplays_sidechain/common/rpc_client.cpp | 4 +++- .../peerplays_sidechain/sidechain_net_handler_hive.cpp | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp index d895193d..6037438a 100644 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp @@ -19,11 +19,10 @@ std::string resolveHostAddr(const std::string & hostName) { std::string stripProtoName(const std::string & url) { auto index = url.find("://"); - if (index == url::npos) + if (index == std::string::npos) return url; return url.substr(index + 3); } - } // net } // peerplays diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.h b/libraries/plugins/peerplays_sidechain/common/net_utl.h index d549fef3..22533aa0 100644 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.h +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.h @@ -6,7 +6,7 @@ namespace peerplays { namespace net { std::string resolveHostAddr(const std::string & hostName); -std::string stripProtoName(const std::string & utl); +std::string stripProtoName(const std::string & url); } // net diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index 32edfc2d..2774e062 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -9,6 +9,8 @@ #include #include +#include + #include "https_call.h" #include "net_utl.h" @@ -152,7 +154,7 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { addr = fc::ip::address(host); } catch (...) { try { - addr = fc::ip::address(peerplays::net::resolveHostIp(host)); + addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); } catch (...) { if (show_log) { std::string url = ip + ":" + std::to_string(port); diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp index 8e270c4c..3565416b 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp @@ -156,7 +156,7 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi addr = fc::ip::address(host); } catch (...) { try { - addr = fc::ip::address(peerplays::net::resolveHostIp(host)); + addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); } catch (...) { elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); FC_ASSERT(false); -- 2.45.2 From 1120e55f993f54af607ac7f580454ad5a1852fcf Mon Sep 17 00:00:00 2001 From: moss9001 Date: Fri, 22 Oct 2021 13:34:56 +0300 Subject: [PATCH 09/15] Comments update and minor fixes --- .../peerplays_sidechain/common/https_call.cpp | 335 +++++++++--------- .../peerplays_sidechain/common/https_call.h | 6 +- .../peerplays_sidechain/common/net_utl.cpp | 33 +- .../peerplays_sidechain/common/net_utl.h | 5 + .../peerplays_sidechain/common/rpc_client.cpp | 128 ++++--- .../sidechain_net_handler_hive.cpp | 27 +- 6 files changed, 269 insertions(+), 265 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/common/https_call.cpp b/libraries/plugins/peerplays_sidechain/common/https_call.cpp index 796296ac..93136b32 100644 --- a/libraries/plugins/peerplays_sidechain/common/https_call.cpp +++ b/libraries/plugins/peerplays_sidechain/common/https_call.cpp @@ -1,11 +1,11 @@ #include "https_call.h" #include -#include #include +#include -#include #include +#include //#include @@ -16,239 +16,238 @@ namespace detail { static const char Cr = 0x0D; static const char Lf = 0x0A; -static const char * CrLf = "\x0D\x0A"; -static const char * CrLfCrLf = "\x0D\x0A\x0D\x0A"; +static const char *CrLf = "\x0D\x0A"; +static const char *CrLfCrLf = "\x0D\x0A\x0D\x0A"; using namespace boost::asio; -[[noreturn]] static void throwError(const std::string & msg) { - throw std::runtime_error(msg); +[[noreturn]] static void throwError(const std::string &msg) { + throw std::runtime_error(msg); } class Impl { public: + Impl(const HttpsCall &call, const HttpRequest &request, HttpResponse &response) : + m_Call(call), + m_Request(request), + m_Response(response), + m_Service(), + m_Context(ssl::context::tlsv12_client), + m_Socket(m_Service, m_Context), + m_Endpoint(), + m_ResponseBuf(call.responseSizeLimitBytes()), + m_ContentLength(0) { + m_Context.set_default_verify_paths(); + } - Impl(const HttpsCall & call, const HttpRequest & request, HttpResponse & response) : - m_Call(call), - m_Request(request), - m_Response(response), - m_Service(), - m_Context(ssl::context::tlsv12_client), - m_Socket(m_Service, m_Context), - m_Endpoint(), - m_ResponseBuf(call.responseSizeLimitBytes()), - m_ContentLength(0) - { - m_Context.set_default_verify_paths(); - } - - void exec() { - resolve(); - connect(); - sendRequest(); - processResponse(); - } + void exec() { + resolve(); + connect(); + sendRequest(); + processResponse(); + } private: + const HttpsCall &m_Call; + const HttpRequest &m_Request; + HttpResponse &m_Response; - const HttpsCall & m_Call; - const HttpRequest & m_Request; - HttpResponse & m_Response; + io_service m_Service; + ssl::context m_Context; + ssl::stream m_Socket; + ip::tcp::endpoint m_Endpoint; + streambuf m_ResponseBuf; + uint32_t m_ContentLength; - io_service m_Service; - ssl::context m_Context; - ssl::stream m_Socket; - ip::tcp::endpoint m_Endpoint; - streambuf m_ResponseBuf; - uint32_t m_ContentLength; + void resolve() { - void resolve() { + // resolve TCP endpoint for host name - // resolve TCP endpoint for host name + ip::tcp::resolver resolver(m_Service); + auto query = ip::tcp::resolver::query(m_Call.host(), "https"); + auto iter = resolver.resolve(query); + m_Endpoint = *iter; - ip::tcp::resolver resolver(m_Service); - auto query = ip::tcp::resolver::query(m_Call.host(), "https"); - auto iter = resolver.resolve(query); - m_Endpoint = *iter; + if (m_Call.port() != 0) // if port was specified + m_Endpoint.port(m_Call.port()); // force set port + } - if (m_Call.port() != 0) // if port was specified - m_Endpoint.port(m_Call.port()); // force set port + void connect() { - } + // TCP connect - void connect() { + m_Socket.lowest_layer().connect(m_Endpoint); - // TCP connect + // SSL connect - m_Socket.lowest_layer().connect(m_Endpoint); + m_Socket.set_verify_mode(ssl::verify_peer); + m_Socket.handshake(ssl::stream_base::client); + } - // SSL connect + void sendRequest() { - m_Socket.set_verify_mode(ssl::verify_peer); - m_Socket.handshake(ssl::stream_base::client); - } + streambuf requestBuf; + std::ostream stream(&requestBuf); - void sendRequest() { + // start string: HTTP/1.0 - streambuf requestBuf; - std::ostream stream(&requestBuf); + stream << m_Request.method << " " << m_Request.path << " HTTP/1.0" << CrLf; - // start string: method path HTTP/1.0 + // host - stream << m_Request.method << " " << m_Request.path << " HTTP/1.0" << CrLf; + stream << "Host: " << m_Call.host(); - // host + if (m_Call.port() != 0) { + //ASSERT(m_Endpoint.port() == m_Call.port()); + stream << ":" << m_Call.port(); + } - stream << "Host: " << m_Call.host(); + stream << CrLf; - if (m_Call.port() != 0) { - //ASSERT(m_Endpoint.port() == m_Call.port()); - stream << ":" << m_Call.port(); - } + // content - stream << CrLf; + if (!m_Request.body.empty()) { + stream << "Content-Type: " << m_Request.contentType << CrLf; + stream << "Content-Length: " << m_Request.body.size() << CrLf; + } - // content + // additional headers - if (!m_Request.body.empty()) { - stream << "Content-Type: " << m_Request.contentType << CrLf; - stream << "Content-Length: " << m_Request.body.size() << CrLf; - } + const auto &h = m_Request.headers; - // additional headers + if (!h.empty()) { + if (h.size() < 2) + throwError("invalid headers data"); + stream << h; + // ensure headers finished correctly + if ((h.substr(h.size() - 2) != CrLf)) + stream << CrLf; + } - const auto & h = m_Request.headers; + // other - if (!h.empty()) { - if (h.size() < 2) - throw 1; - stream << h; - if ((h.substr(h.size() - 2) != CrLf)) - stream << CrLf; - } + stream << "Accept: *\x2F*" << CrLf; + stream << "Connection: close" << CrLf; - // other + // end - stream << "Accept: *\x2F*" << CrLf; - stream << "Connection: close" << CrLf; + stream << CrLf; - // end + // content - stream << CrLf; + if (!m_Request.body.empty()) + stream << m_Request.body; - // content + // send - if (!m_Request.body.empty()) - stream << m_Request.body; + write(m_Socket, requestBuf); + } - // send + void processHeaders() { - write(m_Socket, requestBuf); - } + std::istream stream(&m_ResponseBuf); - void helper1() { + std::string httpVersion; + stream >> httpVersion; + stream >> m_Response.statusCode; - std::istream stream(&m_ResponseBuf); + if (!stream || httpVersion.substr(0, 5) != "HTTP/") { + throwError("invalid response"); + } - std::string httpVersion; - stream >> httpVersion; - stream >> m_Response.statusCode; - - if (!stream || httpVersion.substr(0, 5) != "HTTP/") { - throw "invalid response"; - } + // read/skip headers - // read/skip headers + for (;;) { + std::string header; + if (!std::getline(stream, header, Lf) || (header.size() == 1 && header[0] == Cr)) + break; + if (m_ContentLength) // if content length is already known + continue; // continue skipping headers + auto pos = header.find(':'); + if (pos == std::string::npos) + continue; + auto name = header.substr(0, pos); + boost::algorithm::trim(name); + boost::algorithm::to_lower(name); + if (name != "content-length") + continue; + auto value = header.substr(pos + 1); + boost::algorithm::trim(value); + m_ContentLength = std::stol(value); + } + } - for(;;) { - std::string header; - if (!std::getline(stream, header, Lf) || (header.size() == 1 && header[0] == Cr)) - break; - if (m_ContentLength) // if content length is already known - continue; // continue skipping headers - auto pos = header.find(':'); - if (pos == std::string::npos) - continue; - auto name = header.substr(0, pos); - boost::algorithm::trim(name); - boost::algorithm::to_lower(name); - if (name != "content-length") - continue; - auto value = header.substr(pos + 1); - boost::algorithm::trim(value); - m_ContentLength = std::stol(value); - } + void processResponse() { - } + auto &socket = m_Socket; + auto &buf = m_ResponseBuf; + auto &contentLength = m_ContentLength; + auto &body = m_Response.body; - void processResponse() { + read_until(socket, buf, CrLfCrLf); - auto & socket = m_Socket; - auto & buf = m_ResponseBuf; - auto & contentLength = m_ContentLength; - auto & body = m_Response.body; + processHeaders(); - read_until(socket, buf, CrLfCrLf); + // check content length - helper1(); + if (contentLength < 2) // minimum content is "{}" + throwError("invalid response body (too short)"); - if (contentLength < 2) // minimum content is "{}" - throwError("invalid response body (too short)"); + if (contentLength > m_Call.responseSizeLimitBytes()) + throwError("response body size limit exceeded"); - if (contentLength > m_Call.responseSizeLimitBytes()) - throwError("response body size limit exceeded"); + // read body - auto avail = buf.size(); + auto avail = buf.size(); // size of body data already stored in the buffer - if (avail > contentLength) - throwError("invalid response body (content length mismatch)"); + if (avail > contentLength) + throwError("invalid response body (content length mismatch)"); - body.resize(contentLength); + body.resize(contentLength); - if (avail) { - if (avail != buf.sgetn(&body[0], avail)) { - throwError("stream read failed"); - } - } + if (avail) { + // copy already existing data + if (avail != buf.sgetn(&body[0], avail)) { + throwError("stream read failed"); + } + } - auto rest = contentLength - avail; + auto rest = contentLength - avail; // size of remaining part of response body - boost::system::error_code errorCode; + boost::system::error_code errorCode; - read(socket, buffer(&body[avail], rest), errorCode); - - socket.shutdown(errorCode); - - } + read(socket, buffer(&body[avail], rest), errorCode); // read remaining part + socket.shutdown(errorCode); + } }; -} // detail +} // namespace detail // HttpsCall -HttpsCall::HttpsCall(const std::string & host, uint16_t port) : - m_Host(host), - m_Port(port) -{} - -bool HttpsCall::exec(const HttpRequest & request, HttpResponse * response) { - -// ASSERT(response); - auto & resp = *response; - - detail::Impl impl(*this, request, resp); - - try { - resp.clear(); - impl.exec(); - } catch (...) { - resp.clear(); - return false; - } - - return true; +HttpsCall::HttpsCall(const std::string &host, uint16_t port) : + m_Host(host), + m_Port(port) { } -} // net -} // peerplays +bool HttpsCall::exec(const HttpRequest &request, HttpResponse *response) { + + // ASSERT(response); + auto &resp = *response; + + detail::Impl impl(*this, request, resp); + + try { + resp.clear(); + impl.exec(); + } catch (...) { + resp.clear(); + return false; + } + + return true; +} + +} +} // namespace peerplays::net diff --git a/libraries/plugins/peerplays_sidechain/common/https_call.h b/libraries/plugins/peerplays_sidechain/common/https_call.h index 5a96d481..595b4a58 100644 --- a/libraries/plugins/peerplays_sidechain/common/https_call.h +++ b/libraries/plugins/peerplays_sidechain/common/https_call.h @@ -60,9 +60,7 @@ public: HttpsCall(const std::string & host, uint16_t port = 0); - bool exec(const HttpRequest & request, HttpResponse * response); - - const std::string host() const { + const std::string & host() const { return m_Host; } @@ -74,6 +72,8 @@ public: return ResponseSizeLimitBytes; } + bool exec(const HttpRequest & request, HttpResponse * response); + private: std::string m_Host; uint16_t m_Port; diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp index 6037438a..07271b81 100644 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp @@ -2,27 +2,26 @@ #include - namespace peerplays { namespace net { -std::string resolveHostAddr(const std::string & hostName) { - using namespace boost::asio; - io_service service; - ip::tcp::resolver resolver(service); - auto query = ip::tcp::resolver::query(hostName, ""); - auto iter = resolver.resolve(query); - auto endpoint = *iter; - auto addr = ((ip::tcp::endpoint)endpoint).address(); - return addr.to_string(); +std::string resolveHostAddr(const std::string &hostName) { + using namespace boost::asio; + io_service service; + ip::tcp::resolver resolver(service); + auto query = ip::tcp::resolver::query(hostName, std::string()); + auto iter = resolver.resolve(query); + auto endpoint = *iter; + auto addr = ((ip::tcp::endpoint)endpoint).address(); + return addr.to_string(); } -std::string stripProtoName(const std::string & url) { - auto index = url.find("://"); - if (index == std::string::npos) - return url; - return url.substr(index + 3); +std::string stripProtoName(const std::string &url) { + auto index = url.find("://"); + if (index == std::string::npos) + return url; + return url.substr(index + 3); } -} // net -} // peerplays +} +} // namespace peerplays::net diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.h b/libraries/plugins/peerplays_sidechain/common/net_utl.h index 22533aa0..6cdd2cd5 100644 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.h +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.h @@ -5,7 +5,12 @@ namespace peerplays { namespace net { +// resolve IP address by host name +// ex: api.hive.blog -> 52.79.10.214 std::string resolveHostAddr(const std::string & hostName); + +// remove schema part from URL +// ex: http://api.hive.blog -> api.hive.blog std::string stripProtoName(const std::string & url); diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index 2774e062..d3b9d46e 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -106,89 +106,87 @@ std::string rpc_client::send_post_request(std::string method, std::string params fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { - fc::http::reply reply; - auto start = ip.substr(0, 6); - boost::algorithm::to_lower(start); + fc::http::reply reply; + auto start = ip.substr(0, 6); + boost::algorithm::to_lower(start); - if (start == "https:") { + if (start == "https:") { - auto host = ip.substr(8); // skip "https://" + auto host = ip.substr(8); // skip "https://" - using namespace peerplays::net; + using namespace peerplays::net; - HttpsCall call(host, port); - HttpRequest request("POST", "/", authorization.key + ":" + authorization.val, body); - HttpResponse response; + HttpsCall call(host, port); + HttpRequest request("POST", "/", authorization.key + ":" + authorization.val, body); + HttpResponse response; - if (call.exec(request, &response)) { - reply.status = response.statusCode; - reply.body.resize(response.body.size()); - memcpy(&reply.body[0], &response.body[0], response.body.size()); - } + if (call.exec(request, &response)) { + reply.status = response.statusCode; + reply.body.resize(response.body.size()); + memcpy(&reply.body[0], &response.body[0], response.body.size()); + } - if (show_log) { - std::string url = ip + ":" + std::to_string(port); - ilog("### Request URL: ${url}", ("url", url)); - ilog("### Request: ${body}", ("body", body)); - ilog("### Response code: ${code}", ("code", response.statusCode)); - ilog("### Response len: ${len}", ("len", response.body.size())); - std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); - ilog("### Response body: ${ss}", ("ss", ss.str())); - } + if (show_log) { + std::string url = ip + ":" + std::to_string(port); + ilog("### Request URL: ${url}", ("url", url)); + ilog("### Request: ${body}", ("body", body)); + ilog("### Response code: ${code}", ("code", response.statusCode)); + ilog("### Response len: ${len}", ("len", response.body.size())); + std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); + ilog("### Response body: ${ss}", ("ss", ss.str())); + } - return reply; + return reply; + } - } + std::string host; - std::string host; + if (start == "http:/") + host = ip.substr(7); // skip "http://" + else + host = ip; - if (start == "http:/") - host = ip.substr(7); // skip "http://" - else - host = ip; + std::string url = "http://" + host + ":" + std::to_string(port); + fc::ip::address addr; - std::string url = "http://" + host + ":" + std::to_string(port); - fc::ip::address addr; + try { + addr = fc::ip::address(host); + } catch (...) { + try { + addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); + } catch (...) { + if (show_log) { + std::string url = ip + ":" + std::to_string(port); + ilog("### Request URL: ${url}", ("url", url)); + ilog("### Request: ${body}", ("body", body)); + ilog("### Request: error: host address resolve failed"); + } + return reply; + } + } - try { - addr = fc::ip::address(host); - } catch (...) { - try { - addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); - } catch (...) { - if (show_log) { - std::string url = ip + ":" + std::to_string(port); - ilog("### Request URL: ${url}", ("url", url)); - ilog("### Request: ${body}", ("body", body)); - ilog("### Request: error: host address resolve failed"); - } - return reply; - } - } + try { - try { + fc::http::connection conn; + conn.connect_to(fc::ip::endpoint(addr, port)); - fc::http::connection conn; - conn.connect_to(fc::ip::endpoint(addr, port)); + //if (wallet.length() > 0) { + // url = url + "/wallet/" + wallet; + //} - //if (wallet.length() > 0) { - // url = url + "/wallet/" + wallet; - //} + reply = conn.request("POST", url, body, fc::http::headers{authorization}); - reply = conn.request("POST", url, body, fc::http::headers{authorization}); + } catch (...) { + } - } catch (...) { - } - - if (show_log) { - ilog("### Request URL: ${url}", ("url", url)); - ilog("### Request: ${body}", ("body", body)); - std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); - ilog("### Response: ${ss}", ("ss", ss.str())); - } - - return reply; + if (show_log) { + ilog("### Request URL: ${url}", ("url", url)); + ilog("### Request: ${body}", ("body", body)); + std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); + ilog("### Response: ${ss}", ("ss", ss.str())); + } + return reply; } }} // namespace graphene::peerplays_sidechain diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp index 3565416b..3dfc9aee 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp @@ -150,18 +150,21 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi fc::http::connection conn; try { - auto host = peerplays::net::stripProtoName(node_ip); - fc::ip::address addr; - try { - addr = fc::ip::address(host); - } catch (...) { - try { - addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); - } catch (...) { - elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); - FC_ASSERT(false); - } - } + auto host = peerplays::net::stripProtoName(node_ip); + fc::ip::address addr; + try { + // IP address assumed + addr = fc::ip::address(host); + } catch (...) { + try { + // host name assumed + addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); + } catch (...) { + elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); + FC_ASSERT(false); + } + } + // try to connect to TCP endpoint conn.connect_to(fc::ip::endpoint(addr, node_rpc_port)); } catch (fc::exception &e) { elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port)); -- 2.45.2 From f9cfd542959c8e38e0fc647274537453770997da Mon Sep 17 00:00:00 2001 From: moss9001 Date: Fri, 22 Oct 2021 13:52:53 +0300 Subject: [PATCH 10/15] Create format.sh Format batch file added --- libraries/plugins/peerplays_sidechain/format.sh | 2 ++ 1 file changed, 2 insertions(+) create mode 100755 libraries/plugins/peerplays_sidechain/format.sh diff --git a/libraries/plugins/peerplays_sidechain/format.sh b/libraries/plugins/peerplays_sidechain/format.sh new file mode 100755 index 00000000..7b739d7f --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/format.sh @@ -0,0 +1,2 @@ +#!/bin/bash +find . -regex ".*[c|h]pp" | xargs clang-format -i -- 2.45.2 From 32987edb2a13508e95cdeb7c573bf01b2d899674 Mon Sep 17 00:00:00 2001 From: moss9001 Date: Fri, 22 Oct 2021 18:45:16 +0300 Subject: [PATCH 11/15] Applying comments --- .../peerplays_sidechain/CMakeLists.txt | 1 - .../peerplays_sidechain/common/https_call.cpp | 253 -------------- .../peerplays_sidechain/common/https_call.h | 84 ----- .../peerplays_sidechain/common/net_utl.cpp | 14 +- .../peerplays_sidechain/common/net_utl.h | 18 - .../peerplays_sidechain/common/net_utl.hpp | 15 + .../peerplays_sidechain/common/rpc_client.cpp | 329 +++++++++++++++++- .../sidechain_net_handler_hive.cpp | 6 +- 8 files changed, 343 insertions(+), 377 deletions(-) delete mode 100644 libraries/plugins/peerplays_sidechain/common/https_call.cpp delete mode 100644 libraries/plugins/peerplays_sidechain/common/https_call.h delete mode 100644 libraries/plugins/peerplays_sidechain/common/net_utl.h create mode 100644 libraries/plugins/peerplays_sidechain/common/net_utl.hpp diff --git a/libraries/plugins/peerplays_sidechain/CMakeLists.txt b/libraries/plugins/peerplays_sidechain/CMakeLists.txt index 425be8fa..02e8cb33 100755 --- a/libraries/plugins/peerplays_sidechain/CMakeLists.txt +++ b/libraries/plugins/peerplays_sidechain/CMakeLists.txt @@ -15,7 +15,6 @@ add_library( peerplays_sidechain bitcoin/utils.cpp bitcoin/sign_bitcoin_transaction.cpp common/rpc_client.cpp - common/https_call.cpp common/net_utl.cpp common/utils.cpp hive/asset.cpp diff --git a/libraries/plugins/peerplays_sidechain/common/https_call.cpp b/libraries/plugins/peerplays_sidechain/common/https_call.cpp deleted file mode 100644 index 93136b32..00000000 --- a/libraries/plugins/peerplays_sidechain/common/https_call.cpp +++ /dev/null @@ -1,253 +0,0 @@ -#include "https_call.h" - -#include -#include -#include - -#include -#include - -//#include - -namespace peerplays { -namespace net { - -namespace detail { - -static const char Cr = 0x0D; -static const char Lf = 0x0A; -static const char *CrLf = "\x0D\x0A"; -static const char *CrLfCrLf = "\x0D\x0A\x0D\x0A"; - -using namespace boost::asio; - -[[noreturn]] static void throwError(const std::string &msg) { - throw std::runtime_error(msg); -} - -class Impl { -public: - Impl(const HttpsCall &call, const HttpRequest &request, HttpResponse &response) : - m_Call(call), - m_Request(request), - m_Response(response), - m_Service(), - m_Context(ssl::context::tlsv12_client), - m_Socket(m_Service, m_Context), - m_Endpoint(), - m_ResponseBuf(call.responseSizeLimitBytes()), - m_ContentLength(0) { - m_Context.set_default_verify_paths(); - } - - void exec() { - resolve(); - connect(); - sendRequest(); - processResponse(); - } - -private: - const HttpsCall &m_Call; - const HttpRequest &m_Request; - HttpResponse &m_Response; - - io_service m_Service; - ssl::context m_Context; - ssl::stream m_Socket; - ip::tcp::endpoint m_Endpoint; - streambuf m_ResponseBuf; - uint32_t m_ContentLength; - - void resolve() { - - // resolve TCP endpoint for host name - - ip::tcp::resolver resolver(m_Service); - auto query = ip::tcp::resolver::query(m_Call.host(), "https"); - auto iter = resolver.resolve(query); - m_Endpoint = *iter; - - if (m_Call.port() != 0) // if port was specified - m_Endpoint.port(m_Call.port()); // force set port - } - - void connect() { - - // TCP connect - - m_Socket.lowest_layer().connect(m_Endpoint); - - // SSL connect - - m_Socket.set_verify_mode(ssl::verify_peer); - m_Socket.handshake(ssl::stream_base::client); - } - - void sendRequest() { - - streambuf requestBuf; - std::ostream stream(&requestBuf); - - // start string: HTTP/1.0 - - stream << m_Request.method << " " << m_Request.path << " HTTP/1.0" << CrLf; - - // host - - stream << "Host: " << m_Call.host(); - - if (m_Call.port() != 0) { - //ASSERT(m_Endpoint.port() == m_Call.port()); - stream << ":" << m_Call.port(); - } - - stream << CrLf; - - // content - - if (!m_Request.body.empty()) { - stream << "Content-Type: " << m_Request.contentType << CrLf; - stream << "Content-Length: " << m_Request.body.size() << CrLf; - } - - // additional headers - - const auto &h = m_Request.headers; - - if (!h.empty()) { - if (h.size() < 2) - throwError("invalid headers data"); - stream << h; - // ensure headers finished correctly - if ((h.substr(h.size() - 2) != CrLf)) - stream << CrLf; - } - - // other - - stream << "Accept: *\x2F*" << CrLf; - stream << "Connection: close" << CrLf; - - // end - - stream << CrLf; - - // content - - if (!m_Request.body.empty()) - stream << m_Request.body; - - // send - - write(m_Socket, requestBuf); - } - - void processHeaders() { - - std::istream stream(&m_ResponseBuf); - - std::string httpVersion; - stream >> httpVersion; - stream >> m_Response.statusCode; - - if (!stream || httpVersion.substr(0, 5) != "HTTP/") { - throwError("invalid response"); - } - - // read/skip headers - - for (;;) { - std::string header; - if (!std::getline(stream, header, Lf) || (header.size() == 1 && header[0] == Cr)) - break; - if (m_ContentLength) // if content length is already known - continue; // continue skipping headers - auto pos = header.find(':'); - if (pos == std::string::npos) - continue; - auto name = header.substr(0, pos); - boost::algorithm::trim(name); - boost::algorithm::to_lower(name); - if (name != "content-length") - continue; - auto value = header.substr(pos + 1); - boost::algorithm::trim(value); - m_ContentLength = std::stol(value); - } - } - - void processResponse() { - - auto &socket = m_Socket; - auto &buf = m_ResponseBuf; - auto &contentLength = m_ContentLength; - auto &body = m_Response.body; - - read_until(socket, buf, CrLfCrLf); - - processHeaders(); - - // check content length - - if (contentLength < 2) // minimum content is "{}" - throwError("invalid response body (too short)"); - - if (contentLength > m_Call.responseSizeLimitBytes()) - throwError("response body size limit exceeded"); - - // read body - - auto avail = buf.size(); // size of body data already stored in the buffer - - if (avail > contentLength) - throwError("invalid response body (content length mismatch)"); - - body.resize(contentLength); - - if (avail) { - // copy already existing data - if (avail != buf.sgetn(&body[0], avail)) { - throwError("stream read failed"); - } - } - - auto rest = contentLength - avail; // size of remaining part of response body - - boost::system::error_code errorCode; - - read(socket, buffer(&body[avail], rest), errorCode); // read remaining part - - socket.shutdown(errorCode); - } -}; - -} // namespace detail - -// HttpsCall - -HttpsCall::HttpsCall(const std::string &host, uint16_t port) : - m_Host(host), - m_Port(port) { -} - -bool HttpsCall::exec(const HttpRequest &request, HttpResponse *response) { - - // ASSERT(response); - auto &resp = *response; - - detail::Impl impl(*this, request, resp); - - try { - resp.clear(); - impl.exec(); - } catch (...) { - resp.clear(); - return false; - } - - return true; -} - -} -} // namespace peerplays::net diff --git a/libraries/plugins/peerplays_sidechain/common/https_call.h b/libraries/plugins/peerplays_sidechain/common/https_call.h deleted file mode 100644 index 595b4a58..00000000 --- a/libraries/plugins/peerplays_sidechain/common/https_call.h +++ /dev/null @@ -1,84 +0,0 @@ -#pragma once - -#include - -namespace peerplays { -namespace net { - -struct HttpRequest { - - std::string method; // ex: "POST" - std::string path; // ex: "/" - std::string headers; - std::string body; - std::string contentType; // ex: "application/json" - - HttpRequest() {} - - HttpRequest(const std::string & method_, const std::string & path_, const std::string & headers_, const std::string & body_, const std::string & contentType_) : - method(method_), - path(path_), - headers(headers_), - body(body_), - contentType(contentType_) - {} - - HttpRequest(const std::string & method_, const std::string & path_, const std::string & headers_, const std::string & body_ = std::string()) : - method(method_), - path(path_), - headers(headers_), - body(body_), - contentType("application/json") - {} - - void clear() { - method.clear(); - path.clear(); - headers.clear(); - body.clear(); - contentType.clear(); - } - -}; - -struct HttpResponse { - - uint16_t statusCode; - std::string body; - - void clear() { - statusCode = 0; - body = decltype(body)(); - } - -}; - -class HttpsCall { -public: - - static constexpr auto ResponseSizeLimitBytes = 1024 * 1024; - - HttpsCall(const std::string & host, uint16_t port = 0); - - const std::string & host() const { - return m_Host; - } - - uint16_t port() const { - return m_Port; - } - - uint32_t responseSizeLimitBytes() const { - return ResponseSizeLimitBytes; - } - - bool exec(const HttpRequest & request, HttpResponse * response); - -private: - std::string m_Host; - uint16_t m_Port; -}; - - -} // net -} // peerplays diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp index 07271b81..019c09b7 100644 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp @@ -1,27 +1,25 @@ -#include "net_utl.h" +#include "net_utl.hpp" #include -namespace peerplays { -namespace net { +namespace graphene { namespace peerplays_sidechain { -std::string resolveHostAddr(const std::string &hostName) { +std::string resolve_host_addr(const std::string &host_name) { using namespace boost::asio; io_service service; ip::tcp::resolver resolver(service); - auto query = ip::tcp::resolver::query(hostName, std::string()); + auto query = ip::tcp::resolver::query(host_name, std::string()); auto iter = resolver.resolve(query); auto endpoint = *iter; auto addr = ((ip::tcp::endpoint)endpoint).address(); return addr.to_string(); } -std::string stripProtoName(const std::string &url) { +std::string strip_proto_name(const std::string &url) { auto index = url.find("://"); if (index == std::string::npos) return url; return url.substr(index + 3); } -} -} // namespace peerplays::net +}} // namespace graphene::peerplays_sidechain diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.h b/libraries/plugins/peerplays_sidechain/common/net_utl.h deleted file mode 100644 index 6cdd2cd5..00000000 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include - -namespace peerplays { -namespace net { - -// resolve IP address by host name -// ex: api.hive.blog -> 52.79.10.214 -std::string resolveHostAddr(const std::string & hostName); - -// remove schema part from URL -// ex: http://api.hive.blog -> api.hive.blog -std::string stripProtoName(const std::string & url); - - -} // net -} // peerplays diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.hpp b/libraries/plugins/peerplays_sidechain/common/net_utl.hpp new file mode 100644 index 00000000..86ad72ba --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace graphene { namespace peerplays_sidechain { + +// resolve IP address by host name +// ex: api.hive.blog -> 52.79.10.214 +std::string resolve_host_addr(const std::string &host_name); + +// remove schema part from URL +// ex: http://api.hive.blog -> api.hive.blog +std::string strip_proto_name(const std::string &url); + +}} // namespace graphene::peerplays_sidechain diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index d3b9d46e..0eda945d 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -2,6 +2,10 @@ #include +#include +#include +#include + #include #include @@ -10,9 +14,316 @@ #include #include +#include -#include "https_call.h" -#include "net_utl.h" +#include "net_utl.hpp" + +namespace graphene { namespace peerplays_sidechain { + +struct http_request { + + std::string method; // ex: "POST" + std::string path; // ex: "/" + std::string headers; + std::string body; + std::string content_type; // ex: "application/json" + + http_request() { + } + + http_request(const std::string &method_, const std::string &path_, const std::string &headers_, const std::string &body_, const std::string &content_type_) : + method(method_), + path(path_), + headers(headers_), + body(body_), + content_type(content_type_) { + } + + http_request(const std::string &method_, const std::string &path_, const std::string &headers_, const std::string &body_ = std::string()) : + method(method_), + path(path_), + headers(headers_), + body(body_), + content_type("application/json") { + } + + void clear() { + method.clear(); + path.clear(); + headers.clear(); + body.clear(); + content_type.clear(); + } +}; + +struct http_response { + + uint16_t status_code; + std::string body; + + void clear() { + status_code = 0; + body = decltype(body)(); + } +}; + +class https_call { +public: + https_call(const std::string &host, uint16_t port = 0) : + m_host(host), + m_port(port) { + } + + const std::string &host() const { + return m_host; + } + + uint16_t port() const { + return m_port; + } + + uint32_t response_size_limit_bytes() const { + return 1024 * 1024; + } + + bool exec(const http_request &request, http_response *response); + +private: + std::string m_host; + uint16_t m_port; +}; + +namespace detail { + +static const char cr = 0x0D; +static const char lf = 0x0A; +static const char *crlf = "\x0D\x0A"; +static const char *crlfcrlf = "\x0D\x0A\x0D\x0A"; + +using namespace boost::asio; + +[[noreturn]] static void throw_error(const std::string &msg) { + throw std::runtime_error(msg); +} + +class https_call_impl { +public: + https_call_impl(const https_call &call, const http_request &request, http_response &response) : + m_call(call), + m_request(request), + m_response(response), + m_service(), + m_context(ssl::context::tlsv12_client), + m_socket(m_service, m_context), + m_endpoint(), + m_response_buf(call.response_size_limit_bytes()), + m_content_length(0) { + m_context.set_default_verify_paths(); + } + + void exec() { + resolve(); + connect(); + send_request(); + process_response(); + } + +private: + const https_call &m_call; + const http_request &m_request; + http_response &m_response; + + io_service m_service; + ssl::context m_context; + ssl::stream m_socket; + ip::tcp::endpoint m_endpoint; + streambuf m_response_buf; + uint32_t m_content_length; + + void resolve() { + + // resolve TCP endpoint for host name + + ip::tcp::resolver resolver(m_service); + auto query = ip::tcp::resolver::query(m_call.host(), "https"); + auto iter = resolver.resolve(query); + m_endpoint = *iter; + + if (m_call.port() != 0) // if port was specified + m_endpoint.port(m_call.port()); // force set port + } + + void connect() { + + // TCP connect + + m_socket.lowest_layer().connect(m_endpoint); + + // SSL connect + + m_socket.set_verify_mode(ssl::verify_peer); + m_socket.handshake(ssl::stream_base::client); + } + + void send_request() { + + streambuf request_buf; + std::ostream stream(&request_buf); + + // start string: HTTP/1.0 + + stream << m_request.method << " " << m_request.path << " HTTP/1.0" << crlf; + + // host + + stream << "Host: " << m_call.host(); + + if (m_call.port() != 0) { + //ASSERT(m_Endpoint.port() == m_Call.port()); + stream << ":" << m_call.port(); + } + + stream << crlf; + + // content + + if (!m_request.body.empty()) { + stream << "Content-Type: " << m_request.content_type << crlf; + stream << "Content-Length: " << m_request.body.size() << crlf; + } + + // additional headers + + const auto &h = m_request.headers; + + if (!h.empty()) { + if (h.size() < 2) + throw_error("invalid headers data"); + stream << h; + // ensure headers finished correctly + if ((h.substr(h.size() - 2) != crlf)) + stream << crlf; + } + + // other + + stream << "Accept: *\x2F*" << crlf; + stream << "Connection: close" << crlf; + + // end + + stream << crlf; + + // content + + if (!m_request.body.empty()) + stream << m_request.body; + + // send + + write(m_socket, request_buf); + } + + void process_headers() { + + std::istream stream(&m_response_buf); + + std::string http_version; + stream >> http_version; + stream >> m_response.status_code; + + if (!stream || http_version.substr(0, 5) != "HTTP/") { + throw_error("invalid response"); + } + + // read/skip headers + + for (;;) { + std::string header; + if (!std::getline(stream, header, lf) || (header.size() == 1 && header[0] == cr)) + break; + if (m_content_length) // if content length is already known + continue; // continue skipping headers + auto pos = header.find(':'); + if (pos == std::string::npos) + continue; + auto name = header.substr(0, pos); + boost::algorithm::trim(name); + boost::algorithm::to_lower(name); + if (name != "content-length") + continue; + auto value = header.substr(pos + 1); + boost::algorithm::trim(value); + m_content_length = std::stol(value); + } + } + + void process_response() { + + auto &socket = m_socket; + auto &buf = m_response_buf; + auto &content_length = m_content_length; + auto &body = m_response.body; + + read_until(socket, buf, crlfcrlf); + + process_headers(); + + // check content length + + if (content_length < 2) // minimum content is "{}" + throw_error("invalid response body (too short)"); + + if (content_length > m_call.response_size_limit_bytes()) + throw_error("response body size limit exceeded"); + + // read body + + auto avail = buf.size(); // size of body data already stored in the buffer + + if (avail > content_length) + throw_error("invalid response body (content length mismatch)"); + + body.resize(content_length); + + if (avail) { + // copy already existing data + if (avail != buf.sgetn(&body[0], avail)) { + throw_error("stream read failed"); + } + } + + auto rest = content_length - avail; // size of remaining part of response body + + boost::system::error_code error_code; + + read(socket, buffer(&body[avail], rest), error_code); // read remaining part + + socket.shutdown(error_code); + } +}; + +} // namespace detail + +bool https_call::exec(const http_request &request, http_response *response) { + + // ASSERT(response); + auto &resp = *response; + + detail::https_call_impl impl(*this, request, resp); + + try { + resp.clear(); + impl.exec(); + } catch (...) { + resp.clear(); + return false; + } + + return true; +} + +}} // namespace graphene::peerplays_sidechain namespace graphene { namespace peerplays_sidechain { @@ -114,14 +425,12 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { auto host = ip.substr(8); // skip "https://" - using namespace peerplays::net; - - HttpsCall call(host, port); - HttpRequest request("POST", "/", authorization.key + ":" + authorization.val, body); - HttpResponse response; + https_call call(host, port); + http_request request("POST", "/", authorization.key + ":" + authorization.val, body); + http_response response; if (call.exec(request, &response)) { - reply.status = response.statusCode; + reply.status = response.status_code; reply.body.resize(response.body.size()); memcpy(&reply.body[0], &response.body[0], response.body.size()); } @@ -130,7 +439,7 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { std::string url = ip + ":" + std::to_string(port); ilog("### Request URL: ${url}", ("url", url)); ilog("### Request: ${body}", ("body", body)); - ilog("### Response code: ${code}", ("code", response.statusCode)); + ilog("### Response code: ${code}", ("code", response.status_code)); ilog("### Response len: ${len}", ("len", response.body.size())); std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); ilog("### Response body: ${ss}", ("ss", ss.str())); @@ -153,7 +462,7 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { addr = fc::ip::address(host); } catch (...) { try { - addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); + addr = fc::ip::address(resolve_host_addr(host)); } catch (...) { if (show_log) { std::string url = ip + ":" + std::to_string(port); diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp index 3dfc9aee..3ff63ed9 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp @@ -28,7 +28,7 @@ #include #include -#include "common/net_utl.h" +#include "common/net_utl.hpp" namespace graphene { namespace peerplays_sidechain { @@ -150,7 +150,7 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi fc::http::connection conn; try { - auto host = peerplays::net::stripProtoName(node_ip); + auto host = strip_proto_name(node_ip); fc::ip::address addr; try { // IP address assumed @@ -158,7 +158,7 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi } catch (...) { try { // host name assumed - addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); + addr = fc::ip::address(resolve_host_addr(host)); } catch (...) { elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); FC_ASSERT(false); -- 2.45.2 From d953b9e58abc8f612ed24a3d93d22ba9eb9b4840 Mon Sep 17 00:00:00 2001 From: moss9001 Date: Fri, 22 Oct 2021 22:10:37 +0300 Subject: [PATCH 12/15] Update rpc_client.cpp --- .../plugins/peerplays_sidechain/common/rpc_client.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index 2b845f40..71daa601 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -14,7 +14,7 @@ #include #include -//#include +#include #include #include @@ -418,9 +418,9 @@ std::string rpc_client::send_post_request(std::string method, std::string params return ""; } -fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { +rpc_reply rpc_client::send_post_request(std::string body, bool show_log) { - fc::http::reply reply; + rpc_reply reply; auto start = ip.substr(0, 6); boost::algorithm::to_lower(start); @@ -486,7 +486,9 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { // url = url + "/wallet/" + wallet; //} - reply = conn.request("POST", url, body, fc::http::headers{authorization}); + auto r = conn.request("POST", url, body, fc::http::headers{authorization}); + reply.status = r.status; + reply.body.assign(r.body.begin(), r.body.end()); } catch (...) { } -- 2.45.2 From 62ecaa3c9a4bec5b713026daec14462bd2d07d27 Mon Sep 17 00:00:00 2001 From: serkixenos Date: Sat, 23 Oct 2021 01:27:14 +0200 Subject: [PATCH 13/15] [SON for Hive] - Implement HTTPS RPC client --- .gitignore | 1 + .../peerplays_sidechain/CMakeLists.txt | 1 + .../peerplays_sidechain/common/net_utl.cpp | 25 + .../peerplays_sidechain/common/net_utl.hpp | 15 + .../peerplays_sidechain/common/rpc_client.cpp | 447 +++++++++++++++--- .../sidechain_net_handler_hive.cpp | 149 +++--- 6 files changed, 516 insertions(+), 122 deletions(-) create mode 100644 libraries/plugins/peerplays_sidechain/common/net_utl.cpp create mode 100644 libraries/plugins/peerplays_sidechain/common/net_utl.hpp diff --git a/.gitignore b/.gitignore index 39b23163..1a84b559 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ data CMakeDoxyfile.in build +build__* libraries/utilities/git_revision.cpp diff --git a/libraries/plugins/peerplays_sidechain/CMakeLists.txt b/libraries/plugins/peerplays_sidechain/CMakeLists.txt index bb74df29..218e25fe 100755 --- a/libraries/plugins/peerplays_sidechain/CMakeLists.txt +++ b/libraries/plugins/peerplays_sidechain/CMakeLists.txt @@ -15,6 +15,7 @@ add_library( peerplays_sidechain bitcoin/utils.cpp bitcoin/sign_bitcoin_transaction.cpp common/rpc_client.cpp + common/net_utl.cpp common/utils.cpp hive/asset.cpp hive/operations.cpp diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp new file mode 100644 index 00000000..019c09b7 --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp @@ -0,0 +1,25 @@ +#include "net_utl.hpp" + +#include + +namespace graphene { namespace peerplays_sidechain { + +std::string resolve_host_addr(const std::string &host_name) { + using namespace boost::asio; + io_service service; + ip::tcp::resolver resolver(service); + auto query = ip::tcp::resolver::query(host_name, std::string()); + auto iter = resolver.resolve(query); + auto endpoint = *iter; + auto addr = ((ip::tcp::endpoint)endpoint).address(); + return addr.to_string(); +} + +std::string strip_proto_name(const std::string &url) { + auto index = url.find("://"); + if (index == std::string::npos) + return url; + return url.substr(index + 3); +} + +}} // namespace graphene::peerplays_sidechain diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.hpp b/libraries/plugins/peerplays_sidechain/common/net_utl.hpp new file mode 100644 index 00000000..86ad72ba --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace graphene { namespace peerplays_sidechain { + +// resolve IP address by host name +// ex: api.hive.blog -> 52.79.10.214 +std::string resolve_host_addr(const std::string &host_name); + +// remove schema part from URL +// ex: http://api.hive.blog -> api.hive.blog +std::string strip_proto_name(const std::string &url); + +}} // namespace graphene::peerplays_sidechain diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index cad283a7..df692185 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -3,6 +3,10 @@ #include #include +#include +#include +#include + #include #include @@ -10,7 +14,319 @@ #include #include -//#include +#include + +#include +#include + +#include "net_utl.hpp" + +namespace graphene { namespace peerplays_sidechain { + +struct http_request { + + std::string method; // ex: "POST" + std::string path; // ex: "/" + std::string headers; + std::string body; + std::string content_type; // ex: "application/json" + + http_request() { + } + + http_request(const std::string &method_, const std::string &path_, const std::string &headers_, const std::string &body_, const std::string &content_type_) : + method(method_), + path(path_), + headers(headers_), + body(body_), + content_type(content_type_) { + } + + http_request(const std::string &method_, const std::string &path_, const std::string &headers_, const std::string &body_ = std::string()) : + method(method_), + path(path_), + headers(headers_), + body(body_), + content_type("application/json") { + } + + void clear() { + method.clear(); + path.clear(); + headers.clear(); + body.clear(); + content_type.clear(); + } +}; + +struct http_response { + + uint16_t status_code; + std::string body; + + void clear() { + status_code = 0; + body = decltype(body)(); + } +}; + +class https_call { +public: + https_call(const std::string &host, uint16_t port = 0) : + m_host(host), + m_port(port) { + } + + const std::string &host() const { + return m_host; + } + + uint16_t port() const { + return m_port; + } + + uint32_t response_size_limit_bytes() const { + return 1024 * 1024; + } + + bool exec(const http_request &request, http_response *response); + +private: + std::string m_host; + uint16_t m_port; +}; + +namespace detail { + +static const char cr = 0x0D; +static const char lf = 0x0A; +static const char *crlf = "\x0D\x0A"; +static const char *crlfcrlf = "\x0D\x0A\x0D\x0A"; + +using namespace boost::asio; + +[[noreturn]] static void throw_error(const std::string &msg) { + throw std::runtime_error(msg); +} + +class https_call_impl { +public: + https_call_impl(const https_call &call, const http_request &request, http_response &response) : + m_call(call), + m_request(request), + m_response(response), + m_service(), + m_context(ssl::context::tlsv12_client), + m_socket(m_service, m_context), + m_endpoint(), + m_response_buf(call.response_size_limit_bytes()), + m_content_length(0) { + m_context.set_default_verify_paths(); + } + + void exec() { + resolve(); + connect(); + send_request(); + process_response(); + } + +private: + const https_call &m_call; + const http_request &m_request; + http_response &m_response; + + io_service m_service; + ssl::context m_context; + ssl::stream m_socket; + ip::tcp::endpoint m_endpoint; + streambuf m_response_buf; + uint32_t m_content_length; + + void resolve() { + + // resolve TCP endpoint for host name + + ip::tcp::resolver resolver(m_service); + auto query = ip::tcp::resolver::query(m_call.host(), "https"); + auto iter = resolver.resolve(query); + m_endpoint = *iter; + + if (m_call.port() != 0) // if port was specified + m_endpoint.port(m_call.port()); // force set port + } + + void connect() { + + // TCP connect + + m_socket.lowest_layer().connect(m_endpoint); + + // SSL connect + + m_socket.set_verify_mode(ssl::verify_peer); + m_socket.handshake(ssl::stream_base::client); + } + + void send_request() { + + streambuf request_buf; + std::ostream stream(&request_buf); + + // start string: HTTP/1.0 + + stream << m_request.method << " " << m_request.path << " HTTP/1.0" << crlf; + + // host + + stream << "Host: " << m_call.host(); + + if (m_call.port() != 0) { + //ASSERT(m_Endpoint.port() == m_Call.port()); + stream << ":" << m_call.port(); + } + + stream << crlf; + + // content + + if (!m_request.body.empty()) { + stream << "Content-Type: " << m_request.content_type << crlf; + stream << "Content-Length: " << m_request.body.size() << crlf; + } + + // additional headers + + const auto &h = m_request.headers; + + if (!h.empty()) { + if (h.size() < 2) + throw_error("invalid headers data"); + stream << h; + // ensure headers finished correctly + if ((h.substr(h.size() - 2) != crlf)) + stream << crlf; + } + + // other + + stream << "Accept: *\x2F*" << crlf; + stream << "Connection: close" << crlf; + + // end + + stream << crlf; + + // content + + if (!m_request.body.empty()) + stream << m_request.body; + + // send + + write(m_socket, request_buf); + } + + void process_headers() { + + std::istream stream(&m_response_buf); + + std::string http_version; + stream >> http_version; + stream >> m_response.status_code; + + if (!stream || http_version.substr(0, 5) != "HTTP/") { + throw_error("invalid response"); + } + + // read/skip headers + + for (;;) { + std::string header; + if (!std::getline(stream, header, lf) || (header.size() == 1 && header[0] == cr)) + break; + if (m_content_length) // if content length is already known + continue; // continue skipping headers + auto pos = header.find(':'); + if (pos == std::string::npos) + continue; + auto name = header.substr(0, pos); + boost::algorithm::trim(name); + boost::algorithm::to_lower(name); + if (name != "content-length") + continue; + auto value = header.substr(pos + 1); + boost::algorithm::trim(value); + m_content_length = std::stol(value); + } + } + + void process_response() { + + auto &socket = m_socket; + auto &buf = m_response_buf; + auto &content_length = m_content_length; + auto &body = m_response.body; + + read_until(socket, buf, crlfcrlf); + + process_headers(); + + // check content length + + if (content_length < 2) // minimum content is "{}" + throw_error("invalid response body (too short)"); + + if (content_length > m_call.response_size_limit_bytes()) + throw_error("response body size limit exceeded"); + + // read body + + auto avail = buf.size(); // size of body data already stored in the buffer + + if (avail > content_length) + throw_error("invalid response body (content length mismatch)"); + + body.resize(content_length); + + if (avail) { + // copy already existing data + if (avail != buf.sgetn(&body[0], avail)) { + throw_error("stream read failed"); + } + } + + auto rest = content_length - avail; // size of remaining part of response body + + boost::system::error_code error_code; + + read(socket, buffer(&body[avail], rest), error_code); // read remaining part + + socket.shutdown(error_code); + } +}; + +} // namespace detail + +bool https_call::exec(const http_request &request, http_response *response) { + + // ASSERT(response); + auto &resp = *response; + + detail::https_call_impl impl(*this, request, resp); + + try { + resp.clear(); + impl.exec(); + } catch (...) { + resp.clear(); + return false; + } + + return true; +} + +}} // namespace graphene::peerplays_sidechain namespace graphene { namespace peerplays_sidechain { @@ -102,75 +418,80 @@ std::string rpc_client::send_post_request(std::string method, std::string params return ""; } -//fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { -// fc::http::connection conn; -// conn.connect_to(fc::ip::endpoint(fc::ip::address(ip), port)); -// -// std::string url = "http://" + ip + ":" + std::to_string(port); -// -// //if (wallet.length() > 0) { -// // url = url + "/wallet/" + wallet; -// //} -// -// fc::http::reply reply = conn.request("POST", url, body, fc::http::headers{authorization}); -// -// if (show_log) { -// ilog("### Request URL: ${url}", ("url", url)); -// ilog("### Request: ${body}", ("body", body)); -// std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); -// ilog("### Response: ${ss}", ("ss", ss.str())); -// } -// -// return reply; -//} - -static size_t write_callback(char *ptr, size_t size, size_t nmemb, rpc_reply *reply) { - size_t retval = 0; - if (reply != nullptr) { - reply->body.append(ptr, size * nmemb); - retval = size * nmemb; - } - return retval; -} - rpc_reply rpc_client::send_post_request(std::string body, bool show_log) { - struct curl_slist *headers = nullptr; - headers = curl_slist_append(headers, "Accept: application/json"); - headers = curl_slist_append(headers, "Content-Type: application/json"); - headers = curl_slist_append(headers, "charset: utf-8"); - - CURL *curl = curl_easy_init(); - if (ip.find("https://", 0) != 0) { - curl_easy_setopt(curl, CURLOPT_URL, ip.c_str()); - curl_easy_setopt(curl, CURLOPT_PORT, port); - } else { - std::string full_address = ip + ":" + std::to_string(port); - curl_easy_setopt(curl, CURLOPT_URL, full_address.c_str()); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); - } - if (!user.empty()) { - curl_easy_setopt(curl, CURLOPT_USERNAME, user.c_str()); - curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str()); - } - - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str()); - - //curl_easy_setopt(curl, CURLOPT_VERBOSE, true); - rpc_reply reply; + auto start = ip.substr(0, 6); + boost::algorithm::to_lower(start); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &reply); + if (start == "https:") { - curl_easy_perform(curl); + auto host = ip.substr(8); // skip "https://" - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &reply.status); + https_call call(host, port); + http_request request("POST", "/", authorization.key + ":" + authorization.val, body); + http_response response; - curl_easy_cleanup(curl); - curl_slist_free_all(headers); + if (call.exec(request, &response)) { + reply.status = response.status_code; + reply.body.resize(response.body.size()); + memcpy(&reply.body[0], &response.body[0], response.body.size()); + } + + if (show_log) { + std::string url = ip + ":" + std::to_string(port); + ilog("### Request URL: ${url}", ("url", url)); + ilog("### Request: ${body}", ("body", body)); + ilog("### Response code: ${code}", ("code", response.status_code)); + ilog("### Response len: ${len}", ("len", response.body.size())); + std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); + ilog("### Response body: ${ss}", ("ss", ss.str())); + } + + return reply; + } + + std::string host; + + if (start == "http:/") + host = ip.substr(7); // skip "http://" + else + host = ip; + + std::string url = "http://" + host + ":" + std::to_string(port); + fc::ip::address addr; + + try { + addr = fc::ip::address(host); + } catch (...) { + try { + addr = fc::ip::address(resolve_host_addr(host)); + } catch (...) { + if (show_log) { + std::string url = ip + ":" + std::to_string(port); + ilog("### Request URL: ${url}", ("url", url)); + ilog("### Request: ${body}", ("body", body)); + ilog("### Request: error: host address resolve failed"); + } + return reply; + } + } + + try { + + fc::http::connection conn; + conn.connect_to(fc::ip::endpoint(addr, port)); + + //if (wallet.length() > 0) { + // url = url + "/wallet/" + wallet; + //} + + auto r = conn.request("POST", url, body, fc::http::headers{authorization}); + reply.status = r.status; + reply.body.assign(r.body.begin(), r.body.end()); + + } catch (...) { + } if (show_log) { std::string url = ip + ":" + std::to_string(port); diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp index e1a5c329..a8e296ac 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp @@ -28,6 +28,8 @@ #include #include +#include "common/net_utl.hpp" + namespace graphene { namespace peerplays_sidechain { hive_node_rpc_client::hive_node_rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password, bool _debug_rpc_calls) : @@ -35,7 +37,7 @@ hive_node_rpc_client::hive_node_rpc_client(std::string _ip, uint32_t _port, std: } std::string hive_node_rpc_client::account_history_api_get_transaction(std::string transaction_id) { - std::string params = "{ \"id\": \"" + transaction_id + "\", \"include_reversible\": \"true\" }"; + std::string params = "{ \"id\": \"" + transaction_id + "\" }"; return send_post_request("account_history_api.get_transaction", params, debug_rpc_calls); } @@ -145,13 +147,29 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi } } - //fc::http::connection conn; - //try { - // conn.connect_to(fc::ip::endpoint(fc::ip::address(node_ip), node_rpc_port)); - //} catch (fc::exception &e) { - // elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port)); - // FC_ASSERT(false); - //} + fc::http::connection conn; + + try { + auto host = strip_proto_name(node_ip); + fc::ip::address addr; + try { + // IP address assumed + addr = fc::ip::address(host); + } catch (...) { + try { + // host name assumed + addr = fc::ip::address(resolve_host_addr(host)); + } catch (...) { + elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); + FC_ASSERT(false); + } + } + // try to connect to TCP endpoint + conn.connect_to(fc::ip::endpoint(addr, node_rpc_port)); + } catch (fc::exception &e) { + elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port)); + FC_ASSERT(false); + } node_rpc_client = new hive_node_rpc_client(node_ip, node_rpc_port, node_rpc_user, node_rpc_password, debug_rpc_calls); @@ -161,9 +179,11 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi std::string is_test_net = node_rpc_client->get_is_test_net(); network_type = is_test_net.compare("true") == 0 ? hive::network::testnet : hive::network::mainnet; if (network_type == hive::network::mainnet) { + ilog("Running on Hive mainnet, chain id ${chain_id_str}", ("chain_id_str", chain_id_str)); hive::asset::hbd_symbol_ser = HBD_SYMBOL_SER; hive::asset::hive_symbol_ser = HIVE_SYMBOL_SER; } else { + ilog("Running on Hive testnet, chain id ${chain_id_str}", ("chain_id_str", chain_id_str)); hive::asset::hbd_symbol_ser = TBD_SYMBOL_SER; hive::asset::hive_symbol_ser = TESTS_SYMBOL_SER; } @@ -817,7 +837,6 @@ void sidechain_net_handler_hive::hive_listener_loop() { schedule_hive_listener(); std::string reply = node_rpc_client->database_api_get_dynamic_global_properties(); - if (!reply.empty()) { std::stringstream ss(reply); boost::property_tree::ptree json; @@ -831,6 +850,16 @@ void sidechain_net_handler_hive::hive_listener_loop() { } } } + + //std::string reply = node_rpc_client->get_last_irreversible_block_num(); + //if (!reply.empty()) { + // uint64_t last_irreversible_block = std::stoul(reply); + // if (last_irreversible_block != last_block_received) { + // std::string event_data = std::to_string(last_irreversible_block); + // handle_event(event_data); + // last_block_received = last_irreversible_block; + // } + //} } void sidechain_net_handler_hive::handle_event(const std::string &event_data) { @@ -840,28 +869,25 @@ void sidechain_net_handler_hive::handle_event(const std::string &event_data) { boost::property_tree::ptree block_json; boost::property_tree::read_json(ss, block_json); - for (const auto &tx_ids_child : block_json.get_child("result.block.transaction_ids")) { - const auto &transaction_id = tx_ids_child.second.get_value(); + size_t tx_idx = -1; + for (const auto &tx_ids_child : block_json.get_child("result.block.transactions")) { + boost::property_tree::ptree tx = tx_ids_child.second; + tx_idx = tx_idx + 1; - std::string tx_str = node_rpc_client->account_history_api_get_transaction(transaction_id); - if (tx_str != "") { + size_t op_idx = -1; + for (const auto &ops : tx.get_child("operations")) { + const auto &op = ops.second; + op_idx = op_idx + 1; - std::stringstream ss_tx(tx_str); - boost::property_tree::ptree tx; - boost::property_tree::read_json(ss_tx, tx); + std::string operation_type = op.get("type"); - size_t op_idx = -1; - for (const auto &ops : tx.get_child("result.operations")) { - const auto &op = ops.second; - op_idx = op_idx + 1; + if (operation_type == "transfer_operation") { + const auto &op_value = op.get_child("value"); - std::string operation_type = op.get("type"); + std::string from = op_value.get("from"); + std::string to = op_value.get("to"); - if (operation_type == "transfer_operation") { - const auto &op_value = op.get_child("value"); - - std::string from = op_value.get("from"); - std::string to = op_value.get("to"); + if (to == "son-account") { const auto &amount_child = op_value.get_child("amount"); @@ -885,42 +911,47 @@ void sidechain_net_handler_hive::handle_event(const std::string &event_data) { from = memo; } - if (to == "son-account") { - const auto &sidechain_addresses_idx = database.get_index_type().indices().get(); - const auto &addr_itr = sidechain_addresses_idx.find(std::make_tuple(sidechain, from, time_point_sec::maximum())); - account_id_type accn = account_id_type(); - if (addr_itr == sidechain_addresses_idx.end()) { - const auto &account_idx = database.get_index_type().indices().get(); - const auto &account_itr = account_idx.find(from); - if (account_itr == account_idx.end()) { - continue; - } else { - accn = account_itr->id; - } + const auto &sidechain_addresses_idx = database.get_index_type().indices().get(); + const auto &addr_itr = sidechain_addresses_idx.find(std::make_tuple(sidechain, from, time_point_sec::maximum())); + account_id_type accn = account_id_type(); + if (addr_itr == sidechain_addresses_idx.end()) { + const auto &account_idx = database.get_index_type().indices().get(); + const auto &account_itr = account_idx.find(from); + if (account_itr == account_idx.end()) { + continue; } else { - accn = addr_itr->sidechain_address_account; + accn = account_itr->id; } - - std::stringstream ss; - ss << "hive" - << "-" << transaction_id << "-" << op_idx; - std::string sidechain_uid = ss.str(); - - sidechain_event_data sed; - sed.timestamp = database.head_block_time(); - sed.block_num = database.head_block_num(); - sed.sidechain = sidechain; - sed.sidechain_uid = sidechain_uid; - sed.sidechain_transaction_id = transaction_id; - sed.sidechain_from = from; - sed.sidechain_to = to; - sed.sidechain_currency = sidechain_currency; - sed.sidechain_amount = amount; - sed.peerplays_from = accn; - sed.peerplays_to = database.get_global_properties().parameters.son_account(); - sed.peerplays_asset = asset(sed.sidechain_amount * sidechain_currency_price.base.amount / sidechain_currency_price.quote.amount); - sidechain_event_data_received(sed); + } else { + accn = addr_itr->sidechain_address_account; } + + std::vector transaction_ids; + for (const auto &tx_ids_child : block_json.get_child("result.block.transaction_ids")) { + const auto &transaction_id = tx_ids_child.second.get_value(); + transaction_ids.push_back(transaction_id); + } + std::string transaction_id = transaction_ids.at(tx_idx); + + std::stringstream ss; + ss << "hive" + << "-" << transaction_id << "-" << op_idx; + std::string sidechain_uid = ss.str(); + + sidechain_event_data sed; + sed.timestamp = database.head_block_time(); + sed.block_num = database.head_block_num(); + sed.sidechain = sidechain; + sed.sidechain_uid = sidechain_uid; + sed.sidechain_transaction_id = transaction_id; + sed.sidechain_from = from; + sed.sidechain_to = to; + sed.sidechain_currency = sidechain_currency; + sed.sidechain_amount = amount; + sed.peerplays_from = accn; + sed.peerplays_to = database.get_global_properties().parameters.son_account(); + sed.peerplays_asset = asset(sed.sidechain_amount * sidechain_currency_price.base.amount / sidechain_currency_price.quote.amount); + sidechain_event_data_received(sed); } } } -- 2.45.2 From 934a65969efcc396d2573f1302a98759c1bfcb7b Mon Sep 17 00:00:00 2001 From: serkixenos Date: Sat, 23 Oct 2021 03:41:39 +0200 Subject: [PATCH 14/15] CLang formatter script --- clang-format.sh | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 clang-format.sh diff --git a/clang-format.sh b/clang-format.sh new file mode 100755 index 00000000..6ab95eb9 --- /dev/null +++ b/clang-format.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +find ./libraries/plugins/peerplays_sidechain -regex ".*[c|h]pp" | xargs clang-format -i + -- 2.45.2 From 68b33c816c21e57b01a094e8418f5642189ed99f Mon Sep 17 00:00:00 2001 From: moss9001 Date: Mon, 25 Oct 2021 17:28:30 +0300 Subject: [PATCH 15/15] Comments applied show_log move net_utl to other files single IP resolve removed custom exception handler --- .../peerplays_sidechain/CMakeLists.txt | 1 - .../peerplays_sidechain/common/net_utl.cpp | 25 ------ .../peerplays_sidechain/common/net_utl.hpp | 15 ---- .../peerplays_sidechain/common/rpc_client.cpp | 90 +++++++------------ .../sidechain_net_handler_hive.cpp | 45 ++++++++-- 5 files changed, 71 insertions(+), 105 deletions(-) delete mode 100644 libraries/plugins/peerplays_sidechain/common/net_utl.cpp delete mode 100644 libraries/plugins/peerplays_sidechain/common/net_utl.hpp diff --git a/libraries/plugins/peerplays_sidechain/CMakeLists.txt b/libraries/plugins/peerplays_sidechain/CMakeLists.txt index 218e25fe..bb74df29 100755 --- a/libraries/plugins/peerplays_sidechain/CMakeLists.txt +++ b/libraries/plugins/peerplays_sidechain/CMakeLists.txt @@ -15,7 +15,6 @@ add_library( peerplays_sidechain bitcoin/utils.cpp bitcoin/sign_bitcoin_transaction.cpp common/rpc_client.cpp - common/net_utl.cpp common/utils.cpp hive/asset.cpp hive/operations.cpp diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp deleted file mode 100644 index 019c09b7..00000000 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "net_utl.hpp" - -#include - -namespace graphene { namespace peerplays_sidechain { - -std::string resolve_host_addr(const std::string &host_name) { - using namespace boost::asio; - io_service service; - ip::tcp::resolver resolver(service); - auto query = ip::tcp::resolver::query(host_name, std::string()); - auto iter = resolver.resolve(query); - auto endpoint = *iter; - auto addr = ((ip::tcp::endpoint)endpoint).address(); - return addr.to_string(); -} - -std::string strip_proto_name(const std::string &url) { - auto index = url.find("://"); - if (index == std::string::npos) - return url; - return url.substr(index + 3); -} - -}} // namespace graphene::peerplays_sidechain diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.hpp b/libraries/plugins/peerplays_sidechain/common/net_utl.hpp deleted file mode 100644 index 86ad72ba..00000000 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include - -namespace graphene { namespace peerplays_sidechain { - -// resolve IP address by host name -// ex: api.hive.blog -> 52.79.10.214 -std::string resolve_host_addr(const std::string &host_name); - -// remove schema part from URL -// ex: http://api.hive.blog -> api.hive.blog -std::string strip_proto_name(const std::string &url); - -}} // namespace graphene::peerplays_sidechain diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index df692185..1db7c889 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -19,8 +19,6 @@ #include #include -#include "net_utl.hpp" - namespace graphene { namespace peerplays_sidechain { struct http_request { @@ -105,10 +103,6 @@ static const char *crlfcrlf = "\x0D\x0A\x0D\x0A"; using namespace boost::asio; -[[noreturn]] static void throw_error(const std::string &msg) { - throw std::runtime_error(msg); -} - class https_call_impl { public: https_call_impl(const https_call &call, const http_request &request, http_response &response) : @@ -200,8 +194,9 @@ private: const auto &h = m_request.headers; if (!h.empty()) { - if (h.size() < 2) - throw_error("invalid headers data"); + if (h.size() < 2) { + FC_THROW("invalid headers data"); + } stream << h; // ensure headers finished correctly if ((h.substr(h.size() - 2) != crlf)) @@ -236,7 +231,7 @@ private: stream >> m_response.status_code; if (!stream || http_version.substr(0, 5) != "HTTP/") { - throw_error("invalid response"); + FC_THROW("invalid response data"); } // read/skip headers @@ -274,25 +269,28 @@ private: // check content length - if (content_length < 2) // minimum content is "{}" - throw_error("invalid response body (too short)"); + if (content_length < 2) { // minimum content is "{}" + FC_THROW("invalid response body (too short)"); + } - if (content_length > m_call.response_size_limit_bytes()) - throw_error("response body size limit exceeded"); + if (content_length > m_call.response_size_limit_bytes()) { + FC_THROW("response body size limit exceeded"); + } // read body auto avail = buf.size(); // size of body data already stored in the buffer - if (avail > content_length) - throw_error("invalid response body (content length mismatch)"); + if (avail > content_length) { + FC_THROW("invalid response body (content length mismatch)"); + } body.resize(content_length); if (avail) { // copy already existing data if (avail != buf.sgetn(&body[0], avail)) { - throw_error("stream read failed"); + FC_THROW("stream read failed"); } } @@ -438,59 +436,39 @@ rpc_reply rpc_client::send_post_request(std::string body, bool show_log) { memcpy(&reply.body[0], &response.body[0], response.body.size()); } - if (show_log) { - std::string url = ip + ":" + std::to_string(port); - ilog("### Request URL: ${url}", ("url", url)); - ilog("### Request: ${body}", ("body", body)); - ilog("### Response code: ${code}", ("code", response.status_code)); - ilog("### Response len: ${len}", ("len", response.body.size())); - std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); - ilog("### Response body: ${ss}", ("ss", ss.str())); - } + } else { - return reply; - } + std::string host; - std::string host; + if (start == "http:/") + host = ip.substr(7); // skip "http://" + else + host = ip; - if (start == "http:/") - host = ip.substr(7); // skip "http://" - else - host = ip; + std::string url = "http://" + host + ":" + std::to_string(port); + fc::ip::address addr; - std::string url = "http://" + host + ":" + std::to_string(port); - fc::ip::address addr; - - try { - addr = fc::ip::address(host); - } catch (...) { try { - addr = fc::ip::address(resolve_host_addr(host)); + addr = fc::ip::address(host); } catch (...) { - if (show_log) { - std::string url = ip + ":" + std::to_string(port); - ilog("### Request URL: ${url}", ("url", url)); - ilog("### Request: ${body}", ("body", body)); - ilog("### Request: error: host address resolve failed"); - } return reply; } - } - try { + try { - fc::http::connection conn; - conn.connect_to(fc::ip::endpoint(addr, port)); + fc::http::connection conn; + conn.connect_to(fc::ip::endpoint(addr, port)); - //if (wallet.length() > 0) { - // url = url + "/wallet/" + wallet; - //} + //if (wallet.length() > 0) { + // url = url + "/wallet/" + wallet; + //} - auto r = conn.request("POST", url, body, fc::http::headers{authorization}); - reply.status = r.status; - reply.body.assign(r.body.begin(), r.body.end()); + auto r = conn.request("POST", url, body, fc::http::headers{authorization}); + reply.status = r.status; + reply.body.assign(r.body.begin(), r.body.end()); - } catch (...) { + } catch (...) { + } } if (show_log) { diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp index a8e296ac..2db3063d 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp @@ -28,7 +28,34 @@ #include #include -#include "common/net_utl.hpp" +#include + +namespace graphene { namespace peerplays_sidechain { + +std::string resolve_host_addr(const std::string &host_name) { + using namespace boost::asio; + io_service service; + ip::tcp::resolver resolver(service); + auto query = ip::tcp::resolver::query(host_name, std::string()); + auto iter = resolver.resolve(query); + auto endpoint = *iter; + auto addr = ((ip::tcp::endpoint)endpoint).address(); + return addr.to_string(); +} + +std::string strip_proto_name(const std::string &url, std::string *schema) { + auto index = url.find("://"); + if (index == std::string::npos) { + if (schema) + schema->clear(); + return url; + } + if (schema) + schema->assign(&url[0], &url[index + 3]); + return url.substr(index + 3); +} + +}} // namespace graphene::peerplays_sidechain namespace graphene { namespace peerplays_sidechain { @@ -147,31 +174,33 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi } } - fc::http::connection conn; + std::string schema; + auto host = strip_proto_name(node_ip, &schema); try { - auto host = strip_proto_name(node_ip); - fc::ip::address addr; + fc::ip::address ip_addr; try { // IP address assumed - addr = fc::ip::address(host); + ip_addr = fc::ip::address(host); } catch (...) { try { // host name assumed - addr = fc::ip::address(resolve_host_addr(host)); + host = resolve_host_addr(host); + ip_addr = fc::ip::address(host); } catch (...) { elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); FC_ASSERT(false); } } // try to connect to TCP endpoint - conn.connect_to(fc::ip::endpoint(addr, node_rpc_port)); + fc::http::connection conn; + conn.connect_to(fc::ip::endpoint(ip_addr, node_rpc_port)); } catch (fc::exception &e) { elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port)); FC_ASSERT(false); } - node_rpc_client = new hive_node_rpc_client(node_ip, node_rpc_port, node_rpc_user, node_rpc_password, debug_rpc_calls); + node_rpc_client = new hive_node_rpc_client(schema + host, node_rpc_port, node_rpc_user, node_rpc_password, debug_rpc_calls); std::string chain_id_str = node_rpc_client->get_chain_id(); chain_id = chain_id_type(chain_id_str); -- 2.45.2