Class Socks5ProxyServer
- Namespace
- VpnHood.Net.Proxies.Socks5ProxyServers
- Assembly
- VpnHood.Net.Proxies.dll
Represents a SOCKS5 proxy server (RFC 1928) that serves CONNECT and UDP ASSOCIATE requests, with optional username and password authentication (RFC 1929).
public sealed class Socks5ProxyServer : TcpProxyServerBase, IDisposable
- Inheritance
-
Socks5ProxyServer
- Implements
- Inherited Members
Examples
using var server = new Socks5ProxyServer(new Socks5ProxyServerOptions {
ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0), // 0: the system picks a free port
Username = null, // set a user name to require RFC 1929 authentication
Password = null
});
server.Start(); // or: await server.RunAsync(cancellationToken);
var proxyEndPoint = server.ListenerEndPoint;
Remarks
The server resolves a domain name in a request itself, and prefers an IPv4 address; a name it cannot look up gets HostUnreachable. It answers BIND, and any other command, with CommandNotSupported, and an address type it does not know with AddressTypeNotSupported.
A UDP association relays datagrams between the client and the destinations it sends to, until the request's TCP connection closes; what arrives on that connection meanwhile is discarded. The client's datagrams are taken from the UDP endpoint it declared, where an unspecified address stands for its TCP connection's; when it declared none (port zero), the first datagram from its address claims the association. Replies are taken from the destinations the client has sent to. Datagrams from any other sender, and fragmented ones, are dropped. The relay socket takes the address family of the TCP connection, so an association made over IPv4 reaches only IPv4 destinations. An association remembers up to 2048 destinations; past that, it forgets those silent for 60 seconds, then the least recently active, and drops what a forgotten destination sends.
Constructors
Socks5ProxyServer(Socks5ProxyServerOptions, ILogger<Socks5ProxyServer>?)
Represents a SOCKS5 proxy server (RFC 1928) that serves CONNECT and UDP ASSOCIATE requests, with optional username and password authentication (RFC 1929).
public Socks5ProxyServer(Socks5ProxyServerOptions options, ILogger<Socks5ProxyServer>? logger = null)
Parameters
optionsSocks5ProxyServerOptionsThe server's settings.
loggerILogger<Socks5ProxyServer>The logger for the server's events, or null for none.
Examples
using var server = new Socks5ProxyServer(new Socks5ProxyServerOptions {
ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0), // 0: the system picks a free port
Username = null, // set a user name to require RFC 1929 authentication
Password = null
});
server.Start(); // or: await server.RunAsync(cancellationToken);
var proxyEndPoint = server.ListenerEndPoint;
Remarks
The server resolves a domain name in a request itself, and prefers an IPv4 address; a name it cannot look up gets HostUnreachable. It answers BIND, and any other command, with CommandNotSupported, and an address type it does not know with AddressTypeNotSupported.
A UDP association relays datagrams between the client and the destinations it sends to, until the request's TCP connection closes; what arrives on that connection meanwhile is discarded. The client's datagrams are taken from the UDP endpoint it declared, where an unspecified address stands for its TCP connection's; when it declared none (port zero), the first datagram from its address claims the association. Replies are taken from the destinations the client has sent to. Datagrams from any other sender, and fragmented ones, are dropped. The relay socket takes the address family of the TCP connection, so an association made over IPv4 reaches only IPv4 destinations. An association remembers up to 2048 destinations; past that, it forgets those silent for 60 seconds, then the least recently active, and drops what a forgotten destination sends.
Exceptions
- ArgumentNullException
ListenEndPoint is null.
- ArgumentOutOfRangeException
Backlog or MaxConnections is zero or negative.
Methods
HandleClientAsync(TcpClient, CancellationToken)
Serves one client connection.
protected override Task HandleClientAsync(TcpClient client, CancellationToken cancellationToken)
Parameters
clientTcpClientThe accepted connection.
cancellationTokenCancellationTokenThe token that is canceled when the server stops. An exception that a callback registered on it throws is logged, and the stop goes on.
Returns
- Task
A task that represents the asynchronous operation. It completes when the connection is done.
Remarks
It runs on its own task for each accepted connection. When the returned task completes, the base class
disposes client, and it logs any exception the task throws.
Exceptions
- ArgumentNullException
ListenEndPoint is null.
- ArgumentOutOfRangeException
Backlog or MaxConnections is zero or negative.