黑客编程教程(十五)HTTP代理
黑客编程教程(十四)单线程TCP端口扫描器#include <stdio.h>
#include <winsock2.h>
#define MAXBUFLEN 20480
#define HTTPADDLEN 50
#define TIMEWAIT 2000
#pragma comment(lib,"ws2_32.lib")
SOCKET Global[1000];
DWORD WINAPI Proxy( LPVOID pSocket);
int ParseHttpRequest(char * SourceBuf,int DataLen,void * ServerAddr);
int main(int argc,char * argv[])
{
SOCKET MainSocket,ClientSocket;
struct sockaddr_in Host,Client;
WSADATA WsaData;
int AddLen,i;
//初始化
if(WSAStartup(MAKEWORD(2,2),&WsaData) < 0)
{
printf("初始化失败\n");
return 1;
}
//创建socket端口
MainSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(MainSocket == SOCKET_ERROR)
{
printf("端口创建错误\n");
return 1;
}
Host.sin_family = AF_INET;
Host.sin_port = htons(8080);
Host.sin_addr.s_addr = inet_addr("127.0.0.1");
printf("正在工作\n");
//绑定socket
if(bind(MainSocket,(SOCKADDR *)&Host,sizeof(Host)) != 0)
{
printf("绑定错误\n");
}
i = 0;
//监听
if(listen(MainSocket,5) == SOCKET_ERROR)
{
printf("监听错误\n");
}
AddLen = sizeof(Client);
//连接新的客户
i = 0;
for(;;)
{
ClientSocket = accept(MainSocket,(SOCKADDR *)&Client,&AddLen);
if(ClientSocket == SOCKET_ERROR)
{
printf("接受客户请求错误!\n");
}
printf(".");
i ++ ;
if( i >= 1000)
i = 0;
Global[i] = ClientSocket;
//对于每一个客户启动不同的线程程进行控制
//这个地方在使用ClientSocket的时候,要不要保证在某一时刻内只能有一个进程使用?
CreateThread(NULL,0,Proxy,(LPVOID)Global[i],0,NULL);
}
return 0;
}
DWORD WINAPI Proxy( LPVOID pSocket)
{
SOCKET ClientSocket;
char ReceiveBuf[MAXBUFLEN];
int DataLen;
struct sockaddr_in ServerAddr;
SOCKET ProxySocket;
int i = 0;
int time = TIMEWAIT;
//得到参数中的端口号信息
ClientSocket = (SOCKET)pSocket;
//接受第一次请求信息
memset(ReceiveBuf,0,MAXBUFLEN);
DataLen = recv(ClientSocket,ReceiveBuf,MAXBUFLEN,0);
if(DataLen == SOCKET_ERROR)
{
printf("错误\n");
closesocket(ClientSocket);
return 0;
}
if(DataLen == 0)
{
closesocket(ClientSocket);
return 0;
}
//处理请求信息,分离出服务器地址
if( ParseHttpRequest(ReceiveBuf,DataLen,(void *)&ServerAddr) < 0)
{
closesocket(ClientSocket);
goto error;
}
//创建新的socket用来和服务器进行连接
ProxySocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//设置超时时间
setsockopt(ProxySocket,SOL_SOCKET,SO_RCVTIMEO,(char *)&time,sizeof(time));
if(ProxySocket == SOCKET_ERROR)
{
printf("端口创建错误\n");
return 0;
}
if(connect(ProxySocket,(SOCKADDR *)&ServerAddr,sizeof(ServerAddr)) == SOCKET_ERROR)
{
//printf("连接服务器错误");
goto error;
}
//开始进行数据传输处理
//发送到服务器端
if(send(ProxySocket,ReceiveBuf,DataLen,0) == SOCKET_ERROR)
{
//printf("数据发送错误");
goto error;
}
//从服务器端接受数据
while(DataLen > 0)
{
memset(ReceiveBuf,0,MAXBUFLEN);
if((DataLen = recv(ProxySocket,ReceiveBuf,MAXBUFLEN,0)) <= 0)
{
// printf("数据接受错误");
break;
}
else
//发送到客户端
if(send(ClientSocket,ReceiveBuf,DataLen,0) < 0)
{
 
补充:综合编程 , 安全编程 ,