Advertisement
Guest User

Untitled

a guest
Jan 31st, 2019
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <boost/array.hpp>
  3. #include <boost/asio.hpp>
  4. #include "socks5.hpp"
  5.  
  6. using boost::asio::ip::tcp;
  7.  
  8. int connectSOCKS(tcp::socket& socket, char* serverName, int serverPort, std::string hostname, int port, std::string username = "", std::string password = "") {
  9.    try
  10.    {
  11.       boost::asio::io_context io_context;
  12.       tcp::resolver resolver(io_context);
  13.  
  14.       // Get a list of endpoints corresponding to the SOCKS 5 server name.
  15.       auto endpoints = resolver.resolve(serverName, std::to_string(serverPort), tcp::resolver::numeric_service);
  16.  
  17.       // Try each endpoint until we successfully establish a connection to the
  18.       // SOCKS 5 server.
  19.       socket = tcp::socket(io_context);
  20.       boost::asio::connect(socket, endpoints);
  21.  
  22.       socks5::request socks_request;
  23.       socks5::reply socks_reply;
  24.  
  25.       // Send the request to the SOCKS 5 server.
  26.       boost::asio::write(socket, socks_request.greeting());
  27.  
  28.       // Receive a response from the SOCKS 5 server.
  29.       boost::asio::read(socket, socks_reply.auth_choice());
  30.  
  31.       // Check whether we successfully negotiated with the SOCKS 5 server.
  32.       if (!socks_reply.success())
  33.       {
  34.          std::cout << "Connection failed.\n";
  35.          std::cout << "status = " << socks_reply.status();
  36.          return 1;
  37.       }
  38.  
  39.       if (socks_reply.auth == socks5::PASSWORD) {
  40.          // Send username and pass to the SOCKS 5 server.
  41.          boost::asio::write(socket, socks_request.password(username, password));
  42.  
  43.          // Receive a response from the SOCKS 5 server.
  44.          boost::asio::read(socket, socks_reply.password_verif());
  45.  
  46.          // Check whether we successfully negotiated with the SOCKS 5 server.
  47.          if (!socks_reply.success())
  48.          {
  49.             std::cout << "Connection failed.\n";
  50.             std::cout << "status = " << socks_reply.status();
  51.             return 1;
  52.          }        
  53.       }
  54.  
  55.       // Send request to the SOCKS 5 server.
  56.       boost::asio::write(socket, socks_request.conn(socks5::request::connect, socks5::addr_type::Domain, hostname, port));
  57.  
  58.       // Receive a response from the SOCKS 5 server.
  59.       boost::asio::read(socket, socks_reply.connect_reply());
  60.  
  61.       // Check whether we successfully negotiated with the SOCKS 5 server.
  62.       if (!socks_reply.success())
  63.       {
  64.          std::cout << "Connection failed.\n";
  65.          std::cout << "status = " << socks_reply.status() << std::endl;
  66.          return 1;
  67.       }
  68.  
  69.       return 0;
  70.    }  
  71.    catch (std::exception& e)
  72.    {
  73.       std::cout << "Exception: " << e.what() << "\n";
  74.       return 1;
  75.    }
  76. }
  77.  
  78. int main(int argc, char *argv[]){
  79.    std::cout << "Hello World!" << std::endl;
  80.  
  81.    boost::asio::io_context io_context;
  82.    tcp::resolver resolver(io_context);
  83.  
  84.    tcp::socket socket(io_context);
  85.    int code;
  86.    if (code = connectSOCKS(socket, "192.168.0.59", 9100, "scihub22266oqcxt.onion", 80) == 0) {
  87.  
  88.       // Form the HTTP request. We specify the "Connection: close" header so that
  89.       // the server will close the socket after transmitting the response. This
  90.       // will allow us to treat all data up until the EOF as the response.
  91.       std::string request =
  92.          "GET / HTTP/1.0\r\n"
  93.          "Host: scihub22266oqcxt.onion/\r\n"
  94.          "Accept: */*\r\n"
  95.          "Connection: close\r\n\r\n";
  96.  
  97.       // Send the HTTP request.
  98.       boost::asio::write(socket, boost::asio::buffer(request));
  99.  
  100.       // Read until EOF, writing data to output as we go.
  101.       std::array<char, 512> response;
  102.       boost::system::error_code error;
  103.       while (std::size_t s = socket.read_some(boost::asio::buffer(response), error))
  104.          std::cout.write(response.data(), s);
  105.          if (error != boost::asio::error::eof)
  106.             throw std::system_error(error);
  107.  
  108.       return 0;
  109.    }
  110.    return code;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement