Debian Codegraph @

src/net/socks.cpp

raw symbols SCIP
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 #include "socks.h"
30
31 #include <algorithm>
32 #include <boost/asio/bind_executor.hpp>
33 #include <boost/asio/buffer.hpp>
34 #include <boost/asio/coroutine.hpp>
35 #include <boost/asio/dispatch.hpp>
36 #include <boost/asio/read.hpp>
37 #include <boost/asio/write.hpp>
38 #include <boost/endian/arithmetic.hpp>
39 #include <boost/endian/conversion.hpp>
40 #include <cstring>
41 #include <limits>
42 #include <numeric>
43 #include <string>
44
45 #include "net/parse.h"
46 #include "net/net_utils_base.h"
47 #include "net/tor_address.h"
48 #include "net/i2p_address.h"
49
50 namespace net
51 {
52 namespace socks
53 {
54 namespace
55 {
56 constexpr const std::uint8_t v4_connect_command = 1;
57 constexpr const std::uint8_t v4tor_resolve_command = 0xf0;
58 constexpr const std::uint8_t v4_request_granted = 90;
59
60 constexpr const std::uint8_t v5_noauth_method = 0;
61 constexpr const std::uint8_t v5_userpass_method = 2;
62 constexpr const std::uint8_t v5_connect_command = 1;
63 constexpr const std::uint8_t v5_reserved = 0;
64 constexpr const std::uint8_t v5_ipv4_type = 1;
65 constexpr const std::uint8_t v5_domain_type = 3;
66 constexpr const std::uint8_t v5_ipv6_type = 4;
67 constexpr const std::uint8_t v5_reply_success = 0;
68 constexpr const std::uint8_t v5_userpass_version = 1;
69
70 struct v4_header
71 {
72 std::uint8_t version;
73 std::uint8_t command_code;
74 boost::endian::big_uint16_t port;
75 boost::endian::big_uint32_t ip;
76 };
77
78 struct v5_noauth_initial
79 {
80 std::uint8_t version;
81 std::uint8_t n_methods;
82 std::uint8_t method;
83
84 static constexpr v5_noauth_initial make() noexcept
85 {
86 return {5, 1, v5_noauth_method};
87 }
88 };
89
90 struct v5_auth_initial
91 {
92 std::uint8_t version;
93 std::uint8_t n_methods;
94 std::uint8_t method1;
95 std::uint8_t method2;
96
97 static constexpr v5_auth_initial make() noexcept
98 {
99 return {5, 2, v5_noauth_method, v5_userpass_method};
100 }
101 };
102
103 struct v5_response_initial
104 {
105 std::uint8_t version;
106 std::uint8_t method;
107 };
108
109 struct v5_ipv4_connect
110 {
111 std::uint8_t version;
112 std::uint8_t command;
113 std::uint8_t reserved;
114 std::uint8_t type;
115 boost::endian::big_uint32_t ip;
116 boost::endian::big_uint16_t port;
117
118 static v5_ipv4_connect make(const std::uint32_t ip, const std::uint16_t port) noexcept
119 {
120 return {5, v5_connect_command, v5_reserved, v5_ipv4_type, ip, port};
121 }
122 };
123
124 struct v5_domain_connect
125 {
126 std::uint8_t version;
127 std::uint8_t command;
128 std::uint8_t reserved;
129 std::uint8_t type;
130 std::uint8_t length;
131
132 static constexpr v5_domain_connect make(const std::uint8_t length) noexcept
133 {
134 return {5, v5_connect_command, v5_reserved, v5_domain_type, length};
135 }
136 };
137
138 struct v5_ipv6_connect
139 {
140 std::uint8_t version;
141 std::uint8_t command;
142 std::uint8_t reserved;
143 std::uint8_t type;
144 char ip[16];
145 boost::endian::big_uint16_t port;
146
147 static v5_ipv6_connect make(const boost::asio::ip::address_v6& ip, const std::uint16_t port)
148 {
149 v5_ipv6_connect out{5, v5_connect_command, v5_reserved, v5_ipv6_type};
150 out.port = port;
151
152 const auto ip_bytes = ip.to_bytes();
153 static_assert(sizeof(out.ip) == sizeof(ip_bytes), "unexpected ipv6 bytes size");
154 std::memcpy(std::addressof(out.ip), std::addressof(ip_bytes), sizeof(out.ip));
155
156 return out;
157 }
158 };
159
160 struct v5_response_auth
161 {
162 std::uint8_t version;
163 std::uint8_t status;
164 };
165
166 struct v5_response_connect
167 {
168 std::uint8_t version;
169 std::uint8_t reply;
170 std::uint8_t reserved;
171 std::uint8_t type;
172 };
173
174 struct v5_response_ipv4
175 {
176 boost::endian::big_uint32_t ip;
177 boost::endian::big_uint16_t port;
178 };
179
180 struct v5_response_ipv6
181 {
182 char ip[16];
183 boost::endian::big_uint16_t port;
184 };
185
186 std::size_t write_domain_header(epee::span<std::uint8_t> out, const std::uint8_t command, const std::uint16_t port, const boost::string_ref domain)
187 {
188 if (std::numeric_limits<std::size_t>::max() - sizeof(v4_header) - 2 < domain.size())
189 return 0;
190
191 const std::size_t buf_size = sizeof(v4_header) + domain.size() + 2;
192 if (out.size() < buf_size)
193 return 0;
194
195 // version 4, 1 indicates invalid ip for domain extension
196 const v4_header temp{4, command, port, std::uint32_t(1)};
197 std::memcpy(out.data(), std::addressof(temp), sizeof(temp));
198 out.remove_prefix(sizeof(temp));
199
200 *(out.data()) = 0;
201 out.remove_prefix(1);
202
203 std::memcpy(out.data(), domain.data(), domain.size());
204 out.remove_prefix(domain.size());
205
206 *(out.data()) = 0;
207 return buf_size;
208 }
209
210 std::size_t write_v5_userpass(epee::span<std::uint8_t> out, const user_and_pass& userinfo)
211 {
212 static constexpr const std::uint8_t max_length = std::numeric_limits<std::uint8_t>::max();
213 if (max_length < userinfo.user.size())
214 return 0;
215 if (max_length < userinfo.pass.size())
216 return 0;
217
218 static_assert(max_length < std::numeric_limits<std::size_t>::max());
219 static_assert(max_length < std::numeric_limits<std::size_t>::max() - max_length);
220 static_assert(3 <= std::numeric_limits<std::size_t>::max() - max_length - max_length);
221
222 if (out.size() < 3 + userinfo.user.size() + userinfo.pass.size())
223 return 0;
224
225 const std::size_t initial = out.size();
226
227 out[0] = v5_userpass_version;
228 out[1] = std::uint8_t(userinfo.user.size());
229 out.remove_prefix(2);
230
231 std::memcpy(out.data(), userinfo.user.data(), userinfo.user.size());
232 out.remove_prefix(userinfo.user.size());
233
234 out[0] = std::uint8_t(userinfo.pass.size());
235 out.remove_prefix(1);
236
237 std::memcpy(out.data(), userinfo.pass.data(), userinfo.pass.size());
238 out.remove_prefix(userinfo.pass.size());
239 return initial - out.size();
240 }
241
242 std::array<std::uint16_t, 2> write_v5_initial(epee::span<std::uint8_t> out, const user_and_pass* userinfo)
243 {
244 std::array<std::uint16_t, 2> sizes{{}};
245
246 if (userinfo && (!userinfo->user.empty() || !userinfo->pass.empty()))
247 {
248 const auto header = v5_auth_initial::make();
249 if (out.size() < sizeof(header))
250 return sizes;
251 std::memcpy(out.data(), std::addressof(header), sizeof(header));
252 out.remove_prefix(sizeof(header));
253
254 const std::size_t auth = write_v5_userpass(out, *userinfo);
255 if (!auth)
256 return sizes;
257 out.remove_prefix(auth);
258
259 std::get<0>(sizes) = sizeof(header);
260 std::get<1>(sizes) = auth;
261 }
262 else
263 {
264 const auto header = v5_noauth_initial::make();
265 if (out.size() < sizeof(header))
266 return sizes;
267 std::memcpy(out.data(), std::addressof(header), sizeof(header));
268 out.remove_prefix(sizeof(header));
269
270 std::get<0>(sizes) = sizeof(header);
271 }
272
273 return sizes;
274 }
275
276 template<typename T>
277 std::array<std::uint16_t, 3> write_v5_address_connect(epee::span<std::uint8_t> out, const T& address, const user_and_pass* userinfo)
278 {
279 std::array<std::uint16_t, 3> sizes{{}};
280
281 const auto result = write_v5_initial(out, userinfo);
282 if (!std::get<0>(result))
283 return sizes;
284
285 for (std::size_t length : result)
286 out.remove_prefix(length);
287
288 if (out.size() < sizeof(address))
289 return sizes;
290 std::memcpy(out.data(), std::addressof(address), sizeof(address));
291
292 std::get<0>(sizes) = std::get<0>(result);
293 std::get<1>(sizes) = std::get<1>(result);
294 std::get<2>(sizes) = sizeof(address);
295 return sizes;
296 }
297
298 std::array<std::uint16_t, 3> write_v5_domain_connect(epee::span<std::uint8_t> out, const std::uint16_t port, const boost::string_ref domain, const user_and_pass* userinfo)
299 {
300 std::array<std::uint16_t, 3> sizes{{}};
301 if (std::numeric_limits<std::uint8_t>::max() < domain.size())
302 return sizes;
303
304 const auto result = write_v5_initial(out, userinfo);
305 if (!std::get<0>(result))
306 return sizes;
307
308 for (std::size_t length : result)
309 out.remove_prefix(length);
310
311 const auto request = v5_domain_connect::make(std::uint8_t(domain.size()));
312 static_assert(sizeof(port) <= std::numeric_limits<std::size_t>::max() - sizeof(request));
313 if (std::numeric_limits<std::size_t>::max() - sizeof(request) - sizeof(port) < domain.size())
314 return sizes;
315
316 const std::size_t last_size = sizeof(request) + sizeof(port) + domain.size();
317 if (out.size() < last_size)
318 return sizes;
319
320 std::memcpy(out.data(), std::addressof(request), sizeof(request));
321 out.remove_prefix(sizeof(request));
322
323 std::memcpy(out.data(), domain.data(), domain.size());
324 out.remove_prefix(domain.size());
325
326 const boost::endian::big_uint16_t big_port{port};
327 std::memcpy(out.data(), std::addressof(big_port), sizeof(big_port));
328
329 std::get<0>(sizes) = std::get<0>(result);
330 std::get<1>(sizes) = std::get<1>(result);
331 std::get<2>(sizes) = last_size;
332 return sizes;
333 }
334
335 struct socks_category : boost::system::error_category
336 {
337 explicit socks_category() noexcept
338 : boost::system::error_category()
339 {}
340
341 const char* name() const noexcept override
342 {
343 return "net::socks::error_category";
344 }
345
346 virtual std::string message(int value) const override
347 {
348 switch (socks::error(value))
349 {
350 case socks::error::general_failure:
351 return "Socks general server failure";
352 case socks::error::not_allowed:
353 return "Socks connection not allowed by ruleset";
354 case socks::error::network_unreachable:
355 return "Socks network unreachable";
356 case socks::error::host_unreachable:
357 return "Socks host unreachable";
358 case socks::error::connection_refused:
359 return "Socks connection refused";
360 case socks::error::ttl_expired:
361 return "Socks TTL expired";
362 case socks::error::command_not_supported:
363 return "Socks command not supported";
364 case socks::error::address_type_not_supported:
365 return "Socks address type not supported";
366
367 case socks::error::rejected:
368 return "Socks request rejected or failed";
369 case socks::error::identd_connection:
370 return "Socks request rejected because server cannot connect to identd on the client";
371 case socks::error::identd_user:
372 return "Socks request rejected because the client program and identd report different user-ids";
373
374 case socks::error::auth_failure:
375 return "Socks authentication failure";
376 case socks::error::bad_read:
377 return "Socks boost::async_read read fewer bytes than expected";
378 case socks::error::bad_write:
379 return "Socks boost::async_write wrote fewer bytes than expected";
380 case socks::error::unexpected_version:
381 return "Socks server returned unexpected version in reply";
382
383 default:
384 break;
385 }
386 return "Unknown net::socks::error";
387 }
388
389 boost::system::error_condition default_error_condition(int value) const noexcept override
390 {
391 switch (socks::error(value))
392 {
393 case socks::error::network_unreachable:
394 return boost::system::errc::host_unreachable;
395 case socks::error::connection_refused:
396 return boost::system::errc::connection_refused;
397 case socks::error::bad_read:
398 case socks::error::bad_write:
399 return boost::system::errc::io_error;
400 case socks::error::unexpected_version:
401 return boost::system::errc::protocol_error;
402 default:
403 break;
404 };
405 if (1 <= value && value <= 256)
406 return boost::system::errc::protocol_error;
407
408 return boost::system::error_condition{value, *this};
409 }
410 };
411 }
412
413 const boost::system::error_category& error_category() noexcept
414 {
415 static const socks_category instance{};
416 return instance;
417 }
418
419 struct client::completed
420 {
421 std::shared_ptr<client> self_;
422
423 void operator()(const boost::system::error_code error, const std::size_t bytes) const
424 {
425 static_assert(1 < sizeof(self_->buffer_), "buffer too small for v4 response");
426
427 if (self_)
428 {
429 client& self = *self_;
430 std::get<0>(self.buffer_size_) = std::min(bytes, sizeof(self.buffer_));
431
432 if (error)
433 self.done(error, self_);
434 else if (std::get<0>(self.buffer_size_) < sizeof(v4_header))
435 self.done(socks::error::bad_read, self_);
436 else if (self.buffer_[0] != 0) // response version
437 self.done(socks::error::unexpected_version, self_);
438 else if (self.buffer_[1] != v4_request_granted)
439 self.done(socks::error(int(self.buffer_[1]) + 1), self_);
440 else
441 self.done(boost::system::error_code{}, self_);
442 }
443 }
444 };
445
446 struct client::read
447 {
448 std::shared_ptr<client> self_;
449
450 static boost::asio::mutable_buffer get_buffer(client& self) noexcept
451 {
452 static_assert(sizeof(v4_header) <= sizeof(self.buffer_), "buffer too small for v4 response");
453 std::get<0>(self.buffer_size_) = sizeof(v4_header);
454 return boost::asio::buffer(self.buffer_, sizeof(v4_header));
455 }
456
457 void operator()(const boost::system::error_code error, const std::size_t bytes)
458 {
459 if (self_)
460 {
461 client& self = *self_;
462 if (error)
463 self.done(error, self_);
464 else if (bytes < std::get<0>(self.buffer_size_))
465 self.done(socks::error::bad_write, self_);
466 else
467 boost::asio::async_read(self.proxy_, get_buffer(self), boost::asio::bind_executor(self.strand_, completed{std::move(self_)}));
468 }
469 }
470 };
471
472 struct client::process_v5 : boost::asio::coroutine
473 {
474 std::shared_ptr<client> self_;
475
476 explicit process_v5(std::shared_ptr<client> self)
477 : boost::asio::coroutine(), self_(std::move(self))
478 {}
479
480 static boost::asio::mutable_buffer get_read_buffer(client& self, const std::size_t size)
481 {
482 const std::size_t offset =
483 std::accumulate(self.buffer_size_.begin(), self.buffer_size_.end(), std::size_t(0));
484 if (sizeof(self.buffer_) < offset || sizeof(self.buffer_) - offset < size)
485 throw std::runtime_error{"Not enough room for reading socks v5 buffer"};
486 return boost::asio::buffer(self.buffer_ + offset, size);
487 }
488
489 template<unsigned I>
490 static boost::asio::const_buffer get_write_buffer(const client& self) noexcept
491 {
492 const std::size_t offset =
493 std::accumulate(self.buffer_size_.begin(), self.buffer_size_.begin() + I, std::size_t(0));
494 return boost::asio::buffer(
495 self.buffer_ + offset, std::get<I>(self.buffer_size_)
496 );
497 }
498
499 void operator()(const boost::system::error_code error, std::size_t bytes)
500 {
501 if (!self_)
502 return;
503
504 client& self = *self_;
505 if (error)
506 {
507 self.done(error, self_);
508 return;
509 }
510
511 bool send_userpass = false;
512 BOOST_ASIO_CORO_REENTER(this)
513 {
514 // initial header already written
515
516 BOOST_ASIO_CORO_YIELD boost::asio::async_read(
517 self.proxy_,
518 get_read_buffer(self, sizeof(v5_response_initial)),
519 boost::asio::bind_executor(self.strand_, std::move(*this))
520 );
521 {
522 v5_response_initial header{};
523
524 assert(bytes == sizeof(header));
525 const auto buf = get_read_buffer(self, sizeof(header));
526 std::memcpy(std::addressof(header), buf.data(), sizeof(header));
527 if (header.version != 5)
528 {
529 self.done(socks::error::unexpected_version, self_);
530 return;
531 }
532 if (header.method != v5_noauth_method && header.method != v5_userpass_method)
533 {
534 self.done(socks::error::auth_failure, self_);
535 return;
536 }
537 send_userpass = (header.method == v5_userpass_method);
538 }
539
540 if (send_userpass)
541 {
542 if (!std::get<1>(self.buffer_size_))
543 {
544 self.done(socks::error::auth_failure, self_);
545 return;
546 }
547
548 BOOST_ASIO_CORO_YIELD boost::asio::async_write(
549 self.proxy_, get_write_buffer<1>(self), boost::asio::bind_executor(self.strand_, std::move(*this))
550 );
551 assert(bytes == std::get<1>(self.buffer_size_));
552
553 BOOST_ASIO_CORO_YIELD boost::asio::async_read(
554 self.proxy_,
555 get_read_buffer(self, sizeof(v5_response_auth)),
556 boost::asio::bind_executor(self.strand_, std::move(*this))
557 );
558 {
559 v5_response_auth header{};
560
561 assert(bytes == sizeof(header));
562 const auto buf = get_read_buffer(self, sizeof(header));
563 std::memcpy(std::addressof(header), buf.data(), sizeof(header));
564 if (header.version != v5_userpass_version)
565 {
566 self.done(socks::error::unexpected_version, self_);
567 return;
568 }
569 if (header.status != v5_reply_success)
570 {
571 self.done(socks::error::auth_failure, self_);
572 return;
573 }
574 }
575 }
576
577 BOOST_ASIO_CORO_YIELD boost::asio::async_write(
578 self.proxy_, get_write_buffer<2>(self), boost::asio::bind_executor(self.strand_, std::move(*this))
579 );
580 assert(bytes == std::get<2>(self.buffer_size_));
581
582 self.buffer_size_ = {};
583 BOOST_ASIO_CORO_YIELD boost::asio::async_read(
584 self.proxy_,
585 get_read_buffer(self, sizeof(v5_response_connect)),
586 boost::asio::bind_executor(self.strand_, std::move(*this))
587 );
588 {
589 v5_response_connect header{};
590
591 assert(bytes == sizeof(header));
592 const auto buf = get_read_buffer(self, sizeof(header));
593 std::memcpy(std::addressof(header), buf.data(), sizeof(header));
594 if (header.version != 5)
595 {
596 self.done(socks::error::unexpected_version, self_);
597 return;
598 }
599 if (header.reply != v5_reply_success)
600 {
601 self.done(socks::error(int(header.reply)), self_);
602 return;
603 }
604
605 if (header.type == v5_ipv4_type)
606 bytes = sizeof(v5_response_ipv4);
607 else if (header.type == v5_ipv6_type)
608 bytes = sizeof(v5_response_ipv6);
609 else
610 {
611 self.done(socks::error::unexpected_version, self_);
612 return;
613 }
614 }
615
616 std::get<0>(self.buffer_size_) = sizeof(v5_response_connect);
617 BOOST_ASIO_CORO_YIELD boost::asio::async_read(
618 self.proxy_,
619 get_read_buffer(self, bytes),
620 boost::asio::bind_executor(self.strand_, std::move(*this))
621 );
622 std::get<0>(self.buffer_size_) =
623 std::min(sizeof(self.buffer_), sizeof(v5_response_connect) + bytes);
624 self.done(error, self_);
625 }
626 }
627 };
628
629 struct client::write
630 {
631 std::shared_ptr<client> self_;
632
633 static boost::asio::const_buffer get_buffer(client const& self) noexcept
634 {
635 return boost::asio::buffer(self.buffer_, std::get<0>(self.buffer_size_));
636 }
637
638 void operator()(const boost::system::error_code error)
639 {
640 if (self_)
641 {
642 client& self = *self_;
643 if (error)
644 self.done(error, self_);
645 else if (self.ver_ == version::v5)
646 boost::asio::async_write(self.proxy_, get_buffer(self), boost::asio::bind_executor(self.strand_, process_v5{std::move(self_)}));
647 else
648 boost::asio::async_write(self.proxy_, get_buffer(self), boost::asio::bind_executor(self.strand_, read{std::move(self_)}));
649 }
650 }
651 };
652
653
654
655 client::client(stream_type::socket&& proxy, socks::version ver)
656 : proxy_(std::move(proxy)), strand_(proxy_.get_executor()), buffer_size_{{}}, buffer_(), ver_(ver)
657 {}
658
659 client::~client() {}
660
661 bool client::set_connect_command(const epee::net_utils::ipv4_network_address& address, const user_and_pass* userinfo)
662 {
663 switch (socks_version())
664 {
665 case version::v4:
666 case version::v4a:
667 case version::v4a_tor:
668 break;
669 case version::v5:
670 buffer_size_ = write_v5_address_connect(
671 buffer_,
672 v5_ipv4_connect::make(boost::endian::big_to_native(address.ip()), address.port()),
673 userinfo
674 );
675 return std::get<0>(buffer_size_) != 0;
676 default:
677 return false;
678 }
679
680 static_assert(sizeof(v4_header) < sizeof(buffer_), "buffer size too small for request");
681 static_assert(0 < sizeof(buffer_), "buffer size too small for null termination");
682
683 if (userinfo && (!userinfo->user.empty() || !userinfo->pass.empty()))
684 return false;
685
686 // version 4
687 const v4_header temp{4, v4_connect_command, address.port(), boost::endian::big_to_native(address.ip())};
688 std::memcpy(std::addressof(buffer_), std::addressof(temp), sizeof(temp));
689 buffer_[sizeof(temp)] = 0;
690
691 buffer_size_ = {};
692 std::get<0>(buffer_size_) = sizeof(temp) + 1;
693
694 return true;
695 }
696
697 bool client::set_connect_command(const epee::net_utils::ipv6_network_address& address, const user_and_pass* userinfo)
698 {
699 if (socks_version() != version::v5)
700 return false;
701 buffer_size_ = write_v5_address_connect(
702 buffer_, v5_ipv6_connect::make(address.ip(), address.port()), userinfo
703 );
704 return std::get<0>(buffer_size_) != 0;
705 }
706
707 bool client::set_connect_command(const boost::string_ref domain, std::uint16_t port, const user_and_pass* userinfo)
708 {
709 switch (socks_version())
710 {
711 case version::v4a:
712 case version::v4a_tor:
713 break;
714 case version::v5:
715 buffer_size_ = write_v5_domain_connect(buffer_, port, domain, userinfo);
716 return std::get<0>(buffer_size_) != 0;
717 default:
718 return false;
719 }
720
721 if (userinfo && (!userinfo->user.empty() || !userinfo->pass.empty()))
722 return false;
723
724 const std::size_t buf_used = write_domain_header(buffer_, v4_connect_command, port, domain);
725 buffer_size_ = {};
726 std::get<0>(buffer_size_) = buf_used;
727 return buf_used != 0;
728 }
729
730 bool client::set_connect_command(const net::tor_address& address, const user_and_pass* userinfo)
731 {
732 if (!address.is_unknown())
733 return set_connect_command(address.host_str(), address.port(), userinfo);
734 return false;
735 }
736
737 bool client::set_connect_command(const net::i2p_address& address, const user_and_pass* userinfo)
738 {
739 if (!address.is_unknown())
740 return set_connect_command(address.host_str(), address.port(), userinfo);
741 return false;
742 }
743
744 bool client::set_resolve_command(boost::string_ref domain)
745 {
746 if (socks_version() != version::v4a_tor)
747 return false;
748
749 const std::size_t buf_used = write_domain_header(buffer_, v4tor_resolve_command, 0, domain);
750 buffer_size_ = {};
751 std::get<0>(buffer_size_) = buf_used;
752 return buf_used != 0;
753 }
754
755 bool client::connect_and_send(std::shared_ptr<client> self, const stream_type::endpoint& proxy_address)
756 {
757 if (self && std::get<0>(self->buffer_size_))
758 {
759 client& alias = *self;
760 alias.proxy_.async_connect(proxy_address, boost::asio::bind_executor(alias.strand_, write{std::move(self)}));
761 return true;
762 }
763 return false;
764 }
765
766 bool client::send(std::shared_ptr<client> self)
767 {
768 if (self && std::get<0>(self->buffer_size_))
769 {
770 client& alias = *self;
771 if (alias.ver_ == version::v5)
772 boost::asio::async_write(alias.proxy_, write::get_buffer(alias), boost::asio::bind_executor(alias.strand_, process_v5{std::move(self)}));
773 else
774 boost::asio::async_write(alias.proxy_, write::get_buffer(alias), boost::asio::bind_executor(alias.strand_, read{std::move(self)}));
775 return true;
776 }
777 return false;
778 }
779
780 void client::async_close::operator()(boost::system::error_code error)
781 {
782 if (self_ && error != boost::system::errc::operation_canceled)
783 {
784 const std::shared_ptr<client> self = std::move(self_);
785 boost::asio::dispatch(self->strand_, [self] ()
786 {
787 if (self && self->proxy_.is_open())
788 {
789 boost::system::error_code ec;
790 self->proxy_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
791 self->proxy_.close(ec);
792 }
793 });
794 }
795 }
796 } // socks
797 } // net