public static SocksReply Deserialize(byte[] buffer) { SocksReply reply; buffer.ThrowIfNull <byte[]>("buffer"); using (MemoryStream stream = new MemoryStream(buffer)) { using (BinaryReader reader = new BinaryReader(stream)) { if (reader.ReadByte() != 5) { throw new SerializationException("Invalid SOCKS5 reply."); } ReplyStatus status = (ReplyStatus)reader.ReadByte(); reader.ReadByte(); S22.Xmpp.Extensions.Socks5.ATyp typ = (S22.Xmpp.Extensions.Socks5.ATyp)reader.ReadByte(); IPAddress address = null; string domain = null; switch (typ) { case S22.Xmpp.Extensions.Socks5.ATyp.IPv4: case S22.Xmpp.Extensions.Socks5.ATyp.IPv6: address = new IPAddress(reader.ReadBytes((typ == S22.Xmpp.Extensions.Socks5.ATyp.IPv4) ? 4 : 0x10)); break; case S22.Xmpp.Extensions.Socks5.ATyp.Domain: { byte count = reader.ReadByte(); domain = Encoding.ASCII.GetString(reader.ReadBytes(count)); break; } } ushort port = reader.ReadUInt16(true); if (typ == S22.Xmpp.Extensions.Socks5.ATyp.Domain) { return(new SocksReply(status, domain, port)); } reply = new SocksReply(status, address, port); } } return(reply); }
public static SocksRequest Deserialize(byte[] buffer) { SocksRequest request; using (MemoryStream stream = new MemoryStream(buffer)) { using (BinaryReader reader = new BinaryReader(stream)) { if (reader.ReadByte() != 5) { throw new SerializationException("Invalid SOCKS5 request."); } SocksCommand command = (SocksCommand)reader.ReadByte(); reader.ReadByte(); S22.Xmpp.Extensions.Socks5.ATyp typ = (S22.Xmpp.Extensions.Socks5.ATyp)reader.ReadByte(); IPAddress destination = null; string domain = null; switch (typ) { case S22.Xmpp.Extensions.Socks5.ATyp.IPv4: case S22.Xmpp.Extensions.Socks5.ATyp.IPv6: destination = new IPAddress(reader.ReadBytes((typ == S22.Xmpp.Extensions.Socks5.ATyp.IPv4) ? 4 : 0x10)); break; case S22.Xmpp.Extensions.Socks5.ATyp.Domain: { byte count = reader.ReadByte(); domain = Encoding.ASCII.GetString(reader.ReadBytes(count)); break; } } ushort port = reader.ReadUInt16(true); if (typ == S22.Xmpp.Extensions.Socks5.ATyp.Domain) { return(new SocksRequest(command, domain, port)); } request = new SocksRequest(command, destination, port); } } return(request); }