diff --git a/src/common/util.cpp b/src/common/util.cpp index 6dec6af2..bfcf86bc 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -35,6 +35,7 @@ using namespace epee; #include "util.h" #include "cryptonote_config.h" +#include "net/http_client.h" // epee::net_utils::... #ifdef WIN32 #include @@ -44,6 +45,7 @@ using namespace epee; #include #endif #include +#include namespace tools @@ -531,4 +533,39 @@ std::string get_nix_version_display_string() boost::lock_guard lock(max_concurrency_lock); return max_concurrency; } + + bool is_local_address(const std::string &address) + { + // extract host + epee::net_utils::http::url_content u_c; + if (!epee::net_utils::parse_url(address, u_c)) + { + MWARNING("Failed to determine whether address '" << address << "' is local, assuming not"); + return false; + } + if (u_c.host.empty()) + { + MWARNING("Failed to determine whether address '" << address << "' is local, assuming not"); + return false; + } + + // resolve to IP + boost::asio::io_service io_service; + boost::asio::ip::tcp::resolver resolver(io_service); + boost::asio::ip::tcp::resolver::query query(u_c.host, ""); + boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query); + while (i != boost::asio::ip::tcp::resolver::iterator()) + { + const boost::asio::ip::tcp::endpoint &ep = *i; + if (ep.address().is_loopback()) + { + MDEBUG("Address '" << address << "' is local"); + return true; + } + ++i; + } + + MDEBUG("Address '" << address << "' is not local"); + return false; + } } diff --git a/src/common/util.h b/src/common/util.h index 4437d821..c2ffc44c 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -181,4 +181,6 @@ namespace tools void set_max_concurrency(unsigned n); unsigned get_max_concurrency(); + + bool is_local_address(const std::string &address); } diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 8fc2559b..f5e20be4 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -892,33 +892,6 @@ void simple_wallet::print_seed(std::string seed) //---------------------------------------------------------------------------------------------------- static bool is_local_daemon(const std::string &address) { - // extract host - epee::net_utils::http::url_content u_c; - if (!epee::net_utils::parse_url(address, u_c)) - { - LOG_PRINT_L1("Failed to determine whether daemon is local, assuming not"); - return false; - } - if (u_c.host.empty()) - { - LOG_PRINT_L1("Failed to determine whether daemon is local, assuming not"); - return false; - } - - // resolve to IP - boost::asio::io_service io_service; - boost::asio::ip::tcp::resolver resolver(io_service); - boost::asio::ip::tcp::resolver::query query(u_c.host, ""); - boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query); - while (i != boost::asio::ip::tcp::resolver::iterator()) - { - const boost::asio::ip::tcp::endpoint &ep = *i; - if (ep.address().is_loopback()) - return true; - ++i; - } - - return false; } //---------------------------------------------------------------------------------------------------- bool simple_wallet::init(const boost::program_options::variables_map& vm) @@ -1182,9 +1155,9 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) // set --trusted-daemon if local try { - if (is_local_daemon(m_wallet->get_daemon_address())) + if (tools::is_local_address(m_wallet->get_daemon_address())) { - LOG_PRINT_L1(tr("Daemon is local, assuming trusted")); + MINFO(tr("Daemon is local, assuming trusted")); m_trusted_daemon = true; } } diff --git a/src/wallet/api/utils.cpp b/src/wallet/api/utils.cpp index 0d30b61c..788d98f2 100644 --- a/src/wallet/api/utils.cpp +++ b/src/wallet/api/utils.cpp @@ -31,50 +31,16 @@ #include "include_base_utils.h" // LOG_PRINT_x -#include "net/http_client.h" // epee::net_utils::... -#include +#include "common/util.h" using namespace std; namespace Monero { namespace Utils { - -// copy-pasted from simplewallet. - bool isAddressLocal(const std::string &address) { - // extract host - epee::net_utils::http::url_content u_c; - if (!epee::net_utils::parse_url(address, u_c)) - { - LOG_PRINT_L1("Failed to determine whether daemon is local, assuming not"); - return false; - } - if (u_c.host.empty()) - { - LOG_PRINT_L1("Failed to determine whether daemon is local, assuming not"); - return false; - } - // resolver::resolve can throw an exception - try { - // resolve to IP - boost::asio::io_service io_service; - boost::asio::ip::tcp::resolver resolver(io_service); - boost::asio::ip::tcp::resolver::query query(u_c.host, "", boost::asio::ip::tcp::resolver::query::canonical_name); - boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query); - while (i != boost::asio::ip::tcp::resolver::iterator()) - { - const boost::asio::ip::tcp::endpoint &ep = *i; - if (ep.address().is_loopback()) - return true; - ++i; - } - } catch (const boost::system::system_error &e) { - LOG_ERROR("Failed to resolve " << address << ", :" << e.what()); - } - - return false; + return tools::is_local_address(address); } }