public static SocksError SendRequest(Client cli, SocksEncryption enc, string ipOrDomain, int port) { AddressType type; IPAddress ipAddress; if (!IPAddress.TryParse(ipOrDomain, out ipAddress)) //it's a domain. :D (hopefully). type = AddressType.Domain; else type = AddressType.IP; SocksRequest sr = new SocksRequest(StreamTypes.Stream, type, ipOrDomain, port); //send data. byte[] p = sr.GetData(false); p[1] = 0x01; //process data. cli.Send(enc.ProcessOutputData(p, 0, p.Length)); byte[] buffer = new byte[512]; //process input data. int recv = cli.Receive(buffer, 0, buffer.Length); if(recv == -1) { return SocksError.Failure; } byte[] buff = enc.ProcessInputData(buffer, 0, recv); return (SocksError)buff[1]; }
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; } }