public void Reply(SocksReply reply)
 {
     reply.ThrowIfNull <SocksReply>("reply");
     this.AssertValid();
     byte[] buffer = reply.Serialize();
     this.stream.Write(buffer, 0, buffer.Length);
 }
Exemple #2
0
        /// <summary>
        /// Performs the specified SOCKS5 request.
        /// </summary>
        /// <param name="request">The SOCKS5 request to issue to the server.</param>
        /// <returns>The SOCKS5 reply sent by the server.</returns>
        /// <exception cref="ArgumentNullException">The request parameter is
        /// null.</exception>
        /// <exception cref="ObjectDisposedException">The object has been
        /// disposed.</exception>
        /// <exception cref="Socks5Exception">The request could not be performed.
        /// Consult the InnerException property of the Socks5Exception to learn
        /// the reason.</exception>
        public SocksReply Request(SocksRequest request)
        {
            request.ThrowIfNull("request");
            AssertValid();
            try {
                byte[] bytes = request.Serialize();
                stream.Write(bytes, 0, bytes.Length);
                ByteBuilder b = new ByteBuilder();
                using (var r = new BinaryReader(stream, Encoding.UTF8, true)) {
                    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));
                }
                return(SocksReply.Deserialize(b.ToArray()));
            } catch (Exception e) {
                throw new Socks5Exception("The request could not be performed.", e);
            }
        }
Exemple #3
0
        /// <summary>
        /// Sends the specified SOCKS5 reply to the connected client.
        /// </summary>
        /// <param name="reply">The SOCKS5 reply to send to the connected
        /// client.</param>
        /// <exception cref="ArgumentNullException">The reply parameter is
        /// null.</exception>
        /// <exception cref="ObjectDisposedException">The object has been
        /// disposed.</exception>
        public void Reply(SocksReply reply)
        {
            reply.ThrowIfNull("reply");
            AssertValid();
            var bytes = reply.Serialize();

            stream.Write(bytes, 0, bytes.Length);
        }
Exemple #4
0
        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 SocksReply Request(SocksRequest request)
        {
            SocksReply reply;

            request.ThrowIfNull <SocksRequest>("request");
            this.AssertValid();
            try
            {
                byte[] buffer = request.Serialize();
                this.stream.Write(buffer, 0, buffer.Length);
                ByteBuilder  builder = new ByteBuilder();
                BinaryReader reader  = new BinaryReader(this.stream, Encoding.UTF8);
                buffer = reader.ReadBytes(4);
                builder.Append(buffer);
                ATyp typ = (ATyp)buffer[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));
                reply = SocksReply.Deserialize(builder.ToArray());
            }
            catch (Exception exception)
            {
                CommonConfig.Logger.WriteError("The request could not be performed.", exception);
                throw new Socks5Exception("The request could not be performed.", exception);
            }
            return(reply);
        }
Exemple #6
0
		/// <summary>
		/// Sends the specified SOCKS5 reply to the connected client.
		/// </summary>
		/// <param name="reply">The SOCKS5 reply to send to the connected
		/// client.</param>
		/// <exception cref="ArgumentNullException">The reply parameter is
		/// null.</exception>
		/// <exception cref="ObjectDisposedException">The object has been
		/// disposed.</exception>
		public void Reply(SocksReply reply) {
			reply.ThrowIfNull("reply");
			AssertValid();
			var bytes = reply.Serialize();
			stream.Write(bytes, 0, bytes.Length);
		}