Class Socks4ProxyClient
- Namespace
- VpnHood.Net.Proxies.Socks4ProxyClients
- Assembly
- VpnHood.Net.Proxies.dll
Opens TCP tunnels through a SOCKS4 proxy, or a SOCKS4a proxy for host names. It supports CONNECT only, and IPv4 destinations only.
public class Socks4ProxyClient : IProxyClient
- Inheritance
-
Socks4ProxyClient
- Implements
- Inherited Members
Examples
var client = new Socks4ProxyClient(new Socks4ProxyClientOptions {
ProxyEndPoint = proxyEndPoint,
UserName = null // the SOCKS4 user ID, when the proxy wants one
});
using var tcp = new TcpClient();
await client.ConnectAsync(tcp, destinationHost, destinationPort, CancellationToken.None); // a host name uses SOCKS4a
var stream = tcp.GetStream(); // the tunnel
Remarks
An instance keeps no per-connection state, so it can serve many connections at once.
Constructors
Socks4ProxyClient(Socks4ProxyClientOptions)
Opens TCP tunnels through a SOCKS4 proxy, or a SOCKS4a proxy for host names. It supports CONNECT only, and IPv4 destinations only.
public Socks4ProxyClient(Socks4ProxyClientOptions options)
Parameters
optionsSocks4ProxyClientOptionsThe proxy's address and the user ID to send.
Examples
var client = new Socks4ProxyClient(new Socks4ProxyClientOptions {
ProxyEndPoint = proxyEndPoint,
UserName = null // the SOCKS4 user ID, when the proxy wants one
});
using var tcp = new TcpClient();
await client.ConnectAsync(tcp, destinationHost, destinationPort, CancellationToken.None); // a host name uses SOCKS4a
var stream = tcp.GetStream(); // the tunnel
Remarks
An instance keeps no per-connection state, so it can serve many connections at once.
Properties
ProxyEndPoint
Gets the address and port of the proxy server.
public IPEndPoint ProxyEndPoint { get; }
Property Value
Methods
CheckConnectionAsync(TcpClient, CancellationToken)
Opens a TCP connection to the proxy. SOCKS4 has no request that checks a proxy without opening a tunnel, so this checks only that the proxy accepts connections.
public Task CheckConnectionAsync(TcpClient tcpClient, CancellationToken cancellationToken)
Parameters
tcpClientTcpClientA connection that is not connected yet. When the check fails, it is closed.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation. It completes when the connection is open.
Exceptions
- ArgumentNullException
tcpClientis null.- SocketException
The connection to the proxy failed.
- OperationCanceledException
cancellationTokenwas canceled.
ConnectAsync(TcpClient, IPEndPoint, CancellationToken)
Opens a tunnel through the proxy to an IPv4 endpoint.
public Task ConnectAsync(TcpClient tcpClient, IPEndPoint destination, CancellationToken cancellationToken)
Parameters
tcpClientTcpClientThe connection to use. When it is not connected, it is connected to ProxyEndPoint first. When the task completes, its GetStream() carries the tunnel; when the exchange with the proxy fails, it is closed.
destinationIPEndPointThe destination's IPv4 address and port.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation. It completes when the tunnel is open.
Exceptions
- ArgumentNullException
tcpClientordestinationis null.- ArgumentOutOfRangeException
The port of
destinationis zero.- ProxyClientException
destinationis IPv6, which SOCKS4 cannot carry; the proxy's answer is not SOCKS4; or the proxy refused the request. SocketErrorCode tells which.- SocketException
The connection to the proxy failed.
- EndOfStreamException
The proxy closed the connection before its answer was complete.
- OperationCanceledException
cancellationTokenwas canceled.
ConnectAsync(TcpClient, string, int, CancellationToken)
Opens a tunnel through the proxy to a host, given by name or IPv4 address.
public Task ConnectAsync(TcpClient tcpClient, string host, int port, CancellationToken cancellationToken)
Parameters
tcpClientTcpClientThe connection to use. When it is not connected, it is connected to ProxyEndPoint first. When the task completes, its GetStream() carries the tunnel; when the exchange with the proxy fails, it is closed.
hoststringThe destination's host name or IPv4 address literal. A name is sent for the proxy to resolve, as SOCKS4a does; a proxy that speaks only SOCKS4 refuses it.
portintThe destination's port, from 1 to 65535.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation. It completes when the tunnel is open.
Exceptions
- ArgumentNullException
tcpClientorhostis null.- ArgumentException
hostis empty or white space.- ArgumentOutOfRangeException
portis less than 1 or greater than 65535.- ProxyClientException
hostis an IPv6 literal, which SOCKS4 cannot carry; the proxy's answer is not SOCKS4; or the proxy refused the request. SocketErrorCode tells which.- SocketException
The connection to the proxy failed.
- EndOfStreamException
The proxy closed the connection before its answer was complete.
- OperationCanceledException
cancellationTokenwas canceled.