| 1 | // Copyright (c) 2018-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 | #pragma once |
| 30 | |
| 31 | #include <cstdint> |
| 32 | #include <boost/asio/io_context.hpp> |
| 33 | #include <boost/asio/ip/tcp.hpp> |
| 34 | #include <boost/asio/strand.hpp> |
| 35 | #include <boost/system/error_code.hpp> |
| 36 | #include <boost/type_traits/integral_constant.hpp> |
| 37 | #include <boost/utility/string_ref.hpp> |
| 38 | #include <memory> |
| 39 | #include <utility> |
| 40 | |
| 41 | #include "net/fwd.h" |
| 42 | #include "span.h" |
| 43 | |
| 44 | namespace epee |
| 45 | { |
| 46 | namespace net_utils |
| 47 | { |
| 48 | class ipv4_network_address; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | namespace net |
| 53 | { |
| 54 | namespace socks |
| 55 | { |
| 56 | //! Supported socks variants. |
| 57 | enum class version : std::uint8_t |
| 58 | { |
| 59 | v4 = 0, |
| 60 | v4a, |
| 61 | v4a_tor //!< Extensions defined in Tor codebase |
| 62 | }; |
| 63 | |
| 64 | //! Possible errors with socks communication. Defined in https://www.openssh.com/txt/socks4.protocol |
| 65 | enum class error : int |
| 66 | { |
| 67 | // 0 is reserved for success value |
| 68 | // 1-256 -> reserved for error values from socks server (+1 from wire value). |
| 69 | rejected = 92, |
| 70 | identd_connection, |
| 71 | identd_user, |
| 72 | // Specific to application |
| 73 | bad_read = 257, |
| 74 | bad_write, |
| 75 | unexpected_version |
| 76 | }; |
| 77 | |
| 78 | /* boost::system::error_code is extended for easier compatibility with |
| 79 | boost::asio errors. If std::error_code is needed (with expect<T> for |
| 80 | instance), then upgrade to boost 1.65+ or use conversion code in |
| 81 | develop branch at boost/system/detail/std_interoperability.hpp */ |
| 82 | |
| 83 | //! \return boost::system::error_category for net::socks namespace |
| 84 | const boost::system::error_category& error_category() noexcept; |
| 85 | |
| 86 | //! \return net::socks::error as a boost::system::error_code. |
| 87 | inline boost::system::error_code make_error_code(error value) noexcept |
| 88 | { |
| 89 | return boost::system::error_code{int(value), socks::error_category()}; |
| 90 | } |
| 91 | |
| 92 | //! Client support for socks connect and resolve commands. |
| 93 | class client |
| 94 | { |
| 95 | boost::asio::ip::tcp::socket proxy_; |
| 96 | boost::asio::strand<boost::asio::ip::tcp::socket::executor_type> strand_; |
| 97 | std::uint16_t buffer_size_; |
| 98 | std::uint8_t buffer_[1024]; |
| 99 | socks::version ver_; |
| 100 | |
| 101 | /*! |
| 102 | Only invoked after `*send(...)` function completes or fails. |
| 103 | `bool(error) == false` indicates success; `self.get()` is always |
| 104 | `this` and allows implementations to skip |
| 105 | `std::enable_shared_from_this<T>` (ASIO callbacks need shared_ptr). |
| 106 | The design saves space and reduces cycles (everything uses moves, |
| 107 | so no atomic operations are ever necessary). |
| 108 | |
| 109 | \param error when processing last command (if any). |
| 110 | \param self `shared_ptr<client>` handle to `this`. |
| 111 | */ |
| 112 | virtual void done(boost::system::error_code error, std::shared_ptr<client> self) = 0; |
| 113 | |
| 114 | public: |
| 115 | using stream_type = boost::asio::ip::tcp; |
| 116 | |
| 117 | // defined in cpp |
| 118 | struct write; |
| 119 | struct read; |
| 120 | struct completed; |
| 121 | |
| 122 | /*! |
| 123 | \param proxy ownership is passed into `this`. Does not have to be |
| 124 | in connected state. |
| 125 | \param ver socks version for the connection. |
| 126 | */ |
| 127 | explicit client(stream_type::socket&& proxy, socks::version ver); |
| 128 | |
| 129 | client(const client&) = delete; |
| 130 | virtual ~client(); |
| 131 | client& operator=(const client&) = delete; |
| 132 | |
| 133 | //! \return Ownership of socks client socket object. |
| 134 | stream_type::socket take_socket() |
| 135 | { |
| 136 | return stream_type::socket{std::move(proxy_)}; |
| 137 | } |
| 138 | |
| 139 | //! \return Socks version. |
| 140 | socks::version socks_version() const noexcept { return ver_; } |
| 141 | |
| 142 | //! \return Contents of internal buffer. |
| 143 | epee::span<const std::uint8_t> buffer() const noexcept |
| 144 | { |
| 145 | return {buffer_, buffer_size_}; |
| 146 | } |
| 147 | |
| 148 | //! \post `buffer.empty()`. |
| 149 | void clear_command() noexcept { buffer_size_ = 0; } |
| 150 | |
| 151 | //! Try to set `address` as remote connection request. |
| 152 | bool set_connect_command(const epee::net_utils::ipv4_network_address& address); |
| 153 | |
| 154 | //! Try to set `domain` + `port` as remote connection request. |
| 155 | bool set_connect_command(boost::string_ref domain, std::uint16_t port); |
| 156 | |
| 157 | //! Try to set `address` as remote Tor hidden service connection request. |
| 158 | bool set_connect_command(const net::tor_address& address); |
| 159 | |
| 160 | //! Try to set `address` as remote i2p hidden service connection request. |
| 161 | bool set_connect_command(const net::i2p_address& address); |
| 162 | |
| 163 | //! Try to set `domain` as remote DNS A record lookup request. |
| 164 | bool set_resolve_command(boost::string_ref domain); |
| 165 | |
| 166 | /*! |
| 167 | Asynchronously connect to `proxy_address` then issue command in |
| 168 | `buffer()`. The `done(...)` method will be invoked upon completion |
| 169 | with `self` and potential `error`s. |
| 170 | |
| 171 | \note Must use one of the `self->set_*_command` calls before using |
| 172 | this function. |
| 173 | \note Only `async_close` can be invoked on `self` until the `done` |
| 174 | callback is invoked. |
| 175 | |
| 176 | \param self ownership of object is given to function. |
| 177 | \param proxy_address of the socks server. |
| 178 | \return False if `self->buffer().empty()` (no command set). |
| 179 | */ |
| 180 | static bool connect_and_send(std::shared_ptr<client> self, const stream_type::endpoint& proxy_address); |
| 181 | |
| 182 | /*! |
| 183 | Assume existing connection to proxy server; asynchronously issue |
| 184 | command in `buffer()`. The `done(...)` method will be invoked |
| 185 | upon completion with `self` and potential `error`s. |
| 186 | |
| 187 | \note Must use one of the `self->set_*_command` calls before using |
| 188 | the function. |
| 189 | \note Only `async_close` can be invoked on `self` until the `done` |
| 190 | callback is invoked. |
| 191 | |
| 192 | \param self ownership of object is given to function. |
| 193 | \return False if `self->buffer().empty()` (no command set). |
| 194 | */ |
| 195 | static bool send(std::shared_ptr<client> self); |
| 196 | |
| 197 | /*! Callback for closing socket. Thread-safe with `*send` functions; |
| 198 | never blocks (uses strands). */ |
| 199 | struct async_close |
| 200 | { |
| 201 | std::shared_ptr<client> self_; |
| 202 | void operator()(boost::system::error_code error = boost::system::error_code{}); |
| 203 | }; |
| 204 | |
| 205 | //! Calls `async_close` on `self` at destruction. NOP if `nullptr`. |
| 206 | struct close_on_exit |
| 207 | { |
| 208 | std::shared_ptr<client> self; |
| 209 | ~close_on_exit() { async_close{std::move(self)}(); } |
| 210 | }; |
| 211 | }; |
| 212 | |
| 213 | template<typename Handler> |
| 214 | class connect_client : public client |
| 215 | { |
| 216 | Handler handler_; |
| 217 | |
| 218 | virtual void done(boost::system::error_code error, std::shared_ptr<client>) override |
| 219 | { |
| 220 | handler_(error, take_socket()); |
| 221 | } |
| 222 | |
| 223 | public: |
| 224 | explicit connect_client(stream_type::socket&& proxy, socks::version ver, Handler&& handler) |
| 225 | : client(std::move(proxy), ver), handler_(std::move(handler)) |
| 226 | {} |
| 227 | |
| 228 | virtual ~connect_client() override {} |
| 229 | }; |
| 230 | |
| 231 | template<typename Handler> |
| 232 | inline std::shared_ptr<client> |
| 233 | make_connect_client(client::stream_type::socket&& proxy, socks::version ver, Handler handler) |
| 234 | { |
| 235 | return std::make_shared<connect_client<Handler>>(std::move(proxy), ver, std::move(handler)); |
| 236 | } |
| 237 | } // socks |
| 238 | } // net |
| 239 | |
| 240 | namespace boost |
| 241 | { |
| 242 | namespace system |
| 243 | { |
| 244 | template<> |
| 245 | struct is_error_code_enum<net::socks::error> |
| 246 | : true_type |
| 247 | {}; |
| 248 | } // system |
| 249 | } // boost |