求助:关于Socks5客户端的实现

dreaminger 2004-11-10 11:36:15
测试环境:
client:就我自己的机器,ip:192.168.1.180
socks5proxy server:就在我的机器上,监听9811端口。
访问目标: 192.168.1.250:1026

如果不使用代理,直接访问192.168.1.250:1026 ,肯定是成功的,说明目标server没有问题。



如果使用代理:
1. 打开到proxy的tcp socket: 192.168.1.180:9811 成功。

2. 协商版本和认证方式:
VER=5
NMETHODS=1
METHODS=0 //采用非认证方式

返回 VER=5 METHOD=0 也成功了。

3. 请求连接远端主机,数据包如下:
VER=5
CMD=1 //请求连接
RSV=0
ATYP=1 //IPV4
DST.ADDR = 192.168.1.250 //4bytes
DST.PORT = 1026 //2bytes

返回的响应数据包和请求的完全相同,也就是说REP=1,失败了。 RFC1928的解释是“普通的SOCKS服务器请求失败”。

Why?是否socks5proxy server和client不能在相同的机器上? socks5proxy server是否必须要2个IP地址?

感激涕零!!!
...全文
149 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gdy119 2004-11-11
  • 打赏
  • 举报
回复
CUdpSock::CUdpSock()
{
}

CUdpSock::~CUdpSock()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CUdpSock, CSocket)
//{{AFX_MSG_MAP(CUdpSock)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0

/////////////////////////////////////////////////////////////////////////////
// CUdpSock member functions

void CUdpSock::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
/*
msg ReceiveMsg;
memset(&ReceiveMsg,0,sizeof(ReceiveMsg));
AfxMessageBox("hello");
CString strIp;
UINT nPort;
ReceiveFrom(&ReceiveMsg,sizeof(ReceiveMsg),strIp,nPort);


if(ReceiveMsg.flag=='\6')
{

switch(ReceiveMsg.action)
{
case 's':
if(ReceiveMsg.subAction!='s')
{
((CMsgReadApp *)AfxGetApp())->SetMsg(&ReceiveMsg);
}
else
{
((CMsgReadApp *)AfxGetApp())->ShowAudio(ReceiveMsg.szMsg);
}


break;
}
}
*/
AfxMessageBox("hello");
CSocket::OnReceive(nErrorCode);

}

BOOL CUdpSock::Create()
{

if(m_bProxy)
{
Sock.Create();

if( !Sock.Connect( m_ProxyHost,m_nProxyPort) )
{
m_sError = _T("不能连接到代理服务器!");
Sock.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;
Sock.Send(buff,4);
struct sock5ans1 *m_proxyans1;
m_proxyans1 = (struct sock5ans1 *)buff;
memset(buff,0,600);
Sock.Receive(buff,600);
if(m_proxyans1->Ver != 5 || (m_proxyans1->Method!=0 && m_proxyans1->Method!=2))
{
m_sError = _T("通过代理连接主站不成功!");
Sock.Close();
return FALSE;
}
if(m_proxyans1->Method == 2)
{
int nUserLen = strlen(m_ProxyUser);
int nPassLen = strlen(m_ProxyPass);
struct authreq *m_authreq;
m_authreq = (struct authreq *)buff;
m_authreq->Ver = 1;
m_authreq->Ulen = nUserLen;
strcpy(m_authreq->Name,m_ProxyUser);
m_authreq->PLen = nPassLen;
strcpy(m_authreq->Pass,m_ProxyPass);
Sock.Send(buff,513);
struct authans *m_authans;
m_authans = (struct authans *)buff;
memset(buff,0,600);
Sock.Receive(buff,600);
if(m_authans->Ver != 1 || m_authans->Status != 0)
{
m_sError = _T("代理服务器用户验证不成功!");
Sock.Close();
return FALSE;
}
}
struct sock5req2 *m_proxyreq2;
m_proxyreq2 = (struct sock5req2 *)buff;
m_proxyreq2->Ver = 5;
m_proxyreq2->Cmd = 3;
m_proxyreq2->Rsv = 0;
m_proxyreq2->Atyp = 1;
unsigned long tmpLong = 0;
unsigned short port = 0;
memcpy(m_proxyreq2->other,&tmpLong,4);
memcpy(m_proxyreq2->other+4,&port,2);
Sock.Send(buff,sizeof(struct sock5req2)+5);
struct sock5ans2 *m_proxyans2;
memset(buff,0,600);
m_proxyans2 = (struct sock5ans2 *)buff;
Sock.Receive(buff,600);
if(m_proxyans2->Ver != 5 || m_proxyans2->Rep != 0)
{
m_sError = _T("通过代理连接主站不成功!");
Sock.Close();
return FALSE;
}
memcpy(&tmpLong,m_proxyans2->other,4);
memcpy(&port,m_proxyans2->other+4,2);
in_addr a;
a.S_un.S_addr=tmpLong;
m_strProxyBindIp=inet_ntoa(a);
m_nProxyBindPort=htons(port);

}

return CSocket::Create(0,SOCK_DGRAM);

}

BOOL CUdpSock::SetProxyInfo(BOOL bProxy, CString strProxy, UINT nPort, CString strUser, CString strPass)
{
m_bProxy=bProxy;
m_ProxyHost=strProxy;
m_nProxyPort=nPort;
strUser=m_ProxyUser;
strPass=m_ProxyPass;
return TRUE;
}

int CUdpSock::SendTo(const void *lpBuf, int nBufLen, UINT nHostPort, LPCTSTR lpszHostAddress, int nFlags)
{
if(!m_bProxy)
{
return CSocket::SendTo(lpBuf,nBufLen,nHostPort,lpszHostAddress,nFlags);
}
char * buf= new char[nBufLen+sizeof(udpdataheader)+100];
memset(buf,0,nBufLen+sizeof(udpdataheader)+100);
udpdataheader * udpheader;

udpheader = (udpdataheader *)buf;
udpheader->atyp=0x01;
unsigned long tmpIp;
unsigned short tmpPort;
tmpIp=inet_addr(lpszHostAddress);
tmpPort=ntohs(nHostPort);
memcpy(udpheader->other,&tmpIp,4);
memcpy(udpheader->other+4,&tmpPort,2);
memcpy(udpheader->other+4+2,lpBuf,nBufLen);



CSocket::SendTo(udpheader,nBufLen+sizeof(udpdataheader)+100,m_nProxyBindPort,m_strProxyBindIp);

delete [] buf;

}
tangrh 2004-11-11
  • 打赏
  • 举报
回复
port=htons(1026);????
如果上面已经好了,那么估计你socks服务器配置有问题,1是代理服务器内部错误
zzxenjoy 2004-11-11
  • 打赏
  • 举报
回复
ding
oun 2004-11-11
  • 打赏
  • 举报
回复
up

18,357

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