Example #1
0
        /// <summary>
        /// Deserializes a new instance of the SocksRequest class from the
        /// specified byte array.
        /// </summary>
        /// <param name="buffer">An array of bytes to deserialize a new instance
        /// of the SocksRequest class from.</param>
        /// <returns>An initialized instance of the SocksRequest class.</returns>
        /// <exception cref="ArgumentNullException">The buffer parameter is
        /// null.</exception>
        /// <exception cref="SerializationException">The specified buffer does
        /// not contain a valid SOCKS5 request message.</exception>
        public static SocksRequest Deserialize(byte[] buffer)
        {
            using (var ms = new MemoryStream(buffer)) {
                using (BinaryReader r = new BinaryReader(ms)) {
                    if (r.ReadByte() != version)
                    {
                        throw new SerializationException("Invalid SOCKS5 request.");
                    }
                    SocksCommand command = (SocksCommand)r.ReadByte();
                    // Skip reserved octet.
                    r.ReadByte();
                    ATyp      atyp   = (ATyp)r.ReadByte();
                    IPAddress addr   = null;
                    string    domain = null;
                    switch (atyp)
                    {
                    case ATyp.IPv4:
                    case ATyp.IPv6:
                        addr = new IPAddress(r.ReadBytes(atyp == ATyp.IPv4 ? 4 : 16));
                        break;

                    case ATyp.Domain:
                        byte len = r.ReadByte();
                        domain = Encoding.ASCII.GetString(r.ReadBytes(len));
                        break;
                    }
                    ushort port = r.ReadUInt16(true);
                    if (atyp == ATyp.Domain)
                    {
                        return(new SocksRequest(command, domain, port));
                    }
                    return(new SocksRequest(command, addr, port));
                }
            }
        }
Example #2
0
        public static Int32 MakeRequest(Byte[] packet, SocksCommand command, IPAddress ip, Int32 port)
        {
            Byte[] address = ip.GetAddressBytes();

            packet[0] = 0x05;          // Version 5
            packet[1] = (Byte)command;
            packet[2] = 0;             // Reserved

            Int32 offset;

            if (address.Length == 4)
            {
                packet[3] = (Byte)SocksAddressType.IPv4;
                packet[4] = address[0];
                packet[5] = address[0];
                packet[6] = address[0];
                packet[7] = address[0];
                offset    = 8;
            }
            else if (address.Length == 16)
            {
                packet[3] = (Byte)SocksAddressType.IPv6;
                Array.Copy(address, 0, packet, 4, 16);
                offset = 20;
            }
            else
            {
                throw new InvalidOperationException(String.Format("IPAddress byte length is {0} bytes but only 4 and 16 are supported", address.Length));
            }

            packet[offset++] = (Byte)(port >> 8);
            packet[offset++] = (Byte)(port);

            return(offset);
        }
Example #3
0
 public SocksRequest(SocksCommand command, IPAddress destination, ushort port)
 {
     destination.ThrowIfNull <IPAddress>("destination");
     this.Command     = command;
     this.ATyp        = (destination.AddressFamily == AddressFamily.InterNetworkV6) ? S22.Xmpp.Extensions.Socks5.ATyp.IPv6 : S22.Xmpp.Extensions.Socks5.ATyp.IPv4;
     this.Destination = destination;
     this.Port        = port;
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the SocksRequest class.
 /// </summary>
 /// <param name="command">The command of the request.</param>
 /// <param name="destination">The IP address of the destination host.</param>
 /// <param name="port">The port of the destination host.</param>
 /// <exception cref="ArgumentNullException">The destination parameter
 /// is null.</exception>
 public SocksRequest(SocksCommand command, IPAddress destination,
                     ushort port)
 {
     Command = command;
     ATyp    = destination.AddressFamily == AddressFamily.InterNetworkV6 ?
               ATyp.IPv6 : ATyp.IPv4;
     Destination = destination;
     Port        = port;
 }
Example #5
0
 public SocksRequest(SocksCommand command, string domain, ushort port)
 {
     domain.ThrowIfNull <string>("domain");
     if (domain.Length > 0xff)
     {
         throw new ArgumentException("The length of the domain string must not exceed 255 characters.");
     }
     this.Command     = command;
     this.ATyp        = S22.Xmpp.Extensions.Socks5.ATyp.Domain;
     this.Destination = domain;
     this.Port        = port;
 }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the SocksRequest class.
 /// </summary>
 /// <param name="command">The command of the request.</param>
 /// <param name="domain">The fully-qualified domain name of the
 /// destination host.</param>
 /// <param name="port">The port of the destination host.</param>
 /// <exception cref="ArgumentNullException">The domain parameter
 /// is null.</exception>
 /// <exception cref="ArgumentException">The length of the domain string
 /// exceeds 255 characters.</exception>
 public SocksRequest(SocksCommand command, string domain, ushort port)
 {
     if (domain.Length > 255)
     {
         throw new ArgumentException("The length of the domain string must " +
                                     "not exceed 255 characters.");
     }
     Command     = command;
     ATyp        = ATyp.Domain;
     Destination = domain;
     Port        = port;
 }
Example #7
0
        public static Int32 MakeRequest(Byte[] packet, SocksCommand command, Byte[] domainName, Int32 port)
        {
            packet[0] = 0x05;          // Version 5
            packet[1] = (Byte)command;
            packet[2] = 0;             // Reserved

            packet[3] = (Byte)SocksAddressType.DomainName;
            packet[4] = (Byte)domainName.Length;

            Int32 offset;
            Array.Copy(domainName, 0, packet, 5, domainName.Length);
            offset = 5 + domainName.Length;

            packet[offset++] = (Byte)(port >> 8);
            packet[offset++] = (Byte)(port);

            return offset;
        }
Example #8
0
        public static Int32 MakeRequest(Byte[] packet, SocksCommand command, String asciiHostName, Int32 port)
        {
            packet[0] = 0x05;          // Version 5
            packet[1] = (Byte)command;
            packet[2] = 0;             // Reserved

            packet[3] = (Byte)SocksAddressType.DomainName;
            packet[4] = (Byte)asciiHostName.Length;

            Int32 offset = 5;
            for (int i = 0; i < asciiHostName.Length; i++)
            {
                packet[offset++] = (Byte)asciiHostName[i];
            }

            packet[offset++] = (Byte)(port >> 8);
            packet[offset++] = (Byte)(port);

            return offset;
        }
Example #9
0
        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);
        }
