/// <summary>
        /// Validates input values and constructs parameter objects.
        /// </summary>
        /// <param name="loginParam">SSH parameter object is set when this method returns true.</param>
        /// <param name="terminalSettings">terminal settings object is set when this method returns true.</param>
        /// <param name="errorMessage">validation error message is set when this method returns false. this can be null when displaying error message is not needed.</param>
        /// <returns>true if all validations passed and parameter objects were created.</returns>
        private bool Validate(out ISSHLoginParameter loginParam, out ITerminalSettings terminalSettings, out string errorMessage)
        {
            loginParam       = null;
            terminalSettings = null;
            errorMessage     = null;

            var ssh = TerminalSessionsPlugin.Instance.ProtocolService.CreateDefaultSSHParameter();
            var tcp = (ITCPParameter)ssh.GetAdapter(typeof(ITCPParameter));

            //--- SSH connection settings

            if (_ssh1RadioButton.Checked)
            {
                ssh.Method = SSHProtocol.SSH1;
            }
            else if (_ssh2RadioButton.Checked)
            {
                ssh.Method = SSHProtocol.SSH2;
            }
            else
            {
                errorMessage = TEnv.Strings.GetString("Message.LoginDialog.ProtocolVersionIsNotSpecified");
                return(false);
            }

            tcp.Destination = _hostBox.Text;
            if (String.IsNullOrEmpty(tcp.Destination))
            {
                errorMessage = TEnv.Strings.GetString("Message.LoginDialog.HostIsEmpty");
                return(false);
            }

            int port;

            if (Int32.TryParse(_portBox.Text, out port) && port >= 0 && port <= 65535)
            {
                tcp.Port = port;
            }
            else
            {
                errorMessage = String.Format(
                    TEnv.Strings.GetString("Message.LoginDialog.InvalidPort"),
                    _portBox.Text);
                return(false);
            }

            ssh.Account = _userNameBox.Text;

            AuthType authType = ((EnumListItem <AuthType>)_authOptions.SelectedItem).Value;

            ssh.AuthenticationType = authType.ToAuthenticationType();

            if (ssh.AuthenticationType == AuthenticationType.PublicKey)
            {
                ssh.IdentityFileName = _privateKeyFile.Text;

                if (String.IsNullOrEmpty(ssh.IdentityFileName))
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.PrivateKeyFileIsNotSpecified");
                    return(false);
                }

                if (!File.Exists(ssh.IdentityFileName))
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.KeyFileNotExist");
                    return(false);
                }
            }

            if (ssh.AuthenticationType == AuthenticationType.Password || ssh.AuthenticationType == AuthenticationType.PublicKey)
            {
                ssh.PasswordOrPassphrase = _passphraseBox.Text;
            }

            //--- Log settings

            ISimpleLogSettings logSettings = TerminalSessionsPlugin.Instance.TerminalEmulatorService.CreateDefaultSimpleLogSettings();

            logSettings.LogType = ((EnumListItem <LogType>)_logTypeBox.SelectedItem).Value;
            if (logSettings.LogType != LogType.None)
            {
                logSettings.LogPath = _logFileBox.Text;
                LogFileCheckResult r = LogUtil.CheckLogFileName(logSettings.LogPath, this.ParentForm);
                if (r == LogFileCheckResult.Cancel || r == LogFileCheckResult.Error)
                {
                    errorMessage = null;
                    return(false);
                }
                logSettings.LogAppend = (r == LogFileCheckResult.Append);
            }

            //--- Terminal settings

            ITerminalParameter termParam    = (ITerminalParameter)tcp.GetAdapter(typeof(ITerminalParameter));
            TerminalType       terminalType = ((EnumListItem <TerminalType>)_terminalTypeBox.SelectedItem).Value;

            termParam.SetTerminalName(terminalType.ToTermValue());

            string            terminalCaption = tcp.Destination;
            Image             terminalIcon    = Poderosa.TerminalSession.Properties.Resources.NewConnection16x16;
            ITerminalSettings termSettings    = TerminalSessionsPlugin.Instance.TerminalEmulatorService.CreateDefaultTerminalSettings(terminalCaption, terminalIcon);

            termSettings.BeginUpdate();
            termSettings.Encoding     = ((EnumListItem <EncodingType>)_encodingBox.SelectedItem).Value;
            termSettings.LocalEcho    = ((ListItem <bool>)_localEchoBox.SelectedItem).Value;
            termSettings.TransmitNL   = ((EnumListItem <NewLine>)_newLineBox.SelectedItem).Value;
            termSettings.TerminalType = terminalType;
            termSettings.LogSettings.Reset(logSettings);
            termSettings.EndUpdate();

            //--- X11 forwarding settings

            if (_useX11ForwardingCheckBox.Checked)
            {
                if (String.IsNullOrEmpty(_x11DisplayText.Text))
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.X11DisplayIsNotEntered");
                    return(false);
                }
                int display;
                if (!Int32.TryParse(_x11DisplayText.Text, out display) || display < 0 || display > (65535 - 6000))
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.InvalidX11Display");
                    return(false);
                }

                X11ForwardingParams x11Param = new X11ForwardingParams(display);

                if (String.IsNullOrEmpty(_x11ScreenText.Text))
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.X11ScreenIsNotEntered");
                    return(false);
                }
                int screen;
                if (!Int32.TryParse(_x11ScreenText.Text, out screen) || screen < 0)
                {
                    errorMessage = TEnv.Strings.GetString("Message.LoginDialog.InvalidX11Screen");
                    return(false);
                }
                x11Param.Screen = screen;

                if (_x11NeedAuthCheckBox.Checked)
                {
                    x11Param.NeedAuth       = true;
                    x11Param.XauthorityFile = _x11XauthorityText.Text;
                    if (String.IsNullOrEmpty(x11Param.XauthorityFile))
                    {
                        errorMessage = TEnv.Strings.GetString("Message.LoginDialog.XauthorityFileIsNotSpecified");
                        return(false);
                    }
                }
                else
                {
                    x11Param.NeedAuth = false;
                }

                if (_x11UseCygwinDomainSocketCheckBox.Checked)
                {
                    x11Param.UseCygwinUnixDomainSocket = true;
                    x11Param.X11UnixFolder             = _x11CygwinX11UnixFolderText.Text;
                    if (String.IsNullOrEmpty(x11Param.X11UnixFolder))
                    {
                        errorMessage = TEnv.Strings.GetString("Message.LoginDialog.X11UnixFolderIsNotSpecified");
                        return(false);
                    }
                }

                ssh.EnableX11Forwarding = true;
                ssh.X11Forwarding       = x11Param;
            }
            else
            {
                ssh.EnableX11Forwarding = false;
                ssh.X11Forwarding       = null;
            }

            //--- Agent forwarding settings

            if (_useAgentForwardingCheckBox.Checked)
            {
                ssh.EnableAgentForwarding          = true;
                ssh.AgentForwardingAuthKeyProvider = null;  // set later
            }
            else
            {
                ssh.EnableAgentForwarding          = false;
                ssh.AgentForwardingAuthKeyProvider = null;
            }

            //--- Macro

            IAutoExecMacroParameter autoExecParams = tcp.GetAdapter(typeof(IAutoExecMacroParameter)) as IAutoExecMacroParameter;

            if (autoExecParams != null)     // macro plugin is enabled
            {
                if (!String.IsNullOrEmpty(_autoExecMacroPathBox.Text))
                {
                    autoExecParams.AutoExecMacroPath = _autoExecMacroPathBox.Text;
                }
                else
                {
                    autoExecParams.AutoExecMacroPath = null;
                }
            }

            loginParam       = ssh;
            terminalSettings = termSettings;
            return(true);
        }
