Esempio n. 1
0
        /// <summary>
        /// Starts negotiating with the SOCKS server.
        /// </summary>
        /// <param name="connect">The bytes to send when trying to authenticate.</param>
        /// <exception cref="ArgumentNullException"><c>connect</c> is null.</exception>
        /// <exception cref="ArgumentException"><c>connect</c> is too small.</exception>
        /// <exception cref="SocksProxyException">The proxy rejected the request.</exception>
        /// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception>
        /// <exception cref="ObjectDisposedException">The Socket has been closed.</exception>
        /// <exception cref="ProtocolViolationException">The proxy server uses an invalid protocol.</exception>
        private void Negotiate(byte[] connect)
        {
            Authenticate();
            Server.Send(connect);
            var buffer = ReadBytes(4);

            if (buffer[1] != 0)
            {
                Server.Close();
                throw SocksProxyException.FromSocks5Error(buffer[1]);
            }
            switch (buffer[3])
            {
            case 1:
                ReadBytes(6);     //IPv4 address with port
                break;

            case 3:
                buffer = ReadBytes(1);
                ReadBytes(buffer[0] + 2);     //domain name with port
                break;

            case 4:
                ReadBytes(18);     //IPv6 address with port
                break;

            default:
                Server.Close();
                throw new ProtocolViolationException();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Starts negotiating with the SOCKS server.
        /// </summary>
        /// <param name="connect">The bytes to send when trying to authenticate.</param>
        /// <exception cref="ArgumentNullException"><c>connect</c> is null.</exception>
        /// <exception cref="ArgumentException"><c>connect</c> is too small.</exception>
        /// <exception cref="SocksProxyException">The proxy rejected the request.</exception>
        /// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception>
        /// <exception cref="ObjectDisposedException">The Socket has been closed.</exception>
        private void Negotiate(byte [] connect)
        {
            if (connect == null)
            {
                throw new ArgumentNullException();
            }
            if (connect.Length < 2)
            {
                throw new ArgumentException();
            }

            Server.Send(connect);

            var buffer = ReadBytes(8);

            if (buffer[1] == 90)
            {
                return;
            }

            Server.Close();
            throw SocksProxyException.FromSocks4Error(buffer[1]);
        }