private bool CommitConnectionOptions()
        {
            string itemname = "";

            try {
                _options.UseSocks = _useSocks.Checked;
                if (_options.UseSocks && _socksServerBox.Text.Length == 0)
                {
                    throw new Exception(Env.Strings.GetString("Message.OptionDialog.EmptySOCKSServer"));
                }
                _options.SocksServer   = _socksServerBox.Text;
                itemname               = Env.Strings.GetString("Caption.OptionDialog.SOCKSPort");
                _options.SocksPort     = Int32.Parse(_socksPortBox.Text);
                _options.SocksAccount  = _socksAccountBox.Text;
                _options.SocksPassword = _socksPasswordBox.Text;
                itemname               = Env.Strings.GetString("Caption.OptionDialog.NetworkAddress");
                foreach (string c in _socksNANetworksBox.Text.Split(';'))
                {
                    if (!NetUtil.IsNetworkAddress(c))
                    {
                        throw new FormatException();
                    }
                }
                _options.SocksNANetworks = _socksNANetworksBox.Text;

                return(true);
            }
            catch (FormatException) {
                Util.Warning(this, String.Format(Env.Strings.GetString("Message.OptionDialog.InvalidItem"), itemname));
                return(false);
            }
            catch (Exception ex) {
                Util.Warning(this, ex.Message);
                return(false);
            }
        }
Esempio n. 2
0
        protected virtual void MakeConnection()
        {
#if false
            //まず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;
            }
#endif
            //上記の#ifをSOCKSなしに書き換え
            string dest = _host;
            int    port = _port;
            string msg  = GetHostDescription();
            if (_addressSet == null)
            {
                IPAddress address;
                if (IPAddress.TryParse(dest, out address))
                {
                    _addressSet = new IPAddressSet(address);
                }
                else
                {
                    _addressSet = new IPAddressSet(dest); //DNSで
                }
            }
            _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;

            _tcpConnected = true;
        }