📄 client.cpp
字号:
WaitForMultipleObjects(threadNum,pThreadHand,TRUE,INFINITE);
for(i=0; i<threadNum; i++)
CloseHandle(pThreadHand[i]);
CloseHandle(g_WriteEvent);
delete []pThreadHand;
delete []pHttpThreadPara;
g_File.close();
return 0;
}
/**********************************************************************************/
u_long ConvertHostnameToLongHostAddress(const char * destAddress)
{
hostent * host=gethostbyname((const char *)destAddress);
if(host==NULL)
{
cout<<"Cann't resolve this host name: "<<GetLastError()<<endl;
return -1;
}
return ntohl((*(struct in_addr*)host->h_addr).S_un.S_addr);
}
int Socks5StartIPv4(const HTTP_Request_Struct& request, SOCKET& sock)
{
sockaddr_in serverAddr;
int retCode;
char buffer[BUFFER_BLOCK_SIZE];
BYTE version=5;
BYTE authorWay1=0; //no authority
BYTE authorWay2=2; //user/pass authority
int rsv=0;
string addrStr;
u_int t;
u_long destAddress=ConvertHostnameToLongHostAddress(request.serverHostName.c_str());
sock=socket(AF_INET, SOCK_STREAM,0);
serverAddr.sin_addr.S_un.S_addr=htonl(ConvertHostnameToLongHostAddress(request.proxyServerName.c_str()));
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(request.proxyServerPort);
cout<<"Connecting to "<<inet_ntoa(serverAddr.sin_addr)<<"\tPort:"<<request.proxyServerPort<<endl;
cout.flush();
if(connect(sock,(const sockaddr*)&serverAddr,sizeof(sockaddr))==SOCKET_ERROR)
{
cout<<"Cannot connect to Server: "<<request.proxyServerName.c_str()<<endl;
cout.flush();
closesocket(sock);
return -1;
}
//1. authoutication negotiation
sprintf(buffer,"%c%c%c%c",version,2,authorWay1,authorWay2);
send(sock,buffer,4,0);
retCode=recv(sock,buffer,BUFFER_BLOCK_SIZE,0);
DEAL_SOCK_ERROR(retCode,sock);
if(char(retCode)==0xFF)
return 1;
buffer[retCode]='\0';
cout<<buffer;
//user/pass authentication
if(buffer[1]==authorWay2)
{
//user name
sprintf(buffer,"%c%c%s%c%s",char(0x01),char(request.proxyUser.length()),request.proxyUser.c_str(),
char(request.proxyPass.length()),request.proxyPass.c_str());
t=3+request.proxyUser.length()+request.proxyPass.length();
send(sock, buffer, t, 0);
retCode=recv(sock,buffer,BUFFER_BLOCK_SIZE,0);
DEAL_SOCK_ERROR(retCode,sock);
if(char(retCode)==0xFF)
return 1;
buffer[retCode]='\0';
if(buffer[1]!=0)
return 1;
}
//1. authoutication negotiation
addrStr=buffer;
t=htons(request.serverPort);
sprintf(buffer,"%c%c%c%c%c%c%c%c%c%c",version,char(0x01),rsv,char(0x01),
((char*)&destAddress)[3],((char*)&destAddress)[2],((char*)&destAddress)[1],((char*)&destAddress)[0],
((char *)&t)[0],((char *)&t)[1]);
cout<<buffer;
send(sock,buffer,10,0);
retCode=recv(sock,buffer,BUFFER_BLOCK_SIZE,0);
DEAL_SOCK_ERROR(retCode,sock);
buffer[retCode]='\0';
if(buffer[1]!=0)
{
sprintf(buffer,"%x",buffer[1]);
cout<<"Failure in negotiation: "<<buffer<<endl;
return 1;
}
return 0;
}
//"CONNECT " + destaddr + ":" + destport + " HTTP/1.1" + "\r\n" + "Host: " + destAddr + ":" + destPort + "\r\n\r\n"
int HttpProxyStart(const HTTP_Request_Struct& request,SOCKET& sock)
{
sockaddr_in serverAddr;
int retCode;
char buffer[BUFFER_BLOCK_SIZE];
string addrStr;
char destAddrStr[BUFFER_BLOCK_SIZE];
u_long destAddress=ConvertHostnameToLongHostAddress(request.serverHostName.c_str());
sock=socket(AF_INET, SOCK_STREAM,0);
serverAddr.sin_addr.S_un.S_addr=htonl(ConvertHostnameToLongHostAddress(request.proxyServerName.c_str()));
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(request.proxyServerPort);
cout<<"Connecting to "<<inet_ntoa(serverAddr.sin_addr)<<"\tPort:"<<request.proxyServerPort<<endl;
cout.flush();
if(connect(sock,(const sockaddr*)&serverAddr,sizeof(sockaddr))==SOCKET_ERROR)
{
cout<<"Cannot connect to Server: "<<request.proxyServerName.c_str()<<endl;
cout.flush();
closesocket(sock);
return -1;
}
//"CONNECT " + destaddr + ":" + destport + " HTTP/1.1" + "\r\n" + "Host: " + destAddr + ":" + destPort + "\r\n\r\n"
serverAddr.sin_addr.S_un.S_addr=htonl(destAddress);
strcpy(destAddrStr,inet_ntoa(serverAddr.sin_addr));
sprintf(buffer,"CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n",destAddrStr,request.serverPort,destAddrStr,request.serverPort);
if(request.proxyUser.length())
{
//Encode username, pass using base64
//Proxy-Authorization: Basic <base64-stuff> <CRLF>
char t1[BUFFER_BLOCK_SIZE],t2[BUFFER_BLOCK_SIZE];
EncodingBase64(request.proxyUser.c_str(),t1);
EncodingBase64(request.proxyPass.c_str(),t2);
strcat(buffer,"Proxy-Authorization: Basic ");
strcat(buffer,t1);
strcat(buffer,":");
strcat(buffer,t2);
strcat(buffer,"\r\n");
}
strcat(buffer,"\r\n");
send(sock,buffer,strlen(buffer),0);
retCode=recv(sock,buffer,BUFFER_BLOCK_SIZE,0);
DEAL_SOCK_ERROR(retCode,sock);
buffer[retCode]='\0';
sscanf(buffer,"%s%d",destAddrStr,&retCode);
if(retCode/100!=2)
{
cout<<"Failure in negotiation: "<<buffer<<endl;
return 1;
}
return 0;
}
int ConnectToServer(const HTTP_Request_Struct& request, SOCKET& sock)
{
sockaddr_in serverAddr;
hostent * host;
if(request.proxyServerName.length()!=0)//using socks5 proxy
{
// if(Socks5StartIPv4(request,sock)!=0)
if(HttpProxyStart(request,sock)!=0)
{
cerr<<"Proxy server error!"<<endl;
return -1;
}
}
else
{
host=gethostbyname(request.serverHostName.c_str());
if(host==NULL)
{
cout<<"Cann't resolve this host name: "<<GetLastError()<<endl;
return -1;
}
sock=socket(AF_INET, SOCK_STREAM,0);
serverAddr.sin_addr=*(struct in_addr*)host->h_addr;
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(request.serverPort);
cout<<"Connecting to "<<inet_ntoa(serverAddr.sin_addr)<<endl;
cout.flush();
if(connect(sock,(const sockaddr*)&serverAddr,sizeof(sockaddr))==SOCKET_ERROR)
{
cout<<"Cannot connect to Server: "<<request.serverHostName.c_str()<<endl;
cout.flush();
closesocket(sock);
return -1;
}
}
return 0;
}
void GivenUniqueFileName(const string& dir,
const string& subject,
string& name,
string postfix)
{
//if subject is NULL,
WIN32_FIND_DATA FileData;
HANDLE hSearch;
char buffer[BUFFER_BLOCK_SIZE];
int count=1;
string curdir=".\\";
string title=subject;
RenameFileName(title);
sprintf(buffer,"%s",postfix.c_str());
while(1)
{
name=curdir+dir+"\\"+title+buffer;
hSearch = FindFirstFile(name.c_str(), &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
break;
if(FileData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
{
hSearch = FindFirstFile(name.c_str(), &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
break;
}
sprintf(buffer,"(%d)%s",count++,postfix.c_str());
}
FindClose(hSearch);
}
void SplitURLToHostAndFilename(const string& url, string& hostname, string& filename)
{
string::size_type pos=0;
string str=url;
//filter blank space
while(str.find(' ',0)!=string::npos)
str.replace(0,1,"");
//filter http://
if(str.find("http://",0)!=string::npos)
str=str.substr(7,str.length()-7);
//filter hostname and suburl
if((pos=str.find('/',0))==string::npos)
{
hostname=str;
filename="/";
return;
}
hostname=str.substr(0,pos);
pos=str.find_last_of('/',str.length()-1);
filename=str.substr(pos+1,str.length()-pos-1);
}
void SplitURLToNameAndPostfix(const string& src, string& name, string& postfix)
{
string::size_type pos=0,apos=0;
if((pos=src.find_last_of('.',src.length()))==string::npos)
pos=src.length();
postfix=src.substr(pos+1,src.length()-pos);
if((apos=src.find_last_of('/',src.length()))==string::npos)
name=src.substr(0,pos);
else
name=src.substr(apos+1,src.length()-apos-postfix.length()-2);
}
void GetStartString(string& str, string& startStr)
{
//front blank space
string::size_type pos=0;
while(str.find(' ',0)==0)
str.replace(0,1,"");
if((pos=str.find('"',0))!=string::npos)
{
startStr=str.substr(0,pos);
str=str.substr(pos+1,str.length());
}
else
{
pos=str.find(' ',0);
if(pos==string::npos)
pos=str.find('>',0);
assert(pos!=string::npos);
if(pos==string::npos)
{
startStr=str;
}
else
{
startStr=str.substr(0,pos);
str=str.substr(pos+1,str.length()-pos);
}
}
}
BOOL FilterStartTag(string& str, const string& tag)
{
//front blank space
while(str.find(' ',0)==0)
str.replace(0,1,"");
if(str.find(tag,0)!=0)
return FALSE;
else
{
str=str.substr(tag.length(),str.length()-tag.length());
while(str.find(' ',0)==0)
str.replace(0,1,"");
return TRUE;
}
}
void RenameFileName(string& filename)
{
char illegalChar[]="\\/:*?\"<>|";
int i=0;
string::size_type pos=0;
while((pos=filename.find_first_of(illegalChar, pos))!=string::npos)
{
filename.replace(pos,1,"_");
}
}
void EncodingBase64(const char* src, char* des)
{
assert(src&&des);
char sixbit_encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
while(*src!=NULL)
{
*des++=sixbit_encoding[(UCHAR)*src++>>2]; //6
if(*src==NULL)
{
*des++=sixbit_encoding[(*(src-1)&0x3)<<4];//2+4
*des++='=';
*des++='=';
break;
}
else
{
*des++=sixbit_encoding[(*(src-1)&0x3)<<4 | UCHAR(*src)>>4];//2+4
src++;
}
if(*src==NULL)
{
*des++=sixbit_encoding[(*(src-1)&0xF)<<2];//4+2
*des++='=';
}
else
{
*des++=sixbit_encoding[(*(src-1)&0xF)<<2 | UCHAR(*src)>>6];//4+2
*des++=sixbit_encoding[*src&0x3F];//6
src++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -