| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | #include <boost/algorithm/string.hpp> |
| 30 | #include "cryptonote_config.h" |
| 31 | #include "version.h" |
| 32 | #include "string_tools.h" |
| 33 | #include "daemon/command_server.h" |
| 34 | |
| 35 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 36 | #define MONERO_DEFAULT_LOG_CATEGORY "daemon" |
| 37 | |
| 38 | namespace daemonize { |
| 39 | |
| 40 | namespace p = std::placeholders; |
| 41 | |
| 42 | t_command_server::t_command_server( |
| 43 | uint32_t ip |
| 44 | , uint16_t port |
| 45 | , const boost::optional<tools::login>& login |
| 46 | , const epee::net_utils::ssl_options_t& ssl_options |
| 47 | , bool is_rpc |
| 48 | , cryptonote::core_rpc_server* rpc_server |
| 49 | ) |
| 50 | : m_parser(ip, port, login, ssl_options, is_rpc, rpc_server) |
| 51 | , m_command_lookup() |
| 52 | , m_is_rpc(is_rpc) |
| 53 | { |
| 54 | m_command_lookup.set_handler( |
| 55 | "help" |
| 56 | , std::bind(&t_command_server::help, this, p::_1) |
| 57 | , "help [<command>]" |
| 58 | , "Show the help section or the documentation about a <command>." |
| 59 | ); |
| 60 | m_command_lookup.set_handler( |
| 61 | "apropos" |
| 62 | , std::bind(&t_command_server::apropos, this, p::_1) |
| 63 | , "apropos <keyword> [<keyword> ...]" |
| 64 | , "Search all command descriptions for keyword(s)." |
| 65 | ); |
| 66 | m_command_lookup.set_handler( |
| 67 | "print_height" |
| 68 | , std::bind(&t_command_parser_executor::print_height, &m_parser, p::_1) |
| 69 | , "Print the local blockchain height." |
| 70 | ); |
| 71 | m_command_lookup.set_handler( |
| 72 | "print_pl" |
| 73 | , std::bind(&t_command_parser_executor::print_peer_list, &m_parser, p::_1) |
| 74 | , "print_pl [white] [gray] [pruned] [publicrpc] [<limit>]" |
| 75 | , "Print the current peer list." |
| 76 | ); |
| 77 | m_command_lookup.set_handler( |
| 78 | "print_pl_stats" |
| 79 | , std::bind(&t_command_parser_executor::print_peer_list_stats, &m_parser, p::_1) |
| 80 | , "Print the peer list statistics." |
| 81 | ); |
| 82 | m_command_lookup.set_handler( |
| 83 | "print_cn" |
| 84 | , std::bind(&t_command_parser_executor::print_connections, &m_parser, p::_1) |
| 85 | , "Print the current connections." |
| 86 | ); |
| 87 | m_command_lookup.set_handler( |
| 88 | "print_net_stats" |
| 89 | , std::bind(&t_command_parser_executor::print_net_stats, &m_parser, p::_1) |
| 90 | , "Print network statistics." |
| 91 | ); |
| 92 | m_command_lookup.set_handler( |
| 93 | "print_bc" |
| 94 | , std::bind(&t_command_parser_executor::print_blockchain_info, &m_parser, p::_1) |
| 95 | , "print_bc <begin_height> [<end_height>]" |
| 96 | , "Print the blockchain info in a given blocks range." |
| 97 | ); |
| 98 | m_command_lookup.set_handler( |
| 99 | "print_block" |
| 100 | , std::bind(&t_command_parser_executor::print_block, &m_parser, p::_1) |
| 101 | , "print_block <block_hash> | <block_height>" |
| 102 | , "Print a given block." |
| 103 | ); |
| 104 | m_command_lookup.set_handler( |
| 105 | "print_tx" |
| 106 | , std::bind(&t_command_parser_executor::print_transaction, &m_parser, p::_1) |
| 107 | , "print_tx <transaction_hash> [+hex] [+json]" |
| 108 | , "Print a given transaction." |
| 109 | ); |
| 110 | m_command_lookup.set_handler( |
| 111 | "is_key_image_spent" |
| 112 | , std::bind(&t_command_parser_executor::is_key_image_spent, &m_parser, p::_1) |
| 113 | , "is_key_image_spent <key_image>" |
| 114 | , "Print whether a given key image is in the spent key images set." |
| 115 | ); |
| 116 | m_command_lookup.set_handler( |
| 117 | "start_mining" |
| 118 | , std::bind(&t_command_parser_executor::start_mining, &m_parser, p::_1) |
| 119 | , "start_mining <addr> [<threads>|auto] [do_background_mining] [ignore_battery]" |
| 120 | , "Start mining for specified address. Defaults to 1 thread and no background mining. Use \"auto\" to autodetect optimal number of threads." |
| 121 | ); |
| 122 | m_command_lookup.set_handler( |
| 123 | "stop_mining" |
| 124 | , std::bind(&t_command_parser_executor::stop_mining, &m_parser, p::_1) |
| 125 | , "Stop mining." |
| 126 | ); |
| 127 | m_command_lookup.set_handler( |
| 128 | "mining_status" |
| 129 | , std::bind(&t_command_parser_executor::mining_status, &m_parser, p::_1) |
| 130 | , "Show current mining status." |
| 131 | ); |
| 132 | m_command_lookup.set_handler( |
| 133 | "print_pool" |
| 134 | , std::bind(&t_command_parser_executor::print_transaction_pool_long, &m_parser, p::_1) |
| 135 | , "Print the transaction pool using a long format." |
| 136 | ); |
| 137 | m_command_lookup.set_handler( |
| 138 | "print_pool_sh" |
| 139 | , std::bind(&t_command_parser_executor::print_transaction_pool_short, &m_parser, p::_1) |
| 140 | , "Print transaction pool using a short format." |
| 141 | ); |
| 142 | m_command_lookup.set_handler( |
| 143 | "print_pool_stats" |
| 144 | , std::bind(&t_command_parser_executor::print_transaction_pool_stats, &m_parser, p::_1) |
| 145 | , "Print the transaction pool's statistics." |
| 146 | ); |
| 147 | m_command_lookup.set_handler( |
| 148 | "show_hr" |
| 149 | , std::bind(&t_command_parser_executor::show_hash_rate, &m_parser, p::_1) |
| 150 | , "Start showing the current hash rate." |
| 151 | ); |
| 152 | m_command_lookup.set_handler( |
| 153 | "hide_hr" |
| 154 | , std::bind(&t_command_parser_executor::hide_hash_rate, &m_parser, p::_1) |
| 155 | , "Stop showing the hash rate." |
| 156 | ); |
| 157 | m_command_lookup.set_handler( |
| 158 | "save" |
| 159 | , std::bind(&t_command_parser_executor::save_blockchain, &m_parser, p::_1) |
| 160 | , "Save the blockchain." |
| 161 | ); |
| 162 | m_command_lookup.set_handler( |
| 163 | "set_log" |
| 164 | , std::bind(&t_command_parser_executor::set_log_level, &m_parser, p::_1) |
| 165 | , "set_log <level>|<{+,-,}categories>" |
| 166 | , "Change the current log level/categories where <level> is a number 0-4." |
| 167 | ); |
| 168 | m_command_lookup.set_handler( |
| 169 | "diff" |
| 170 | , std::bind(&t_command_parser_executor::show_difficulty, &m_parser, p::_1) |
| 171 | , "Show the current difficulty." |
| 172 | ); |
| 173 | m_command_lookup.set_handler( |
| 174 | "status" |
| 175 | , std::bind(&t_command_parser_executor::show_status, &m_parser, p::_1) |
| 176 | , "Show the current status." |
| 177 | ); |
| 178 | m_command_lookup.set_handler( |
| 179 | "stop_daemon" |
| 180 | , std::bind(&t_command_parser_executor::stop_daemon, &m_parser, p::_1) |
| 181 | , "Stop the daemon." |
| 182 | ); |
| 183 | m_command_lookup.set_handler( |
| 184 | "exit" |
| 185 | , std::bind(&t_command_parser_executor::stop_daemon, &m_parser, p::_1) |
| 186 | , "Stop the daemon." |
| 187 | ); |
| 188 | m_command_lookup.set_handler( |
| 189 | "print_status" |
| 190 | , std::bind(&t_command_parser_executor::print_status, &m_parser, p::_1) |
| 191 | , "Print the current daemon status." |
| 192 | ); |
| 193 | m_command_lookup.set_handler( |
| 194 | "limit" |
| 195 | , std::bind(&t_command_parser_executor::set_limit, &m_parser, p::_1) |
| 196 | , "limit [<kB/s>]" |
| 197 | , "Get or set the download and upload limit." |
| 198 | ); |
| 199 | m_command_lookup.set_handler( |
| 200 | "limit_up" |
| 201 | , std::bind(&t_command_parser_executor::set_limit_up, &m_parser, p::_1) |
| 202 | , "limit_up [<kB/s>]" |
| 203 | , "Get or set the upload limit." |
| 204 | ); |
| 205 | m_command_lookup.set_handler( |
| 206 | "limit_down" |
| 207 | , std::bind(&t_command_parser_executor::set_limit_down, &m_parser, p::_1) |
| 208 | , "limit_down [<kB/s>]" |
| 209 | , "Get or set the download limit." |
| 210 | ); |
| 211 | m_command_lookup.set_handler( |
| 212 | "out_peers" |
| 213 | , std::bind(&t_command_parser_executor::out_peers, &m_parser, p::_1) |
| 214 | , "out_peers <max_number>" |
| 215 | , "Set the <max_number> of out peers." |
| 216 | ); |
| 217 | m_command_lookup.set_handler( |
| 218 | "in_peers" |
| 219 | , std::bind(&t_command_parser_executor::in_peers, &m_parser, p::_1) |
| 220 | , "in_peers <max_number>" |
| 221 | , "Set the <max_number> of in peers." |
| 222 | ); |
| 223 | m_command_lookup.set_handler( |
| 224 | "hard_fork_info" |
| 225 | , std::bind(&t_command_parser_executor::hard_fork_info, &m_parser, p::_1) |
| 226 | , "hard_fork_info <version>" |
| 227 | , "Print the hard fork voting information. If given a version, prints whether is this version enabled." |
| 228 | ); |
| 229 | m_command_lookup.set_handler( |
| 230 | "bans" |
| 231 | , std::bind(&t_command_parser_executor::show_bans, &m_parser, p::_1) |
| 232 | , "Show the currently banned IPs." |
| 233 | ); |
| 234 | m_command_lookup.set_handler( |
| 235 | "ban" |
| 236 | , std::bind(&t_command_parser_executor::ban, &m_parser, p::_1) |
| 237 | , "ban [<IP>|@<filename>] [<seconds>]" |
| 238 | , "Ban a given <IP> or list of IPs from a file for a given amount of <seconds>." |
| 239 | ); |
| 240 | m_command_lookup.set_handler( |
| 241 | "unban" |
| 242 | , std::bind(&t_command_parser_executor::unban, &m_parser, p::_1) |
| 243 | , "unban <address>" |
| 244 | , "Unban a given <IP>." |
| 245 | ); |
| 246 | m_command_lookup.set_handler( |
| 247 | "banned" |
| 248 | , std::bind(&t_command_parser_executor::banned, &m_parser, p::_1) |
| 249 | , "banned <address>" |
| 250 | , "Check whether an <address> is banned." |
| 251 | ); |
| 252 | m_command_lookup.set_handler( |
| 253 | "flush_txpool" |
| 254 | , std::bind(&t_command_parser_executor::flush_txpool, &m_parser, p::_1) |
| 255 | , "flush_txpool [<txid>]" |
| 256 | , "Flush a transaction from the tx pool by its <txid>, or the whole tx pool." |
| 257 | ); |
| 258 | m_command_lookup.set_handler( |
| 259 | "output_histogram" |
| 260 | , std::bind(&t_command_parser_executor::output_histogram, &m_parser, p::_1) |
| 261 | , "output_histogram [@<amount>] <min_count> [<max_count>]" |
| 262 | , "Print the output histogram of outputs." |
| 263 | ); |
| 264 | m_command_lookup.set_handler( |
| 265 | "print_coinbase_tx_sum" |
| 266 | , std::bind(&t_command_parser_executor::print_coinbase_tx_sum, &m_parser, p::_1) |
| 267 | , "print_coinbase_tx_sum <start_height> [<block_count>]" |
| 268 | , "Print the sum of coinbase transactions." |
| 269 | ); |
| 270 | m_command_lookup.set_handler( |
| 271 | "alt_chain_info" |
| 272 | , std::bind(&t_command_parser_executor::alt_chain_info, &m_parser, p::_1) |
| 273 | , "alt_chain_info [blockhash]" |
| 274 | , "Print the information about alternative chains." |
| 275 | ); |
| 276 | m_command_lookup.set_handler( |
| 277 | "bc_dyn_stats" |
| 278 | , std::bind(&t_command_parser_executor::print_blockchain_dynamic_stats, &m_parser, p::_1) |
| 279 | , "bc_dyn_stats <last_block_count>" |
| 280 | , "Print the information about current blockchain dynamic state." |
| 281 | ); |
| 282 | m_command_lookup.set_handler( |
| 283 | "update" |
| 284 | , std::bind(&t_command_parser_executor::update, &m_parser, p::_1) |
| 285 | , "update (check|download)" |
| 286 | , "Check if an update is available, optionally downloads it if there is. Updating is not yet implemented." |
| 287 | ); |
| 288 | m_command_lookup.set_handler( |
| 289 | "relay_tx" |
| 290 | , std::bind(&t_command_parser_executor::relay_tx, &m_parser, p::_1) |
| 291 | , "relay_tx <txid>" |
| 292 | , "Relay a given transaction by its <txid>." |
| 293 | ); |
| 294 | m_command_lookup.set_handler( |
| 295 | "sync_info" |
| 296 | , std::bind(&t_command_parser_executor::sync_info, &m_parser, p::_1) |
| 297 | , "Print information about the blockchain sync state." |
| 298 | ); |
| 299 | m_command_lookup.set_handler( |
| 300 | "pop_blocks" |
| 301 | , std::bind(&t_command_parser_executor::pop_blocks, &m_parser, p::_1) |
| 302 | , "pop_blocks <nblocks>" |
| 303 | , "Remove blocks from end of blockchain" |
| 304 | ); |
| 305 | m_command_lookup.set_handler( |
| 306 | "rpc_payments" |
| 307 | , std::bind(&t_command_parser_executor::rpc_payments, &m_parser, p::_1) |
| 308 | , "Print information about RPC payments." |
| 309 | ); |
| 310 | m_command_lookup.set_handler( |
| 311 | "version" |
| 312 | , std::bind(&t_command_parser_executor::version, &m_parser, p::_1) |
| 313 | , "Print version information." |
| 314 | ); |
| 315 | m_command_lookup.set_handler( |
| 316 | "prune_blockchain" |
| 317 | , std::bind(&t_command_parser_executor::prune_blockchain, &m_parser, p::_1) |
| 318 | , "prune_blockchain [confirm]" |
| 319 | , "Prune the blockchain." |
| 320 | ); |
| 321 | m_command_lookup.set_handler( |
| 322 | "check_blockchain_pruning" |
| 323 | , std::bind(&t_command_parser_executor::check_blockchain_pruning, &m_parser, p::_1) |
| 324 | , "Check the blockchain pruning." |
| 325 | ); |
| 326 | m_command_lookup.set_handler( |
| 327 | "set_bootstrap_daemon" |
| 328 | , std::bind(&t_command_parser_executor::set_bootstrap_daemon, &m_parser, p::_1) |
| 329 | , "set_bootstrap_daemon (auto | none | host[:port] [username] [password]) [proxy_ip:proxy_port]" |
| 330 | , "URL of a 'bootstrap' remote daemon that the connected wallets can use while this daemon is still not fully synced.\n" |
| 331 | "Use 'auto' to enable automatic public nodes discovering and bootstrap daemon switching" |
| 332 | ); |
| 333 | m_command_lookup.set_handler( |
| 334 | "flush_cache" |
| 335 | , std::bind(&t_command_parser_executor::flush_cache, &m_parser, p::_1) |
| 336 | , "flush_cache [bad-txs] [bad-blocks]" |
| 337 | , "Flush the specified cache(s)." |
| 338 | ); |
| 339 | } |
| 340 | |
| 341 | bool t_command_server::process_command_str(const std::string& cmd) |
| 342 | { |
| 343 | return m_command_lookup.process_command_str(cmd); |
| 344 | } |
| 345 | |
| 346 | bool t_command_server::process_command_vec(const std::vector<std::string>& cmd) |
| 347 | { |
| 348 | bool result = m_command_lookup.process_command_vec(cmd); |
| 349 | if (!result) |
| 350 | { |
| 351 | help(std::vector<std::string>()); |
| 352 | } |
| 353 | return result; |
| 354 | } |
| 355 | |
| 356 | bool t_command_server::start_handling(std::function<void(void)> exit_handler) |
| 357 | { |
| 358 | if (m_is_rpc) return false; |
| 359 | |
| 360 | m_command_lookup.start_handling("", "Use \"help\" to list all commands and their usage\n", exit_handler); |
| 361 | |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | void t_command_server::stop_handling() |
| 366 | { |
| 367 | if (m_is_rpc) return; |
| 368 | |
| 369 | m_command_lookup.stop_handling(); |
| 370 | } |
| 371 | |
| 372 | bool t_command_server::help(const std::vector<std::string>& args) |
| 373 | { |
| 374 | if(args.empty()) |
| 375 | { |
| 376 | std::cout << get_commands_str() << std::endl; |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | std::cout << get_command_usage(args) << std::endl; |
| 381 | } |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | bool t_command_server::apropos(const std::vector<std::string>& args) |
| 386 | { |
| 387 | if (args.empty()) |
| 388 | { |
| 389 | std::cout << "Missing keyword" << std::endl; |
| 390 | return true; |
| 391 | } |
| 392 | const std::vector<std::string>& command_list = m_command_lookup.get_command_list(args); |
| 393 | if (command_list.empty()) |
| 394 | { |
| 395 | std::cout << "Nothing found" << std::endl; |
| 396 | return true; |
| 397 | } |
| 398 | |
| 399 | std::cout << std::endl; |
| 400 | for(auto const& command:command_list) |
| 401 | { |
| 402 | std::vector<std::string> cmd; |
| 403 | cmd.push_back(command); |
| 404 | std::pair<std::string, std::string> documentation = m_command_lookup.get_documentation(cmd); |
| 405 | std::cout << " " << documentation.first << std::endl; |
| 406 | } |
| 407 | std::cout << std::endl; |
| 408 | |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | std::string t_command_server::get_commands_str() |
| 413 | { |
| 414 | std::stringstream ss; |
| 415 | ss << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << std::endl; |
| 416 | ss << "Commands: " << std::endl; |
| 417 | std::string usage = m_command_lookup.get_usage(); |
| 418 | boost::replace_all(usage, "\n", "\n "); |
| 419 | usage.insert(0, " "); |
| 420 | ss << usage; |
| 421 | return ss.str(); |
| 422 | } |
| 423 | |
| 424 | std::string t_command_server::get_command_usage(const std::vector<std::string> &args) |
| 425 | { |
| 426 | std::pair<std::string, std::string> documentation = m_command_lookup.get_documentation(args); |
| 427 | std::stringstream ss; |
| 428 | if(documentation.first.empty()) |
| 429 | { |
| 430 | ss << "Unknown command: " << args.front() << std::endl; |
| 431 | } |
| 432 | else |
| 433 | { |
| 434 | std::string usage = documentation.second.empty() ? args.front() : documentation.first; |
| 435 | std::string description = documentation.second.empty() ? documentation.first : documentation.second; |
| 436 | usage.insert(0, " "); |
| 437 | ss << "Command usage: " << std::endl << usage << std::endl << std::endl; |
| 438 | boost::replace_all(description, "\n", "\n "); |
| 439 | description.insert(0, " "); |
| 440 | ss << "Command description: " << std::endl << description << std::endl; |
| 441 | } |
| 442 | return ss.str(); |
| 443 | } |
| 444 | |
| 445 | } // namespace daemonize |