Socks5 2.0.0

dotnet add package Socks5 --version 2.0.0
                    
NuGet\Install-Package Socks5 -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Socks5" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Socks5" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Socks5" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Socks5 --version 2.0.0
                    
#r "nuget: Socks5, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Socks5@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Socks5&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Socks5&version=2.0.0
                    
Install as a Cake Tool

Socks5

NuGet

SOCKS5 (RFC 1928, RFC 1929) for .NET: allocation-free message codecs, and client and server handshakes over a Stream or IDuplexPipe you already have.

Usage

Client

using System.Net.Security;
using System.Net.Sockets;
using Socks5;

using TcpClient tcp = new();
await tcp.ConnectAsync("proxy.example", 1080, cancellationToken);
NetworkStream stream = tcp.GetStream();

await Socks5Client.ConnectAsync(stream, new Socks5Address("example.com", 443), new Socks5Credential("user", "password"), cancellationToken);

await using SslStream tls = new(stream);
await tls.AuthenticateAsClientAsync(new SslClientAuthenticationOptions { TargetHost = "example.com" }, cancellationToken);

Server

using TcpClient client = await listener.AcceptTcpClientAsync(cancellationToken);
NetworkStream stream = client.GetStream();
Socks5ServerRequest request = await Socks5Server.ReadRequestAsync(stream, new Socks5ServerOptions
{
    Authenticator = (credential, _) => ValueTask.FromResult(credential == new Socks5Credential("user", "password")),
}, cancellationToken);

if (request.Command is not Socks5Command.Connect)
{
    await request.ReplyAsync(Socks5Reply.CommandNotSupported, cancellationToken: cancellationToken);
    return;
}

using Socket target = new(SocketType.Stream, ProtocolType.Tcp);

try
{
    await target.ConnectAsync(request.Destination.ToEndPoint(), cancellationToken);
}
catch (Exception e) when (e is not OperationCanceledException)
{
    await request.ReplyAsync(Socks5Reply.FromException(e), cancellationToken: cancellationToken);
    return;
}

await request.ReplyAsync(Socks5Reply.Succeeded, new Socks5Address(target.LocalEndPoint!), cancellationToken);
await using NetworkStream upstream = new(target);
await Task.WhenAll(RelayAsync(stream, upstream), RelayAsync(upstream, stream));

// Passes on the end of each direction, which a peer may wait for.
async Task RelayAsync(NetworkStream from, NetworkStream to)
{
    await from.CopyToAsync(to, cancellationToken);
    to.Socket.Shutdown(SocketShutdown.Send);
}

UDP

// The association lasts as long as this connection.
using TcpClient control = new();
await control.ConnectAsync("proxy.example", 1080, cancellationToken);
Socks5Address relay = await Socks5Client.UdpAssociateAsync(control.GetStream(), cancellationToken: cancellationToken);

// 0.0.0.0 or :: stands for the address of the proxy.
EndPoint relayEndPoint = relay.ToEndPoint() switch
{
    IPEndPoint ip when ip.Address.Equals(IPAddress.Any) || ip.Address.Equals(IPAddress.IPv6Any) => new IPEndPoint(((IPEndPoint)control.Client.RemoteEndPoint!).Address, ip.Port),
    EndPoint endPoint => endPoint,
};

using Socket udp = new(SocketType.Dgram, ProtocolType.Udp);
await udp.ConnectAsync(relayEndPoint, cancellationToken);

// The header goes in front of the payload.
byte[] datagram = new byte[ushort.MaxValue];
int headerLength = new Socks5UdpHeader(new Socks5Address("stun.example", 3478)).Encode(datagram);
payload.CopyTo(datagram, headerLength);
await udp.SendAsync(datagram.AsMemory(0, headerLength + payload.Length), cancellationToken);

int received = await udp.ReceiveAsync(datagram, cancellationToken);

if (Socks5UdpHeader.Decode(datagram.AsMemory(0, received), out Socks5UdpHeader header, out int bytesConsumed) is OperationStatus.Done && header.Fragment is 0)
{
    // header.Address is the sender.
    ReadOnlyMemory<byte> response = datagram.AsMemory(bytesConsumed, received - bytesConsumed);
}

Codecs

Each message is a struct with Encode, TryEncode, Decode (an OperationStatus) and GetRequiredLength (how far to read without reading past the message). Decoded domain names, methods, user names and passwords reference the source rather than a copy, so it must stay unchanged while they are in use.

Type Message
Socks5Greeting Methods that the client offers
Socks5MethodSelection Method that the server selects
Socks5Credential User name and password (RFC 1929)
Socks5PasswordResponse Authentication status
Socks5Request Command and destination
Socks5Response Reply and bound address
Socks5UdpHeader Header of a UDP datagram
Socks5Address Address and port, also to and from EndPoint and SocketAddress
// Reads one request from a stream, and nothing past it.
byte[] buffer = new byte[Socks5Request.MaxEncodedLength];
int length = 0;

for (int required = Socks5Request.GetRequiredLength([]); required > length; required = Socks5Request.GetRequiredLength(buffer.AsSpan(0, length)))
{
    await stream.ReadExactlyAsync(buffer.AsMemory(length, required - length), cancellationToken);
    length = required;
}

OperationStatus status = Socks5Request.Decode(buffer.AsMemory(0, length), out Socks5Request request, out _);

Not supported

  • GSSAPI authentication.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Socks5:

Package Downloads
Stun.Net

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Socks5:

Repository Stars
HMBSbige/NatTypeTester
测试当前网络的 NAT 类型(STUN)
Version Downloads Last Updated
2.0.0 51 12/2/2026
2.0.0-alpha.1 124 4/13/2026
1.1.0 32,175 10/4/2025
1.0.2 21,406 9/14/2023
1.0.1 1,093 6/16/2023
1.0.0 17,361 11/9/2021
0.9.9 4,036 9/20/2021
0.9.8 1,130 9/1/2021
0.9.7 1,586 8/27/2021
0.9.6 818 8/25/2021
0.9.5 827 8/22/2021
0.9.4 1,171 8/13/2021
0.9.3 873 8/8/2021
0.9.2 798 8/6/2021
0.9.1 785 8/4/2021
0.9.1-alpha 510 8/3/2021