| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#pragma once |
| 30 |
|
| 31 |
#include <boost/optional/optional.hpp> |
| 32 |
|
| 33 |
#include "common/http_connection.h" |
| 34 |
#include "common/scoped_message_writer.h" |
| 35 |
#include "rpc/core_rpc_server_commands_defs.h" |
| 36 |
#include "storages/http_abstract_invoke.h" |
| 37 |
#include "net/http_auth.h" |
| 38 |
#include "net/http_client.h" |
| 39 |
#include "net/net_ssl.h" |
| 40 |
#include "string_tools.h" |
| 41 |
|
| 42 |
namespace tools |
| 43 |
{ |
| 44 |
class t_rpc_client final |
| 45 |
{ |
| 46 |
private: |
| 47 |
epee::net_utils::http::http_simple_client m_http_client; |
| 48 |
public: |
| 49 |
t_rpc_client( |
| 50 |
uint32_t ip |
| 51 |
, uint16_t port |
| 52 |
, boost::optional<epee::net_utils::http::login> user |
| 53 |
, epee::net_utils::ssl_options_t ssl_options |
| 54 |
) |
| 55 |
: m_http_client{} |
| 56 |
{ |
| 57 |
m_http_client.set_server( |
| 58 |
epee::string_tools::get_ip_string_from_int32(ip), std::to_string(port), std::move(user), std::move(ssl_options) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
template <typename T_req, typename T_res> |
| 63 |
bool basic_json_rpc_request( |
| 64 |
T_req & req |
| 65 |
, T_res & res |
| 66 |
, std::string const & method_name |
| 67 |
) |
| 68 |
{ |
| 69 |
t_http_connection connection(&m_http_client); |
| 70 |
|
| 71 |
bool ok = connection.is_open(); |
| 72 |
if (!ok) |
| 73 |
{ |
| 74 |
fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port(); |
| 75 |
return false; |
| 76 |
} |
| 77 |
ok = epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT()); |
| 78 |
if (!ok) |
| 79 |
{ |
| 80 |
fail_msg_writer() << "basic_json_rpc_request: Daemon request failed"; |
| 81 |
return false; |
| 82 |
} |
| 83 |
else |
| 84 |
{ |
| 85 |
return true; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
template <typename T_req, typename T_res> |
| 90 |
bool json_rpc_request( |
| 91 |
T_req & req |
| 92 |
, T_res & res |
| 93 |
, std::string const & method_name |
| 94 |
, std::string const & fail_msg |
| 95 |
) |
| 96 |
{ |
| 97 |
t_http_connection connection(&m_http_client); |
| 98 |
|
| 99 |
bool ok = connection.is_open(); |
| 100 |
if (!ok) |
| 101 |
{ |
| 102 |
fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port(); |
| 103 |
return false; |
| 104 |
} |
| 105 |
ok = epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT()); |
| 106 |
if (!ok || res.status != CORE_RPC_STATUS_OK) |
| 107 |
{ |
| 108 |
fail_msg_writer() << fail_msg << " -- json_rpc_request: " << res.status; |
| 109 |
return false; |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
return true; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
template <typename T_req, typename T_res> |
| 118 |
bool rpc_request( |
| 119 |
T_req & req |
| 120 |
, T_res & res |
| 121 |
, std::string const & relative_url |
| 122 |
, std::string const & fail_msg |
| 123 |
) |
| 124 |
{ |
| 125 |
t_http_connection connection(&m_http_client); |
| 126 |
|
| 127 |
bool ok = connection.is_open(); |
| 128 |
if (!ok) |
| 129 |
{ |
| 130 |
fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port(); |
| 131 |
return false; |
| 132 |
} |
| 133 |
ok = epee::net_utils::invoke_http_json(relative_url, req, res, m_http_client, t_http_connection::TIMEOUT()); |
| 134 |
if (!ok || res.status != CORE_RPC_STATUS_OK) |
| 135 |
{ |
| 136 |
fail_msg_writer() << fail_msg << "-- rpc_request: " << res.status; |
| 137 |
return false; |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
return true; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
bool check_connection() |
| 146 |
{ |
| 147 |
t_http_connection connection(&m_http_client); |
| 148 |
return connection.is_open(); |
| 149 |
} |
| 150 |
}; |
| 151 |
} |