From 7237ac0e718abcbe2ea7fa7e3d7e261e4d9f4046 Mon Sep 17 00:00:00 2001 From: moss9001 Date: Thu, 21 Oct 2021 19:54:25 +0300 Subject: [PATCH] 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();