| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 45 |
namespace epee |
| 46 |
{ |
| 47 |
namespace net_utils |
| 48 |
{ |
| 49 |
class ipv4_network_address; |
| 50 |
class ipv6_network_address; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
namespace net |
| 55 |
{ |
| 56 |
namespace socks |
| 57 |
{ |
| 58 |
|
| 59 |
enum class version : std::uint8_t |
| 60 |
{ |
| 61 |
v4 = 0, |
| 62 |
v4a, |
| 63 |
v4a_tor, |
| 64 |
v5 |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
enum class error : int |
| 69 |
{ |
| 70 |
|
| 71 |
|
| 72 |
general_failure = 1, |
| 73 |
not_allowed, |
| 74 |
network_unreachable, |
| 75 |
host_unreachable, |
| 76 |
connection_refused, |
| 77 |
ttl_expired, |
| 78 |
command_not_supported, |
| 79 |
address_type_not_supported, |
| 80 |
|
| 81 |
rejected = 92, |
| 82 |
identd_connection, |
| 83 |
identd_user, |
| 84 |
|
| 85 |
auth_failure = 257, |
| 86 |
bad_read, |
| 87 |
bad_write, |
| 88 |
unexpected_version |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
const boost::system::error_category& error_category() noexcept; |
| 98 |
|
| 99 |
|
| 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 |
|
| 105 |
|
| 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]; |
| 112 |
socks::version ver_; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 131 |
struct write; |
| 132 |
struct read; |
| 133 |
struct completed; |
| 134 |
struct process_v5; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 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 |
|
| 147 |
|
| 148 |
stream_type::socket take_socket() |
| 149 |
{ |
| 150 |
return stream_type::socket{std::move(proxy_)}; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
socks::version socks_version() const noexcept { return ver_; } |
| 155 |
|
| 156 |
|
| 157 |
epee::span<const std::uint8_t> buffer() const noexcept |
| 158 |
{ |
| 159 |
return {buffer_, std::get<0>(buffer_size_)}; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
void clear_command() noexcept { buffer_size_ = {}; } |
| 164 |
|
| 165 |
|
| 166 |
bool set_connect_command( |
| 167 |
const epee::net_utils::ipv4_network_address& address, |
| 168 |
const user_and_pass* userinfo = nullptr); |
| 169 |
|
| 170 |
|
| 171 |
bool set_connect_command( |
| 172 |
const epee::net_utils::ipv6_network_address& address, |
| 173 |
const user_and_pass* userinfo = nullptr); |
| 174 |
|
| 175 |
|
| 176 |
bool set_connect_command( |
| 177 |
boost::string_ref domain, |
| 178 |
std::uint16_t port, |
| 179 |
const user_and_pass* userinfo = nullptr); |
| 180 |
|
| 181 |
|
| 182 |
bool set_connect_command( |
| 183 |
const net::tor_address& address, |
| 184 |
const user_and_pass* userinfo = nullptr); |
| 185 |
|
| 186 |
|
| 187 |
bool set_connect_command( |
| 188 |
const net::i2p_address& address, |
| 189 |
const user_and_pass* userinfo = nullptr); |
| 190 |
|
| 191 |
|
| 192 |
bool set_resolve_command(boost::string_ref domain); |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
static bool connect_and_send(std::shared_ptr<client> self, const stream_type::endpoint& proxy_address); |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static bool send(std::shared_ptr<client> self); |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
struct async_close |
| 228 |
{ |
| 229 |
std::shared_ptr<client> self_; |
| 230 |
void operator()(boost::system::error_code error = boost::system::error_code{}); |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
struct close_on_exit |
| 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 |
{ |
| 244 |
Handler handler_; |
| 245 |
|
| 246 |
virtual void done(boost::system::error_code error, const std::shared_ptr<client>&) override |
| 247 |
{ |
| 248 |
handler_(error, take_socket()); |
| 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 |
} |
| 266 |
} |
| 267 |
|
| 268 |
namespace boost |
| 269 |
{ |
| 270 |
namespace system |
| 271 |
{ |
| 272 |
template<> |
| 273 |
struct is_error_code_enum<net::socks::error> |
| 274 |
: true_type |
| 275 |
{}; |
| 276 |
} |
| 277 |
} |