sync_client.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // sync_client.cpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <array>
  11. #include <iostream>
  12. #include <iomanip>
  13. #include <ostream>
  14. #include <string>
  15. #include <boost/asio.hpp>
  16. #include "socks4.hpp"
  17. using boost::asio::ip::tcp;
  18. int main(int argc, char* argv[])
  19. {
  20. try
  21. {
  22. if (argc != 4)
  23. {
  24. std::cout << "Usage: sync_client <socks4server> <socks4port> <user>\n";
  25. std::cout << "Examples:\n";
  26. std::cout << " sync_client 127.0.0.1 1080 chris\n";
  27. std::cout << " sync_client localhost socks chris\n";
  28. return 1;
  29. }
  30. boost::asio::io_context io_context;
  31. // Get a list of endpoints corresponding to the SOCKS 4 server name.
  32. tcp::resolver resolver(io_context);
  33. auto endpoints = resolver.resolve(argv[1], argv[2]);
  34. // Try each endpoint until we successfully establish a connection to the
  35. // SOCKS 4 server.
  36. tcp::socket socket(io_context);
  37. boost::asio::connect(socket, endpoints);
  38. // Get an endpoint for the Boost website. This will be passed to the SOCKS
  39. // 4 server. Explicitly specify IPv4 since SOCKS 4 does not support IPv6.
  40. auto http_endpoint = *resolver.resolve(tcp::v4(), "www.boost.org", "http");
  41. // Send the request to the SOCKS 4 server.
  42. socks4::request socks_request(
  43. socks4::request::connect, http_endpoint, argv[3]);
  44. boost::asio::write(socket, socks_request.buffers());
  45. // Receive a response from the SOCKS 4 server.
  46. socks4::reply socks_reply;
  47. boost::asio::read(socket, socks_reply.buffers());
  48. // Check whether we successfully negotiated with the SOCKS 4 server.
  49. if (!socks_reply.success())
  50. {
  51. std::cout << "Connection failed.\n";
  52. std::cout << "status = 0x" << std::hex << socks_reply.status();
  53. return 1;
  54. }
  55. // Form the HTTP request. We specify the "Connection: close" header so that
  56. // the server will close the socket after transmitting the response. This
  57. // will allow us to treat all data up until the EOF as the response.
  58. std::string request =
  59. "GET / HTTP/1.0\r\n"
  60. "Host: www.boost.org\r\n"
  61. "Accept: */*\r\n"
  62. "Connection: close\r\n\r\n";
  63. // Send the HTTP request.
  64. boost::asio::write(socket, boost::asio::buffer(request));
  65. // Read until EOF, writing data to output as we go.
  66. std::array<char, 512> response;
  67. boost::system::error_code error;
  68. while (std::size_t s = socket.read_some(
  69. boost::asio::buffer(response), error))
  70. std::cout.write(response.data(), s);
  71. if (error != boost::asio::error::eof)
  72. throw std::system_error(error);
  73. }
  74. catch (std::exception& e)
  75. {
  76. std::cout << "Exception: " << e.what() << "\n";
  77. }
  78. return 0;
  79. }