Esempio n. 1
0
        override public void Bind(SocketBase socket)
        {
            CheckDisposed();
            SetProgress(true);
            try
            {
                //-----------------------------------------
                // Get end point for the proxy server
                //
                IPHostEntry host = GetHostByName(_proxyServer);
                if (host == null)
                {
                    throw new SocketException(SockErrors.WSAHOST_NOT_FOUND);
                }

                // throw new HostNotFoundException("Unable to resolve proxy host name.");

                IPEndPoint proxyEndPoint = ConstructEndPoint(host, _proxyPort);

                //-----------------------------------------
                // Connect to proxy server
                //
                _socket.Connect(proxyEndPoint);

                //-----------------------------------------
                // Send BIND command
                //
                byte[] cmd = PrepareBindCmd((Socket_Socks4a)socket);
                NStream.Write(cmd, 0, cmd.Length);

                //-----------------------------------------
                // Read the response from the proxy server.
                //
                int read = 0;
                while (read < 8)
                {
                    read += NStream.Read(
                        _response,
                        read,
                        _response.Length - read);
                }

                VerifyResponse();
                _localEndPoint = ConstructBindEndPoint(proxyEndPoint.Address);

                // remote end point doesn't provided for BIND command
                _remoteEndPoint = null;
            }
            finally
            {
                SetProgress(false);
            }
        }
Esempio n. 2
0
        override internal void Connect(EndPoint remoteEP)
        {
            CheckDisposed();
            SetProgress(true);
            try
            {
                //------------------------------------
                // Get end point for the proxy server
                //
                IPHostEntry proxyEntry = GetHostByName(_proxyServer);
                if (null == proxyEntry)
                {
                    // throw new HostNotFoundException("Unable to resolve proxy name.");
                    throw new SocketException(SockErrors.WSAHOST_NOT_FOUND);
                }

                IPEndPoint proxyEndPoint = ConstructEndPoint(proxyEntry, _proxyPort);

                //------------------------------------------
                // Connect to proxy server
                //
                _socket.Connect(proxyEndPoint);

                _localEndPoint  = null; // CONNECT command doesn't provide us with local end point
                _remoteEndPoint = remoteEP;

                //------------------------------------------
                // Send CONNECT command
                //
                byte[] cmd = PrepareConnectCmd(remoteEP);
                NStream.Write(cmd, 0, cmd.Length);

                //------------------------------------------
                // Read the response from proxy the server.
                //
                int read = 0;
                while (read < 8)
                {
                    read += NStream.Read(
                        _response,
                        read,
                        _response.Length - read);
                }

                VerifyResponse();
            }
            finally
            {
                SetProgress(false);
            }
        }
        override internal void Connect(string hostName, int hostPort)
        {
            CheckDisposed();

            SetProgress(true);
            try
            {
                if (null == hostName)
                {
                    throw new ArgumentNullException("hostName", "The value cannot be null.");
                }

                if (hostPort < IPEndPoint.MinPort || hostPort > IPEndPoint.MaxPort)
                {
                    throw new ArgumentOutOfRangeException("hostPort", "Value, specified for the port is out of the valid range.");
                }

                //------------------------------------
                // Get end point for the proxy server
                IPHostEntry proxyEntry = GetHostByName(_proxyServer);
                if (null == proxyEntry)
                {
                    throw new SocketException(SockErrors.WSAHOST_NOT_FOUND);
                }
                //throw new HostNotFoundException("Unable to resolve proxy name.");

                IPEndPoint proxyEndPoint = ConstructEndPoint(proxyEntry, _proxyPort);

                //------------------------------------------
                // Connect to proxy server
                _socket.Connect(proxyEndPoint);

                bool useCredentials = PreAuthenticate;
                while (true)
                {
                    //------------------------------------------
                    // Send CONNECT command
                    byte[] cmd = GetConnectCmd(hostName, hostPort, useCredentials);
                    NStream.Write(cmd, 0, cmd.Length);

                    //-----------------------------------------
                    // Read the reply
                    ByteVector reply = ReadReply();

                    //------------------------------------------
                    // Analyze reply
                    string reason = null;
                    int    code   = AnalyzeReply(reply, out reason);

                    //------------------------------------------
                    //is good return code?
                    if (code >= 200 && code <= 299)
                    {
                        return;
                    }

                    //------------------------------------------
                    //If Proxy Authentication Required
                    //but we do not issued it, then try again
                    if ((407 == code) &&
                        !useCredentials &&
                        (_proxyUser != null))
                    {
                        useCredentials = true;
                        continue;
                    }

                    //string msg = string.Format("Connection refused by web proxy: {0} ({1}).", reason, code);
                    //throw new ProxyErrorException(msg);
                    throw new SocketException(SockErrors.WSAECONNREFUSED);
                }
            }
            finally
            {
                SetProgress(false);
            }
        }
Esempio n. 4
0
        void Connect(EndPoint remoteEP, string hostName, int port)
        {
            CheckDisposed();
            SetProgress(true);
            try
            {
                if (null == remoteEP)
                {
                    if (_resolveHostEnabled)
                    {
                        IPHostEntry host = GetHostByName(hostName);
                        if (null != host)
                        {
                            remoteEP = ConstructEndPoint(host, port);
                        }
                    }

                    if ((null == hostName) && (null == remoteEP))
                    {
                        throw new ArgumentNullException("hostName", "The value cannot be null.");
                    }
                }

                //------------------------------------
                // Get end point for the proxy server
                //
                IPHostEntry proxyEntry = GetHostByName(_proxyServer);
                if (null == proxyEntry)
                {
                    throw new SocketException(SockErrors.WSAHOST_NOT_FOUND);
                }
                //throw new HostNotFoundException("Unable to resolve proxy name.");

                IPEndPoint proxyEndPoint = ConstructEndPoint(proxyEntry, _proxyPort);

                //------------------------------------------
                // Connect to proxy server
                //
                _socket.Connect(proxyEndPoint);

                _localEndPoint  = null; //CONNECT command doesn't provide us with local end point
                _remoteEndPoint = remoteEP;

                //------------------------------------------
                // Send CONNECT command
                //
                byte[] cmd = PrepareConnectCmd(remoteEP, hostName, port);
                NStream.Write(cmd, 0, cmd.Length);

                //------------------------------------------
                // Read the response from proxy the server.
                //
                int read = 0;
                while (read < 8)
                {
                    read += NStream.Read(_response,
                                         read,
                                         _response.Length - read);
                }

                VerifyResponse();

                //---------------------------------------
                //I we unable to resolve remote host then
                //store information - it will required
                //later for BIND command.
                if (null == remoteEP)
                {
                    _remotePort = port;
                    _remoteHost = hostName;
                }
            }
            finally
            {
                SetProgress(false);
            }
        }