net utl added

This commit is contained in:
moss9001 2021-10-21 21:34:42 +03:00
parent 7237ac0e71
commit 2826f2e83f
4 changed files with 49 additions and 21 deletions

View file

@ -1,4 +1,4 @@
#include "https_call.h"
#include "net_utl.h"
#include <boost/asio.hpp>
@ -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

View file

@ -6,6 +6,8 @@ namespace peerplays {
namespace net {
std::string resolveHostAddr(const std::string & hostName);
std::string stripProtoName(const std::string & utl);
} // net
} // peerplays

View file

@ -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));

View file

@ -28,6 +28,8 @@
#include <graphene/peerplays_sidechain/hive/transaction.hpp>
#include <graphene/utilities/key_conversion.hpp>
#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();