【发布时间】:2012-09-25 10:27:27
【问题描述】:
我正在尝试使用带有 Stacksoft.net 库的 socks 5 协议通过 tor 获取页面:
ProxyClientFactory cf = new ProxyClientFactory();
var p = cf.CreateProxyClient(ProxyType.Socks5, "127.0.0.1", 9051, "", "");
var c = p.CreateConnection("www.google.com",80);
当我运行它时,我得到以下异常:
Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.
Inner exception:An established connection was aborted by the software in your host machine
当程序尝试从 google.com stream.Read(response, 0, response.Length); 读取响应时抛出异常:
private void SendCommand(byte command, string destinationHost, int destinationPort)
{
NetworkStream stream = _tcpClient.GetStream();
byte addressType = GetDestAddressType(destinationHost);
byte[] destAddr = GetDestAddressBytes(addressType, destinationHost);
byte[] destPort = GetDestPortBytes(destinationPort);
// The connection request is made up of 6 bytes plus the
// length of the variable address byte array
//
// +----+-----+-------+------+----------+----------+
// |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
// +----+-----+-------+------+----------+----------+
// | 1 | 1 | X'00' | 1 | Variable | 2 |
// +----+-----+-------+------+----------+----------+
//
// * VER protocol version: X'05'
// * CMD
// * CONNECT X'01'
// * BIND X'02'
// * UDP ASSOCIATE X'03'
// * RSV RESERVED
// * ATYP address itemType of following address
// * IP V4 address: X'01'
// * DOMAINNAME: X'03'
// * IP V6 address: X'04'
// * DST.ADDR desired destination address
// * DST.PORT desired destination port in network octet order
byte[] request = new byte[4 + destAddr.Length + 2];
request[0] = SOCKS5_VERSION_NUMBER;
request[1] = command;
request[2] = SOCKS5_RESERVED;
request[3] = addressType;
destAddr.CopyTo(request, 4);
destPort.CopyTo(request, 4 + destAddr.Length);
// send connect request.
stream.Write(request, 0, request.Length);
// PROXY SERVER RESPONSE
// +----+-----+-------+------+----------+----------+
// |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
// +----+-----+-------+------+----------+----------+
// | 1 | 1 | X'00' | 1 | Variable | 2 |
// +----+-----+-------+------+----------+----------+
//
// * VER protocol version: X'05'
// * REP Reply field:
// * X'00' succeeded
// * X'01' general SOCKS server failure
// * X'02' connection not allowed by ruleset
// * X'03' Network unreachable
// * X'04' Host unreachable
// * X'05' Connection refused
// * X'06' TTL expired
// * X'07' Command not supported
// * X'08' Address itemType not supported
// * X'09' to X'FF' unassigned
//* RSV RESERVED
//* ATYP address itemType of following address
byte[] response = new byte[255];
// read proxy server response
stream.Read(response, 0, response.Length);
我做错了什么?
【问题讨论】:
-
我目前正在使用完全相同的 socks5 库和 Tor,将继续参考这篇文章并发布我的发现,因为我得到了一个可行的解决方案;我认为你的问题的根源不是你的代码而是 Tor,它默认使用 socks4a 来接受连接,所以如果你想使用 socks5,你需要在配置文件中指出,以便 Tor 运行一个 socks5 接受套接字(我认为,将验证):)
-
听起来不错!告诉我你发现了什么
-
看起来他们假设读取调用将获取 255 个字节,这是一个愚蠢的假设,很可能是它中断的原因..
-
@paulm read 将读取 1 到 255 之间的任意字节数
-
是的,所以代码中的假设是错误的,可能为什么高延迟会破坏它? “ProxySocket”似乎是一个更好的库