From 7ae031548261ec79ad7b888a7403415d64929adb Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Sun, 15 Dec 2019 17:05:49 +0800 Subject: [PATCH] Add --daemon-login user:pass option https://github.com/moneroexamples/onion-monero-blockchain-explorer/issues/200 --- main.cpp | 41 ++++++++++++++++++++++++++++++++++++++--- src/CmdLineOptions.cpp | 2 ++ src/MempoolStatus.cpp | 6 +++--- src/MempoolStatus.h | 3 +++ src/page.h | 5 +++-- src/rpccalls.cpp | 14 ++++++++------ src/rpccalls.h | 5 +++++ 7 files changed, 62 insertions(+), 14 deletions(-) diff --git a/main.cpp b/main.cpp index 078aade..c4324fb 100644 --- a/main.cpp +++ b/main.cpp @@ -65,6 +65,7 @@ main(int ac, const char* av[]) auto mainnet_url = opts.get_option("mainnet-url"); auto mempool_info_timeout_opt = opts.get_option("mempool-info-timeout"); auto mempool_refresh_time_opt = opts.get_option("mempool-refresh-time"); + auto daemon_login_opt = opts.get_option("daemon-login"); auto testnet_opt = opts.get_option("testnet"); auto stagenet_opt = opts.get_option("stagenet"); auto enable_key_image_checker_opt = opts.get_option("enable-key-image-checker"); @@ -79,7 +80,6 @@ main(int ac, const char* av[]) auto enable_emission_monitor_opt = opts.get_option("enable-emission-monitor"); - bool testnet {*testnet_opt}; bool stagenet {*stagenet_opt}; @@ -123,9 +123,41 @@ main(int ac, const char* av[]) string ssl_crt_file; string ssl_key_file; + xmreg::rpccalls::login_opt daemon_rpc_login {}; + + + if (daemon_login_opt) + { + + string user {}; + epee::wipeable_string pass {}; + + string daemon_login = *daemon_login_opt; + + size_t colon_location = daemon_login.find_first_of(':'); + + if (colon_location != std::string::npos) + { + // have colon for user:password + user = daemon_login.substr(0, colon_location); + pass = daemon_login.substr(colon_location + 1); + } + else + { + user = *daemon_login_opt; + } + + daemon_rpc_login = epee::net_utils::http::login {user, pass}; + + //cout << "colon_location: " << colon_location << endl; + // cout << "user: " << user << endl; + // cout << "pass: " << std::string(pass.data(), pass.size()) << endl; + } + + // check if ssl enabled and files exist - if (ssl_crt_file_opt and ssl_key_file_opt) + if (ssl_crt_file_opt && ssl_key_file_opt) { if (!boost::filesystem::exists(boost::filesystem::path(*ssl_crt_file_opt))) { @@ -237,6 +269,8 @@ main(int ac, const char* av[]) = nettype; xmreg::MempoolStatus::deamon_url = deamon_url; + xmreg::MempoolStatus::login + = daemon_rpc_login; xmreg::MempoolStatus::set_blockchain_variables( &mcore, core_storage); @@ -281,7 +315,8 @@ main(int ac, const char* av[]) mempool_info_timeout, *testnet_url, *stagenet_url, - *mainnet_url); + *mainnet_url, + daemon_rpc_login); // crow instance crow::SimpleApp app; diff --git a/src/CmdLineOptions.cpp b/src/CmdLineOptions.cpp index e42330a..4304fc1 100644 --- a/src/CmdLineOptions.cpp +++ b/src/CmdLineOptions.cpp @@ -69,6 +69,8 @@ namespace xmreg "path to crt file for ssl (https) functionality") ("ssl-key-file", value(), "path to key file for ssl (https) functionality") + ("daemon-login", value(), + "Specify username[:password] for daemon RPC client") ("deamon-url,d", value()->default_value("http:://127.0.0.1:18081"), "Monero daemon url"); diff --git a/src/MempoolStatus.cpp b/src/MempoolStatus.cpp index 90e9fe0..3000407 100644 --- a/src/MempoolStatus.cpp +++ b/src/MempoolStatus.cpp @@ -4,7 +4,6 @@ #include "MempoolStatus.h" -#include "rpccalls.h" namespace xmreg { @@ -97,7 +96,7 @@ MempoolStatus::start_mempool_status_thread() bool MempoolStatus::read_mempool() { - rpccalls rpc {deamon_url}; + rpccalls rpc {deamon_url, login}; string error_msg; @@ -242,7 +241,7 @@ MempoolStatus::read_mempool() bool MempoolStatus::read_network_info() { - rpccalls rpc {deamon_url}; + rpccalls rpc {deamon_url, login}; COMMAND_RPC_GET_INFO::response rpc_network_info; @@ -352,6 +351,7 @@ atomic MempoolStatus::is_running {false}; boost::thread MempoolStatus::m_thread; Blockchain* MempoolStatus::core_storage {nullptr}; xmreg::MicroCore* MempoolStatus::mcore {nullptr}; +rpccalls::login_opt MempoolStatus::login {}; vector MempoolStatus::mempool_txs; atomic MempoolStatus::current_network_info; atomic MempoolStatus::mempool_no {0}; // no of txs diff --git a/src/MempoolStatus.h b/src/MempoolStatus.h index 0bd10e0..7459f65 100644 --- a/src/MempoolStatus.h +++ b/src/MempoolStatus.h @@ -7,6 +7,7 @@ #include "MicroCore.h" +#include "rpccalls.h" #include @@ -131,6 +132,8 @@ struct MempoolStatus static string deamon_url; static cryptonote::network_type nettype; + static rpccalls::login_opt login; + // make object for accessing the blockchain here static MicroCore* mcore; static Blockchain* core_storage; diff --git a/src/page.h b/src/page.h index ba53859..65c0cd3 100644 --- a/src/page.h +++ b/src/page.h @@ -518,10 +518,11 @@ page(MicroCore* _mcore, uint64_t _mempool_info_timeout, string _testnet_url, string _stagenet_url, - string _mainnet_url) + string _mainnet_url, + rpccalls::login_opt _daemon_rpc_login) : mcore {_mcore}, core_storage {_core_storage}, - rpc {_deamon_url}, + rpc {_deamon_url, _daemon_rpc_login}, server_timestamp {std::time(nullptr)}, nettype {_nettype}, enable_pusher {_enable_pusher}, diff --git a/src/rpccalls.cpp b/src/rpccalls.cpp index 26a763a..f0c3a7a 100644 --- a/src/rpccalls.cpp +++ b/src/rpccalls.cpp @@ -8,7 +8,9 @@ namespace xmreg { -rpccalls::rpccalls(string _deamon_url, +rpccalls::rpccalls( + string _deamon_url, + login_opt login, uint64_t _timeout) : deamon_url {_deamon_url}, timeout_time {_timeout} @@ -17,12 +19,12 @@ rpccalls::rpccalls(string _deamon_url, port = std::to_string(url.port); - timeout_time_ms = std::chrono::milliseconds {timeout_time}; + timeout_time_ms = std::chrono::milliseconds {timeout_time}; - m_http_client.set_server( - deamon_url, - boost::optional{}, - epee::net_utils::ssl_support_t::e_ssl_support_disabled); + m_http_client.set_server( + deamon_url, + login, + epee::net_utils::ssl_support_t::e_ssl_support_disabled); } bool diff --git a/src/rpccalls.h b/src/rpccalls.h index 720936e..17a2d2c 100644 --- a/src/rpccalls.h +++ b/src/rpccalls.h @@ -8,6 +8,8 @@ #include "monero_headers.h" +#include "wipeable_string.h" + #include #include @@ -81,7 +83,10 @@ class rpccalls public: + using login_opt = boost::optional; + rpccalls(string _deamon_url = "http:://127.0.0.1:18081", + login_opt _login = login_opt {}, uint64_t _timeout = 200000); bool