sync_client.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <iostream>
  11. #include <iomanip>
  12. #include <ostream>
  13. #include <string>
  14. #include <boost/asio.hpp>
  15. #include <boost/array.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. tcp::resolver::results_type 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. tcp::endpoint http_endpoint =
  41. *resolver.resolve(tcp::v4(), "www.boost.org", "http").begin();
  42. // Send the request to the SOCKS 4 server.
  43. socks4::request socks_request(
  44. socks4::request::connect, http_endpoint, argv[3]);
  45. boost::asio::write(socket, socks_request.buffers());
  46. // Receive a response from the SOCKS 4 server.
  47. socks4::reply socks_reply;
  48. boost::asio::read(socket, socks_reply.buffers());
  49. // Check whether we successfully negotiated with the SOCKS 4 server.
  50. if (!socks_reply.success())
  51. {
  52. std::cout << "Connection failed.\n";
  53. std::cout << "status = 0x" << std::hex << socks_reply.status();
  54. return 1;
  55. }
  56. // Form the HTTP request. We specify the "Connection: close" header so that
  57. // the server will close the socket after transmitting the response. This
  58. // will allow us to treat all data up until the EOF as the response.
  59. std::string request =
  60. "GET / HTTP/1.0\r\n"
  61. "Host: www.boost.org\r\n"
  62. "Accept: */*\r\n"
  63. "Connection: close\r\n\r\n";
  64. // Send the HTTP request.
  65. boost::asio::write(socket, boost::asio::buffer(request));
  66. // Read until EOF, writing data to output as we go.
  67. boost::array<char, 512> response;
  68. boost::system::error_code error;
  69. while (std::size_t s = socket.read_some(
  70. boost::asio::buffer(response), error))
  71. std::cout.write(response.data(), s);
  72. if (error != boost::asio::error::eof)
  73. throw boost::system::system_error(error);
  74. }
  75. catch (std::exception& e)
  76. {
  77. std::cout << "Exception: " << e.what() << "\n";
  78. }
  79. return 0;
  80. }