diff options
author | Lee Clagett <code@leeclagett.com> | 2019-01-23 21:37:43 +0000 |
---|---|---|
committer | Lee Clagett <code@leeclagett.com> | 2019-03-25 01:35:13 +0000 |
commit | 7acfa9f3cc7b52c0f4776dde3c3f80674cc3306f (patch) | |
tree | 5d1903ab4b132c137b1bb838037f024d6fb8b0d3 /contrib/epee/src/net_helper.cpp | |
parent | Merge pull request #5286 (diff) | |
download | monero-7acfa9f3cc7b52c0f4776dde3c3f80674cc3306f.tar.xz |
Added socks proxy (tor/i2pd/kovri) support to wallet
Diffstat (limited to '')
-rw-r--r-- | contrib/epee/src/net_helper.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/contrib/epee/src/net_helper.cpp b/contrib/epee/src/net_helper.cpp new file mode 100644 index 000000000..3543f5716 --- /dev/null +++ b/contrib/epee/src/net_helper.cpp @@ -0,0 +1,54 @@ +#include "net/net_helper.h" + +namespace epee +{ +namespace net_utils +{ + boost::unique_future<boost::asio::ip::tcp::socket> + direct_connect::operator()(const std::string& addr, const std::string& port, boost::asio::steady_timer& timeout) const + { + // Get a list of endpoints corresponding to the server name. + ////////////////////////////////////////////////////////////////////////// + boost::asio::ip::tcp::resolver resolver(GET_IO_SERVICE(timeout)); + boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), addr, port, boost::asio::ip::tcp::resolver::query::canonical_name); + boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query); + boost::asio::ip::tcp::resolver::iterator end; + if(iterator == end) // Documentation states that successful call is guaranteed to be non-empty + throw boost::system::system_error{boost::asio::error::fault, "Failed to resolve " + addr}; + + ////////////////////////////////////////////////////////////////////////// + + struct new_connection + { + boost::promise<boost::asio::ip::tcp::socket> result_; + boost::asio::ip::tcp::socket socket_; + + explicit new_connection(boost::asio::io_service& io_service) + : result_(), socket_(io_service) + {} + }; + + const auto shared = std::make_shared<new_connection>(GET_IO_SERVICE(timeout)); + timeout.async_wait([shared] (boost::system::error_code error) + { + if (error != boost::system::errc::operation_canceled && shared && shared->socket_.is_open()) + { + shared->socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both); + shared->socket_.close(); + } + }); + shared->socket_.async_connect(*iterator, [shared] (boost::system::error_code error) + { + if (shared) + { + if (error) + shared->result_.set_exception(boost::system::system_error{error}); + else + shared->result_.set_value(std::move(shared->socket_)); + } + }); + return shared->result_.get_future(); + } +} +} + |