在SSH.NET中代理后解析DNS地址

时间:2018-11-29 14:20:26

标签: c# networking proxy dns ssh.net

我在C#中使用 SSH.Net ,并且在SOCKS5代理后面的连接期间,出现以下异常:

System.Net.Sockets.SocketException (0x80004005): No such host is known
   at System.Net.Dns.GetAddrInfo(String name)
   at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
   at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
   at Renci.SshNet.Session.ConnectSocks5()
   at Renci.SshNet.Session.Connect()
   at Renci.SshNet.BaseClient.Connect()

从同一代理后面的同一台计算机到同一服务器的连接与 WinScp 无缝连接,并且在站点设置中选择了在代理端进行DNS名称查询选项:

enter image description here

是否可以告诉SSH.Net进行相同的操作?

1 个答案:

答案 0 :(得分:1)

似乎不可能。

SSH.NET始终为doing DNS resolving locally

var ip = DnsAbstraction.GetHostAddresses(ConnectionInfo.Host)[0];

//  Send address type and address
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
    SocketWriteByte(0x01);
    var address = ip.GetAddressBytes();
    SocketAbstraction.Send(_socket, address);
}
else if (ip.AddressFamily == AddressFamily.InterNetworkV6)
{
    SocketWriteByte(0x04);
    var address = ip.GetAddressBytes();
    SocketAbstraction.Send(_socket, address);
}
else
{
    throw new ProxyException(string.Format("SOCKS5: IP address '{0}' is not supported.", ip));
}

如果您愿意修改SSH.NET代码,则需要将代码替换为(未测试):

// Send address type and address

SocketWriteByte(0x03); // Resolve by proxy

var host = SshData.Ascii.GetBytes(ConnectionInfo.Host);

SocketWriteByte((byte)host.Length);

SocketAbstraction.Send(_socket, host);

等效于this PuTTY/WinSCP code

assert(type == ADDRTYPE_NAME);
command[3] = 3;
sk_getaddr(p->remote_addr, command+5, 256);
command[4] = strlen(command+5);
len = 7 + command[4];  /* 4 hdr, 1 len, N addr, 2 trailer */