コード例 #1
0
        /// <summary>
        /// Создаёт соединение с сервером через прокси-сервер.
        /// </summary>
        /// <param name="destinationHost">Хост сервера, с которым нужно связаться через прокси-сервер.</param>
        /// <param name="destinationPort">Порт сервера, с которым нужно связаться через прокси-сервер.</param>
        /// <param name="tcpClient">Соединение, через которое нужно работать, или значение <see langword="null"/>.</param>
        /// <returns>Соединение с сервером через прокси-сервер.</returns>
        /// <exception cref="System.InvalidOperationException">
        /// Значение свойства <see cref="Host"/> равно <see langword="null"/> или имеет нулевую длину.
        /// -или-
        /// Значение свойства <see cref="Port"/> меньше 1 или больше 65535.
        /// -или-
        /// Значение свойства <see cref="Username"/> имеет длину более 255 символов.
        /// -или-
        /// Значение свойства <see cref="Password"/> имеет длину более 255 символов.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">Значение параметра <paramref name="destinationHost"/> равно <see langword="null"/>.</exception>
        /// <exception cref="System.ArgumentException">Значение параметра <paramref name="destinationHost"/> является пустой строкой.</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Значение параметра <paramref name="destinationPort"/> меньше 1 или больше 65535.</exception>
        /// <exception cref="Extreme.Net.Net.ProxyException">Ошибка при работе с прокси-сервером.</exception>
        public override TcpClient CreateConnection(string destinationHost, int destinationPort, TcpClient tcpClient = null)
        {
            CheckState();

            #region Проверка параметров

            if (destinationHost == null)
            {
                throw new ArgumentNullException("destinationHost");
            }

            if (destinationHost.Length == 0)
            {
                throw ExceptionHelper.EmptyString("destinationHost");
            }

            if (!ExceptionHelper.ValidateTcpPort(destinationPort))
            {
                throw ExceptionHelper.WrongTcpPort("destinationPort");
            }

            #endregion

            TcpClient curTcpClient = tcpClient;

            if (curTcpClient == null)
            {
                curTcpClient = CreateConnectionToProxy();
            }

            try
            {
                NetworkStream nStream = curTcpClient.GetStream();

                InitialNegotiation(nStream);
                SendCommand(nStream, CommandConnect, destinationHost, destinationPort);
            }
            catch (Exception ex)
            {
                curTcpClient.Close();

                if (ex is IOException || ex is SocketException)
                {
                    throw NewProxyException(Resources.ProxyException_Error, ex);
                }

                throw;
            }

            return(curTcpClient);
        }
コード例 #2
0
        public override TcpClient CreateConnection(string destinationHost, int destinationPort, TcpClient tcpClient = null)
        {
            CheckState();
            if (destinationHost == null)
            {
                throw new ArgumentNullException("destinationHost");
            }
            if (destinationHost.Length == 0)
            {
                throw ExceptionHelper.EmptyString("destinationHost");
            }
            if (!ExceptionHelper.ValidateTcpPort(destinationPort))
            {
                throw ExceptionHelper.WrongTcpPort("destinationPort");
            }
            TcpClient tcpClient2 = tcpClient;

            if (tcpClient2 == null)
            {
                tcpClient2 = CreateConnectionToProxy();
            }
            if (destinationPort != 80)
            {
                HttpStatusCode httpStatusCode = HttpStatusCode.OK;
                try
                {
                    NetworkStream stream = tcpClient2.GetStream();
                    SendConnectionCommand(stream, destinationHost, destinationPort);
                    httpStatusCode = ReceiveResponse(stream);
                }
                catch (Exception ex)
                {
                    tcpClient2.Close();
                    if (ex is IOException || ex is SocketException)
                    {
                        throw NewProxyException(Resources.ProxyException_Error, ex);
                    }
                    throw;
                }
                if (httpStatusCode != HttpStatusCode.OK)
                {
                    tcpClient2.Close();
                    throw new ProxyException(string.Format(Resources.ProxyException_ReceivedWrongStatusCode, httpStatusCode, ToString()), this);
                }
            }
            return(tcpClient2);
        }
コード例 #3
0
        public override TcpClient CreateConnection(string destinationHost, int destinationPort, TcpClient tcpClient = null)
        {
            CheckState();
            if (destinationHost == null)
            {
                throw new ArgumentNullException("destinationHost");
            }
            if (destinationHost.Length == 0)
            {
                throw ExceptionHelper.EmptyString("destinationHost");
            }
            if (!ExceptionHelper.ValidateTcpPort(destinationPort))
            {
                throw ExceptionHelper.WrongTcpPort("destinationPort");
            }
            TcpClient tcpClient2 = tcpClient;

            if (tcpClient2 == null)
            {
                tcpClient2 = CreateConnectionToProxy();
            }
            try
            {
                NetworkStream stream = tcpClient2.GetStream();
                InitialNegotiation(stream);
                SendCommand(stream, 1, destinationHost, destinationPort);
                return(tcpClient2);
            }
            catch (Exception ex)
            {
                tcpClient2.Close();
                if (ex is IOException || ex is SocketException)
                {
                    throw NewProxyException(Resources.ProxyException_Error, ex);
                }
                throw;
            }
        }