gpt4 book ai didi

C++ Socks5 通信问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:57 26 4
gpt4 key购买 nike

我仍在尝试通过 SOCKS5 代理连接到服务器。我已经阅读了 RFC1928,现在我正在使用这段代码,但它没有按预期工作。发送域名似乎有问题。当将 addr 位更改为 1 并使用 IP 地址时,它工作得很好。

我使用的代码是这样的:

#define PUT_BYTE(ptr,data) (*(unsigned char*)ptr = data)
struct sockaddr_in saddr;
saddr.sin_port = htons(9150);
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
int rv = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
if (rv < SOCKET_ERROR)
return 1;

char buf[256], *ptr;
ptr = buf;
PUT_BYTE(ptr++, 5); // Version 5
PUT_BYTE(ptr++, 1);
PUT_BYTE(ptr++, 0x00); // No Auth
send(fd, buf, ptr - buf, 0);
recv(fd, buf, 2, 0);
if ((buf[0] != 5) || buf[1] == 0xFF)
{
exit(0);
}
ptr = buf;
PUT_BYTE(ptr++, 5); // Version 5
PUT_BYTE(ptr++, 1); // Connect
PUT_BYTE(ptr++, 0); // Reserved
PUT_BYTE(ptr++, 3); // Use Hostname
//memcpy(ptr, &destaddr.sin_addr.s_addr, sizeof(destaddr.sin_addr));
//ptr += sizeof(destaddr.sin_addr);
//PUT_BYTE(ptr++, static_cast<unsigned char>(21));
PUT_BYTE(ptr++, 22); // Set Hostname lenght
//ptr += static_cast<unsigned char>(22);
//ptr += static_cast<unsigned char>(address.c_str());
//memcpy(ptr, &"iszgnywrejvdg2nc.onion", sizeof("iszgnywrejvdg2nc.onion") + 4);
PUT_BYTE(ptr++, (byte)"iszgnywrejvdg2nc.onion"); // set Hostname
PUT_BYTE(ptr++, 0xFF); //Set Hostname end
PUT_BYTE(ptr++, dest_port >> 8); //set port
PUT_BYTE(ptr++, dest_port & 0xFF); //set port end
send(fd, buf, ptr - buf, 0); //send out data
recv(fd, buf, 4, 0); // read response
if (!buf[1] != 0x00) // check response
{
ptr = buf + 4;
switch (buf[3]) {
case 1:
recv(fd, ptr, 4 + 2, 0);
break;
case 3:
recv(fd, ptr, 1, 0);
recv(fd, ptr + 1, *(unsigned char*)ptr + 2, 0);
break;
case 4:
recv(fd, ptr, 16 + 2, 0);
break;
}
}

这是应该符合上述内容的 RFC

    +----+-----+-------+------+----------+----------+
|VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
+----+-----+-------+------+----------+----------+
| 1 | 1 | X'00' | 1 | Variable | 2 |
+----+-----+-------+------+----------+----------+

Where:

o VER protocol version: X'05'
o CMD
o CONNECT X'01'
o BIND X'02'
o UDP ASSOCIATE X'03'
o RSV RESERVED
o ATYP address type of following address
o IP V4 address: X'01'
o DOMAINNAME: X'03'
o IP V6 address: X'04'
o DST.ADDR desired destination address
o DST.PORT desired destination port in network octet
order

最佳答案

您使用主机名填充 dst.addr 字段的代码是完全错误的。特别是,在这一行:

PUT_BYTE(ptr++, (byte)"iszgnywrejvdg2nc.onion"); // set Hostname

您正在将 char* 指针类型转换为单个字节。您正在存储截断指针值的最后一个字节,您根本没有将主机名数据复制到该字段中。您应该改用 memcpy()strcpy(),类似于您在注释代码中所做的(但也有错误)。

此外,这一行根本不属于,需要删除:

PUT_BYTE(ptr++, 0xFF); //Set Hostname end

尝试更像这样的东西:

char buf[263], *ptr;
...
ptr = buf;
PUT_BYTE(ptr++, 5); // Version 5
PUT_BYTE(ptr++, 1); // Connect
PUT_BYTE(ptr++, 0); // Reserved
PUT_BYTE(ptr++, 3); // Use Hostname
char *hostname = "iszgnywrejvdg2nc.onion";
unsigned char len = (unsigned char) strlen(hostname);
PUT_BYTE(ptr++, len); // Set Hostname length
memcpy(ptr, hostname, len); // set Hostname
ptr += len;
PUT_BYTE(ptr++, dest_port >> 8); //set port
PUT_BYTE(ptr++, dest_port & 0xFF); //set port end

关于C++ Socks5 通信问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com