# < Summary

Information

Class: | Renci.SshNet.Connection.Socks5Connector
Assembly: | Renci.SshNet
File(s): | \home\appveyor\projects\ssh-net\src\Renci.SshNet\Connection\Socks5Connector.cs

Line coverage
84%

Covered lines: | 119
Uncovered lines: | 22
Coverable lines: | 141
Total lines: | 269
Line coverage: | 84.3%

Branch coverage
50%

Covered branches: | 19
Total branches: | 38
Branch coverage: | 50%

Method coverage

Feature is only available for sponsors

Upgrade to PRO version

# Metrics

Method | Branch coverage | Cyclomatic complexity | Line coverage
--- | --- | --- | ---
.ctor(...) | 100% | 1 | 100%
HandleProxyConnect(...) | 46.66% | 30 | 73.13%
CreateSocks5UserNameAndPasswordAuthenticationRequest(...) | 100% | 4 | 100%
CreateSocks5ConnectionRequest(...) | 100% | 1 | 100%
GetSocks5DestinationAddress(...) | 25% | 4 | 66.66%

# File(s)

## \home\appveyor\projects\ssh-net\src\Renci.SshNet\Connection\Socks5Connector.cs

| # | Line |  | Line coverage
--- | --- | --- | --- | ---
|  | `1` |  | `using System;`
|  | `2` |  | `using System.Net.Sockets;`
|  | `3` |  | ``
|  | `4` |  | `using Renci.SshNet.Abstractions;`
|  | `5` |  | `using Renci.SshNet.Common;`
|  | `6` |  | ``
|  | `7` |  | `namespace Renci.SshNet.Connection`
|  | `8` |  | `{`
|  | `9` |  | `/// <summary>`
|  | `10` |  | `/// Establishes a tunnel via a SOCKS5 proxy server.`
|  | `11` |  | `/// </summary>`
|  | `12` |  | `/// <remarks>`
|  | `13` |  | `/// https://en.wikipedia.org/wiki/SOCKS#SOCKS5.`
|  | `14` |  | `/// </remarks>`
|  | `15` |  | `internal sealed class Socks5Connector : ProxyConnector`
|  | `16` |  | `{`
|  | `17` |  | `public Socks5Connector(ISocketFactory socketFactory)`
| 111 | `18` |  | `: base(socketFactory)`
| 111 | `19` |  | `{`
| 111 | `20` |  | `}`
|  | `21` |  | ``
|  | `22` |  | `/// <summary>`
|  | `23` |  | `/// Establishes a connection to the server via a SOCKS5 proxy.`
|  | `24` |  | `/// </summary>`
|  | `25` |  | `/// <param name="connectionInfo">The connection information.</param>`
|  | `26` |  | `/// <param name="socket">The <see cref="Socket"/>.</param>`
|  | `27` |  | `protected override void HandleProxyConnect(IConnectionInfo connectionInfo, Socket socket)`
| 87 | `28` |  | `{`
| 87 | `29` |  | `var greeting = new byte[]`
| 87 | `30` |  | `{`
| 87 | `31` |  | `// SOCKS version number`
| 87 | `32` |  | `0x05,`
| 87 | `33` |  | ``
| 87 | `34` |  | `// Number of supported authentication methods`
| 87 | `35` |  | `0x02,`
| 87 | `36` |  | ``
| 87 | `37` |  | `// No authentication`
| 87 | `38` |  | `0x00,`
| 87 | `39` |  | ``
| 87 | `40` |  | `// Username/Password authentication`
| 87 | `41` |  | `0x02`
| 87 | `42` |  | `};`
| 87 | `43` |  | `SocketAbstraction.Send(socket, greeting);`
|  | `44` |  | ``
| 87 | `45` |  | `var socksVersion = SocketReadByte(socket);`
| 87 | `46` |  | `if (socksVersion != 0x05)`
| 12 | `47` |  | `{`
| 12 | `48` |  | `throw new ProxyException(string.Format("SOCKS Version '{0}' is not supported.", socksVersion));`
|  | `49` |  | `}`
|  | `50` |  | ``
| 75 | `51` |  | `var authenticationMethod = SocketReadByte(socket);`
| 75 | `52` |  | `switch (authenticationMethod)`
|  | `53` |  | `{`
|  | `54` |  | `case 0x00:`
|  | `55` |  | `// No authentication`
| 15 | `56` |  | `break;`
|  | `57` |  | `case 0x02:`
|  | `58` |  | `// Create username/password authentication request`
| 60 | `59` |  | `var authenticationRequest = CreateSocks5UserNameAndPasswordAuthenticationRequest(connectionInfo.Prox`
|  | `60` |  | ``
|  | `61` |  | `// Send authentication request`
| 30 | `62` |  | `SocketAbstraction.Send(socket, authenticationRequest);`
|  | `63` |  | ``
|  | `64` |  | `// Read authentication result`
| 30 | `65` |  | `var authenticationResult = SocketAbstraction.Read(socket, 2, connectionInfo.Timeout);`
|  | `66` |  | ``
| 30 | `67` |  | `if (authenticationResult[0] != 0x01)`
| 0 | `68` |  | `{`
| 0 | `69` |  | `throw new ProxyException("SOCKS5: Server authentication version is not valid.");`
|  | `70` |  | `}`
|  | `71` |  | ``
| 30 | `72` |  | `if (authenticationResult[1] != 0x00)`
| 15 | `73` |  | `{`
| 15 | `74` |  | `throw new ProxyException("SOCKS5: Username/Password authentication failed.");`
|  | `75` |  | `}`
|  | `76` |  | ``
| 15 | `77` |  | `break;`
|  | `78` |  | `case 0xFF:`
| 0 | `79` |  | `throw new ProxyException("SOCKS5: No acceptable authentication methods were offered.");`
|  | `80` |  | `default:`
| 0 | `81` |  | `throw new ProxyException($"SOCKS5: Chosen authentication method '0x{authenticationMethod:x2}' is not`
|  | `82` |  | `}`
|  | `83` |  | ``
| 30 | `84` |  | `var connectionRequest = CreateSocks5ConnectionRequest(connectionInfo.Host, (ushort) connectionInfo.Port);`
| 30 | `85` |  | `SocketAbstraction.Send(socket, connectionRequest);`
|  | `86` |  | ``
|  | `87` |  | `// Read Server SOCKS5 version`
| 30 | `88` |  | `if (SocketReadByte(socket) != 5)`
| 0 | `89` |  | `{`
| 0 | `90` |  | `throw new ProxyException("SOCKS5: Version 5 is expected.");`
|  | `91` |  | `}`
|  | `92` |  | ``
|  | `93` |  | `// Read response code`
| 30 | `94` |  | `var status = SocketReadByte(socket);`
|  | `95` |  | ``
| 30 | `96` |  | `switch (status)`
|  | `97` |  | `{`
|  | `98` |  | `case 0x00:`
| 30 | `99` |  | `break;`
|  | `100` |  | `case 0x01:`
| 0 | `101` |  | `throw new ProxyException("SOCKS5: General failure.");`
|  | `102` |  | `case 0x02:`
| 0 | `103` |  | `throw new ProxyException("SOCKS5: Connection not allowed by ruleset.");`
|  | `104` |  | `case 0x03:`
| 0 | `105` |  | `throw new ProxyException("SOCKS5: Network unreachable.");`
|  | `106` |  | `case 0x04:`
| 0 | `107` |  | `throw new ProxyException("SOCKS5: Host unreachable.");`
|  | `108` |  | `case 0x05:`
| 0 | `109` |  | `throw new ProxyException("SOCKS5: Connection refused by destination host.");`
|  | `110` |  | `case 0x06:`
| 0 | `111` |  | `throw new ProxyException("SOCKS5: TTL expired.");`
|  | `112` |  | `case 0x07:`
| 0 | `113` |  | `throw new ProxyException("SOCKS5: Command not supported or protocol error.");`
|  | `114` |  | `case 0x08:`
| 0 | `115` |  | `throw new ProxyException("SOCKS5: Address type not supported.");`
|  | `116` |  | `default:`
| 0 | `117` |  | `throw new ProxyException("SOCKS5: Not valid response.");`
|  | `118` |  | `}`
|  | `119` |  | ``
|  | `120` |  | `// Read reserved byte`
| 30 | `121` |  | `if (SocketReadByte(socket) != 0)`
| 0 | `122` |  | `{`
| 0 | `123` |  | `throw new ProxyException("SOCKS5: 0 byte is expected.");`
|  | `124` |  | `}`
|  | `125` |  | ``
| 30 | `126` |  | `var addressType = SocketReadByte(socket);`
| 30 | `127` |  | `switch (addressType)`
|  | `128` |  | `{`
|  | `129` |  | `case 0x01:`
| 15 | `130` |  | `var ipv4 = new byte[4];`
| 15 | `131` |  | `_ = SocketRead(socket, ipv4, 0, 4);`
| 15 | `132` |  | `break;`
|  | `133` |  | `case 0x04:`
| 15 | `134` |  | `var ipv6 = new byte[16];`
| 15 | `135` |  | `_ =SocketRead(socket, ipv6, 0, 16);`
| 15 | `136` |  | `break;`
|  | `137` |  | `default:`
| 0 | `138` |  | `throw new ProxyException(string.Format("Address type '{0}' is not supported.", addressType));`
|  | `139` |  | `}`
|  | `140` |  | ``
| 30 | `141` |  | `var port = new byte[2];`
|  | `142` |  | ``
|  | `143` |  | `// Read 2 bytes to be ignored`
| 30 | `144` |  | `_ = SocketRead(socket, port, 0, 2);`
| 30 | `145` |  | `}`
|  | `146` |  | ``
|  | `147` |  | `/// <summary>`
|  | `148` |  | `/// https://tools.ietf.org/html/rfc1929.`
|  | `149` |  | `/// </summary>`
|  | `150` |  | `private static byte[] CreateSocks5UserNameAndPasswordAuthenticationRequest(string username, string password)`
| 60 | `151` |  | `{`
| 60 | `152` |  | `if (username.Length > byte.MaxValue)`
| 15 | `153` |  | `{`
| 15 | `154` |  | `throw new ProxyException("Proxy username is too long.");`
|  | `155` |  | `}`
|  | `156` |  | ``
| 45 | `157` |  | `if (password.Length > byte.MaxValue)`
| 15 | `158` |  | `{`
| 15 | `159` |  | `throw new ProxyException("Proxy password is too long.");`
|  | `160` |  | `}`
|  | `161` |  | ``
| 30 | `162` |  | `var authenticationRequest = new byte[// Version of the negotiation`
| 30 | `163` |  | `1 +`
| 30 | `164` |  | ``
| 30 | `165` |  | `// Length of the username`
| 30 | `166` |  | `1 +`
| 30 | `167` |  | ``
| 30 | `168` |  | `// Username`
| 30 | `169` |  | `username.Length +`
| 30 | `170` |  | ``
| 30 | `171` |  | `// Length of the password`
| 30 | `172` |  | `1 +`
| 30 | `173` |  | ``
| 30 | `174` |  | `// Password`
| 30 | `175` |  | `password.Length];`
|  | `176` |  | ``
| 30 | `177` |  | `var index = 0;`
|  | `178` |  | ``
|  | `179` |  | `// Version of the negiotiation`
| 30 | `180` |  | `authenticationRequest[index++] = 0x01;`
|  | `181` |  | ``
|  | `182` |  | `// Length of the username`
| 30 | `183` |  | `authenticationRequest[index++] = (byte) username.Length;`
|  | `184` |  | ``
|  | `185` |  | `// Username`
| 30 | `186` |  | `_ = SshData.Ascii.GetBytes(username, 0, username.Length, authenticationRequest, index);`
| 30 | `187` |  | `index += username.Length;`
|  | `188` |  | ``
|  | `189` |  | `// Length of the password`
| 30 | `190` |  | `authenticationRequest[index++] = (byte) password.Length;`
|  | `191` |  | ``
|  | `192` |  | `// Password`
| 30 | `193` |  | `_ =SshData.Ascii.GetBytes(password, 0, password.Length, authenticationRequest, index);`
|  | `194` |  | ``
| 30 | `195` |  | `return authenticationRequest;`
| 30 | `196` |  | `}`
|  | `197` |  | ``
|  | `198` |  | `private static byte[] CreateSocks5ConnectionRequest(string hostname, ushort port)`
| 30 | `199` |  | `{`
| 30 | `200` |  | `var addressBytes = GetSocks5DestinationAddress(hostname, out var addressType);`
|  | `201` |  | ``
| 30 | `202` |  | `var connectionRequest = new byte[// SOCKS version number`
| 30 | `203` |  | `1 +`
| 30 | `204` |  | ``
| 30 | `205` |  | `// Command code`
| 30 | `206` |  | `1 +`
| 30 | `207` |  | ``
| 30 | `208` |  | `// Reserved`
| 30 | `209` |  | `1 +`
| 30 | `210` |  | ``
| 30 | `211` |  | `// Address type`
| 30 | `212` |  | `1 +`
| 30 | `213` |  | ``
| 30 | `214` |  | `// Address`
| 30 | `215` |  | `addressBytes.Length +`
| 30 | `216` |  | ``
| 30 | `217` |  | `// Port number`
| 30 | `218` |  | `2];`
|  | `219` |  | ``
| 30 | `220` |  | `var index = 0;`
|  | `221` |  | ``
|  | `222` |  | `// SOCKS version number`
| 30 | `223` |  | `connectionRequest[index++] = 0x05;`
|  | `224` |  | ``
|  | `225` |  | `// Command code`
| 30 | `226` |  | `connectionRequest[index++] = 0x01; // establish a TCP/IP stream connection`
|  | `227` |  | ``
|  | `228` |  | `// Reserved`
| 30 | `229` |  | `connectionRequest[index++] = 0x00;`
|  | `230` |  | ``
|  | `231` |  | `// Address type`
| 30 | `232` |  | `connectionRequest[index++] = addressType;`
|  | `233` |  | ``
|  | `234` |  | `// Address`
| 30 | `235` |  | `Buffer.BlockCopy(addressBytes, 0, connectionRequest, index, addressBytes.Length);`
| 30 | `236` |  | `index += addressBytes.Length;`
|  | `237` |  | ``
|  | `238` |  | `// Port number`
| 30 | `239` |  | `Pack.UInt16ToBigEndian(port, connectionRequest, index);`
|  | `240` |  | ``
| 30 | `241` |  | `return connectionRequest;`
| 30 | `242` |  | `}`
|  | `243` |  | ``
|  | `244` |  | `private static byte[] GetSocks5DestinationAddress(string hostname, out byte addressType)`
| 30 | `245` |  | `{`
| 30 | `246` |  | `var ip = DnsAbstraction.GetHostAddresses(hostname)[0];`
|  | `247` |  | ``
|  | `248` |  | `byte[] address;`
|  | `249` |  | ``
|  | `250` |  | `#pragma warning disable IDE0010 // Add missing cases`
| 30 | `251` |  | `switch (ip.AddressFamily)`
|  | `252` |  | `{`
|  | `253` |  | `case AddressFamily.InterNetwork:`
| 30 | `254` |  | `addressType = 0x01; // IPv4`
| 30 | `255` |  | `address = ip.GetAddressBytes();`
| 30 | `256` |  | `break;`
|  | `257` |  | `case AddressFamily.InterNetworkV6:`
| 0 | `258` |  | `addressType = 0x04; // IPv6`
| 0 | `259` |  | `address = ip.GetAddressBytes();`
| 0 | `260` |  | `break;`
|  | `261` |  | `default:`
| 0 | `262` |  | `throw new ProxyException(string.Format("SOCKS5: IP address '{0}' is not supported.", ip));`
|  | `263` |  | `}`
|  | `264` |  | `#pragma warning restore IDE0010 // Add missing cases`
|  | `265` |  | ``
| 30 | `266` |  | `return address;`
| 30 | `267` |  | `}`
|  | `268` |  | `}`
|  | `269` |  | `}`

# Methods/Properties

.ctor(Renci.SshNet.Connection.ISocketFactory)
HandleProxyConnect(Renci.SshNet.IConnectionInfo,System.Net.Sockets.Socket)
CreateSocks5UserNameAndPasswordAuthenticationRequest(System.String,System.String)
CreateSocks5ConnectionRequest(System.String,System.UInt16)
GetSocks5DestinationAddress(System.String,System.Byte&)