Example #1
0
        /// <summary>
        /// Waits until the connected client sends a SOCKS5 request.
        /// </summary>
        /// <returns>The SOCKS5 request sent by the client.</returns>
        /// <exception cref="Socks5Exception">The data sent by the client
        /// is not a valid SOCKS5 request.</exception>
        SocksRequest WaitForRequest()
        {
            ByteBuilder b = new ByteBuilder();

            using (var r = new BinaryReader(stream, Encoding.UTF8, true)) {
                byte[] bytes = r.ReadBytes(4);
                b.Append(bytes);
                ATyp atyp = (ATyp)bytes[3];
                switch (atyp)
                {
                case ATyp.IPv4:
                case ATyp.IPv6:
                    b.Append(r.ReadBytes(atyp == ATyp.IPv4 ? 4 : 16));
                    break;

                case ATyp.Domain:
                    byte length = r.ReadByte();
                    b.Append(length).Append(r.ReadBytes(length));
                    break;
                }
                b.Append(r.ReadBytes(2));
            }
            try {
                return(SocksRequest.Deserialize(b.ToArray()));
            } catch (Exception e) {
                throw new Socks5Exception("The request could not be serialized.", e);
            }
        }
Example #2
0
        private SocksRequest WaitForRequest()
        {
            SocksRequest request;
            ByteBuilder  builder = new ByteBuilder();

            using (BinaryReader reader = new BinaryReader(this.stream, Encoding.UTF8))
            {
                byte[] values = reader.ReadBytes(4);
                builder.Append(values);
                ATyp typ = (ATyp)values[3];
                switch (typ)
                {
                case ATyp.IPv4:
                case ATyp.IPv6:
                    builder.Append(reader.ReadBytes((typ == ATyp.IPv4) ? 4 : 0x10));
                    break;

                case ATyp.Domain:
                {
                    byte count = reader.ReadByte();
                    builder.Append(new byte[] { count }).Append(reader.ReadBytes(count));
                    break;
                }
                }
                builder.Append(reader.ReadBytes(2));
            }
            try
            {
                request = SocksRequest.Deserialize(builder.ToArray());
            }
            catch (Exception exception)
            {
                throw new Socks5Exception("The request could not be serialized.", exception);
            }
            return(request);
        }