控制台程序 能转发出去但是收不到,谢谢帮忙修改下// VirtualHostProxy.cpp 
//
// Virtual Host Proxy Server
// (c) Franck JEANNIN 1999 - [email protected]
//
// Simulate multiple Web servers on one single Windows 95/98 machine 
//
// Does not support HTTP 1.1 (HTTP 1.0 only)
// Use high level MFC socket classes: CSocket
//#include "stdafx.h"
#include <afxsock.h>        // MFC socket extensions
#include <conio.h>#define BUFFERSIZE 10000
#define SERVERNAMEMAXSIZE 256#define PROXYPORT 5060#define SERVERADDRESS "127.0.0.1"
#define SERVERPORT 80char Buffer[BUFFERSIZE+SERVERNAMEMAXSIZE];int main(int argc, char* argv[])
{
CSocket Proxy;
CSocket Client;
CSocket Server; // initialize MFC
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
printf("Fatal Error: MFC initialization failed\nPress any key\n");
getch();
return -1;
} // initialize Socket support
if (!AfxSocketInit())
{
printf("Fatal Error: Socket initialization failed\nPress any key\n");
getch();
return -1;
}

// create Proxy socket
if (!Proxy.Create(PROXYPORT))
{
printf("Fatal Error: Proxy socket creation failed\nError %d\n\nPress any key\n",GetLastError());
getch();
return -1;
}

if (!Proxy.Listen(1))
{
printf("Fatal Error: Proxy socket activation failed\nError %d\n\nPress any key\n",GetLastError());
getch();
return -1;
}
else
{
printf("Virtual Host Proxy Server started on port %d\n(c) Franck JEANNIN 1999\n\n",PROXYPORT);
} while(1)
{
if (!Proxy.Accept(Client))
{
printf("Fatal Error: Client connection to Proxy failed\nError %d\n\nPress any key\n",GetLastError());
getch();
return -1;
} int n = Client.Receive(Buffer,BUFFERSIZE);

if (n == -1)
{
printf("Fatal Error: Client transmission to Proxy failed\nError %d\n\nPress any key\n",GetLastError());
getch();
return -1;
} Buffer[n] = 0;
printf("Received from client: %s\n",Buffer); // calls to http://www.a.com/mypage.htm are transformed to calls to http://127.0.0.1/www.a.com/mypage.htm
char* p = strstr(Buffer,"//"); if (p)
{
p += 2;
memmove(p+sizeof(SERVERADDRESS),p,n);
memcpy(p,SERVERADDRESS,sizeof(SERVERADDRESS)-1);
*(p+sizeof(SERVERADDRESS)-1) = '/';
n += sizeof(SERVERADDRESS);
} if (!Server.Create())
{
printf("Fatal Error: Server socket creation failed\nError %d\n\nPress any key\n",GetLastError());
getch();
return -1;
}

if (!Server.Connect(SERVERADDRESS,SERVERPORT))
{
printf("Fatal Error: Server socket connection to HTTP server failed\nError %d\n\nPress any key\n",GetLastError());
getch();
return -1;
}

n = Server.Send(Buffer,n); if (n == -1)
{
printf("Fatal Error: Proxy transmission to Server failed\nError %d\n\nPress any key\n",GetLastError());
getch();
return -1;
} Buffer[n] = 0;
printf("Sent to server: %s\n",Buffer); while ( (n = Server.Receive(Buffer,BUFFERSIZE)) != 0)
{ if (n == -1)
{
printf("Fatal Error: Server transmission to Proxy failed\nError %d\n\nPress any key\n",GetLastError());
getch();
return -1;
} Buffer[n] = 0;
printf("Received from server: %s\n",Buffer); n = Client.Send(Buffer,n); if (n == -1)
{
printf("Fatal Error: Proxy transmission to Server failed\nError %d\n\nPress any key\n",GetLastError());
getch();
return -1;
} Buffer[n] = 0;
printf("Sent to Client: %s\n",Buffer);
} printf("Closing Client & Server sockets\n");
Client.Close();
Server.Close();
} return 0;
}

