Table of Contents

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

options Socks4ProxyClientOptions

The 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

IPEndPoint

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

tcpClient TcpClient

A connection that is not connected yet. When the check fails, it is closed.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that represents the asynchronous operation. It completes when the connection is open.

Exceptions

ArgumentNullException

tcpClient is null.

SocketException

The connection to the proxy failed.

OperationCanceledException

cancellationToken was 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

tcpClient TcpClient

The 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.

destination IPEndPoint

The destination's IPv4 address and port.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that represents the asynchronous operation. It completes when the tunnel is open.

Exceptions

ArgumentNullException

tcpClient or destination is null.

ArgumentOutOfRangeException

The port of destination is zero.

ProxyClientException

destination is 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

cancellationToken was 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

tcpClient TcpClient

The 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.

host string

The 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.

port int

The destination's port, from 1 to 65535.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that represents the asynchronous operation. It completes when the tunnel is open.

Exceptions

ArgumentNullException

tcpClient or host is null.

ArgumentException

host is empty or white space.

ArgumentOutOfRangeException

port is less than 1 or greater than 65535.

ProxyClientException

host is 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

cancellationToken was canceled.