示例#2
0
        /// <summary>
        /// Validates input values and constructs parameter objects.
        /// </summary>
        /// <param name="telnetParam">Telnet parameter object is set when this method returns true.</param>
        /// <param name="terminalSettings">terminal settings object is set when this method returns true.</param>
        /// <param name="errorMessage">validation error message is set when this method returns false. this can be null when displaying error message is not needed.</param>
        /// <returns>true if all validations passed and parameter objects were created.</returns>
        private bool Validate(out ITCPParameter telnetParam, out ITerminalSettings terminalSettings, out string errorMessage)
        {
            telnetParam      = null;
            terminalSettings = null;
            errorMessage     = null;

            var telnet        = TerminalSessionsPlugin.Instance.ProtocolService.CreateDefaultTelnetParameter();
            var tcp           = (ITCPParameter)telnet.GetAdapter(typeof(ITCPParameter));
            var protocolParam = (ITelnetParameter)telnet.GetAdapter(typeof(ITelnetParameter));

            tcp.Destination = _hostBox.Text;
            if (String.IsNullOrEmpty(tcp.Destination))
            {
                errorMessage = TEnv.Strings.GetString("Message.LoginDialog.HostIsEmpty");
                return(false);
            }

            int port;

            if (Int32.TryParse(_portBox.Text, out port) && port >= 0 && port <= 65535)
            {
                tcp.Port = port;
            }
            else
            {
                errorMessage = String.Format(
                    TEnv.Strings.GetString("Message.LoginDialog.InvalidPort"),
                    _portBox.Text);
                return(false);
            }

            //--- Log settings

            ISimpleLogSettings logSettings = TerminalSessionsPlugin.Instance.TerminalEmulatorService.CreateDefaultSimpleLogSettings();

            logSettings.LogType = ((EnumListItem <LogType>)_logTypeBox.SelectedItem).Value;
            if (logSettings.LogType != LogType.None)
            {
                logSettings.LogPath = _logFileBox.Text;
                LogFileCheckResult r = LogUtil.CheckLogFileName(logSettings.LogPath, this.ParentForm);
                if (r == LogFileCheckResult.Cancel || r == LogFileCheckResult.Error)
                {
                    errorMessage = null;
                    return(false);
                }
                logSettings.LogAppend = (r == LogFileCheckResult.Append);
            }

            //--- Terminal settings

            ITerminalParameter termParam    = (ITerminalParameter)tcp.GetAdapter(typeof(ITerminalParameter));
            TerminalType       terminalType = ((EnumListItem <TerminalType>)_terminalTypeBox.SelectedItem).Value;

            termParam.SetTerminalName(terminalType.ToTermValue());

            string            terminalCaption = tcp.Destination;
            Image             terminalIcon    = Poderosa.TerminalSession.Properties.Resources.NewConnection16x16;
            ITerminalSettings termSettings    = TerminalSessionsPlugin.Instance.TerminalEmulatorService.CreateDefaultTerminalSettings(terminalCaption, terminalIcon);

            termSettings.BeginUpdate();
            termSettings.Encoding     = ((EnumListItem <EncodingType>)_encodingBox.SelectedItem).Value;
            termSettings.LocalEcho    = ((ListItem <bool>)_localEchoBox.SelectedItem).Value;
            termSettings.TransmitNL   = ((EnumListItem <NewLine>)_newLineBox.SelectedItem).Value;
            termSettings.TerminalType = terminalType;
            termSettings.LogSettings.Reset(logSettings);
            termSettings.EndUpdate();

            //--- TELNET protocol settings

            protocolParam.TelnetNewLine =
                (termSettings.TransmitNL == NewLine.CRLF) ? _telnetNewLine.Checked : false;

            //--- Macro

            IAutoExecMacroParameter autoExecParams = tcp.GetAdapter(typeof(IAutoExecMacroParameter)) as IAutoExecMacroParameter;

            if (autoExecParams != null)     // macro plugin is enabled
            {
                if (!String.IsNullOrEmpty(_autoExecMacroPathBox.Text))
                {
                    autoExecParams.AutoExecMacroPath = _autoExecMacroPathBox.Text;
                }
                else
                {
                    autoExecParams.AutoExecMacroPath = null;
                }
            }

            telnetParam      = tcp;
            terminalSettings = termSettings;
            return(true);
        }