Exemplo n.º 1
0
        public SessionDataStartInfo ToSessionStartInfo()
        {
            SessionDataStartInfo ssi = null;

            if (this.SessionId != null)
            {
                // first try to resolve by sessionId
                SessionData session = SuperPuTTY.GetSessionById(this.SessionId);
                if (session == null)
                {
                    Log.WarnFormat("Session from command line not found, id={0}", this.SessionId);
                }
                else
                {
                    ssi = new SessionDataStartInfo
                    {
                        Session = session,
                        UseScp  = this.UseScp
                    };
                }
            }
            else if (this.Host != null || this.PuttySession != null)
            {
                // Host or puttySession provided
                string sessionName;
                if (this.Host != null)
                {
                    // Decode URL type host spec, if provided (e.g. ssh://localhost:2020)
                    HostConnectionString connStr = new HostConnectionString(this.Host);
                    this.Host     = connStr.Host;
                    this.Protocol = connStr.Protocol.GetValueOrDefault(this.Protocol.GetValueOrDefault(ConnectionProtocol.SSH));
                    this.Port     = connStr.Port.GetValueOrDefault(this.Port.GetValueOrDefault(dlgEditSession.GetDefaultPort(this.Protocol.GetValueOrDefault())));
                    sessionName   = this.Host;
                }
                else
                {
                    // no host provided so assume sss
                    sessionName = this.PuttySession;
                }

                ssi = new SessionDataStartInfo
                {
                    Session = new SessionData
                    {
                        Host         = this.Host,
                        SessionName  = sessionName,
                        SessionId    = SuperPuTTY.MakeUniqueSessionId(SessionData.CombineSessionIds("CLI", this.Host)),
                        Port         = this.Port.GetValueOrDefault(22),
                        Proto        = this.Protocol.GetValueOrDefault(ConnectionProtocol.SSH),
                        Username     = this.UserName,
                        Password     = this.Password,
                        PuttySession = this.PuttySession
                    },
                    UseScp = this.UseScp
                };
            }

            if (ssi == null)
            {
                Log.WarnFormat("Could not determine session or host to connect.  SessionId or Host or PuttySession must be provided");
            }

            return(ssi);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create/Update a session entry
        /// </summary>
        /// <param name="sender">The toolstripmenuitem control that was clicked</param>
        /// <param name="e">An Empty EventArgs object</param>
        private void CreateOrEditSessionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SessionData session = null;
            TreeNode    node    = null;
            TreeNode    nodeRef = nodeRoot;
            string      title   = null;

            if (sender is ToolStripMenuItem menuItem)
            {
                bool isFolderNode = IsFolderNode(treeView1.SelectedNode);
                if (menuItem.Text.ToLower().Equals("new") || isFolderNode)
                {
                    session = new SessionData();
                    nodeRef = isFolderNode ? treeView1.SelectedNode : treeView1.SelectedNode.Parent;
                    title   = "Create New Session";
                }
                else if (menuItem == createLikeToolStripMenuItem)
                {
                    // copy as
                    session             = (SessionData)((SessionData)treeView1.SelectedNode.Tag).Clone();
                    session.SessionId   = SuperPuTTY.MakeUniqueSessionId(session.SessionId);
                    session.SessionName = SessionData.GetSessionNameFromId(session.SessionId);
                    nodeRef             = treeView1.SelectedNode.Parent;
                    title = "Create New Session Like " + session.OldName;
                }
                else
                {
                    // edit, session node selected
                    // We make a clone of the session since we do not want to directly edit the real object.
                    session = (SessionData)((SessionData)treeView1.SelectedNode.Tag).Clone();
                    node    = treeView1.SelectedNode;
                    nodeRef = node.Parent;
                    title   = "Edit Session: " + session.SessionName;
                }
            }

            EditSessionDialog form = new EditSessionDialog(session, treeView1.ImageList)
            {
                Text = title
            };

            form.SessionNameValidator += delegate(string txt, out string error)
            {
                bool IsValid = ValidateSessionNameChange(nodeRef, node, txt, out error);
                return(IsValid);
            };

            if (form.ShowDialog(this) == DialogResult.OK)
            {
                /* "node" will only be assigned if we're editing an existing session entry */
                if (node == null)
                {
                    // get the path up to the ref (parent) node
                    if (nodeRoot != nodeRef)
                    {
                        UpdateSessionId(nodeRef, session);
                        session.SessionId = SessionData.CombineSessionIds(session.SessionId, session.SessionName);
                    }
                    SuperPuTTY.AddSession(session);

                    // find new node and select it
                    TreeNode nodeNew = nodeRef.Nodes[session.SessionName];
                    if (nodeNew != null)
                    {
                        treeView1.SelectedNode = nodeNew;
                    }
                }
                else
                {
                    SessionData RealSession = (SessionData)treeView1.SelectedNode.Tag;
                    RealSession.CopyFrom(session);
                    RealSession.SessionName = session.SessionName;
                    treeView1.SelectedNode  = node;
                }

                //treeView1.ExpandAll();
                SuperPuTTY.SaveSessions();
            }
        }