Monero
Loading...
Searching...
No Matches
socks.h
Go to the documentation of this file.
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 <array>
32#include <cstdint>
33#include <boost/asio/io_context.hpp>
34#include <boost/asio/ip/tcp.hpp>
35#include <boost/asio/strand.hpp>
36#include <boost/system/error_code.hpp>
37#include <boost/type_traits/integral_constant.hpp>
38#include <boost/utility/string_ref.hpp>
39#include <memory>
40#include <utility>
41
42#include "net/fwd.h"
43#include "span.h"
44
45namespace epee
46{
47namespace net_utils
48{
51}
52}
53
54namespace net
55{
56namespace socks
57{
59 enum class version : std::uint8_t
60 {
61 v4 = 0,
65 };
66
90
91 /* boost::system::error_code is extended for easier compatibility with
92 boost::asio errors. If std::error_code is needed (with expect<T> for
93 instance), then upgrade to boost 1.65+ or use conversion code in
94 develop branch at boost/system/detail/std_interoperability.hpp */
95
97 const boost::system::error_category& error_category() noexcept;
98
100 inline boost::system::error_code make_error_code(error value) noexcept
101 {
102 return boost::system::error_code{int(value), socks::error_category()};
103 }
104
106 class client
107 {
108 boost::asio::ip::tcp::socket proxy_;
109 boost::asio::strand<boost::asio::ip::tcp::socket::executor_type> strand_;
110 std::array<std::uint16_t, 3> buffer_size_;
111 std::uint8_t buffer_[1024];
113
125 virtual void done(boost::system::error_code error, const std::shared_ptr<client>& self) = 0;
126
127 public:
128 using stream_type = boost::asio::ip::tcp;
129
130 // defined in cpp
131 struct write;
132 struct read;
133 struct completed;
134 struct process_v5;
135
141 explicit client(stream_type::socket&& proxy, socks::version ver);
142
143 client(const client&) = delete;
144 virtual ~client();
145 client& operator=(const client&) = delete;
146
148 stream_type::socket take_socket()
149 {
150 return stream_type::socket{std::move(proxy_)};
151 }
152
154 socks::version socks_version() const noexcept { return ver_; }
155
158 {
159 return {buffer_, std::get<0>(buffer_size_)};
160 }
161
163 void clear_command() noexcept { buffer_size_ = {}; }
164
168 const user_and_pass* userinfo = nullptr);
169
173 const user_and_pass* userinfo = nullptr);
174
177 boost::string_ref domain,
178 std::uint16_t port,
179 const user_and_pass* userinfo = nullptr);
180
184 const user_and_pass* userinfo = nullptr);
185
189 const user_and_pass* userinfo = nullptr);
190
192 bool set_resolve_command(boost::string_ref domain);
193
208 static bool connect_and_send(std::shared_ptr<client> self, const stream_type::endpoint& proxy_address);
209
223 static bool send(std::shared_ptr<client> self);
224
228 {
229 std::shared_ptr<client> self_;
230 void operator()(boost::system::error_code error = boost::system::error_code{});
231 };
232
235 {
236 std::shared_ptr<client> self;
237 ~close_on_exit() { async_close{std::move(self)}(); }
238 };
239 };
240
241 template<typename Handler>
242 class connect_client : public client
243 {
245
246 virtual void done(boost::system::error_code error, const std::shared_ptr<client>&) override
247 {
249 }
250
251 public:
252 explicit connect_client(stream_type::socket&& proxy, socks::version ver, Handler&& handler)
253 : client(std::move(proxy), ver), handler_(std::move(handler))
254 {}
255
256 virtual ~connect_client() override {}
257 };
258
259 template<typename Handler>
260 inline std::shared_ptr<client>
261 make_connect_client(client::stream_type::socket&& proxy, socks::version ver, Handler handler)
262 {
263 return std::make_shared<connect_client<Handler>>(std::move(proxy), ver, std::move(handler));
264 }
265} // socks
266} // net
267
268namespace boost
269{
270namespace system
271{
272 template<>
273 struct is_error_code_enum<net::socks::error>
274 : true_type
275 {};
276} // system
277} // boost
Definition net_utils_base.h:69
Definition net_utils_base.h:172
Non-owning sequence of data. Does not deep copy.
Definition span.h:55
b32 i2p address; internal format not condensed/decoded.
Definition i2p_address.h:52
boost::asio::ip::tcp::socket proxy_
Definition socks.h:108
bool set_connect_command(const epee::net_utils::ipv4_network_address &address, const user_and_pass *userinfo=nullptr)
Try to set address as remote connection request.
Definition socks.cpp:661
void clear_command() noexcept
Definition socks.h:163
boost::asio::ip::tcp stream_type
Definition socks.h:128
client(const client &)=delete
std::array< std::uint16_t, 3 > buffer_size_
Definition socks.h:110
stream_type::socket take_socket()
Definition socks.h:148
virtual ~client()
Definition socks.cpp:659
bool set_resolve_command(boost::string_ref domain)
Try to set domain as remote DNS A record lookup request.
Definition socks.cpp:744
epee::span< const std::uint8_t > buffer() const noexcept
Definition socks.h:157
static bool connect_and_send(std::shared_ptr< client > self, const stream_type::endpoint &proxy_address)
Definition socks.cpp:755
client(stream_type::socket &&proxy, socks::version ver)
Definition socks.cpp:655
std::uint8_t buffer_[1024]
Definition socks.h:111
virtual void done(boost::system::error_code error, const std::shared_ptr< client > &self)=0
socks::version ver_
Definition socks.h:112
client & operator=(const client &)=delete
static bool send(std::shared_ptr< client > self)
Definition socks.cpp:766
socks::version socks_version() const noexcept
Definition socks.h:154
boost::asio::strand< boost::asio::ip::tcp::socket::executor_type > strand_
Definition socks.h:109
virtual void done(boost::system::error_code error, const std::shared_ptr< client > &) override
Definition socks.h:246
connect_client(stream_type::socket &&proxy, socks::version ver, Handler &&handler)
Definition socks.h:252
Handler handler_
Definition socks.h:244
virtual ~connect_client() override
Definition socks.h:256
Tor onion address; internal format not condensed/decoded.
Definition tor_address.h:53
Concept for receiving events from GenericReader upon parsing. The functions return true if no error o...
static epee::net_utils::http::http_simple_client_template< dummy_client > client
Definition http-client.cpp:62
#define inline
Definition inline_c.h:34
Definition socks.h:271
Definition portable_binary_archive.hpp:29
Definition abstract_http_client.h:36
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
Definition fwd.h:45
std::shared_ptr< client > make_connect_client(client::stream_type::socket &&proxy, socks::version ver, Handler handler)
Definition socks.h:261
const boost::system::error_category & error_category() noexcept
Definition socks.cpp:413
error
Possible errors with socks communication. Defined in https://www.openssh.com/txt/socks4....
Definition socks.h:69
@ ttl_expired
Definition socks.h:77
@ command_not_supported
Definition socks.h:78
@ bad_write
Definition socks.h:87
@ not_allowed
Definition socks.h:73
@ bad_read
Definition socks.h:86
@ address_type_not_supported
Definition socks.h:79
@ connection_refused
Definition socks.h:76
@ auth_failure
Definition socks.h:85
@ network_unreachable
Definition socks.h:74
@ identd_user
Definition socks.h:83
@ rejected
Definition socks.h:81
@ general_failure
Definition socks.h:72
@ unexpected_version
Definition socks.h:88
@ identd_connection
Definition socks.h:82
@ host_unreachable
Definition socks.h:75
version
Supported socks variants.
Definition socks.h:60
@ v4a_tor
Extensions defined in Tor codebase.
Definition socks.h:63
@ v5
Definition socks.h:64
@ v4a
Definition socks.h:62
@ v4
Definition socks.h:61
boost::system::error_code make_error_code(error value) noexcept
Definition socks.h:100
Definition net_utils_base.h:59
Definition enums.h:68
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
static cryptonote::account_public_address address
Definition signature.cpp:38
Definition socks.h:228
void operator()(boost::system::error_code error=boost::system::error_code{})
Definition socks.cpp:780
std::shared_ptr< client > self_
Definition socks.h:229
Calls async_close on self at destruction. NOP if nullptr.
Definition socks.h:235
std::shared_ptr< client > self
Definition socks.h:236
~close_on_exit()
Definition socks.h:237
Definition socks.cpp:420
Definition socks.cpp:473
Definition socks.cpp:447
Definition socks.cpp:630
Separates the user and pass sections from URI userinfo.
Definition parse.h:65