C# need help debugging socks5-connection attemp

Posted by Chuck on Stack Overflow See other posts from Stack Overflow or by Chuck
Published on 2010-04-01T20:29:50Z Indexed on 2010/04/01 20:33 UTC
Read the original article Hit count: 440

Filed under:
|

Hi, I've written the following code to (successfully) connect to a socks5 proxy.

I send a user/pw auth and get an OK reply (0x00), but as soon as I tell the proxy to connect to whichever ip:port, it gives me 0x01 (general error).

Socket socket5_proxy = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint proxyEndPoint = new IPEndPoint(IPAddress.Parse("111.111.111.111"), 1080); // proxy ip, port. fake for posting purposes.
socket5_proxy.Connect(proxyEndPoint);
byte[] init_socks_command = new byte[4];
init_socks_command[0] = 0x05;
init_socks_command[1] = 0x02;
init_socks_command[2] = 0x00;
init_socks_command[3] = 0x02;
socket5_proxy.Send(init_socks_command);
byte[] socket_response = new byte[2];
int bytes_recieved = socket5_proxy.Receive(socket_response, 2, SocketFlags.None);

if (socket_response[1] == 0x02)
{
    byte[] temp_bytes;
    string socks5_user = "foo";
    string socks5_pass = "bar";
    byte[] auth_socks_command = new byte[3 + socks5_user.Length + socks5_pass.Length];
    auth_socks_command[0] = 0x05;
    auth_socks_command[1] = Convert.ToByte(socks5_user.Length);
    temp_bytes = Encoding.Default.GetBytes(socks5_user);
    temp_bytes.CopyTo(auth_socks_command, 2);
    auth_socks_command[2 + socks5_user.Length] = Convert.ToByte(socks5_pass.Length);
    temp_bytes = Encoding.Default.GetBytes(socks5_pass);
    temp_bytes.CopyTo(auth_socks_command, 3 + socks5_user.Length);
    socket5_proxy.Send(auth_socks_command);
    socket5_proxy.Receive(socket_response, 2, SocketFlags.None);
    if (socket_response[1] != 0x00)
        return;
    byte[] connect_socks_command = new byte[10];
    connect_socks_command[0] = 0x05;
    connect_socks_command[1] = 0x02; // streaming
    connect_socks_command[2] = 0x00;
    connect_socks_command[3] = 0x01; // ipv4
    temp_bytes = IPAddress.Parse("222.222.222.222").GetAddressBytes(); // target connection. fake ip, obviously
    temp_bytes.CopyTo(connect_socks_command, 4);
    byte[] portBytes = BitConverter.GetBytes(8888);
    connect_socks_command[8] = portBytes[0];
    connect_socks_command[9] = portBytes[1];
    socket5_proxy.Send(connect_socks_command);
    socket5_proxy.Receive(socket_response);
    if (socket_response[1] != 0x00)
        MessageBox.Show("Damn it"); // I always end here, 0x01

I've used this as a reference: http://en.wikipedia.org/wiki/SOCKS#SOCKS_5

Have I completely misunderstood something here? How I see it, I can connect to the socks5 fine. I can authenticate fine. But I/the proxy can't "do" anything?

  1. Yes, I know the proxy works.
  2. Yes, the target ip is available and yes the target port is open/responsive.
  3. I get 0x01 no matter what I try to connect to.

Any help is VERY MUCH appreciated!

Thanks,

  • Chuck

© Stack Overflow or respective owner

Related posts about c#

Related posts about sockets