Skip to content

Commit 27fad71

Browse files
jjvacaJordi VacaRob-Hague
authored
On SOCKS5 proxy: set hostname, not always IP (#1072)
* On SOCKS5 proxy: The library always resolves the hostname to the IP. Some SOCKS5 proxies don't allow you to specify the IP, they want to resolve the hostname themselves. The patch sends the proxy the same specified value, IP, or hostname. * refactor --------- Co-authored-by: Jordi Vaca <jjvaca@indra.es> Co-authored-by: Rob Hague <rob.hague00@gmail.com>
1 parent 6933e09 commit 27fad71

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/Renci.SshNet/Connection/Socks5Connector.cs

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Net;
34
using System.Net.Sockets;
5+
using System.Text;
46

57
using Renci.SshNet.Abstractions;
68
using Renci.SshNet.Common;
@@ -244,25 +246,29 @@ private static byte[] CreateSocks5ConnectionRequest(string hostname, ushort port
244246

245247
private static byte[] GetSocks5DestinationAddress(string hostname, out byte addressType)
246248
{
247-
var ip = Dns.GetHostAddresses(hostname)[0];
249+
if (IPAddress.TryParse(hostname, out var ipAddress))
250+
{
251+
Debug.Assert(ipAddress.AddressFamily is AddressFamily.InterNetwork or AddressFamily.InterNetworkV6);
252+
253+
addressType = ipAddress.AddressFamily == AddressFamily.InterNetwork
254+
? (byte)0x01 // IPv4
255+
: (byte)0x04; // IPv6
256+
257+
return ipAddress.GetAddressBytes();
258+
}
259+
260+
addressType = 0x03; // Domain name
248261

249-
byte[] address;
262+
var byteCount = Encoding.UTF8.GetByteCount(hostname);
250263

251-
#pragma warning disable IDE0010 // Add missing cases
252-
switch (ip.AddressFamily)
264+
if (byteCount > byte.MaxValue)
253265
{
254-
case AddressFamily.InterNetwork:
255-
addressType = 0x01; // IPv4
256-
address = ip.GetAddressBytes();
257-
break;
258-
case AddressFamily.InterNetworkV6:
259-
addressType = 0x04; // IPv6
260-
address = ip.GetAddressBytes();
261-
break;
262-
default:
263-
throw new ProxyException(string.Format("SOCKS5: IP address '{0}' is not supported.", ip));
266+
throw new ProxyException(string.Format("SOCKS5: SOCKS 5 cannot support host names longer than 255 chars ('{0}').", hostname));
264267
}
265-
#pragma warning restore IDE0010 // Add missing cases
268+
269+
var address = new byte[1 + byteCount];
270+
address[0] = (byte)byteCount;
271+
_ = Encoding.UTF8.GetBytes(hostname, 0, hostname.Length, address, 1);
266272

267273
return address;
268274
}

0 commit comments

Comments
 (0)