我正在尝试使用带有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);
我究竟做错了什么?