|
作者: jhrs123321 [jhrs123321] 论坛用户 | 登录 |
各位,有谁能够提供一些关于编写代理服务器的资料?我现在想写一个能够进行http代理的软件,就像httponline一样的软件(因为公司关闭了socks端口,只开放http80端口),没办法,只有自己动手写了,自己动手,丰衣足食! Thanks for your help! |
地主 发表时间: 09/26 00:57 |
回复: flytiger [flytiger] 论坛用户 | 登录 |
什么时候有这个资料的时候也给我发一份啊! |
B1层 发表时间: 09/26 11:01 |
回复: jhrs123321 [jhrs123321] 论坛用户 | 登录 |
|
B2层 发表时间: 09/26 14:20 |
回复: z_verygood [z_verygood] 论坛用户 | 登录 |
#include "stdafx.h" #include "MyProxy.h" #include "Winsock.h" //宏定义部分 #define PROXY_TYPE_SOCK4 0 #define PROXY_TYPE_SOCK5 1 #define PROXY_TYPE_HTTP 2 #define ERR_ERR -1 #define ERR_CREATE -2 #define ERR_CONNECT -3 #define ERR_EXP -4 #define ERR_PASSWORD -5 //使用到的结构 typedef struct tagProxyInfo { CString m_strProxyPassword; //校验密码 CString m_strProxyUserName; //校验用户名 UINT m_nProxyPort; //代理服务器的端口 CString m_strProxyHost; //代理服务器的地址或主机名 UINT m_nProxyType; //代理协议类型 //目标地址和端口 CString m_strDestIP; //目标地址 UINT m_nDestPort; //目标的端口 //代理成功后的Socket unsigned long m_sck; //代理成功后的Socket }PROXYINFO; typedef struct tagSock4Req1 { char VN; char CD; unsigned short Port; unsigned long IPAddr; char other[1]; }SOCK4REQ1; typedef struct tagSock4Ans1 { char VN; char CD; }SOCK4ANS1; typedef struct tagSock5Req1 { char Ver; char nMethods; char Methods[255]; }SOCK5REQ1; typedef struct tagSock5Ans1 { char Ver; char Method; }SOCK5ANS1; typedef struct tagSock5Req2 { char Ver; char Cmd; char Rsv; char Atyp; char other[1]; }SOCK5REQ2; typedef struct tagSock5Ans2 { char Ver; char Rep; char Rsv; char Atyp; char other[1]; }SOCK5ANS2; /* typedef struct tagAuthReq { char Ver; char Ulen; char Name[255]; char PLen; char Pass[255]; }AUTHREQ; */ //说明,此结构有问题,应改为 /* 1.char Ver; //版本固定值,恒为1 2.char ULen;//用户名长 3.用户名 (注:变长变量) 4.char PLen;//密码长 5.密码 ; (注:变长变量) */ typedef struct tagAuthReq { char Ver; char Ulen; char Name[255]; char PLen; char Pass[255]; }AUTHREQ; typedef struct tagAuthAns { char Ver; char Status; }AUTHANS; //公用的函数 int DoProxy(PROXYINFO *pProxyInfo); //SOCKS4 static int Socks4(PROXYINFO *pProxyInfo); //SOCKS5 static int Socks5(PROXYINFO *pProxyInfo); //HTTP static int Http(PROXYINFO *pProxyInfo); //create socket static int CreateSocket(unsigned long *sck); //connect to proxy server static int ConnectProxy(unsigned long sck,CString strIP,UINT nPort); //======================================================== //======================================================== //======================================================== //SOCKS4 static int Socks4(PROXYINFO *pProxyInfo) { if(ConnectProxy(pProxyInfo->m_sck,pProxyInfo->m_strProxyHost,pProxyInfo->m_nProxyPort)!=0) { closesocket(pProxyInfo->m_sck); return ERR_CONNECT; } char ReqBuff[100]; memset(ReqBuff,0,sizeof(ReqBuff)); SOCK4REQ1 *m_proxyreq; m_proxyreq = (SOCK4REQ1 *)ReqBuff; m_proxyreq->VN = 4; m_proxyreq->CD = 1; m_proxyreq->Port = ntohs(pProxyInfo->m_nDestPort ); m_proxyreq->IPAddr = inet_addr(pProxyInfo->m_strDestIP); send(pProxyInfo->m_sck,ReqBuff,9,0); char AckBuff[100]; SOCK4ANS1 *m_proxyans; m_proxyans = (SOCK4ANS1 *)AckBuff; memset(AckBuff,0,sizeof(AckBuff)); int nRet=recv(pProxyInfo->m_sck,AckBuff,100,0); if(m_proxyans->VN != 0 || m_proxyans->CD != 90) { //m_sError = _T("通过代理连接主站不成功!"); closesocket(pProxyInfo->m_sck); return ERR_ERR; } return 0; } //SOCKS5 static int Socks5(PROXYINFO *pProxyInfo) { if(ConnectProxy(pProxyInfo->m_sck,pProxyInfo->m_strProxyHost,pProxyInfo->m_nProxyPort)!=0) { closesocket(pProxyInfo->m_sck); return ERR_CONNECT; } char ReqBuff[600]; memset(ReqBuff,0,sizeof(ReqBuff)); SOCK5REQ1 *m_proxyreq1; m_proxyreq1 = (SOCK5REQ1 *)ReqBuff; m_proxyreq1->Ver = 5; m_proxyreq1->nMethods = 2; m_proxyreq1->Methods[0] = 0; m_proxyreq1->Methods[1] = 2; send(pProxyInfo->m_sck,ReqBuff,4,0); char AnsBuff[600]; SOCK5ANS1 *m_proxyans1; m_proxyans1 = (SOCK5ANS1 *)AnsBuff; memset(AnsBuff,0,sizeof(AnsBuff)); int nGet=recv(pProxyInfo->m_sck,AnsBuff,600,0); if(m_proxyans1->Ver != 5 || (m_proxyans1->Method!=0 && m_proxyans1->Method!=2)) { //m_sError = _T("通过代理连接主站不成功!"); //ClientSock.Close(); closesocket(pProxyInfo->m_sck); return ERR_ERR; } if(m_proxyans1->Method == 2) { int nUserLen = strlen(pProxyInfo->m_strProxyUserName ); int nPassLen = strlen(pProxyInfo->m_strProxyPassword ); //...AUTHREQ *m_authreq; memset(ReqBuff,0,sizeof(ReqBuff)); //m_authreq = (AUTHREQ *)ReqBuff; char *pAuthReq=ReqBuff; //Ver *pAuthReq=1; pAuthReq++; //UserName Length *pAuthReq = nUserLen; pAuthReq++; //UserName memcpy(pAuthReq, pProxyInfo->m_strProxyUserName, nUserLen); pAuthReq+=nUserLen; //Password Length *pAuthReq=nPassLen; pAuthReq++; //Password memcpy(pAuthReq,pProxyInfo->m_strProxyPassword ,nPassLen); pAuthReq+= nPassLen; /* m_authreq->Ver = 1; m_authreq->Ulen = nUserLen; strcpy(m_authreq->Name,pProxyInfo->m_strProxyUserName ); m_authreq->PLen = nPassLen; strcpy(m_authreq->Pass,pProxyInfo->m_strProxyPassword ); */ send(pProxyInfo->m_sck,ReqBuff,pAuthReq-ReqBuff,0); AUTHANS *m_authans; m_authans = (AUTHANS *)AnsBuff; memset(AnsBuff,0,600); recv(pProxyInfo->m_sck,AnsBuff,600,0); if(m_authans->Ver != 1 || m_authans->Status != 0) { //m_sError = _T("代理服务器用户验证不成功!"); //ClientSock.Close(); closesocket(pProxyInfo->m_sck); return ERR_PASSWORD; } } //---------------------------------------------->>>> @1 unsigned long sck=socket(AF_INET,SOCK_DGRAM,0); sockaddr_in addr; int len = sizeof(addr); memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = 0; bind(sck, (sockaddr *) &addr, len); //---------------------------------------------<<<<< @1 SOCK5REQ2 *m_proxyreq2; m_proxyreq2 = (SOCK5REQ2 *)AnsBuff; m_proxyreq2->Ver = 5; m_proxyreq2->Cmd = 1; m_proxyreq2->Rsv = 0; m_proxyreq2->Atyp = 1; //unsigned long tmpLong = inet_addr(pProxyInfo->m_strProxyHost ); //unsigned short port = ntohs(pProxyInfo->m_nProxyPort ); unsigned long tmpLong = inet_addr(pProxyInfo->m_strDestIP ); unsigned short port = ntohs(pProxyInfo->m_nDestPort ); memcpy(m_proxyreq2->other,&tmpLong,4); memcpy(m_proxyreq2->other+4,&port,2); send(pProxyInfo->m_sck ,AnsBuff,sizeof(SOCK5REQ2)+5,0); SOCK5ANS2 *m_proxyans2; memset(AnsBuff,0,sizeof(AnsBuff)); m_proxyans2 = (SOCK5ANS2 *)AnsBuff; int nRet=recv(pProxyInfo->m_sck,AnsBuff,600,0); if(m_proxyans2->Ver != 5 || m_proxyans2->Rep != 0) { //m_sError = _T("通过代理连接主站不成功!"); //ClientSock.Close(); closesocket(pProxyInfo->m_sck); return ERR_ERR; } return 0; } //HTTP static int Http(PROXYINFO *pProxyInfo) { if(ConnectProxy(pProxyInfo->m_sck,pProxyInfo->m_strProxyHost,pProxyInfo->m_nProxyPort)!=0) { closesocket(pProxyInfo->m_sck); return ERR_CONNECT; } char ReqBuff[600]; memset(ReqBuff,0,sizeof(ReqBuff)); //sprintf( ReqBuff, "%s%s:%d%s","CONNECT ",pProxyInfo->m_strProxyHost,pProxyInfo->m_nProxyPort," HTTP/1.1\r\nUser-Agent: MyApp/0.1\r\n\r\n"); sprintf( ReqBuff, "%s%s:%d%s","CONNECT ",pProxyInfo->m_strDestIP,pProxyInfo->m_nDestPort," HTTP/1.1\r\nUser-Agent: MyApp/0.1\r\n\r\n"); send(pProxyInfo->m_sck ,ReqBuff,strlen(ReqBuff),0); //发送请求 memset(ReqBuff,0,sizeof(ReqBuff)); recv(pProxyInfo->m_sck,ReqBuff,600,0); if(strstr(ReqBuff, "HTTP/1.0 200 Connection established") == NULL) //连接不成功 { //m_sError = _T("通过代理连接主站不成功!"); //ClientSock.Close(); closesocket(pProxyInfo->m_sck); return ERR_ERR; } return 0; } //connect to proxy server static int ConnectProxy(unsigned long sck,CString strIP,UINT nPort) { sockaddr_in destAddr; memset(&destAddr, 0, sizeof(destAddr)); destAddr.sin_family = AF_INET; if ((destAddr.sin_addr.s_addr = inet_addr(strIP.operator LPCTSTR())) == INADDR_NONE) { hostent *he = gethostbyname(strIP); if (he==NULL) return ERR_ERR; destAddr.sin_addr = *(in_addr *) he->h_addr; } destAddr.sin_port = htons(nPort); if(connect(sck,(sockaddr *) &destAddr, sizeof(destAddr)) == SOCKET_ERROR ) return ERR_CONNECT; return 0; } static int CreateSocket(unsigned long *sck) { unsigned long tempSck= socket(AF_INET,SOCK_STREAM,0); if(tempSck ==INVALID_SOCKET ) return ERR_CREATE; *sck=tempSck; return 0; } int DoProxy(PROXYINFO *pProxyInfo) { pProxyInfo->m_sck = 0; int nRet=0; WSADATA wsaData; WORD version = MAKEWORD(2, 2); if (WSAStartup(version, &wsaData) != 0) return ERR_ERR; if(CreateSocket(&pProxyInfo->m_sck)!=0) { pProxyInfo->m_sck=0; WSACleanup(); return ERR_ERR; } switch(pProxyInfo->m_nProxyType) { case PROXY_TYPE_SOCK4: nRet=Socks4(pProxyInfo);break; case PROXY_TYPE_SOCK5: nRet=Socks5(pProxyInfo);break; case PROXY_TYPE_HTTP: nRet=Http(pProxyInfo);break; default : nRet=-1 ;break; } if(nRet!=0) pProxyInfo->m_sck=0; // WSACleanup(); return nRet; } |
B3层 发表时间: 11/12 11:00 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号