Electroneum
tools::wallet_rpc_server Class Reference

#include <wallet_rpc_server.h>

Inheritance diagram for tools::wallet_rpc_server:
Collaboration diagram for tools::wallet_rpc_server:

Public Types

typedef epee::net_utils::connection_context_base connection_context
 

Public Member Functions

 wallet_rpc_server ()
 
 ~wallet_rpc_server ()
 
bool init (const boost::program_options::variables_map *vm)
 
bool run ()
 
void stop ()
 
void set_wallet (wallet2 *cr)
 
- Public Member Functions inherited from epee::http_server_impl_base< wallet_rpc_server >
 http_server_impl_base ()
 
 http_server_impl_base (boost::asio::io_service &external_io_service)
 
bool init (std::function< void(size_t, uint8_t *)> rng, const std::string &bind_port="0", const std::string &bind_ip="0.0.0.0", std::vector< std::string > access_control_origins=std::vector< std::string >(), boost::optional< net_utils::http::login > user=boost::none, net_utils::ssl_options_t ssl_options=net_utils::ssl_support_t::e_ssl_support_autodetect)
 
bool run (size_t threads_count, bool wait=true)
 
bool deinit ()
 
bool timed_wait_server_stop (uint64_t ms)
 
bool send_stop_signal ()
 
int get_binded_port ()
 
long get_connections_count () const
 
- Public Member Functions inherited from epee::net_utils::http::i_http_server_handler< epee::net_utils::connection_context_base >
virtual ~i_http_server_handler ()
 
virtual bool handle_http_request (const http_request_info &query_info, http_response_info &response, epee::net_utils::connection_context_base &m_conn_context)=0
 
virtual bool init_server_thread ()
 
virtual bool deinit_server_thread ()
 

Static Public Member Functions

static const char * tr (const char *str)
 

Additional Inherited Members

- Protected Attributes inherited from epee::http_server_impl_base< wallet_rpc_server >
net_utils::boosted_tcp_server< net_utils::http::http_custom_handler< epee::net_utils::connection_context_base > > m_net_server
 

Detailed Description

Definition at line 51 of file wallet_rpc_server.h.

Member Typedef Documentation

◆ connection_context

Constructor & Destructor Documentation

◆ wallet_rpc_server()

tools::wallet_rpc_server::wallet_rpc_server ( )

Definition at line 109 of file wallet_rpc_server.cpp.

109  :m_wallet(NULL), rpc_login_file(), m_stop(false), m_restricted(false), m_vm(NULL)
110  {
111  }

◆ ~wallet_rpc_server()

tools::wallet_rpc_server::~wallet_rpc_server ( )

Definition at line 113 of file wallet_rpc_server.cpp.

114  {
115  if (m_wallet)
116  delete m_wallet;
117  }

Member Function Documentation

◆ init()

bool tools::wallet_rpc_server::init ( const boost::program_options::variables_map *  vm)

Definition at line 163 of file wallet_rpc_server.cpp.