解决方案 »

  1.   

    这代码明显不是socks5代理的代码,Simulate multiple Web servers on one single Windows 95/98 machine ,自己看这是什么意思吧.从网上下载回来的代码也没搞清楚是做啥的?
      

  2.   

    bool CProxyDlg::ConnectByProxyWay(CAsyncSocket *pClientSocket, int nConnectWay, CString &strProxyIP, int nProxyPort, CString strUserName, CString strPwd)
    {
    if (NULL == pClientSocket)
    return false;
    if (INVALID_SOCKET == pClientSocket->m_hSocket) 
    return false;

    if (1 > nConnectWay || nConnectWay > 2)
    return false;

    //去掉sockt4的代理 lxm 07-12-26
    //if (0 == nConnectWay) 
    //{  
    // //发送请求
    // struct   Sock4Req1   sProxyReq;   
    // memset(&sProxyReq, NULL, sizeof(Sock4Req1));
    // sProxyReq.VN =   4;   
    // sProxyReq.CD =   1;   
    // sProxyReq.Port =   ntohs( nProxyPort);   
    // sProxyReq.IPAddr =   inet_addr( (LPCTSTR)strProxyIP);   
    // pClientSocket->Send(&sProxyReq, sizeof(Sock4Req1));   
    // //接收回应包
    // char buff[100]={NULL};
    // pClientSocket->Receive(buff,100);   
    // TRACE("%s\n", buff);
    // struct   Sock4Ans1   *psProxyAns = NULL;   
    // psProxyAns   =   (struct   Sock4Ans1   *)buff; 
    // if(psProxyAns->VN   !=   0   ||   psProxyAns->CD   !=   90)   
    // return false;
    // return true;
    //}
    if (1 == nConnectWay)
    {
    //发送
    struct   Sock5Req1   sProxyReq1;   
    memset(&sProxyReq1, NULL, sizeof(Sock5Req1));
    sProxyReq1.Ver =   5;   
    sProxyReq1.nMethods =   2;   
    sProxyReq1.Methods[0]   =   0;   
    sProxyReq1.Methods[1]   =   2;   
    pClientSocket->Send( &sProxyReq1, sizeof(Sock5Req1));   
    //接收并判断
    char   buff[600]={NULL};   
    pClientSocket->Receive(buff,600);   
    struct   Sock5Ans1   *psProxyAns1;   
    psProxyAns1   =   (struct   Sock5Ans1   *)buff;  
    if(psProxyAns1->Ver   !=   5   ||   (psProxyAns1->Method!=0   &&   psProxyAns1->Method!=2))   
    return   false;   

    //需要验证,发用户名,密码数据包
    if(psProxyAns1->Method   ==   2)   
    {   
      int   nUserLen   =   strUserName.GetLength();;   
    int   nPassLen   =   strPwd.GetLength();   

    char pSendBuf[600]={NULL};
    pSendBuf[0]=1;   
    pSendBuf[1]=nUserLen;   
    memcpy(pSendBuf+2,  (LPCTSTR)strUserName,nUserLen);   
    pSendBuf[2+nUserLen]=nPassLen;   
    memcpy(pSendBuf+3+nUserLen,(LPCTSTR)strPwd,nPassLen);    int nSendLen = 3+nUserLen+nPassLen;
    pClientSocket->Send(pSendBuf,  3+nUserLen+nPassLen);  

    char buff[600]={NULL};
    pClientSocket->Receive(buff,600);  
    struct   AuthAns   *psAuthAns = NULL;   
    psAuthAns   =   (struct   AuthAns   *)buff;   
    if(psAuthAns->Ver   !=   1   ||   psAuthAns->Status   !=   0)   
    {   
    CString strError   =   _T("代理服务器用户验证不成功!");   
    return   false;   
    }   
    }   

    //第二次验证
    struct   Sock5Req2   sProxyReq2;   
    memset(&sProxyReq2, NULL, sizeof(Sock5Req2) );
      sProxyReq2.Ver   =   5;   
    sProxyReq2.Cmd   =   1;   
    sProxyReq2.Rsv   =   0;   
    sProxyReq2.Atyp  =   1;   
    sProxyReq2.IPAddr =   inet_addr((LPCTSTR)strProxyIP);   
    sProxyReq2.Port =   ntohs(nProxyPort);   
    pClientSocket->Send( &sProxyReq2,sizeof(struct   Sock5Req2));   

    //接收并验证
    memset(buff,0,600);   
    pClientSocket->Receive(buff,600);   
    struct   Sock5Ans2   *psProxyAns2 = NULL;   
    psProxyAns2   =   (struct   Sock5Ans2   *)buff;   

    if(psProxyAns2->Ver   !=   5   ||   psProxyAns2->Rep   !=   0)   
    return   false;   
    return true;
    }
    else
    {
    char   buff[600] = {NULL};   
    sprintf(   buff,   "%s%s:%d%s","CONNECT   ", (LPCTSTR)strProxyIP, nProxyPort, "   HTTP/1.1\r\nUser-Agent:   MyApp/0.1\r\n\r\n");   
    pClientSocket->Send(buff,strlen(buff));   //发送请求   
    char pRcvBuf[1024]={NULL};
    pClientSocket->Receive(pRcvBuf,600);   
    TRACE(pRcvBuf);
    //if(strstr(pRcvBuf,   "HTTP/1.0 200 Connection established")   ==   NULL )   //连接不成功   
    // return   false;     
    if( NULL != strstr(pRcvBuf,   "HTTP/1.0 200") || NULL != strstr(pRcvBuf,   "HTTP/1.1 200") || 
    NULL != strstr(pRcvBuf,   "http/1.0 200") || NULL != strstr(pRcvBuf,   "http/1.1 200"))   
    return   true;   //连接成功   
    return false;
    }
    }