Jump to content
dizzy

Exemplu C++ sockets si proxy-uri

Recommended Posts

Posted

Programul citeste niste proxy-uri dintr-un fisier text sub forma ip:port, se conecteaza la fiecare proxy, se duce la showmyip.com prin acel proxy dandu-se drept mozilla firefox, apoi inchide conexiunea si trece la urmatorul. Inlocuiti "proxy_no_codeen_stripped.txt" cu numele fisierului vostru de proxy-uri.

Note: Nu folositi proxy-uri din reteaua CodEEn ca nu-s bune. fac mofturi.

Note2: Merge pentru http proxies



#include <cstdlib>
#include <iostream>
#include <winsock.h>
#include <fstream>
#include <string>

#define WSASTARTUP_ERR 1
#define SOCKETCREATION_ERR 2
#define CONNECT_ERR 3

using namespace std;

class MySocket
{
public:
MySocket();
~MySocket();
int Connect(const string);
int Send(string);
int Receive(char*&);

public:
int errFlag;
const char* host_;
int port_;
SOCKET s;
private:
WSADATA wsaData;
WORD version;
sockaddr_in proxy;

};

MySocket::MySocket()
{
version=MAKEWORD(1,1);
if(WSAStartup(version,&wsaData)!=0)
{
errFlag = WSASTARTUP_ERR;
return;
}
}

MySocket::~MySocket()
{
}

int MySocket::Receive(char*& message)
{

if(recv(s,message,strlen((char*)&message),0)>0)
{
return 1;
}
else
{
return 0;
}
}
int MySocket::Connect(string host)
{
errFlag = 0;

int rConnect = 0;

string::size_type loc = host.find(":");

if( loc == string::npos )
{
cout << "Wrong host info format" <<endl;

}
else
{
s = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);

if( s==INVALID_SOCKET)
{
errFlag = SOCKETCREATION_ERR;
return 0;
}

memset(&proxy,0x00,sizeof(proxy));

host_ = host.substr(0,loc).c_str();
proxy.sin_addr.s_addr = inet_addr(host_);

port_ = atoi(host.substr(loc+1).c_str());
proxy.sin_port = htons(port_);

proxy.sin_family = AF_INET;

rConnect = connect(s,(SOCKADDR *)&proxy,sizeof(proxy));
if(rConnect < 0)
{
errFlag = CONNECT_ERR;
return 0;
}
}

}

int main(int argc, char *argv[])
{
string line;
int result = -1;
char* msg;
char * header = "GET [url]http://www.showmyip.com/[/url] HTTP/1.1\r\nHost: [url]www.showmyip.com\r\n\[/url]
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3\r\n\
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n\
Accept-Language: en-us,en;q=0.5\r\n\
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n\
Keep-Alive: 300\r\n\
Proxy-Connection: keep-alive\r\n\r\n\0";

// char response[20480]; //20kb
char response[4096]; //4kb

ifstream file("proxy_no_codeen_stripped.txt");
ofstream dump("dump.txt",ios::app);

if(file.is_open())
{
cout << "It's open!" <<endl;

while(!file.eof())
{
getline(file,line);

MySocket *myS = new MySocket();

result = myS->Connect(line);

if (result == 0)
{
cout << "Error connecting: " << myS->host_ << ":" << myS->port_ <<endl;
}
else
{
cout << "Connected to: " << myS->host_ << ":" << myS->port_;
/* if(myS->Receive(msg)>0)
{
cout<<"Message: " << *msg <<endl;
}*/
if(dump.is_open())
{
send(myS->s,header,strlen(header),0);

while(1)
{
if (recv(myS->s,response,sizeof(response),0)>0)
{
if(strstr(response," 200 ")!=NULL)
{
cout<<" ------> OK " << endl;
}
dump << response ;
}
else
{
dump << endl <<"###########################################################################" <<endl;
break;
}
}
cout << endl;
}
else
{
cout << "Can't open dump file" << endl;
}
}

closesocket(myS->s);

delete myS;

}

file.close();
dump.close();
}
else
{
cout << "It's not open!" <<endl;
}


/*if(s->errFlag!=0)
{
cout << "Eroare initializare socket" <<endl;
}*/
WSACleanup();
system("PAUSE");
return EXIT_SUCCESS;
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...