Advertisement
asqapro

Proxy-Client

Dec 6th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <winsock.h> //note that this requires a linked .lib
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. WSADATA wsadata;
  8.  
  9. bool init(){
  10.     int error = WSAStartup(0x0202, &wsadata);
  11.  
  12.     if(error){
  13.         return false;
  14.     }
  15.     if(wsadata.wVersion != 0x0202)
  16.     {
  17.         WSACleanup(); //Clean up Winsock
  18.         return false;
  19.     }
  20.     return true;
  21. }
  22.  
  23. string MESSAGE;
  24.  
  25. char file_piece[1024];
  26. string file_full;
  27.  
  28. int TCP_PORT = 5721;
  29. string TCP_IP;
  30.  
  31. bool CloseConnection (SOCKET s)
  32. {
  33.     //Close the socket if it exists
  34.     if(s)
  35.         closesocket(s);
  36.  
  37.     return true;
  38. }
  39.  
  40. char* get_local_address(){
  41.     char ac[80];
  42.     gethostname(ac, sizeof(ac));
  43.  
  44.     struct hostent *phe = gethostbyname(ac);
  45.     if (phe == 0) {
  46.         cerr << "Yow! Bad host lookup." << endl;
  47.         return NULL;
  48.     }
  49.  
  50.     struct in_addr addr;
  51.     memcpy(&addr, phe->h_addr_list[0], sizeof(struct in_addr));
  52.     return inet_ntoa(addr);
  53. }
  54.  
  55. //CONNECTTOHOST – Connects to a remote host
  56. bool ConnectToHost(int PortNo, const char* IPAddress)
  57. {
  58.     //Fill out the information needed to initialize a socket…
  59.     SOCKADDR_IN target; //Socket address information
  60.  
  61.     target.sin_family = AF_INET; // address family Internet
  62.     target.sin_port = htons (PortNo); //Port to connect on
  63.     target.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
  64.  
  65.     SOCKET s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
  66.     if (s == INVALID_SOCKET)
  67.     {
  68.         return false; //Couldn't create the socket
  69.     }
  70.  
  71.     //Try connecting...
  72.  
  73.     if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
  74.     {
  75.         return false; //Couldn't connect
  76.     }
  77.     else
  78.         send(s, MESSAGE.c_str(), MESSAGE.length(), 0);
  79.         return true; //Success
  80.     CloseConnection(s);
  81. }
  82.  
  83. void handshake_send(){
  84.     ConnectToHost(TCP_PORT, TCP_IP.c_str());
  85. }
  86.  
  87. SOCKET SetUpListener(const char* pcAddress, int nPort)
  88. {
  89.     u_long nInterfaceAddr = inet_addr(pcAddress);
  90.     if (nInterfaceAddr != INADDR_NONE) {
  91.         SOCKET sd = socket(AF_INET, SOCK_STREAM, 0);
  92.         if (sd != INVALID_SOCKET) {
  93.             sockaddr_in sinInterface;
  94.             sinInterface.sin_family = AF_INET;
  95.             sinInterface.sin_addr.s_addr = nInterfaceAddr;
  96.             sinInterface.sin_port = nPort;
  97.             if (bind(sd, (sockaddr*)&sinInterface,
  98.                     sizeof(sockaddr_in)) != SOCKET_ERROR) {
  99.                 listen(sd, 1);
  100.                 return sd;
  101.             }
  102.         }
  103.     }
  104.  
  105.     return INVALID_SOCKET;
  106. }
  107.  
  108. SOCKET AcceptConnection(SOCKET ListeningSocket, sockaddr_in& sinRemote)
  109. {
  110.     int nAddrSize = sizeof(sinRemote);
  111.     return accept(ListeningSocket, (sockaddr*)&sinRemote, &nAddrSize);
  112. }
  113.  
  114. SOCKET SetUpListener(const char* pcAddress, int nPort);
  115. SOCKET AcceptConnection(SOCKET ListeningSocket, sockaddr_in& sinRemote);
  116.  
  117. char acReadBuffer[1024];
  118. bool exit_server_1;
  119. bool exit_server_2;
  120. bool password_entered = false;
  121. int password_try_count = 3;
  122. bool request_done = false;
  123.  
  124. bool read_sock(SOCKET sd)
  125. {
  126.     // Read data from client
  127.     int nReadBytes = 1;
  128.     while(nReadBytes != 0){
  129.         nReadBytes = recv(sd, acReadBuffer, 5000, 0);
  130.         if((string)acReadBuffer == "got"){
  131.             password_entered = true;
  132.             return true;
  133.         }
  134.         else if((string)acReadBuffer == "password"){
  135.             password_entered = false;
  136.             cout << "You have " << password_try_count << " attempts left." << endl;
  137.             password_try_count--;
  138.             if(password_try_count <= 0){
  139.                 MESSAGE = "kill";
  140.                 handshake_send();
  141.                 exit(-1);
  142.             }
  143.             return true;
  144.         }
  145.         if(strlen(acReadBuffer) > 0){
  146.             memcpy(acReadBuffer, file_piece, sizeof(acReadBuffer));
  147.             return true;
  148.         }
  149.         if (nReadBytes == SOCKET_ERROR) {
  150.             return false;
  151.         }
  152.     }
  153.     if(exit_server_1){
  154.         exit_server_2 = true;
  155.     }
  156.     return false;
  157. }
  158.  
  159. WSADATA w;
  160.  
  161. int handshake_receive()
  162. {
  163.     string IP_BIND = get_local_address();
  164.     cout << IP_BIND << endl;
  165.     //SOCKET ListeningSocket = SetUpListener(IP_BIND.c_str(), htons(TCP_PORT));
  166.     //if (ListeningSocket == INVALID_SOCKET) {
  167.     //    cout << "Error with listener: " << WSAGetLastError() << endl;
  168.     //    return -1;
  169.     //}
  170.  
  171.     // Spin forever handling clients
  172.     while (1) {
  173.         SOCKET ListeningSocket = SetUpListener(IP_BIND.c_str(), htons(TCP_PORT));
  174.         if (ListeningSocket == INVALID_SOCKET) {
  175.             cout << "Error with listener: " << WSAGetLastError() << endl;
  176.             return -1;
  177.         }
  178.         // Wait for a connection, and accepting it when one arrives.
  179.         cout << "Waiting for a connection..." << flush;
  180.         sockaddr_in sinRemote;
  181.         SOCKET sd = AcceptConnection(ListeningSocket, sinRemote);
  182.         if (sd != INVALID_SOCKET) {
  183.             cout << "Accepted connection from " <<
  184.                     inet_ntoa(sinRemote.sin_addr) << ":" <<
  185.                     ntohs(sinRemote.sin_port) << "." << endl;
  186.         }
  187.         else {
  188.             cout << "Error with accepter: " << WSAGetLastError() << endl;
  189.             return -1;
  190.         }
  191.         if(read_sock(sd)){
  192.             if(password_entered){
  193.                 exit_server_2 = true;
  194.             }
  195.             exit_server_1 = true;
  196.         }
  197.         if(exit_server_1 && exit_server_2){
  198.             exit_server_1 = false;
  199.             exit_server_2 = false;
  200.             break;
  201.         }
  202.         if(CloseConnection(sd) && CloseConnection(ListeningSocket)){}
  203.         else{
  204.             cout << "Error disconnecting: " << WSAGetLastError() << endl;
  205.             return -1;
  206.         }
  207.     }
  208.     return 0;
  209. }
  210.  
  211. string url_request;
  212. string password;
  213.  
  214. void client(){
  215.     cout << "Setting up client." << endl;
  216.     cout << "IP of server? ";
  217.     cin >> TCP_IP;
  218.     while(true){
  219.         if(!password_entered){
  220.             cout << "Enter password: ";
  221.             cin >> password;
  222.             MESSAGE = password;
  223.             handshake_send();
  224.             if(handshake_receive() == -1){
  225.                 cout << "Error occurred, restarting server." << endl;
  226.                 return;
  227.             }
  228.             cout << "here" << endl;
  229.         }
  230.         else{
  231.             cout << "URL: " << endl;
  232.             cin >> url_request;
  233.             MESSAGE = url_request;
  234.             handshake_send();
  235.             if(handshake_receive() == -1){
  236.                 cout << "Error occurred, restarting server." << endl;
  237.                 return;
  238.             }
  239.         }
  240.         if(request_done){
  241.             cout << "Requested URL " << url_request << " fetched." << endl;
  242.             cout << "You will find the HTML file to your URL in your current directory." << endl;
  243.         }
  244.     }
  245. }
  246.  
  247. int main()
  248. {
  249.     if(!init()){
  250.         cout << "Something went wrong. Please restart the program." << endl;
  251.         return 0;
  252.     }
  253.     while(true){
  254.         client();
  255.     }
  256.     WSACleanup(); //Clean up Winsock
  257.     return 0;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement