Exemple #1
0
        /// <summary>
        /// Copy sessions from PuTTY into SuperPutty Sessions
        /// </summary>
        public static void copySessionsFromPuTTY()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\SimonTatham\PuTTY\Sessions");

            if (key != null)
            {
                string[] savedSessionNames = key.GetSubKeyNames();
                foreach (string keyName in savedSessionNames)
                {
                    RegistryKey sessionKey = key.OpenSubKey(keyName);
                    if (sessionKey != null)
                    {
                        SessionData session = new SessionData();
                        session.Host  = (string)sessionKey.GetValue("HostName", "");
                        session.Port  = (int)sessionKey.GetValue("PortNUmber", 22);
                        session.Proto = (ConnectionProtocol)Enum.Parse(typeof(ConnectionProtocol),
                                                                       (string)sessionKey.GetValue("Protocol", "SSH"), true);
                        session.PuttySession     = (string)sessionKey.GetValue("PuttySession", HttpUtility.UrlDecode(keyName));
                        session.SessionName      = HttpUtility.UrlDecode(keyName);
                        session.Username         = (string)sessionKey.GetValue("UserName", "");
                        session.LastDockstate    = DockState.Document;
                        session.AutoStartSession = false;
                        session.SaveToRegistry();
                    }
                }
            }
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            Session.SessionName  = textBoxSessionName.Text.Trim();
            Session.PuttySession = comboBoxPuttyProfile.Text.Trim();
            Session.Host         = textBoxHostname.Text.Trim();
            Session.Port         = int.Parse(textBoxPort.Text.Trim());
            Session.Username     = textBoxUsername.Text.Trim();

            for (int i = 0; i < groupBox1.Controls.Count; i++)
            {
                RadioButton rb = (RadioButton)groupBox1.Controls[i];
                if (rb.Checked)
                {
                    Session.Proto = (ConnectionProtocol)rb.Tag;
                }
            }

            Session.SaveToRegistry();

            DialogResult = DialogResult.OK;
        }
Exemple #3
0
        /// <summary>
        /// Opens the selected session when the node is double clicked in the treeview
        /// </summary>
        /// <param name="sender">The treeview control that was double clicked</param>
        /// <param name="e">An Empty EventArgs object</param>
        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode.ImageIndex > 0)
            {
                SessionData   sessionData  = (SessionData)treeView1.SelectedNode.Tag;
                ctlPuttyPanel sessionPanel = null;

                // This is the callback fired when the panel containing the terminal is closed
                // We use this to save the last docking location
                PuttyClosedCallback callback = delegate(bool closed)
                {
                    if (sessionPanel != null)
                    {
                        // save the last dockstate (if it has been changed)
                        if (sessionData.LastDockstate != sessionPanel.DockState &&
                            sessionPanel.DockState != DockState.Unknown &&
                            sessionPanel.DockState != DockState.Hidden)
                        {
                            sessionData.LastDockstate = sessionPanel.DockState;
                            sessionData.SaveToRegistry();
                        }

                        if (sessionPanel.InvokeRequired)
                        {
                            this.BeginInvoke((MethodInvoker) delegate()
                            {
                                sessionPanel.Close();
                            });
                        }
                        else
                        {
                            sessionPanel.Close();
                        }
                    }
                };

                sessionPanel = new ctlPuttyPanel(sessionData, callback);
                sessionPanel.Show(m_DockPanel, sessionData.LastDockstate);
            }
        }
Exemple #4
0
        public void CreatePuttyPanel(SessionData sessionData, bool isPutty)
        {
            ctlPuttyPanel sessionPanel = null;

            // This is the callback fired when the panel containing the terminal is closed
            // We use this to save the last docking location
            PuttyClosedCallback callback = delegate(bool closed)
            {
                if (sessionPanel != null)
                {
                    // save the last dockstate (if it has been changed)
                    if (sessionData.LastDockstate != sessionPanel.DockState &&
                        sessionPanel.DockState != DockState.Unknown &&
                        sessionPanel.DockState != DockState.Hidden)
                    {
                        sessionData.LastDockstate = sessionPanel.DockState;
                        sessionData.SaveToRegistry();
                    }

                    if (sessionPanel.InvokeRequired)
                    {
                        this.BeginInvoke((MethodInvoker) delegate()
                        {
                            sessionPanel.Close();
                        });
                    }
                    else
                    {
                        sessionPanel.Close();
                    }
                }
            };

            sessionPanel = new ctlPuttyPanel(this, sessionData, callback, isPutty);
            sessionPanel.Show(dockPanel1, sessionData.LastDockstate);
            FocusCurrentTab();
        }
Exemple #5
0
        public void BeginGetDirectoryListing(string path, DirListingCallback callback)
        {
            if (m_Session == null)
            {
                callback(RequestResult.SessionInvalid, null);
                return;
            }

            List <FileEntry> files        = new List <FileEntry>();
            Stopwatch        timeoutWatch = new Stopwatch();

            /*
             * Check that we have a username either stored from previous sessions, or loaded
             * from the registry. If PPK Authentication is being used that will override
             * any values entered in the login dialog
             */
            if (String.IsNullOrEmpty(m_Session.Username))
            {
                if (m_Login.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    m_Session.Username = m_Login.Username;
                    m_Session.Password = m_Login.Password;

                    if (m_Login.Remember)
                    {
                        m_Session.SaveToRegistry(); // passwords are *never* saved and stored permanently
                    }
                }
            }

            Thread threadListFiles = new Thread(delegate()
            {
                m_processDir = new Process();

                m_processDir.EnableRaisingEvents             = true;
                m_processDir.StartInfo.UseShellExecute       = false;
                m_processDir.StartInfo.RedirectStandardError = true;
                //m_processDir.StartInfo.RedirectStandardInput = true;
                m_processDir.StartInfo.RedirectStandardOutput = true;
                m_processDir.StartInfo.CreateNoWindow         = true;
                m_processDir.StartInfo.FileName = frmSuperPutty.PscpExe;
                // process the various options from the session object and convert them into arguments pscp can understand
                string args = "-ls "; // default arguments
                args       += (!String.IsNullOrEmpty(m_Session.PuttySession)) ? "-load \"" + m_Session.PuttySession + "\" " : "";
                args       += (!String.IsNullOrEmpty(m_Session.Password) && m_Session.Password.Length > 0) ? "-pw " + m_Session.Password + " " : "";
                args       += "-P " + m_Session.Port + " ";
                args       += (!String.IsNullOrEmpty(m_Session.Username)) ? m_Session.Username + "@" : "";
                args       += m_Session.Host + ":" + path;
                Logger.Log("Sending Command: '{0} {1}'", m_processDir.StartInfo.FileName, args);
                m_processDir.StartInfo.Arguments = args;

                /*
                 * Handle output from spawned pscp.exe process, handle any data received and parse
                 * any lines that look like a directory listing.
                 */
                m_processDir.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e)
                {
                    if (!String.IsNullOrEmpty(e.Data))
                    {
                        if (e.Data.Equals(PUTTY_ARGUMENTS_HELP_HEADER))
                        {
                            m_processDir.CancelOutputRead();
                            m_processDir.Kill();
                            return;
                        }
                        else if (e.Data.StartsWith("Listing directory "))
                        {
                            // This just tells us the current directory, however since we're the ones that requested it
                            // we already have this information. But this traps it so its not sent through the directory
                            // entry parser.
                        }
                        else if (e.Data.Equals(PUTTY_INTERACTIVE_AUTH) || e.Data.Contains("password: "******"Username/Password invalid or not sent");
                            callback(RequestResult.RetryAuthentication, null);
                        }
                        else
                        {
                            timeoutWatch.Reset();
                            lock (files)
                            {
                                FileEntry file;
                                if (TryParseFileLine(e.Data, out file))
                                {
                                    files.Add(file);
                                }

                                if (files.Count > 0)
                                {
                                    callback(RequestResult.ListingFollows, files);
                                }
                            }
                        }
                    }
                };

                m_processDir.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e)
                {
                    if (!String.IsNullOrEmpty(e.Data))
                    {
                        if (e.Data.Contains(PUTTY_NO_KEY))
                        {
                            m_processDir.CancelErrorRead();
                            m_processDir.Kill();
                            System.Windows.Forms.MessageBox.Show("The key of the host you are attempting to connect to has changed or is not cached \n" +
                                                                 "You must connect to this host with with a PuTTY ssh terminal to accept the key and store it in the cache", "Host Key not found or changed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                        }
                        else
                        {
                            Logger.Log("Error Data:\n\t'{0}'", e.Data.TrimEnd());
                            // 'ssh_init: Host does not exist'
                        }
                    }
                };

                m_processDir.Exited += delegate(object sender, EventArgs e)
                {
                    if (m_processDir.ExitCode != 0)
                    {
                        Logger.Log("Process Exited (Failure): {0}", m_processDir.ExitCode);
                        callback(RequestResult.UnknownError, null);
                        if (m_PuttyClosed != null)
                        {
                            m_PuttyClosed(true);
                        }
                    }
                    else
                    {
                        Logger.Log("Process Exited: {0}", m_processDir.ExitCode);
                        if (m_PuttyClosed != null)
                        {
                            m_PuttyClosed(false);
                        }
                    }
                    m_DirIsBusy = false;
                };

                try
                {
                    m_processDir.Start();
                }
                catch (Win32Exception e)
                {
                    if (e.NativeErrorCode == 2) // File Not Found
                    {
                        Logger.Log(e);
                    }
                    else if (e.NativeErrorCode == 4) // Acess Denied
                    {
                        Logger.Log(e);
                    }
                }

                m_processDir.BeginErrorReadLine();
                m_processDir.BeginOutputReadLine();
                m_processDir.WaitForExit();
            });

            /* Only allow one directory list request at a time */
            if (!m_DirIsBusy)
            {
                m_DirIsBusy                  = true;
                threadListFiles.Name         = "List Remote Directory";
                threadListFiles.IsBackground = true;
                threadListFiles.Start();
            }
            else
            {
                return;
            }

            Thread timeoutThread = new Thread(delegate()
            {
                while (m_DirIsBusy)
                {
                    /*
                     * if no data received in 5 seconds we'll stop the process,
                     * This allows us to capture any interactive prompts/messages
                     * sent to us by putty.
                     */
                    if (timeoutWatch.Elapsed.Seconds >= 5)
                    {
                        Logger.Log("Timeout after {0} seconds", timeoutWatch.Elapsed.Seconds);

                        if (!m_processDir.HasExited)
                        {
                            m_processDir.Kill();
                        }
                        m_processDir.CancelErrorRead();
                        m_processDir.CancelOutputRead();
                        return;
                    }
                    Thread.Sleep(1000);
                }
            });

            timeoutThread.Name         = "Timeout Watcher";
            timeoutThread.IsBackground = true;
            timeoutThread.Start();
            timeoutWatch.Start();
        }
Exemple #6
0
        public void CreatePuttyPanel(SessionData sessionData)
        {
            ctlPuttyPanel sessionPanel = null;

            // This is the callback fired when the panel containing the terminal is closed
            // We use this to save the last docking location
            PuttyClosedCallback callback = delegate(bool closed)
            {
                if (sessionPanel != null)
                {
                    // save the last dockstate (if it has been changed)
                    if (sessionData.LastDockstate != sessionPanel.DockState
                        && sessionPanel.DockState != DockState.Unknown
                        && sessionPanel.DockState != DockState.Hidden)
                    {
                        sessionData.LastDockstate = sessionPanel.DockState;
                        sessionData.SaveToRegistry();
                    }

                    if (sessionPanel.InvokeRequired)
                    {
                        this.BeginInvoke((MethodInvoker)delegate()
                        {
                            sessionPanel.Close();
                         });
                    }
                    else
                    {
                        sessionPanel.Close();
                    }
                }
            };

            sessionPanel = new ctlPuttyPanel(sessionData, callback);
            sessionPanel.Show(dockPanel1, sessionData.LastDockstate);
        }
 /// <summary>
 /// Copy sessions from PuTTY into SuperPutty Sessions
 /// </summary>
 public static void copySessionsFromPuTTY()
 {
     RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\SimonTatham\PuTTY\Sessions");
     if (key != null)
     {
         string[] savedSessionNames = key.GetSubKeyNames();
         foreach (string keyName in savedSessionNames)
         {
             RegistryKey sessionKey = key.OpenSubKey(keyName);
             if (sessionKey != null)
             {
                 SessionData session = new SessionData();
                 session.Host = (string)sessionKey.GetValue("HostName", "");
                 session.Port = (int)sessionKey.GetValue("PortNUmber", 22);
                 session.Proto = (ConnectionProtocol)Enum.Parse(typeof(ConnectionProtocol),
                     (string)sessionKey.GetValue("Protocol", "SSH"), true);
                 session.PuttySession = (string)sessionKey.GetValue("PuttySession", HttpUtility.UrlDecode(keyName));
                 session.SessionName = HttpUtility.UrlDecode(keyName);
                 session.Username = (string)sessionKey.GetValue("UserName", "");
                 session.LastDockstate = DockState.Document;
                 session.AutoStartSession = false;
                 session.SaveToRegistry();
             }
         }
     }
 }