Skip to content

Commit

Permalink
Fixed protocol error when connecting to socks5 without password
Browse files Browse the repository at this point in the history
  • Loading branch information
wj8400684 committed Dec 18, 2024
1 parent e0fd4ad commit 8345d24
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 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

0 comments on commit 8345d24

Please sign in to comment.