|
1 | 1 | using System;
|
| 2 | +using System.Diagnostics; |
2 | 3 | using System.Net;
|
3 | 4 | using System.Net.Sockets;
|
| 5 | +using System.Text; |
4 | 6 |
|
5 | 7 | using Renci.SshNet.Abstractions;
|
6 | 8 | using Renci.SshNet.Common;
|
@@ -244,25 +246,29 @@ private static byte[] CreateSocks5ConnectionRequest(string hostname, ushort port
|
244 | 246 |
|
245 | 247 | private static byte[] GetSocks5DestinationAddress(string hostname, out byte addressType)
|
246 | 248 | {
|
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 |
248 | 261 |
|
249 |
| - byte[] address; |
| 262 | + var byteCount = Encoding.UTF8.GetByteCount(hostname); |
250 | 263 |
|
251 |
| -#pragma warning disable IDE0010 // Add missing cases |
252 |
| - switch (ip.AddressFamily) |
| 264 | + if (byteCount > byte.MaxValue) |
253 | 265 | {
|
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)); |
264 | 267 | }
|
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); |
266 | 272 |
|
267 | 273 | return address;
|
268 | 274 | }
|
|
0 commit comments