From 76c16822d09fbebbc792dc999ea2d3432a63253a Mon Sep 17 00:00:00 2001 From: xiphon Date: Mon, 20 Jul 2020 04:31:58 +0000 Subject: wallet2_api: implement runtime proxy configuration --- src/net/CMakeLists.txt | 4 +-- src/net/http.cpp | 70 +++++++++++++++++++++++++++++++++++++++ src/net/http.h | 51 ++++++++++++++++++++++++++++ src/net/parse.cpp | 35 ++++++++++++++++++++ src/net/parse.h | 3 ++ src/wallet/api/wallet.cpp | 13 +++++--- src/wallet/api/wallet.h | 5 +-- src/wallet/api/wallet2_api.h | 7 +++- src/wallet/api/wallet_manager.cpp | 4 +++ src/wallet/api/wallet_manager.h | 5 +-- src/wallet/wallet2.cpp | 40 +++++++--------------- src/wallet/wallet2.h | 7 ++-- 12 files changed, 202 insertions(+), 42 deletions(-) create mode 100644 src/net/http.cpp create mode 100644 src/net/http.h (limited to 'src') diff --git a/src/net/CMakeLists.txt b/src/net/CMakeLists.txt index 1ce7118ad..afcd42ef7 100644 --- a/src/net/CMakeLists.txt +++ b/src/net/CMakeLists.txt @@ -26,9 +26,9 @@ # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -set(net_sources dandelionpp.cpp error.cpp i2p_address.cpp parse.cpp socks.cpp +set(net_sources dandelionpp.cpp error.cpp http.cpp i2p_address.cpp parse.cpp socks.cpp socks_connect.cpp tor_address.cpp zmq.cpp) -set(net_headers dandelionpp.h error.h i2p_address.h parse.h socks.h socks_connect.h +set(net_headers dandelionpp.h error.h http.cpp i2p_address.h parse.h socks.h socks_connect.h tor_address.h zmq.h) monero_add_library(net ${net_sources} ${net_headers}) diff --git a/src/net/http.cpp b/src/net/http.cpp new file mode 100644 index 000000000..f54b65c29 --- /dev/null +++ b/src/net/http.cpp @@ -0,0 +1,70 @@ +// Copyright (c) 2020, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "http.h" + +#include "parse.h" +#include "socks_connect.h" + +namespace net +{ +namespace http +{ + +bool client::set_proxy(const std::string &address) +{ + if (address.empty()) + { + set_connector(epee::net_utils::direct_connect{}); + } + else + { + const auto endpoint = get_tcp_endpoint(address); + if (!endpoint) + { + auto always_fail = net::socks::connector{boost::asio::ip::tcp::endpoint()}; + set_connector(always_fail); + } + else + { + set_connector(net::socks::connector{*endpoint}); + } + } + + disconnect(); + + return true; +} + +std::unique_ptr client_factory::create() +{ + return std::unique_ptr(new client()); +} + +} // namespace http +} // namespace net diff --git a/src/net/http.h b/src/net/http.h new file mode 100644 index 000000000..91a345851 --- /dev/null +++ b/src/net/http.h @@ -0,0 +1,51 @@ +// Copyright (c) 2020, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#pragma once + +#include "net/http_client.h" + +namespace net +{ +namespace http +{ + +class client : public epee::net_utils::http::http_simple_client +{ +public: + bool set_proxy(const std::string &address) override; +}; + +class client_factory : public epee::net_utils::http::http_client_factory +{ +public: + std::unique_ptr create() override; +}; + +} // namespace http +} // namespace net diff --git a/src/net/parse.cpp b/src/net/parse.cpp index ce580afe7..95b22cd44 100644 --- a/src/net/parse.cpp +++ b/src/net/parse.cpp @@ -122,4 +122,39 @@ namespace net return {epee::net_utils::ipv4_network_subnet{ip, (uint8_t)mask}}; } + + expect get_tcp_endpoint(const boost::string_ref address) + { + uint16_t port = 0; + expect parsed = get_network_address(address, port); + if (!parsed) + { + return parsed.error(); + } + + boost::asio::ip::tcp::endpoint result; + switch (parsed->get_type_id()) + { + case epee::net_utils::ipv4_network_address::get_type_id(): + { + const auto &ipv4 = parsed->as(); + result = boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(ipv4.ip()), ipv4.port()); + break; + } + case epee::net_utils::ipv6_network_address::get_type_id(): + { + const auto &ipv6 = parsed->as(); + result = boost::asio::ip::tcp::endpoint(ipv6.ip(), ipv6.port()); + break; + } + default: + return make_error_code(net::error::unsupported_address); + } + if (result.port() == 0) + { + return make_error_code(net::error::invalid_port); + } + + return result; + } } diff --git a/src/net/parse.h b/src/net/parse.h index 0d8fda711..4d5efe161 100644 --- a/src/net/parse.h +++ b/src/net/parse.h @@ -28,6 +28,7 @@ #pragma once +#include #include #include @@ -65,5 +66,7 @@ namespace net */ expect get_ipv4_subnet_address(boost::string_ref address, bool allow_implicit_32 = false); + + expect get_tcp_endpoint(const boost::string_ref address); } diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 66e248427..73ab88a9f 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -938,13 +938,13 @@ string WalletImpl::keysFilename() const return m_wallet->get_keys_file(); } -bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit, const std::string &daemon_username, const std::string &daemon_password, bool use_ssl, bool lightWallet) +bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit, const std::string &daemon_username, const std::string &daemon_password, bool use_ssl, bool lightWallet, const std::string &proxy_address) { clearStatus(); m_wallet->set_light_wallet(lightWallet); if(daemon_username != "") m_daemon_login.emplace(daemon_username, daemon_password); - return doInit(daemon_address, upper_transaction_size_limit, use_ssl); + return doInit(daemon_address, proxy_address, upper_transaction_size_limit, use_ssl); } bool WalletImpl::lightWalletLogin(bool &isNewWallet) const @@ -2088,6 +2088,11 @@ bool WalletImpl::trustedDaemon() const return m_wallet->is_trusted_daemon(); } +bool WalletImpl::setProxy(const std::string &address) +{ + return m_wallet->set_proxy(address); +} + bool WalletImpl::watchOnly() const { return m_wallet->watch_only(); @@ -2241,9 +2246,9 @@ void WalletImpl::pendingTxPostProcess(PendingTransactionImpl * pending) pending->m_pending_tx = exported_txs.ptx; } -bool WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction_size_limit, bool ssl) +bool WalletImpl::doInit(const string &daemon_address, const std::string &proxy_address, uint64_t upper_transaction_size_limit, bool ssl) { - if (!m_wallet->init(daemon_address, m_daemon_login, boost::asio::ip::tcp::endpoint{}, upper_transaction_size_limit)) + if (!m_wallet->init(daemon_address, m_daemon_login, proxy_address, upper_transaction_size_limit)) return false; // in case new wallet, this will force fast-refresh (pulling hashes instead of blocks) diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 43d85b704..caf1e9ed4 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -102,11 +102,12 @@ public: bool store(const std::string &path) override; std::string filename() const override; std::string keysFilename() const override; - bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit = 0, const std::string &daemon_username = "", const std::string &daemon_password = "", bool use_ssl = false, bool lightWallet = false) override; + bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit = 0, const std::string &daemon_username = "", const std::string &daemon_password = "", bool use_ssl = false, bool lightWallet = false, const std::string &proxy_address = "") override; bool connectToDaemon() override; ConnectionStatus connected() const override; void setTrustedDaemon(bool arg) override; bool trustedDaemon() const override; + bool setProxy(const std::string &address) override; uint64_t balance(uint32_t accountIndex = 0) const override; uint64_t unlockedBalance(uint32_t accountIndex = 0) const override; uint64_t blockChainHeight() const override; @@ -225,7 +226,7 @@ private: void stopRefresh(); bool isNewWallet() const; void pendingTxPostProcess(PendingTransactionImpl * pending); - bool doInit(const std::string &daemon_address, uint64_t upper_transaction_size_limit = 0, bool ssl = false); + bool doInit(const std::string &daemon_address, const std::string &proxy_address, uint64_t upper_transaction_size_limit = 0, bool ssl = false); private: friend class PendingTransactionImpl; diff --git a/src/wallet/api/wallet2_api.h b/src/wallet/api/wallet2_api.h index e2f96a069..50df7e5dd 100644 --- a/src/wallet/api/wallet2_api.h +++ b/src/wallet/api/wallet2_api.h @@ -533,9 +533,10 @@ struct Wallet * \param daemon_username * \param daemon_password * \param lightWallet - start wallet in light mode, connect to a openmonero compatible server. + * \param proxy_address - set proxy address, empty string to disable * \return - true on success */ - virtual bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit = 0, const std::string &daemon_username = "", const std::string &daemon_password = "", bool use_ssl = false, bool lightWallet = false) = 0; + virtual bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit = 0, const std::string &daemon_username = "", const std::string &daemon_password = "", bool use_ssl = false, bool lightWallet = false, const std::string &proxy_address = "") = 0; /*! * \brief createWatchOnly - Creates a watch only wallet @@ -594,6 +595,7 @@ struct Wallet virtual ConnectionStatus connected() const = 0; virtual void setTrustedDaemon(bool arg) = 0; virtual bool trustedDaemon() const = 0; + virtual bool setProxy(const std::string &address) = 0; virtual uint64_t balance(uint32_t accountIndex = 0) const = 0; uint64_t balanceAll() const { uint64_t result = 0; @@ -1298,6 +1300,9 @@ struct WalletManager std::string subdir, const char *buildtag = nullptr, const char *current_version = nullptr); + + //! sets proxy address, empty string to disable + virtual bool setProxy(const std::string &address) = 0; }; diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp index 69d17eb02..900fe91e5 100644 --- a/src/wallet/api/wallet_manager.cpp +++ b/src/wallet/api/wallet_manager.cpp @@ -375,6 +375,10 @@ std::tuple WalletManag return std::make_tuple(false, "", "", "", ""); } +bool WalletManagerImpl::setProxy(const std::string &address) +{ + return m_http_client.set_proxy(address); +} ///////////////////// WalletManagerFactory implementation ////////////////////// WalletManager *WalletManagerFactory::getWalletManager() diff --git a/src/wallet/api/wallet_manager.h b/src/wallet/api/wallet_manager.h index ca8e9ada2..2f603b0a9 100644 --- a/src/wallet/api/wallet_manager.h +++ b/src/wallet/api/wallet_manager.h @@ -30,7 +30,7 @@ #include "wallet/api/wallet2_api.h" -#include "net/http_client.h" +#include "net/http.h" #include namespace Monero { @@ -92,11 +92,12 @@ public: bool startMining(const std::string &address, uint32_t threads = 1, bool background_mining = false, bool ignore_battery = true) override; bool stopMining() override; std::string resolveOpenAlias(const std::string &address, bool &dnssec_valid) const override; + bool setProxy(const std::string &address) override; private: WalletManagerImpl() {} friend struct WalletManagerFactory; - epee::net_utils::http::http_simple_client m_http_client; + net::http::client m_http_client; std::string m_errorString; }; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index d7ed3e999..2ffb90dbf 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -48,6 +48,7 @@ using namespace epee; #include "wallet_rpc_helpers.h" #include "wallet2.h" #include "cryptonote_basic/cryptonote_format_utils.h" +#include "net/parse.h" #include "rpc/core_rpc_server_commands_defs.h" #include "rpc/core_rpc_server_error_codes.h" #include "rpc/rpc_payment_signature.h" @@ -440,30 +441,14 @@ std::unique_ptr make_basic(const boost::program_options::variabl ); } - boost::asio::ip::tcp::endpoint proxy{}; + std::string proxy; if (use_proxy) { - namespace ip = boost::asio::ip; - - const auto proxy_address = command_line::get_arg(vm, opts.proxy); - - boost::string_ref proxy_port{proxy_address}; - boost::string_ref proxy_host = proxy_port.substr(0, proxy_port.rfind(":")); - if (proxy_port.size() == proxy_host.size()) - proxy_host = "127.0.0.1"; - else - proxy_port = proxy_port.substr(proxy_host.size() + 1); - - uint16_t port_value = 0; + proxy = command_line::get_arg(vm, opts.proxy); THROW_WALLET_EXCEPTION_IF( - !epee::string_tools::get_xtype_from_string(port_value, std::string{proxy_port}), + !net::get_tcp_endpoint(proxy), tools::error::wallet_internal_error, - std::string{"Invalid port specified for --"} + opts.proxy.name - ); - - boost::system::error_code error{}; - proxy = ip::tcp::endpoint{ip::address::from_string(std::string{proxy_host}, error), port_value}; - THROW_WALLET_EXCEPTION_IF(bool(error), tools::error::wallet_internal_error, std::string{"Invalid IP address specified for --"} + opts.proxy.name); + std::string{"Invalid address specified for --"} + opts.proxy.name); } boost::optional trusted_daemon; @@ -1325,18 +1310,17 @@ bool wallet2::set_daemon(std::string daemon_address, boost::optional daemon_login, boost::asio::ip::tcp::endpoint proxy, uint64_t upper_transaction_weight_limit, bool trusted_daemon, epee::net_utils::ssl_options_t ssl_options) +bool wallet2::set_proxy(const std::string &address) { + return m_http_client->set_proxy(address); +} +//---------------------------------------------------------------------------------------------------- +bool wallet2::init(std::string daemon_address, boost::optional daemon_login, const std::string &proxy_address, uint64_t upper_transaction_weight_limit, bool trusted_daemon, epee::net_utils::ssl_options_t ssl_options) +{ + CHECK_AND_ASSERT_MES(set_proxy(proxy_address), false, "failed to set proxy address"); m_checkpoints.init_default_checkpoints(m_nettype); m_is_initialized = true; m_upper_transaction_weight_limit = upper_transaction_weight_limit; - if (proxy != boost::asio::ip::tcp::endpoint{}) - { - epee::net_utils::http::abstract_http_client* abstract_http_client = m_http_client.get(); - epee::net_utils::http::http_simple_client* http_simple_client = dynamic_cast(abstract_http_client); - CHECK_AND_ASSERT_MES(http_simple_client != nullptr, false, "http_simple_client must be used to set proxy"); - http_simple_client->set_connector(net::socks::connector{std::move(proxy)}); - } return set_daemon(daemon_address, daemon_login, trusted_daemon, std::move(ssl_options)); } //---------------------------------------------------------------------------------------------------- diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 712f91613..4a10e3d23 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -45,7 +45,7 @@ #include "cryptonote_basic/account.h" #include "cryptonote_basic/account_boost_serialization.h" #include "cryptonote_basic/cryptonote_basic_impl.h" -#include "net/http_client.h" +#include "net/http.h" #include "storages/http_abstract_invoke.h" #include "rpc/core_rpc_server_commands_defs.h" #include "cryptonote_basic/cryptonote_format_utils.h" @@ -269,7 +269,7 @@ private: static bool verify_password(const std::string& keys_file_name, const epee::wipeable_string& password, bool no_spend_key, hw::device &hwdev, uint64_t kdf_rounds); static bool query_device(hw::device::device_type& device_type, const std::string& keys_file_name, const epee::wipeable_string& password, uint64_t kdf_rounds = 1); - wallet2(cryptonote::network_type nettype = cryptonote::MAINNET, uint64_t kdf_rounds = 1, bool unattended = false, std::unique_ptr http_client_factory = std::unique_ptr(new epee::net_utils::http::http_simple_client_factory())); + wallet2(cryptonote::network_type nettype = cryptonote::MAINNET, uint64_t kdf_rounds = 1, bool unattended = false, std::unique_ptr http_client_factory = std::unique_ptr(new net::http::client_factory())); ~wallet2(); struct multisig_info @@ -753,13 +753,14 @@ private: bool deinit(); bool init(std::string daemon_address = "http://localhost:8080", boost::optional daemon_login = boost::none, - boost::asio::ip::tcp::endpoint proxy = {}, + const std::string &proxy = "", uint64_t upper_transaction_weight_limit = 0, bool trusted_daemon = true, epee::net_utils::ssl_options_t ssl_options = epee::net_utils::ssl_support_t::e_ssl_support_autodetect); bool set_daemon(std::string daemon_address = "http://localhost:8080", boost::optional daemon_login = boost::none, bool trusted_daemon = true, epee::net_utils::ssl_options_t ssl_options = epee::net_utils::ssl_support_t::e_ssl_support_autodetect); + bool set_proxy(const std::string &address); void stop() { m_run.store(false, std::memory_order_relaxed); m_message_store.stop(); } -- cgit v1.2.3