Пример #1
0
        protected virtual void MakeConnection()
        {
            if (_socks != null)
            {
                IPAddressSet a = null;
                try
                {
                    a = new IPAddressSet(IPAddress.Parse(_socks.DestName));
                }
                catch (FormatException)
                {
                    try
                    {
                        a = new IPAddressSet(_socks.DestName);
                    }
                    catch (Exception)
                    {
                    }
                }

                if (a != null && !SocksApplicapable(_socks.ExcludingNetworks, a))
                {
                    _addressSet = a;
                    _host       = _socks.DestName;
                    _port       = _socks.DestPort;
                    _socks      = null;
                }
            }

            string dest = _socks == null ? _host : _socks.ServerName;
            int    port = _socks == null ? _port : _socks.ServerPort;
            string msg  = _socks == null?GetHostDescription() : "SOCKS Server";

            if (_addressSet == null)
            {
                try
                {
                    _addressSet = new IPAddressSet(IPAddress.Parse(dest));
                }
                catch (FormatException)
                {
                    _errorMessage = String.Format("The {0} {1} was not found.", msg, dest);
                    _addressSet   = new IPAddressSet(dest);
                }
            }

            _errorMessage     = String.Format("Failed to connect {0} {1}. Please check the address and the port.", msg, dest);
            _socket           = NetUtil.ConnectTCPSocket(_addressSet, port);
            _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address;

            if (_socks != null)
            {
                _errorMessage = "An error occurred while SOCKS negotiation.";
                _socks.Connect(_socket);
                _host = _socks.DestName;
                _port = _socks.DestPort;
            }

            _tcpConnected = true;
        }
Пример #2
0
        protected virtual void MakeConnection()
        {
            //まずSOCKSを使うべきかどうかを判定する
            if (_socks != null)
            {
                IPAddressSet a = null;
                try {
                    a = new IPAddressSet(IPAddress.Parse(_socks.DestName));
                }
                catch (FormatException) {
                    try {
                        a = new IPAddressSet(_socks.DestName);
                    }
                    catch (Exception) {                    //ここで名前解決できずとも、SOCKSサーバが解決できるかもしれないのでエラーにはしない
                    }
                }

                if (a != null && !SocksApplicapable(_socks.ExcludingNetworks, a))                //SOCKSを使わないことが確定
                {
                    _addressSet = a;
                    _host       = _socks.DestName;
                    _port       = _socks.DestPort;
                    _socks      = null;
                }
            }

            string dest = _socks == null? _host : _socks.ServerName;
            int    port = _socks == null? _port : _socks.ServerPort;
            string msg  = _socks == null?GetHostDescription() : "SOCKS Server";

            if (_addressSet == null)
            {
                try {
                    _addressSet = new IPAddressSet(IPAddress.Parse(dest));                     //最初からIPアドレス形式のときは手で変換。そうでないとDNSの逆引きをしてタイムアウト、とかややこしいことが起こる
                }
                catch (FormatException) {
                    _errorMessage = String.Format("The {0} {1} was not found.", msg, dest);
                    _addressSet   = new IPAddressSet(dest);
                }
            }

            _errorMessage     = String.Format("Failed to connect {0} {1}. Please check the address and the port.", msg, dest);
            _socket           = NetUtil.ConnectTCPSocket(_addressSet, port);
            _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address;

            if (_socks != null)
            {
                _errorMessage = "An error occurred while SOCKS negotiation.";
                _socks.Connect(_socket);
                _host = _socks.DestName;
                _port = _socks.DestPort;
            }

            _tcpConnected = true;
        }