Esempio n. 1
0
        public void Start(bool interactive)
        {
            if (!PuttyTunnelManagerSettings.Instance().HasPlink)
            {
                throw new PlinkNotFoundException();
            }

            this.active = true;
            Session.OpenSessions.Add(this.session);

            this.guardian.Start();
            Debug.WriteLine("Plink: Starting Guardian!");

            if (interactive)
            {
                this.process.StartInfo.RedirectStandardOutput = true;
                this.process.StartInfo.RedirectStandardInput  = true;
            }

            StringBuilder args = new StringBuilder("-agent -N -load \"" + this.session.Name + "\"");

            if (this.session.UsePtmForTunnels)
            {
                foreach (Tunnel tunnel in this.session.Tunnels)
                {
                    switch (tunnel.Type)
                    {
                    case TunnelType.LOCAL:
                        args.Append(" -L " + tunnel.LongConnectionString);
                        break;

                    case TunnelType.REMOTE:
                        args.Append(" -R " + tunnel.LongConnectionString);
                        break;

                    case TunnelType.DYNAMIC:
                        args.Append(" -D " + tunnel.ShortConnectionString);
                        break;
                    }
                }
            }

            this.process.StartInfo.Arguments = args.ToString();
            this.process.Start();
            Debug.WriteLine("Plink: Started!");

            if (interactive)
            {
                this.process.StandardInput.AutoFlush = true;

                StringBuilder buffer   = new StringBuilder();
                string        username = this.session.Username;
                while (!this.process.HasExited)
                {
                    while (this.process.StandardOutput.Peek() > 0)
                    {
                        char c = (char)this.process.StandardOutput.Read();
                        buffer.Append(c);
                    }

                    string data = buffer.ToString().ToLower();

                    if (data.Contains("login"))
                    {
                        LoginForm form = new LoginForm();
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            username = form.Username;
                            if (form.SaveUsername)
                            {
                                session.Username = username;
                                session.Serialize();
                            }
                            this.process.StandardInput.WriteLine(username);
                        }
                        else
                        {
                            Stop();
                        }
                    }
                    else if (data.Contains("password"))
                    {
                        LoginForm form = new LoginForm(username);
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            this.process.StandardInput.WriteLine(form.Password);
                        }
                        else
                        {
                            Stop();
                        }
                    }
                    this.process.StandardOutput.DiscardBufferedData();
                    buffer.Remove(0, buffer.Length);
                }
            }
            else
            {
                this.process.WaitForExit();
            }

            Debug.WriteLine("Plink: Stopped!");

            Session.OpenSessions.Remove(this.session);
            this.active = false;
        }
Esempio n. 2
0
        public void Start(bool interactive)
        {
            if (!PuttyTunnelManagerSettings.Instance().HasPlink)
            {
                throw new PlinkNotFoundException();
            }
            restart     = true;
            this.active = true;

            Session.OpenSessions.Add(this.session);

            if (interactive)
            {
                this.process.StartInfo.RedirectStandardOutput = true;
                this.process.StartInfo.RedirectStandardInput  = true;
            }

            StringBuilder args = new StringBuilder("-agent -N -load \"" + this.session.Name + "\"");

            if (this.session.UsePtmForTunnels)
            {
                foreach (Tunnel tunnel in this.session.Tunnels)
                {
                    switch (tunnel.Type)
                    {
                    case TunnelType.LOCAL:
                        args.Append(" -L " + tunnel.SourcePort + ":" + tunnel.Destination + ":" + tunnel.DestinationPort);
                        break;

                    case TunnelType.REMOTE:
                        args.Append(" -R " + tunnel.SourcePort + ":" + tunnel.Destination + ":" + tunnel.DestinationPort);
                        break;

                    case TunnelType.DYNAMIC:
                        args.Append(" -D " + tunnel.SourcePort);
                        break;
                    }
                }
            }

            this.process.StartInfo.Arguments = args.ToString();
            this.process.Start();
            createGuardian().Start();
            Debug.WriteLine("Plink: Started!");

            if (interactive)
            {
                this.process.StandardInput.AutoFlush = true;
                var           plinkstart = true;
                StringBuilder buffer     = new StringBuilder();
                string        username   = this.session.Username;
                while (!this.process.HasExited)
                {
                    while (this.process.StandardOutput.Peek() > 0)
                    {
                        char c = (char)this.process.StandardOutput.Read();
                        buffer.Append(c);
                    }

                    string data = buffer.ToString().ToLower();

                    if (plinkstart && data.Length == 0 && !killing)
                    {
                        // TODO: automatic accept new cert at first connection
                        // trying to pass and see if process exists.
                        throw new CurruptedSessionException();
                    }

                    if (data.Contains("login"))
                    {
                        LoginForm form = new LoginForm();
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            username = form.Username;
                            if (form.SaveUsername)
                            {
                                session.Username = username;
                                session.Serialize();
                            }
                            this.process.StandardInput.WriteLine(username);
                        }
                        else
                        {
                            Stop();
                        }
                    }
                    else if (data.Contains("password"))
                    {
                        if (session.Password == null)
                        {
                            LoginForm form = new LoginForm(username);
                            if (form.ShowDialog() == DialogResult.OK)
                            {
                                session.Password = form.Password;
                            }
                        }
                        if (session.Password == null)
                        {
                            Stop();
                        }
                        this.process.StandardInput.WriteLine(session.Password);
                        plinkstart = false;
                    }
                    this.process.StandardOutput.DiscardBufferedData();
                    buffer.Remove(0, buffer.Length);
                }
                Debug.WriteLine("Interactive session closed: process finished");
            }
            else
            {
                this.process.WaitForExit();
                Debug.WriteLine("non-Interactive session closed: process finished");
            }

            Debug.WriteLine("Plink: Stopped!");
            Session.OpenSessions.Remove(this.session);
            this.active = false;
        }