Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed protocol error when connecting to socks5 without password #771

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading