Author allfresh article original Www.allfresh.net/program/Proxy.htm Text In the network program design process, we often have to deal with various types of proxy servers, such as accessing Internet online servers at the enterprise internal network. Wait, the general proxy server supports several common proxy protocol standards, such as SOCKS4, SOCKS5, HTTP proxy, where SOCKS5 requires user authentication, and the agent is relatively complicated. After reviewing the RFC documentation and related information, I summarize some of the program blocks of TCP protocol penetrating the proxy server, I hope to help everyone.
// Structure Struct Sock4Req1 {Char Vn; CHAR CD; Unsigned long ipaddr; char ipaddr; char}; struct sock4ans1 {char vn; char cd;}; struct sock5req1 {char ver; char nmethods CHAR Methods [255];}; struct sock5ans1 {char ver; char method;}; struct sock5Req2 {char; char cmd; char {char atyp; char other [1];}; struct sock5ans2 {char; REP; Char RSV; char atyp; char atyp; char}; struct authreq {char ver; char ulen; char name [255]; char Plen; char pass [255];}; struct authans {char ver; char status ;}; // Agents by SOCKS4 method if (! Clientsock.connect (g_proxyinfo.m_nproxyip, g_proxyinfo.m_nproxyport) {m_Serror = _t ("cannot be connected to proxy servers!"); Clientsock.close (); return false;} char buff [100]; memset (buff, 0,100); struct sock4req1 * m_proxyreq; m_proxyreq = (struct sock4req1 *) buff; m_proxyreq-> VN = 4; m_proxyreq-> CD = 1; m_proxyreq-> Port = ntohs (getPort ( )); m_proxyReq-> ipaddr = inet_addr (GetServerHostName ()); ClientSock.send (buff, 9); struct sock4ans1 * m_proxyans; m_proxya NS = (Struct Sock4ans1 *) BUFF; MEMSET (BUFF, 0, 100); ClientSock.Receive (BUFF, 100); if (m_proxyans-> vn! = 0 || M_Proxyans-> CD! = 90) {m_Serror = _t (" The agent connection master is not successful! "); Clientsock.close (); return false;} // through the SOCKS5 method agent if (! Clientsock.connect (g_proxyinfo.m_nproxyport) {m_Serror = _t (" cannot connect to a proxy server ");! ClientSock.Close (); return FALSE;} char buff [600]; struct sock5req1 * m_proxyreq1; m_proxyreq1 = (struct sock5req1 *) buff; m_proxyreq1-> Ver = 5; m_proxyreq1-> nMethods = 2; m_proxyreq1-> methods [0] = 0; m_proxyreq1-> methods [1] = 2; ClientSock.send (buff, 4); struct sock5ans1 * m_proxyans1; m_proxyans1 =
(strunt sock5ans1 *) BUFF; MEMSET (BUFF, 0, 600); ClientSock.Receive (BUFF, 600); if (m_Proxyans1-> Ver! = 5 || (m_Proxyans1-> Method! = 0 && m_proxyans1-> method! = 2 )) {m_Serror = _t ("Connect to the primary station through the agent!"); clientsock.close (); return false;} if (m_proxyans1-> method == 2) {int NUserlen = Strlen (g_proxyinfo.m_strproxyuse; int nPassLen = strlen (g_ProxyInfo.m_strProxyPass); struct authreq * m_authreq; m_authreq = (struct authreq *) buff; m_authreq-> Ver = 1; m_authreq-> Ulen = nUserLen; strcpy (m_authreq-> Name, g_ProxyInfo.m_strProxyUser); m_authreq-> PLen = nPassLen; strcpy (m_authreq-> Pass, g_ProxyInfo.m_strProxyPass); ClientSock.Send (buff, 513); struct authans * m_authans; m_authans = (struct authans *) buff; memset (buff, 0,600); ClientSock .Receive (BUFF, 600); if (m_authans-> ver! = 1 || m_authans-> status! = 0) {m_Serror = _T ("Proxy server user verification is not successful!"); Clientsock.close (); return False;}} Struct Sock5Req2 * m_proxyreq2; m_proxyreq2 = (struct sock5req2 *) BUFF; m_proxyreq2-> ver = 5; m_proxyReq2-> cmd = 1; m_proxyreq2-> rsv = 0; m_proxyreq2-> Atyp = 1; unsigned long tmpLong = inet_addr (GetServerHostName ()); unsigned short port = ntohs (GetPort ()); memcpy (m_proxyreq2-> other, & tmpLong, 4); memcpy (m_proxyreq2-> other 4, & port, 2); ClientSock.Send (buff, sizeof (struct sock5req2) 5); struct sock5ans2 * m_proxyans2; memset (buff, 0,600); m_proxyans2 = (struct sock5ans2 *) buff; ClientSock.Receive (buff, 600); IF (m_proxyans2-> ver! = 5 || m_proxyans2-> rep! = 0) {m_SERROR = _t ("Connect to the primary station!"); clientsock.close (); return false;