TerminalControl.cs
上传用户:szltgg
上传日期:2019-05-16
资源大小:604k
文件大小:13k
源码类别:

Telnet服务器

开发平台:

C#

  1. using System;
  2. using System.Resources;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Collections;
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.Threading;
  13. using Microsoft.Win32;
  14. using Poderosa;
  15. using Poderosa.Toolkit;
  16. using Poderosa.Connection;
  17. using Poderosa.ConnectionParam;
  18. using Poderosa.Terminal;
  19. using Poderosa.Forms;
  20. using Poderosa.Communication;
  21. using Poderosa.Config;
  22. using Poderosa.MacroEnv;
  23. using Poderosa.Text;
  24. using Poderosa.UI;
  25. using Granados.SSHC;
  26. namespace WalburySoftware
  27. {
  28.     public class TerminalControl : Control
  29.     {
  30.         #region fields
  31.         private string _username = "";
  32.         private string _password = "";
  33.         private string _hostname = "";
  34.         private string _identifyFile = "";
  35.         private AuthType _authType = AuthType.Password;
  36.         private ConnectionMethod _connectionMethod;
  37.         private TerminalPane _terminalPane;
  38.         #endregion
  39.         #region Constructors
  40.         public TerminalControl(string UserName, string Password, string Hostname, ConnectionMethod Method)
  41.         {
  42.             this._connectionMethod = Method;
  43.             this._hostname = Hostname;
  44.             this._password = Password;
  45.             this._username = UserName;
  46.             this.InitializeTerminalPane();
  47.         }
  48.         public TerminalControl()
  49.         {
  50.             this.InitializeTerminalPane();
  51.         }
  52.         private void InitializeTerminalPane()
  53.         {
  54.             if (GApp._frame == null)
  55.             {
  56.                 string[] args = new string[0];
  57.                 GApp.Run(args);
  58.                 GApp._frame._multiPaneControl.InitUI(null, GApp.Options);
  59.                 GEnv.InterThreadUIService.MainFrameHandle = GApp._frame.Handle;
  60.             }
  61.             this._terminalPane = new TerminalPane();
  62.             this.TerminalPane.Dock = DockStyle.Fill;
  63.             this.Controls.Add(this.TerminalPane);
  64.         }
  65.         #endregion
  66.         #region methods
  67.         public void ApplyNewDisplayDialog()
  68.         { 
  69.         
  70.         }
  71.         public void Connect()
  72.         {
  73.             #region old stuff
  74.             /*
  75.             Poderosa.ConnectionParam.LogType logType = Poderosa.ConnectionParam.LogType.Default;
  76.             string file = null;
  77.             if (this.TerminalPane.Connection != null)
  78.             {
  79.                 logType = this.TerminalPane.Connection.LogType;
  80.                 file = this.TerminalPane.Connection.LogPath;
  81.                 //GApp.GetConnectionCommandTarget().Close();
  82.                 this.TerminalPane.Connection.Close();
  83.                 this.TerminalPane.Detach();
  84.             }
  85.             SSHTerminalParam p = new SSHTerminalParam((Poderosa.ConnectionParam.ConnectionMethod)this.Method, this.Host, this.UserName, this.Password);
  86.             
  87.             GApp.GlobalCommandTarget.SilentNewConnection(p);
  88.             
  89.             if (file != null)
  90.                 this.SetLog((LogType) logType, file, true);
  91.             */
  92.             #endregion
  93.             // Save old log info in case this is a reconnect
  94.             Poderosa.ConnectionParam.LogType logType = Poderosa.ConnectionParam.LogType.Default;
  95.             string file = null;
  96.             if (this.TerminalPane.Connection != null)
  97.             {
  98.                 logType = this.TerminalPane.Connection.LogType;
  99.                 file = this.TerminalPane.Connection.LogPath;
  100.                 //GApp.GetConnectionCommandTarget().Close();
  101.                 this.TerminalPane.Connection.Close();
  102.                 this.TerminalPane.Detach();
  103.             }
  104.             try
  105.             {
  106.                 //------------------------------------------------------------------------
  107.                 SSHTerminalParam sshp = new SSHTerminalParam((Poderosa.ConnectionParam.ConnectionMethod)this.Method, this.Host, this.UserName, this.Password);
  108.                 sshp.AuthType = this.AuthType;
  109.                 sshp.IdentityFile = this.IdentifyFile;
  110.                 sshp.Encoding = EncodingType.ISO8859_1;
  111.                 sshp.Port = 22;
  112.                 sshp.RenderProfile = new RenderProfile();
  113.                 sshp.TerminalType = TerminalType.XTerm;
  114.                 CommunicationUtil.SilentClient s = new CommunicationUtil.SilentClient();
  115.                 Size sz = this.Size;
  116.                 SocketWithTimeout swt;
  117.                 swt = new SSHConnector((Poderosa.ConnectionParam.SSHTerminalParam)sshp, sz, sshp.Passphrase, (HostKeyCheckCallback)null);
  118.                 swt.AsyncConnect(s, sshp.Host, sshp.Port);
  119.                 ConnectionTag ct = s.Wait(swt);
  120.                 this.TerminalPane.FakeVisible = true;
  121.                 this.TerminalPane.Attach(ct);
  122.                 ct.Receiver.Listen();
  123.                 //-------------------------------------------------------------
  124.                 if (file != null)
  125.                     this.SetLog((LogType)logType, file, true);
  126.                 this.TerminalPane.ConnectionTag.RenderProfile = new RenderProfile();
  127.                 this.SetPaneColors(Color.LightBlue, Color.Black);
  128.             }
  129.             catch
  130.             {
  131.                 //MessageBox.Show(e.Message, "Connection Error");
  132.                 return;
  133.             }
  134.         }
  135.         public void Close()
  136.         {
  137.             if (this.TerminalPane.Connection != null)
  138.             {
  139.                 this.TerminalPane.Connection.Close();
  140.                 this.TerminalPane.Detach();
  141.             }
  142.         }
  143.         public void SendText(string command)
  144.         {
  145.             //GApp.GetConnectionCommandTarget().Connection.WriteChars(command.ToCharArray());
  146.             this.TerminalPane.Connection.WriteChars(command.ToCharArray());
  147.         }
  148.         public string GetLastLine()
  149.         {
  150.             return new string(this.TerminalPane.Document.LastLine.Text);
  151.         }
  152.         /// <summary>
  153.         /// Wait until some data is recieved
  154.         /// </summary>
  155.         public void WaitConnected()
  156.         {
  157.             while (this.TerminalPane.Connection.ReceivedDataSize == 0)
  158.             { }
  159.         }
  160.         /// <summary>
  161.         /// Create New Log
  162.         /// </summary>
  163.         /// <param name="logType">I guess just use Default all the time here</param>
  164.         /// <param name="File">This should be a full path. Example: @"C:Templogfilename.txt"</param>
  165.         /// <param name="append">Set this to true</param>
  166.         public void SetLog(LogType logType, string File, bool append)
  167.         {
  168.             // make sure directory exists
  169.             string dir = File.Substring(0, File.LastIndexOf(@""));
  170.             if (!System.IO.Directory.Exists(dir))
  171.               System.IO.Directory.CreateDirectory(dir);
  172.             this.TerminalPane.Connection.ResetLog((Poderosa.ConnectionParam.LogType)logType, File, append);
  173.             //this.TerminalPane.Connection.ResetLog(Poderosa.ConnectionParam.LogType.Default, File, append);
  174.         }
  175.         public void CommentLog(string comment)
  176.         {
  177.             DateTime dt = new DateTime();
  178.             string s = "rn----- Comment added " + dt.Date + " -----rn";
  179.             this.TerminalPane.Connection.TextLogger.Comment(s);
  180.             this.TerminalPane.Connection.BinaryLogger.Comment(s);
  181.             this.TerminalPane.Connection.TextLogger.Comment(comment);
  182.             this.TerminalPane.Connection.BinaryLogger.Comment(comment);
  183.             s = "rn----------------------------------------------rn";
  184.             this.TerminalPane.Connection.TextLogger.Comment(s);
  185.             this.TerminalPane.Connection.BinaryLogger.Comment(s);
  186.         }
  187.         public void SetPaneColors(Color TextColor, Color BackColor)
  188.         {
  189.             RenderProfile prof = this.TerminalPane.ConnectionTag.RenderProfile;
  190.             prof.BackColor = BackColor;
  191.             prof.ForeColor = TextColor;
  192.             
  193.             this.TerminalPane.ApplyRenderProfile(prof);
  194.         }
  195.         public void CopySelectedTextToClipboard()
  196.         {
  197.             //GApp.GlobalCommandTarget.Copy();
  198.             if (GEnv.TextSelection.IsEmpty) return;
  199.             string t = GEnv.TextSelection.GetSelectedText();
  200.             if (t.Length > 0)
  201.                 Clipboard.SetDataObject(t, false);
  202.             
  203.         }
  204.         public void PasteTextFromClipboard()
  205.         {
  206.             //GApp.GetConnectionCommandTarget().Paste();
  207.             string value = (string)Clipboard.GetDataObject().GetData("Text");
  208.             if (value == null || value.Length == 0 || this.TerminalPane == null || this.TerminalPane.ConnectionTag == null) return ;
  209.             PasteProcessor p = new PasteProcessor(this.TerminalPane.ConnectionTag, value);
  210.             p.Perform();
  211.             
  212.         }
  213.         #endregion
  214.         #region Properties
  215.         public AuthType AuthType
  216.         {
  217.             get
  218.             {
  219.                 return this._authType;
  220.             }
  221.             set
  222.             {
  223.                 this._authType = value; ;
  224.             }
  225.         }
  226.         public string IdentifyFile
  227.         {
  228.             get
  229.             {
  230.                 return this._identifyFile;
  231.             }
  232.             set
  233.             {
  234.                 this._identifyFile = value;
  235.             }
  236.         }
  237.         public TerminalPane TerminalPane
  238.         {
  239.             get
  240.             {
  241.                 return this._terminalPane;
  242.             }
  243.         }
  244.         public string UserName
  245.         {
  246.             get
  247.             {
  248.                 return this._username;
  249.             }
  250.             set
  251.             {
  252.                 this._username = value;
  253.             }
  254.         }
  255.         public string Password
  256.         {
  257.             get
  258.             {
  259.                 return this._password;
  260.             }
  261.             set
  262.             {
  263.                 this._password = value;
  264.             }
  265.         }
  266.         public string Host
  267.         {
  268.             get
  269.             {
  270.                 return this._hostname;
  271.             }
  272.             set
  273.             {
  274.                 this._hostname = value;
  275.             }
  276.         }
  277.         public ConnectionMethod Method
  278.         {
  279.             get
  280.             {
  281.                 return this._connectionMethod;
  282.             }
  283.             set
  284.             {
  285.                 this._connectionMethod = value;
  286.             }
  287.         }
  288.         public static int ScrollBackBuffer
  289.         {
  290.             get
  291.             {
  292.                 return GApp.Options.TerminalBufferSize;
  293.             }
  294.             set
  295.             {
  296.                 GApp.Options.TerminalBufferSize = value;
  297.              }
  298.         }
  299.         #endregion
  300.         #region overrides
  301.         protected override void OnKeyDown(KeyEventArgs e)
  302.         {
  303.             // this don't work :(
  304.             //Console.WriteLine(e.KeyCode);
  305.             base.OnKeyDown(e);
  306.         }
  307.         protected override void OnKeyPress(KeyPressEventArgs e)
  308.         {
  309.             //Console.WriteLine(e.KeyChar);
  310.             // this don't work :(
  311.             base.OnKeyPress(e);
  312.         }
  313.         protected override void OnGotFocus(EventArgs e)
  314.         {
  315.             base.OnGotFocus(e);
  316.             this.TerminalPane.Focus();
  317.         }
  318.         #endregion
  319.     }
  320.     #region enums
  321.     public enum ConnectionMethod
  322.     {
  323.         /// <summary>
  324.         /// Telnet
  325.         /// </summary>
  326.         Telnet,
  327.         /// <summary>
  328.         /// SSH1
  329.         /// </summary>
  330.         SSH1,
  331.         /// <summary>
  332.         /// SSH2
  333.         /// </summary>
  334.         SSH2
  335.     }
  336.     public enum LogType
  337.     {
  338.         /// <summary>
  339.         /// <ja>儘僌偼偲傝傑偣傫丅</ja>
  340.         /// <en>The log is not recorded.</en>
  341.         /// </summary>
  342.         [EnumValue(Description = "Enum.LogType.None")]
  343.         None,
  344.         /// <summary>
  345.         /// <ja>僥僉僗僩儌乕僪偺儘僌偱偡丅偙傟偑昗弨偱偡丅</ja>
  346.         /// <en>The log is a plain text file. This is standard.</en>
  347.         /// </summary>
  348.         [EnumValue(Description = "Enum.LogType.Default")]
  349.         Default,
  350.         /// <summary>
  351.         /// <ja>僶僀僫儕儌乕僪偺儘僌偱偡丅</ja>
  352.         /// <en>The log is a binary file.</en>
  353.         /// </summary>
  354.         [EnumValue(Description = "Enum.LogType.Binary")]
  355.         Binary,
  356.         /// <summary>
  357.         /// <ja>XML偱曐懚偟傑偡丅傑偨撪晹揑側僶僌捛愓偵偍偄偰偙偺儌乕僪偱偺儘僌嵦庢傪偍婅偄偡傞偙偲偑偁傝傑偡丅</ja>
  358.         /// <en>The log is an XML file. We may ask you to record the log in this type for debugging.</en>
  359.         /// </summary>
  360.         [EnumValue(Description = "Enum.LogType.Xml")]
  361.         Xml
  362.     }    
  363.     #endregion
  364. }