Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // License : MIT
- // Yaseen Mohamed Twati
- #ifdef _WIN32 // Windows
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #define MSG_NOSIGNAL 0
- #else // Linuc + Max
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <string.h>
- #include <netdb.h>
- #include <errno.h>
- #include <err.h>
- #define SOCKET_ERROR -1
- #define INVALID_SOCKET -1
- typedef int SOCKET;
- typedef sockaddr SOCKADDR;
- typedef sockaddr_in SOCKADDR_IN;
- #define closesocket close
- #ifdef __APPLE__
- #define MSG_NOSIGNAL 0
- #endif
- #endif
- #include <iostream>
- // todo: Replace with http://stackoverflow.com/questions/24611215/one-liner-for-raii-on-non-pointer ?
- // From http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
- struct WndSocketDeleter
- {
- typedef SOCKET pointer;
- void operator()(SOCKET h)
- {
- ::closesocket(h);
- }
- };
- typedef std::unique_ptr<SOCKET, WndSocketDeleter> unique_socket;
- bool operator==(const unique_socket& lhs, const SOCKET &rhs);
- bool operator!=(const unique_socket& lhs, const SOCKET &rhs);
- SOCKET setup_tor(const char* host, const unsigned short port)
- {
- const unsigned short TOR_PORT = 9050;
- const char* TOR_IP = "127.0.0.1";
- SOCKET sock;
- if((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
- return INVALID_SOCKET;
- SOCKADDR_IN sockAddr;
- sockAddr.sin_family = AF_INET;
- sockAddr.sin_port = htons(TOR_PORT);
- sockAddr.sin_addr.s_addr = inet_addr(TOR_IP);
- if(SOCKET_ERROR == connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR_IN)))
- return INVALID_SOCKET;
- const char AUTH_PACKET[3] =
- {
- 0x05, // SOCKS 5
- 0x01, // One Authentication Method
- 0x00 // No AUthentication
- };
- send(sock, AUTH_PACKET, sizeof(AUTH_PACKET), MSG_NOSIGNAL); //! todo error handling
- char Resp1[2];
- recv(sock, Resp1, 2, 0); //! todo error handling
- if(Resp1[1] != 0x00)
- return INVALID_SOCKET;
- //! todo IPv6
- // Is it an IP?
- if(INADDR_NONE != inet_addr(host))
- {
- const char IP_CONNECTION[10] = {
- 0x05, // SOCKS5
- 0x01, // CONNECT
- 0x00, // RESERVED
- 0x01, // IPV4
- };
- unsigned long ip = inet_addr(host);
- memcpy(IP_CONNECTION+4, &ip, sizeof(ip));
- memcpy(IP_CONNECTION+4+sizeof(ip), &Port, 2);
- send(sock, IP_CONNECTION, sizeof(IP_CONNECTION), 0); //! todo error handling
- }
- else
- {
- const char DOMAIN_CONNECTION[4] = {
- 0x05, // SOCKS5
- 0x01, // CONNECT
- 0x00, // RESERVED
- 0x03, // DOMAIN
- };
- const unsigned char domainLen = strlen(host);
- /*! todo error handling */
- send(sock, DOMAIN_CONNECTION, sizeof(DOMAIN_CONNECTION), 0);
- send(sock, &domainLen, 1, 0);
- send(sock, host, domainLen, 0);
- send(sock, &port, sizeof(port), MSG_NOSIGNAL);
- }
- char Resp2[10];
- recv(sock, Resp2, 10, 0);
- if(Resp2[1] != 0x00)
- {
- return INVALID_SOCKET;
- }
- return sock;
- }
- int main()
- {
- std::cout << " [*] C++ Tor Exmaple " << std::endl;
- std::cout << " [*] By Yaseen Eltii " << std::endl;
- std::cout << " [*] Connecting " << std::endl;
- SOCKET sock = setup_tor("104.236.192.175", 80);
- if(sock == INVALID_SOCKET)
- return 1;
- std::cout << " [*] Connected " << std::endl;
- // Here you can normally use send and recv
- // Testing With a HTTP GET Request
- std::cout << " [*] Testing with GET Request \n" << std::endl;
- send(sock, "GET / \n\r\n\r", strlen("GET / \n\r\n\r"), MSG_NOSIGNAL);
- char RecvBuffer[2048];
- size_t Rcved = recv(sock, RecvBuffer, 2048, 0);
- std::cout.write(RecvBuffer, Rcved);
- std::cout << std::endl;
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement