| 1 | // Copyright (c) 2019-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 "socks_connect.h" |
| 30 | |
| 31 | #include <boost/asio/ip/address_v6.hpp> |
| 32 | #include <boost/system/error_code.hpp> |
| 33 | #include <boost/system/system_error.hpp> |
| 34 | #include <cstdint> |
| 35 | #include <memory> |
| 36 | #include <system_error> |
| 37 | |
| 38 | #include "net/error.h" |
| 39 | #include "net/net_utils_base.h" |
| 40 | #include "net/parse.h" |
| 41 | #include "net/socks.h" |
| 42 | #include "string_tools.h" |
| 43 | #include "string_tools_lexical.h" |
| 44 | |
| 45 | namespace net |
| 46 | { |
| 47 | namespace socks |
| 48 | { |
| 49 | namespace |
| 50 | { |
| 51 | bool get_v6_address(boost::asio::ip::address_v6& out, const std::string& source) |
| 52 | { |
| 53 | boost::system::error_code error{}; |
| 54 | out = boost::asio::ip::make_address_v6(source, error); |
| 55 | return !error; |
| 56 | } |
| 57 | } // anonymous |
| 58 | |
| 59 | boost::unique_future<boost::asio::ip::tcp::socket> |
| 60 | connector::operator()(const std::string& remote_host, const std::string& remote_port, boost::asio::steady_timer& timeout) const |
| 61 | { |
| 62 | if (!proxy_address) |
| 63 | throw std::runtime_error{"Unexpected nullptr of net::socks::endpoint"}; |
| 64 | |
| 65 | struct future_socket |
| 66 | { |
| 67 | boost::promise<boost::asio::ip::tcp::socket> result_; |
| 68 | |
| 69 | void operator()(boost::system::error_code error, boost::asio::ip::tcp::socket&& socket) |
| 70 | { |
| 71 | if (error) |
| 72 | result_.set_exception(boost::system::system_error{error}); |
| 73 | else |
| 74 | result_.set_value(std::move(socket)); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | boost::unique_future<boost::asio::ip::tcp::socket> out{}; |
| 79 | { |
| 80 | std::uint16_t port = 0; |
| 81 | if (!epee::string_tools::get_xtype_from_string(port, remote_port)) |
| 82 | throw std::system_error{net::error::invalid_port, "Remote port for socks proxy"}; |
| 83 | |
| 84 | bool is_set = false; |
| 85 | std::uint32_t ip_address = 0; |
| 86 | boost::asio::ip::address_v6 v6_address{}; |
| 87 | boost::promise<boost::asio::ip::tcp::socket> result{}; |
| 88 | out = result.get_future(); |
| 89 | const auto proxy = net::socks::make_connect_client( |
| 90 | boost::asio::ip::tcp::socket{MONERO_GET_EXECUTOR(timeout)}, proxy_address->ver, future_socket{std::move(result)} |
| 91 | ); |
| 92 | |
| 93 | if (epee::string_tools::get_ip_int32_from_string(ip_address, remote_host)) |
| 94 | is_set = proxy->set_connect_command(epee::net_utils::ipv4_network_address{ip_address, port}, std::addressof(proxy_address->userinfo)); |
| 95 | else if (get_v6_address(v6_address, remote_host)) |
| 96 | is_set = proxy->set_connect_command(epee::net_utils::ipv6_network_address{v6_address, port}, std::addressof(proxy_address->userinfo)); |
| 97 | else |
| 98 | is_set = proxy->set_connect_command(remote_host, port, std::addressof(proxy_address->userinfo)); |
| 99 | |
| 100 | if (!is_set || !net::socks::client::connect_and_send(proxy, proxy_address->address)) |
| 101 | throw std::system_error{net::error::invalid_host, "Address for socks proxy"}; |
| 102 | |
| 103 | timeout.async_wait(net::socks::client::async_close{std::move(proxy)}); |
| 104 | } |
| 105 | |
| 106 | return out; |
| 107 | } |
| 108 | } // socks |
| 109 | } // net |