int socksProxy(SOCKET s, const struct sockaddr *name, int namelen)
{
int rc = 0;
//连接代理服务器
SOCKADDR_IN* addr =(SOCKADDR_IN*)name;
DWORD GameServerPort;
char GameServerIP[128];
GameServerPort = ntohs(addr->sin_port);
strcpy(GameServerIP,inet_ntoa(addr->sin_addr));
sockaddr_in serveraddr;
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.S_un.S_addr = inet_addr(lpmycurmsg->DailiIP); //代理服务器地址,从无忧代理网获取的,质量还行,不过只能用几天,发现连不上的话重新获取吧
serveraddr.sin_port = htons(lpmycurmsg->Port); // 端口号
WSABUF DataBuf;
char buffer[4];
memset(buffer, 0, sizeof(buffer));
DataBuf.len = 4;
DataBuf.buf = buffer;
int err = 0;
if((rc = NextProcTable.lpWSPConnect(s, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr), &DataBuf, NULL, NULL, NULL, &err)) != 0)
{
PutDbgStr("Error %d : attempting to connect to SOCKS server!", err);
return rc;
}
//发送请求来协商版本和认证方法
//VER NMETHODS METHODS
//1 1 1 to 255
struct timeval timeout ; //后面的代码就是连接代理服务器的代码
fd_set r;
FD_ZERO(&r);
FD_SET(s, &r);
timeout.tv_sec = 5;
timeout.tv_usec =0;
rc = select(0, 0, &r, 0, &timeout);
if (rc < 0)
{
PutDbgStr("Select Error!");
return rc;
}
int nTimeout = 5000;
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *)&nTimeout, sizeof(int)); //设置
char buf[512];
memset(buf, 0, sizeof(buf));
struct TSock5req1 *proxyreq1;
proxyreq1 = (struct TSock5req1 *)buf;
proxyreq1->Ver = 5;
proxyreq1->nMethods = 2;
proxyreq1->Methods[0] = 0;
proxyreq1->Methods[1] = 2;
if((rc = send(s, buf, 4,0)) < 0)
{
PutDbgStr("Error %d : attempting to send SOCKS method negotiation!", WSAGetLastError());
return rc;
}
//接收代理服务器返回信息
struct TSock5ans1 *proxyans1;
proxyans1 = (struct TSock5ans1 *)buf;
memset(buf, 0, sizeof(buf));
FD_ZERO(&r);
FD_SET(s, &r);
timeout.tv_sec = 10;
timeout.tv_usec =0;
rc = select(0, &r, 0, 0, &timeout);
if (rc < 0)
{
PutDbgStr("Select Error!");
return rc;
}
rc = recv(s, buf, sizeof(buf),0);
if(proxyans1->Ver != 5 || (proxyans1->Method != 0 && proxyans1->Method != 2))
{
PutDbgStr("版本不支持!");
closesocket(s);
return rc;
}
if(proxyans1->Method == 2)
{
int nUserLen = strlen(lpmycurmsg->User); //代理服务器的账号密码
int nPassLen = strlen(lpmycurmsg->Pass);
buf[0] = 1;
buf[1] = nUserLen;
memcpy(buf + 2, lpmycurmsg->User, nUserLen);
buf[2 + nUserLen] = nPassLen;
memcpy(buf + 3 + nUserLen, lpmycurmsg->Pass, nPassLen);
send(s, buf, 3 + nUserLen + nPassLen,0);
struct TAuthans *authans;
authans = (struct TAuthans *)buf;
memset(buf, 0, sizeof(buf));
FD_ZERO(&r);
FD_SET(s, &r);
timeout.tv_sec = 5;
timeout.tv_usec =0;
rc = select(0, 0, &r, 0, &timeout);
if (rc < 0)
{
PutDbgStr("Select Error!");
return rc;
}
rc = recv(s, buf, sizeof(buf),0);
if(authans->Ver != 1 || authans->Status != 0)
{
PutDbgStr("设置账号密码错误!");
return rc;
}
}
memset(buf, 0, sizeof(buf));
struct TSock5req2 *proxyreq2;
proxyreq2 = (struct TSock5req2 *)buf;
proxyreq2->Ver = 5;
proxyreq2->Cmd = 1;
proxyreq2->Rsv = 0;
proxyreq2->Atyp = 1;
unsigned long tmpLong = inet_addr(GameServerIP); //转发的游戏IP
unsigned short port1 = ntohs(GameServerPort);
memcpy((char*)&proxyreq2->other, &tmpLong, 4);
memcpy((char*)(&proxyreq2->other) + 4, &port1, 2);
send(s, buf, 10,0);
struct TSock5ans2 *proxyans2;
memset(buf ,0, sizeof(buf));
proxyans2 = (struct TSock5ans2 *)buf;
FD_ZERO(&r);
FD_SET(s, &r);
timeout.tv_sec = 5;
timeout.tv_usec =0;
rc = select(0, &r, 0, 0, &timeout);
if (rc < 0)
{
PutDbgStr("Select Error!");
return rc;
}
rc = recv(s, buf, sizeof(buf),0);
if(proxyans2->Ver != 5 || proxyans2->Rep != 0)
{
PutDbgStr("创建联接失败!");
return rc;
}
return 0;
}
代码如上:
首先这个代码有问题吗?为什么每次都是输出”版本不支持“
麻烦大牛些给我看看啊。哪里可以找到这方面的资料啊。
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)