Example #10
0
		/// <summary>
		/// Initializes a new instance of the SocksRequest class.
		/// </summary>
		/// <param name="command">The command of the request.</param>
		/// <param name="domain">The fully-qualified domain name of the
		/// destination host.</param>
		/// <param name="port">The port of the destination host.</param>
		/// <exception cref="ArgumentNullException">The domain parameter
		/// is null.</exception>
		/// <exception cref="ArgumentException">The length of the domain string
		/// exceeds 255 characters.</exception>
		public SocksRequest(SocksCommand command, string domain, ushort port) {
			domain.ThrowIfNull("domain");
			if (domain.Length > 255) {
				throw new ArgumentException("The length of the domain string must " +
					"not exceed 255 characters.");
			}
			Command = command;
			ATyp = ATyp.Domain;
			Destination = domain;
			Port = port;
		}
Example #11
0
		/// <summary>
		/// Initializes a new instance of the SocksRequest class.
		/// </summary>
		/// <param name="command">The command of the request.</param>
		/// <param name="destination">The IP address of the destination host.</param>
		/// <param name="port">The port of the destination host.</param>
		/// <exception cref="ArgumentNullException">The destination parameter
		/// is null.</exception>
		public SocksRequest(SocksCommand command, IPAddress destination,
			ushort port) {
			destination.ThrowIfNull("destination");
			Command = command;
			ATyp = destination.AddressFamily == AddressFamily.InterNetworkV6 ?
				ATyp.IPv6 : ATyp.IPv4;
			Destination = destination;
			Port = port;
		}
 /// <summary>
 /// Performs the specified SOCKS5 request.
 /// </summary>
 /// <param name="command">The command of the SOCKS5 request.</param>
 /// <param name="address">The IP address of the remote host.</param>
 /// <param name="port">The port of the remote host.</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(SocksCommand command, IPAddress address, ushort port)
 {
     return(Request(new SocksRequest(command, address, port)));
 }
 public Socks5ConnectionRequest(SocksCommand command, IPAddress destination, int port)
     : base(destination, port)
 {
     Command = command;
 }
Example #14
0
 /// <summary>
 /// Performs the specified SOCKS5 request.
 /// </summary>
 /// <param name="command">The command of the SOCKS5 request.</param>
 /// <param name="domain">The domain of the remote host.</param>
 /// <param name="port">The port of the remote host.</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 async Task <SocksReply> Request(SocksCommand command, string domain, ushort port)
 {
     return(await Request(new SocksRequest(command, domain, port)));
 }
Example #15
0
 /// <summary>
 /// Performs the specified SOCKS5 request.
 /// </summary>
 /// <param name="command">The command of the SOCKS5 request.</param>
 /// <param name="address">The IP address of the remote host.</param>
 /// <param name="port">The port of the remote host.</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 async Task <SocksReply> Request(SocksCommand command, IPAddress address, ushort port)
 {
     return(await Request(new SocksRequest(command, address, port)));
 }
Example #16
0
 /// <summary>
 /// Performs the specified SOCKS5 request.
 /// </summary>
 /// <param name="command">The command of the SOCKS5 request.</param>
 /// <param name="address">The IP address of the remote host.</param>
 /// <param name="port">The port of the remote host.</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(SocksCommand command, IPAddress address, ushort port)
 {
     return Request(new SocksRequest(command, address, port));
 }
Example #17
0
 /// <summary>
 /// Performs the specified SOCKS5 request.
 /// </summary>
 /// <param name="command">The command of the SOCKS5 request.</param>
 /// <param name="domain">The domain of the remote host.</param>
 /// <param name="port">The port of the remote host.</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(SocksCommand command, string domain, ushort port)
 {
     return Request(new SocksRequest(command, domain, port));
 }
 public Socks5ConnectionRequest(SocksCommand command, string domain, int port = 80)
     : base(domain, port)
 {
     Command = command;
 }
 /// <summary>
 /// Performs the specified SOCKS5 request.
 /// </summary>
 /// <param name="command">The command of the SOCKS5 request.</param>
 /// <param name="domain">The domain of the remote host.</param>
 /// <param name="port">The port of the remote host.</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(SocksCommand command, string domain, ushort port)
 {
     return(Request(new SocksRequest(command, domain, port)));
 }
 public override void DeserializeHeader(IList<byte> serialized)
 {
     Command = (SocksCommand) serialized[1];
     base.DeserializeHeader(serialized);
 }