public static SocksRequest RequestTunnel(SocksClient client, SocksEncryption ph)
{
byte[] data;
int recv = Receive(client.Client, out data);
byte[] buff = ph.ProcessInputData(data, 0, recv);
if (buff == null || (HeaderTypes)buff[0] != HeaderTypes.Socks5) return null;
switch ((StreamTypes)buff[1])
{
case StreamTypes.Stream:
{
int fwd = 4;
StringBuilder address = new StringBuilder();
switch ((AddressType)buff[3])
{
case AddressType.IP:
{
for (int i = 4; i < 8; i++)
{
//grab IP.
address.Append(Convert.ToInt32(buff[i]).ToString() + (i != 7 ? "." : ""));
}
fwd += 4;
}
break;
case AddressType.Domain:
{
int domainlen = Convert.ToInt32(buff[4]);
address.Append(Encoding.ASCII.GetString(buff, 5, domainlen));
fwd += domainlen + 1;
}
break;
case AddressType.IPv6:
//can't handle IPV6 traffic just yet.
return null;
}
byte[] po = new byte[2];
Array.Copy(buff, fwd, po, 0, 2);
Int16 x = BitConverter.ToInt16(po, 0);
int port = Convert.ToInt32(IPAddress.NetworkToHostOrder(x));
port = (port < 1 ? port + 65536 : port);
return new SocksRequest(StreamTypes.Stream, (AddressType)buff[3], address.ToString(), port);
}
default:
//not supported.
return null;
}
}