Skip to content

Commit

Permalink
Fixed protocol error when connecting to socks5 without password (#771)
Browse files Browse the repository at this point in the history
* Fixed protocol error when connecting to socks5 without password

* The prefix length of the sending EndPoint should be 6
  • Loading branch information
wj8400684 authored Dec 18, 2024
1 parent e0fd4ad commit e86ace9
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/SuperSocket.Client.Proxy/Socks5Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,40 @@ public class Socks5Connector : ProxyConnectorBase

private string _password;

readonly static byte[] _authenHandshakeRequest = new byte[] { 0x05, 0x02, 0x00, 0x02 };
private readonly byte[] _authenHandshakeRequest;
private static readonly byte[] Handshake = new byte[] { 0x05, 0x01, 0x00 };
private static readonly byte[] AuthenHandshake = new byte[] { 0x05, 0x02, 0x00, 0x02 };

public Socks5Connector(EndPoint proxyEndPoint)
: base(proxyEndPoint)
{

_authenHandshakeRequest = Handshake;
}

public Socks5Connector(EndPoint proxyEndPoint, string username, string password)
: this(proxyEndPoint)
{
_username = username;
_password = password;
_authenHandshakeRequest = AuthenHandshake;
}

protected override async ValueTask<ConnectState> ConnectProxyAsync(EndPoint remoteEndPoint, ConnectState state, CancellationToken cancellationToken)
{
var connection = state.CreateConnection(new ConnectionOptions { ReadAsDemand = true });

var packStream = connection.GetPackageStream(new Socks5AuthPipelineFilter());
var pipeLineFilter = new Socks5AuthPipelineFilter();

if (string.IsNullOrWhiteSpace(_username) || string.IsNullOrWhiteSpace(_password))
{
pipeLineFilter.AuthStep = 1;
}
else
{
pipeLineFilter.AuthStep = 0;
}

var packStream = connection.GetPackageStream(pipeLineFilter);

await connection.SendAsync(_authenHandshakeRequest);

Expand Down Expand Up @@ -233,7 +247,7 @@ private byte[] GetEndPointBytes(EndPoint remoteEndPoint)

port = endPoint.Port;

var maxLen = 7 + Encoding.ASCII.GetMaxByteCount(endPoint.Host.Length);
var maxLen = 6 + Encoding.ASCII.GetMaxByteCount(endPoint.Host.Length);
buffer = new byte[maxLen];

buffer[3] = 0x03;
Expand Down

0 comments on commit e86ace9

Please sign in to comment.