164  {
165  auto rpc_config = cryptonote::rpc_args::process(*vm);
166  if (!rpc_config)
167  return false;
168 
169  m_vm = vm;
170 
172  m_testnet = command_line::get_arg(*m_vm, arg_testnet);
173 
174  boost::optional<epee::net_utils::http::login> http_login{};
175  std::string bind_port = command_line::get_arg(*m_vm, arg_rpc_bind_port);
176  const bool disable_auth = command_line::get_arg(*m_vm, arg_disable_rpc_login);
177  m_restricted = command_line::get_arg(*m_vm, arg_restricted);
178  if (!command_line::is_arg_defaulted(*m_vm, arg_wallet_dir))
179  {
181  {
182  MERROR(arg_wallet_dir.name << " and " << wallet_args::arg_wallet_file().name << " are incompatible, use only one of them");
183  return false;
184  }
185  m_wallet_dir = command_line::get_arg(*m_vm, arg_wallet_dir);
186 #ifdef _WIN32
187 #define MKDIR(path, mode) mkdir(path)
188 #else
189 #define MKDIR(path, mode) mkdir(path, mode)
190 #endif
191  if (!m_wallet_dir.empty() && MKDIR(m_wallet_dir.c_str(), 0700) < 0 && errno != EEXIST)
192  {
193 #ifdef _WIN32
194  LOG_ERROR(tr("Failed to create directory ") + m_wallet_dir);
195 #else
196  LOG_ERROR((boost::format(tr("Failed to create directory %s: %s")) % m_wallet_dir % strerror(errno)).str());
197 #endif
198  return false;
199  }
200  }
201 
202  if (disable_auth)
203  {
204  if (rpc_config->login)
205  {
207  LOG_ERROR(tr("Cannot specify --") << arg_disable_rpc_login.name << tr(" and --") << arg.rpc_login.name);
208  return false;
209  }
210  }
211  else // auth enabled
212  {
213  if (!rpc_config->login)
214  {
215  std::array<std::uint8_t, 16> rand_128bit{{}};
216  crypto::rand(rand_128bit.size(), rand_128bit.data());
217  http_login.emplace(
218  default_rpc_username,
219  string_encoding::base64_encode(rand_128bit.data(), rand_128bit.size())
220  );
221 
222  std::string temp = "electroneum-wallet-rpc." + bind_port + ".login";
223  rpc_login_file = tools::private_file::create(temp);
224  if (!rpc_login_file.handle())
225  {
226  LOG_ERROR(tr("Failed to create file ") << temp << tr(". Check permissions or remove file"));
227  return false;
228  }
229  std::fputs(http_login->username.c_str(), rpc_login_file.handle());
230  std::fputc(':', rpc_login_file.handle());
231  const epee::wipeable_string password = http_login->password;
232  std::fwrite(password.data(), 1, password.size(), rpc_login_file.handle());
233  std::fflush(rpc_login_file.handle());
234  if (std::ferror(rpc_login_file.handle()))
235  {
236  LOG_ERROR(tr("Error writing to file ") << temp);
237  return false;
238  }
239  LOG_PRINT_L0(tr("RPC username/password is stored in file ") << temp);
240  }
241  else // chosen user/pass
242  {
243  http_login.emplace(
244  std::move(rpc_config->login->username), std::move(rpc_config->login->password).password()
245  );
246  }
247  assert(bool(http_login));
248  } // end auth enabled
249 
250  m_auto_refresh_period = DEFAULT_AUTO_REFRESH_PERIOD;
251  m_last_auto_refresh_time = boost::posix_time::min_date_time;
252 
253  check_background_mining();
254 
255  m_net_server.set_threads_prefix("RPC");
256  auto rng = [](size_t len, uint8_t *ptr) { return crypto::rand(len, ptr); };
258  rng, std::move(bind_port), std::move(rpc_config->bind_ip), std::move(rpc_config->access_control_origins), std::move(http_login),
259  std::move(rpc_config->ssl_options)
260  );
261  }
std::string base64_encode(unsigned char const *bytes_to_encode, size_t in_len)
std::FILE * handle() const noexcept
Definition: util.h:91
#define MERROR(x)
Definition: misc_log_ex.h:73
net_utils::boosted_tcp_server< net_utils::http::http_custom_handler< epee::net_utils::connection_context_base > > m_net_server
#define MKDIR(path, mode)
command_line::arg_descriptor< std::string > arg_wallet_file()
Definition: wallet_args.cpp:76
::std::string string
Definition: gtest-port.h:1097
static boost::optional< rpc_args > process(const boost::program_options::variables_map &vm, const bool any_cert_option=false)
Definition: rpc_args.cpp:125
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
unsigned char uint8_t
Definition: stdint.h:124
boost::filesystem::path data_dir
Definition: main.cpp:50
#define DEFAULT_AUTO_REFRESH_PERIOD
void rand(size_t N, uint8_t *bytes)
Definition: crypto.h:209
bool init(std::function< void(size_t, uint8_t *)> rng, const std::string &bind_port="0", const std::string &bind_ip="0.0.0.0", std::vector< std::string > access_control_origins=std::vector< std::string >(), boost::optional< net_utils::http::login > user=boost::none, net_utils::ssl_options_t ssl_options=net_utils::ssl_support_t::e_ssl_support_autodetect)
const command_line::arg_descriptor< std::string, false, true, 2 > arg_data_dir
const T & move(const T &t)
Definition: gtest-port.h:1317
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
static const char * tr(const char *str)
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
Definition: command_line.h:271
static private_file create(std::string filename)
Definition: util.cpp:125
bool is_arg_defaulted(const boost::program_options::variables_map &vm, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg)
Definition: command_line.h:265
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

bool tools::wallet_rpc_server::run ( )

Definition at line 124 of file wallet_rpc_server.cpp.

125  {
126  m_stop = false;
127  m_net_server.add_idle_handler([this](){
128  if (m_auto_refresh_period == 0) // disabled
129  return true;
130  if (boost::posix_time::microsec_clock::universal_time() < m_last_auto_refresh_time + boost::posix_time::seconds(m_auto_refresh_period))
131  return true;
132  try {
133  if (m_wallet) m_wallet->refresh(m_wallet->is_trusted_daemon());
134  } catch (const std::exception& ex) {
135  LOG_ERROR("Exception at while refreshing, what=" << ex.what());
136  }
137  m_last_auto_refresh_time = boost::posix_time::microsec_clock::universal_time();
138  return true;
139  }, 1000);
140  m_net_server.add_idle_handler([this](){
141  if (m_stop.load(std::memory_order_relaxed))
142  {
144  return false;
145  }
146  return true;
147  }, 500);
148 
149  //DO NOT START THIS SERVER IN MORE THEN 1 THREADS WITHOUT REFACTORING
151  }
net_utils::boosted_tcp_server< net_utils::http::http_custom_handler< epee::net_utils::connection_context_base > > m_net_server
bool is_trusted_daemon() const
Definition: wallet2.h:765
bool run(size_t threads_count, bool wait=true)
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
void refresh(bool trusted_daemon)
Definition: wallet2.cpp:3060
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_wallet()

void tools::wallet_rpc_server::set_wallet ( wallet2 cr)

Definition at line 119 of file wallet_rpc_server.cpp.

120  {
121  m_wallet = cr;
122  }
Here is the caller graph for this function:

◆ stop()

void tools::wallet_rpc_server::stop ( )

Definition at line 153 of file wallet_rpc_server.cpp.

154  {
155  if (m_wallet)
156  {
157  m_wallet->store();
158  delete m_wallet;
159  m_wallet = NULL;
160  }
161  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tr()

const char * tools::wallet_rpc_server::tr ( const char *  str)
static

Definition at line 103 of file wallet_rpc_server.cpp.

104  {
105  return i18n_translate(str, "tools::wallet_rpc_server");
106  }
const char * i18n_translate(const char *s, const std::string &context)
Definition: i18n.cpp:323
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: