Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <boost/array.hpp>
- #include <boost/asio.hpp>
- #include "socks5.hpp"
- using boost::asio::ip::tcp;
- int connectSOCKS(tcp::socket& socket, char* serverName, int serverPort, std::string hostname, int port, std::string username = "", std::string password = "") {
- try
- {
- boost::asio::io_context io_context;
- tcp::resolver resolver(io_context);
- // Get a list of endpoints corresponding to the SOCKS 5 server name.
- auto endpoints = resolver.resolve(serverName, std::to_string(serverPort), tcp::resolver::numeric_service);
- // Try each endpoint until we successfully establish a connection to the
- // SOCKS 5 server.
- socket = tcp::socket(io_context);
- boost::asio::connect(socket, endpoints);
- socks5::request socks_request;
- socks5::reply socks_reply;
- // Send the request to the SOCKS 5 server.
- boost::asio::write(socket, socks_request.greeting());
- // Receive a response from the SOCKS 5 server.
- boost::asio::read(socket, socks_reply.auth_choice());
- // Check whether we successfully negotiated with the SOCKS 5 server.
- if (!socks_reply.success())
- {
- std::cout << "Connection failed.\n";
- std::cout << "status = " << socks_reply.status();
- return 1;
- }
- if (socks_reply.auth == socks5::PASSWORD) {
- // Send username and pass to the SOCKS 5 server.
- boost::asio::write(socket, socks_request.password(username, password));
- // Receive a response from the SOCKS 5 server.
- boost::asio::read(socket, socks_reply.password_verif());
- // Check whether we successfully negotiated with the SOCKS 5 server.
- if (!socks_reply.success())
- {
- std::cout << "Connection failed.\n";
- std::cout << "status = " << socks_reply.status();
- return 1;
- }
- }
- // Send request to the SOCKS 5 server.
- boost::asio::write(socket, socks_request.conn(socks5::request::connect, socks5::addr_type::Domain, hostname, port));
- // Receive a response from the SOCKS 5 server.
- boost::asio::read(socket, socks_reply.connect_reply());
- // Check whether we successfully negotiated with the SOCKS 5 server.
- if (!socks_reply.success())
- {
- std::cout << "Connection failed.\n";
- std::cout << "status = " << socks_reply.status() << std::endl;
- return 1;
- }
- return 0;
- }
- catch (std::exception& e)
- {
- std::cout << "Exception: " << e.what() << "\n";
- return 1;
- }
- }
- int main(int argc, char *argv[]){
- std::cout << "Hello World!" << std::endl;
- boost::asio::io_context io_context;
- tcp::resolver resolver(io_context);
- tcp::socket socket(io_context);
- int code;
- if (code = connectSOCKS(socket, "192.168.0.59", 9100, "scihub22266oqcxt.onion", 80) == 0) {
- // Form the HTTP request. We specify the "Connection: close" header so that
- // the server will close the socket after transmitting the response. This
- // will allow us to treat all data up until the EOF as the response.
- std::string request =
- "GET / HTTP/1.0\r\n"
- "Host: scihub22266oqcxt.onion/\r\n"
- "Accept: */*\r\n"
- "Connection: close\r\n\r\n";
- // Send the HTTP request.
- boost::asio::write(socket, boost::asio::buffer(request));
- // Read until EOF, writing data to output as we go.
- std::array<char, 512> response;
- boost::system::error_code error;
- while (std::size_t s = socket.read_some(boost::asio::buffer(response), error))
- std::cout.write(response.data(), s);
- if (error != boost::asio::error::eof)
- throw std::system_error(error);
- return 0;
- }
- return code;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement