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

Telnet服务器

开发平台:

C#

  1. /*
  2. * Copyright (c) 2005 Poderosa Project, All Rights Reserved.
  3. * $Id: LoginDialog.cs,v 1.2 2005/04/20 08:45:45 okajima Exp $
  4. */
  5. using System;
  6. using System.Drawing;
  7. using System.Collections;
  8. using System.Collections.Specialized;
  9. using System.ComponentModel;
  10. using System.Windows.Forms;
  11. using System.Diagnostics;
  12. using System.Text;
  13. using System.Net;
  14. using System.Net.Sockets;
  15. using System.IO;
  16. using Poderosa.Toolkit;
  17. using EnumDescAttributeT = Poderosa.Toolkit.EnumDescAttribute;
  18. using Poderosa.Communication;
  19. using Poderosa.Connection;
  20. using Poderosa.ConnectionParam;
  21. using Poderosa.Terminal;
  22. using Poderosa.Config;
  23. using Poderosa.SSH;
  24. using Poderosa.UI;
  25. using Granados.SSHC;
  26. namespace Poderosa.Forms
  27. {
  28. internal class LoginDialog : System.Windows.Forms.Form, ISocketWithTimeoutClient
  29.     {
  30.         #region fields
  31.         private string _errorMessage;
  32. private bool _initializing;
  33. private bool _firstFlag;
  34. private ConnectionHistory _history;
  35. private ConnectionTag _result;
  36. private SocketWithTimeout _connector;
  37. private IntPtr _savedHWND;
  38. private System.ComponentModel.Container components = null;
  39. private System.Windows.Forms.Label _hostLabel;
  40. public ComboBox _hostBox;
  41. private System.Windows.Forms.Label _portLabel;
  42.         public ComboBox _portBox;
  43. private System.Windows.Forms.Label _methodLabel;
  44.         public ComboBox _methodBox;
  45. private System.Windows.Forms.GroupBox _sshGroup;
  46. private System.Windows.Forms.Label _usernameLabel;
  47.         public ComboBox _userNameBox;
  48. private System.Windows.Forms.Label _authenticationLabel;
  49.         public ComboBox _authOptions;
  50. private System.Windows.Forms.Label _passphraseLabel;
  51.         public TextBox _passphraseBox;
  52. private System.Windows.Forms.Label _privateKeyLabel;
  53.         public TextBox _privateKeyFile;
  54. private Button _privateKeySelect;
  55. private System.Windows.Forms.GroupBox _terminalGroup;
  56.         public ComboBox _encodingBox;
  57. private System.Windows.Forms.Label _encodingLabel;
  58. private System.Windows.Forms.Label _logFileLabel;
  59.         public ComboBox _logFileBox;
  60. private Button _selectLogButton;
  61. private System.Windows.Forms.Label _newLineLabel;
  62. private System.Windows.Forms.Label _localEchoLabel;
  63.         public ComboBox _localEchoBox;
  64.         public ComboBox _newLineBox;
  65. private System.Windows.Forms.Label _logTypeLabel;
  66.         public ComboBox _logTypeBox;
  67.         public ComboBox _terminalTypeBox;
  68. private System.Windows.Forms.Label _terminalTypeLabel;
  69. private Button _loginButton;
  70. private Button _cancelButton;
  71.         #endregion
  72.         public LoginDialog()
  73. {
  74. _firstFlag = true;
  75. _initializing = true;
  76. _history = GApp.ConnectionHistory;
  77. //
  78. // Windows 僼僅乕儉 僨僓僀僫 僒億乕僩偵昁梫偱偡丅
  79. //
  80. InitializeComponent();
  81. InitializeText();
  82. //
  83. // TODO: InitializeComponent 屇傃弌偟偺屻偵丄僐儞僗僩儔僋僞 僐乕僪傪捛壛偟偰偔偩偝偄丅
  84. //
  85. InitializeLoginParams();
  86. _initializing = false;
  87. }
  88. protected override void Dispose( bool disposing )
  89. {
  90. if( disposing )
  91. {
  92. if(components != null)
  93. {
  94. components.Dispose();
  95. }
  96. }
  97. base.Dispose( disposing );
  98. }
  99.         #region Windows Form Designer generated code
  100. private void InitializeComponent()
  101. {
  102. this._loginButton = new System.Windows.Forms.Button();
  103. this._cancelButton = new System.Windows.Forms.Button();
  104. this._sshGroup = new System.Windows.Forms.GroupBox();
  105. this._hostLabel = new System.Windows.Forms.Label();
  106. this._hostBox = new ComboBox();
  107. this._methodLabel = new System.Windows.Forms.Label();
  108. this._methodBox = new ComboBox();
  109. this._portLabel = new System.Windows.Forms.Label();
  110. this._portBox = new ComboBox();
  111. this._authenticationLabel = new System.Windows.Forms.Label();
  112. this._authOptions = new ComboBox();
  113. this._passphraseLabel = new System.Windows.Forms.Label();
  114. this._passphraseBox = new TextBox();
  115. this._privateKeyLabel = new System.Windows.Forms.Label();
  116. this._privateKeyFile = new TextBox();
  117. this._privateKeySelect = new Button();
  118. this._usernameLabel = new System.Windows.Forms.Label();
  119. this._userNameBox = new ComboBox();
  120. this._terminalGroup = new System.Windows.Forms.GroupBox();
  121. this._newLineBox = new ComboBox();
  122. this._localEchoBox = new ComboBox();
  123. this._localEchoLabel = new System.Windows.Forms.Label();
  124. this._newLineLabel = new System.Windows.Forms.Label();
  125. this._logFileBox = new ComboBox();
  126. this._logFileLabel = new System.Windows.Forms.Label();
  127. this._encodingBox = new ComboBox();
  128. this._encodingLabel = new System.Windows.Forms.Label();
  129. this._selectLogButton = new Button();
  130. this._logTypeLabel = new System.Windows.Forms.Label();
  131. this._logTypeBox = new ComboBox();
  132. this._terminalTypeBox = new ComboBox();
  133. this._terminalTypeLabel = new Label();
  134. this._sshGroup.SuspendLayout();
  135. this._terminalGroup.SuspendLayout();
  136. this.SuspendLayout();
  137. // 
  138. // _hostLabel
  139. // 
  140. this._hostLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  141. this._hostLabel.Location = new System.Drawing.Point(16, 12);
  142. this._hostLabel.Name = "_hostLabel";
  143. this._hostLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  144. this._hostLabel.Size = new System.Drawing.Size(80, 16);
  145. this._hostLabel.TabIndex = 0;
  146. this._hostLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  147. // 
  148. // _hostBox
  149. // 
  150. this._hostBox.Location = new System.Drawing.Point(104, 8);
  151. this._hostBox.Name = "_hostBox";
  152. this._hostBox.Size = new System.Drawing.Size(208, 20);
  153. this._hostBox.TabIndex = 1;
  154. this._hostBox.SelectedIndexChanged += new System.EventHandler(this.OnHostIsSelected);
  155. // 
  156. // _methodLabel
  157. // 
  158. this._methodLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  159. this._methodLabel.Location = new System.Drawing.Point(16, 36);
  160. this._methodLabel.Name = "_methodLabel";
  161. this._methodLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  162. this._methodLabel.Size = new System.Drawing.Size(80, 16);
  163. this._methodLabel.TabIndex = 2;
  164. this._methodLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  165. // 
  166. // _methodBox
  167. // 
  168. this._methodBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  169. this._methodBox.Items.AddRange(new object[] { "Telnet",
  170. "SSH1",
  171. "SSH2"
  172. });
  173. this._methodBox.Location = new System.Drawing.Point(104, 32);
  174. this._methodBox.Name = "_methodBox";
  175. this._methodBox.Size = new System.Drawing.Size(208, 20);
  176. this._methodBox.TabIndex = 3;
  177. this._methodBox.SelectedIndexChanged += new System.EventHandler(this.AdjustConnectionUI);
  178. // 
  179. // _portLabel
  180. // 
  181. this._portLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  182. this._portLabel.Location = new System.Drawing.Point(16, 60);
  183. this._portLabel.Name = "_portLabel";
  184. this._portLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  185. this._portLabel.Size = new System.Drawing.Size(80, 16);
  186. this._portLabel.TabIndex = 4;
  187. this._portLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  188. // 
  189. // _portBox
  190. // 
  191. this._portBox.Location = new System.Drawing.Point(104, 56);
  192. this._portBox.Name = "_portBox";
  193. this._portBox.Size = new System.Drawing.Size(208, 20);
  194. this._portBox.TabIndex = 5;
  195. // 
  196. // _sshGroup
  197. // 
  198. this._sshGroup.Controls.AddRange(new System.Windows.Forms.Control[] {
  199. this._privateKeyFile,
  200. this._authOptions,
  201. this._privateKeyLabel,
  202. this._passphraseBox,
  203. this._userNameBox,
  204. this._authenticationLabel,
  205. this._passphraseLabel,
  206. this._usernameLabel,
  207. this._privateKeySelect});
  208. this._sshGroup.Location = new System.Drawing.Point(8, 88);
  209. this._sshGroup.Name = "_sshGroup";
  210. this._sshGroup.FlatStyle = FlatStyle.System;
  211. this._sshGroup.Size = new System.Drawing.Size(312, 112);
  212. this._sshGroup.TabIndex = 6;
  213. this._sshGroup.TabStop = false;
  214. // 
  215. // _usernameLabel
  216. // 
  217. this._usernameLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  218. this._usernameLabel.Location = new System.Drawing.Point(8, 16);
  219. this._usernameLabel.Name = "_usernameLabel";
  220. this._usernameLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  221. this._usernameLabel.Size = new System.Drawing.Size(80, 16);
  222. this._usernameLabel.TabIndex = 7;
  223. this._usernameLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  224. // 
  225. // _userNameBox
  226. // 
  227. this._userNameBox.Location = new System.Drawing.Point(96, 16);
  228. this._userNameBox.Name = "_userNameBox";
  229. this._userNameBox.Size = new System.Drawing.Size(200, 20);
  230. this._userNameBox.TabIndex = 8;
  231. // 
  232. // _authenticationLabel
  233. // 
  234. this._authenticationLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  235. this._authenticationLabel.Location = new System.Drawing.Point(8, 40);
  236. this._authenticationLabel.Name = "_authenticationLabel";
  237. this._authenticationLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  238. this._authenticationLabel.Size = new System.Drawing.Size(80, 16);
  239. this._authenticationLabel.TabIndex = 9;
  240. this._authenticationLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  241. // 
  242. // _authOptions
  243. // 
  244. this._authOptions.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  245. this._authOptions.Location = new System.Drawing.Point(96, 40);
  246. this._authOptions.Name = "_authOptions";
  247. this._authOptions.Size = new System.Drawing.Size(200, 20);
  248. this._authOptions.TabIndex = 10;
  249. this._authOptions.SelectedIndexChanged += new System.EventHandler(this.AdjustAuthenticationUI);
  250. // 
  251. // _passphraseLabel
  252. // 
  253. this._passphraseLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  254. this._passphraseLabel.Location = new System.Drawing.Point(8, 64);
  255. this._passphraseLabel.Name = "_passphraseLabel";
  256. this._passphraseLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  257. this._passphraseLabel.Size = new System.Drawing.Size(80, 16);
  258. this._passphraseLabel.TabIndex = 11;
  259. this._passphraseLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  260. // 
  261. // _passphraseBox
  262. // 
  263. this._passphraseBox.Location = new System.Drawing.Point(96, 64);
  264. this._passphraseBox.Name = "_passphraseBox";
  265. this._passphraseBox.PasswordChar = '*';
  266. this._passphraseBox.Size = new System.Drawing.Size(200, 19);
  267. this._passphraseBox.TabIndex = 12;
  268. this._passphraseBox.Text = "";
  269. // 
  270. // _privateKeyLabel
  271. // 
  272. this._privateKeyLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  273. this._privateKeyLabel.Location = new System.Drawing.Point(8, 88);
  274. this._privateKeyLabel.Name = "_privateKeyLabel";
  275. this._privateKeyLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  276. this._privateKeyLabel.Size = new System.Drawing.Size(72, 16);
  277. this._privateKeyLabel.TabIndex = 14;
  278. this._privateKeyLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  279. // 
  280. // _privateKeyFile
  281. // 
  282. this._privateKeyFile.Location = new System.Drawing.Point(96, 88);
  283. this._privateKeyFile.Name = "_privateKeyFile";
  284. this._privateKeyFile.Size = new System.Drawing.Size(176, 19);
  285. this._privateKeyFile.TabIndex = 15;
  286. this._privateKeyFile.Text = "";
  287. // 
  288. // _privateKeySelect
  289. // 
  290. this._privateKeySelect.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  291. this._privateKeySelect.ImageIndex = 0;
  292. this._privateKeySelect.FlatStyle = FlatStyle.System;
  293. this._privateKeySelect.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  294. this._privateKeySelect.Location = new System.Drawing.Point(272, 88);
  295. this._privateKeySelect.Name = "_privateKeySelect";
  296. this._privateKeySelect.RightToLeft = System.Windows.Forms.RightToLeft.No;
  297. this._privateKeySelect.Size = new System.Drawing.Size(19, 19);
  298. this._privateKeySelect.TabIndex = 16;
  299. this._privateKeySelect.Text = "...";
  300. this._privateKeySelect.Click += new System.EventHandler(this.OnOpenPrivateKey);
  301. // 
  302. // _terminalGroup
  303. // 
  304. this._terminalGroup.Controls.AddRange(new System.Windows.Forms.Control[] {
  305.  this._logTypeBox,
  306.  this._logTypeLabel,
  307.  this._newLineBox,
  308.  this._localEchoBox,
  309.  this._localEchoLabel,
  310.  this._newLineLabel,
  311.  this._logFileBox,
  312.  this._logFileLabel,
  313.  this._encodingBox,
  314.  this._encodingLabel,
  315.  this._selectLogButton, this._terminalTypeLabel, this._terminalTypeBox});
  316. this._terminalGroup.Location = new System.Drawing.Point(8, 208);
  317. this._terminalGroup.Name = "_terminalGroup";
  318. this._terminalGroup.FlatStyle = FlatStyle.System;
  319. this._terminalGroup.Size = new System.Drawing.Size(312, 168);
  320. this._terminalGroup.TabIndex = 17;
  321. this._terminalGroup.TabStop = false;
  322. // 
  323. // _logTypeLabel
  324. // 
  325. this._logTypeLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  326. this._logTypeLabel.Location = new System.Drawing.Point(8, 16);
  327. this._logTypeLabel.Name = "_logTypeLabel";
  328. this._logTypeLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  329. this._logTypeLabel.Size = new System.Drawing.Size(96, 16);
  330. this._logTypeLabel.TabIndex = 18;
  331. this._logTypeLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  332. // 
  333. // _logTypeBox
  334. // 
  335. this._logTypeBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  336. this._logTypeBox.Items.AddRange(EnumDescAttributeT.For(typeof(LogType)).DescriptionCollection());
  337. this._logTypeBox.Location = new System.Drawing.Point(112, 16);
  338. this._logTypeBox.Name = "_logTypeBox";
  339. this._logTypeBox.Size = new System.Drawing.Size(96, 20);
  340. this._logTypeBox.TabIndex = 19;
  341. this._logTypeBox.SelectionChangeCommitted += new EventHandler(OnLogTypeChanged);
  342. // 
  343. // _logFileLabel
  344. // 
  345. this._logFileLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  346. this._logFileLabel.Location = new System.Drawing.Point(8, 40);
  347. this._logFileLabel.Name = "_logFileLabel";
  348. this._logFileLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  349. this._logFileLabel.Size = new System.Drawing.Size(88, 16);
  350. this._logFileLabel.TabIndex = 20;
  351. this._logFileLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  352. // 
  353. // _logFileBox
  354. // 
  355. this._logFileBox.Location = new System.Drawing.Point(112, 40);
  356. this._logFileBox.Name = "_logFileBox";
  357. this._logFileBox.Size = new System.Drawing.Size(160, 20);
  358. this._logFileBox.TabIndex = 21;
  359. // 
  360. // _selectLogButton
  361. // 
  362. this._selectLogButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  363. this._selectLogButton.ImageIndex = 0;
  364. this._selectLogButton.FlatStyle = FlatStyle.System;
  365. this._selectLogButton.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  366. this._selectLogButton.Location = new System.Drawing.Point(272, 40);
  367. this._selectLogButton.Name = "_selectLogButton";
  368. this._selectLogButton.RightToLeft = System.Windows.Forms.RightToLeft.No;
  369. this._selectLogButton.Size = new System.Drawing.Size(19, 19);
  370. this._selectLogButton.TabIndex = 22;
  371. this._selectLogButton.Text = "...";
  372. this._selectLogButton.Click += new System.EventHandler(this.SelectLog);
  373. // 
  374. // _encodingLabel
  375. // 
  376. this._encodingLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  377. this._encodingLabel.Location = new System.Drawing.Point(8, 64);
  378. this._encodingLabel.Name = "_encodingLabel";
  379. this._encodingLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  380. this._encodingLabel.Size = new System.Drawing.Size(96, 16);
  381. this._encodingLabel.TabIndex = 23;
  382. this._encodingLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  383. // 
  384. // _encodingBox
  385. // 
  386. this._encodingBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  387. this._encodingBox.Items.AddRange(EnumDescAttributeT.For(typeof(EncodingType)).DescriptionCollection());
  388. this._encodingBox.Location = new System.Drawing.Point(112, 64);
  389. this._encodingBox.Name = "_encodingBox";
  390. this._encodingBox.Size = new System.Drawing.Size(96, 20);
  391. this._encodingBox.TabIndex = 24;
  392. // 
  393. // _localEchoLabel
  394. // 
  395. this._localEchoLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  396. this._localEchoLabel.Location = new System.Drawing.Point(8, 88);
  397. this._localEchoLabel.Name = "_localEchoLabel";
  398. this._localEchoLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  399. this._localEchoLabel.Size = new System.Drawing.Size(96, 16);
  400. this._localEchoLabel.TabIndex = 25;
  401. this._localEchoLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  402. // 
  403. // _localEchoBox
  404. // 
  405. this._localEchoBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  406. this._localEchoBox.Items.AddRange(new object[] {
  407.    GApp.Strings.GetString("Common.DoNot"),
  408.    GApp.Strings.GetString("Common.Do")});
  409. this._localEchoBox.Location = new System.Drawing.Point(112, 88);
  410. this._localEchoBox.Name = "_localEchoBox";
  411. this._localEchoBox.Size = new System.Drawing.Size(96, 20);
  412. this._localEchoBox.TabIndex = 26;
  413. // 
  414. // _newLineLabel
  415. // 
  416. this._newLineLabel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  417. this._newLineLabel.Location = new System.Drawing.Point(8, 112);
  418. this._newLineLabel.Name = "_newLineLabel";
  419. this._newLineLabel.RightToLeft = System.Windows.Forms.RightToLeft.No;
  420. this._newLineLabel.Size = new System.Drawing.Size(96, 16);
  421. this._newLineLabel.TabIndex = 27;
  422. this._newLineLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  423. // 
  424. // _newLineBox
  425. // 
  426. this._newLineBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  427. this._newLineBox.Items.AddRange(EnumDescAttributeT.For(typeof(NewLine)).DescriptionCollection());
  428. this._newLineBox.Location = new System.Drawing.Point(112, 112);
  429. this._newLineBox.Name = "_newLineBox";
  430. this._newLineBox.Size = new System.Drawing.Size(96, 20);
  431. this._newLineBox.TabIndex = 28;
  432. // 
  433. // _terminalTypeLabel
  434. // 
  435. this._terminalTypeLabel.Location = new System.Drawing.Point(8, 136);
  436. this._terminalTypeLabel.Name = "_terminalTypeLabel";
  437. this._terminalTypeLabel.Size = new System.Drawing.Size(96, 23);
  438. this._terminalTypeLabel.TabIndex = 29;
  439. this._terminalTypeLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  440. // 
  441. // _terminalTypeBox
  442. // 
  443. this._terminalTypeBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  444. this._terminalTypeBox.Items.AddRange(EnumDescAttributeT.For(typeof(TerminalType)).DescriptionCollection());
  445. this._terminalTypeBox.Location = new System.Drawing.Point(112, 136);
  446. this._terminalTypeBox.Name = "_terminalType";
  447. this._terminalTypeBox.Size = new System.Drawing.Size(96, 20);
  448. this._terminalTypeBox.TabIndex = 30;
  449. // 
  450. // _loginButton
  451. // 
  452. this._loginButton.ImageIndex = 0;
  453. this._loginButton.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  454. this._loginButton.Location = new System.Drawing.Point(160, 384);
  455. this._loginButton.Name = "_loginButton";
  456. this._loginButton.FlatStyle = FlatStyle.System;
  457. this._loginButton.RightToLeft = System.Windows.Forms.RightToLeft.No;
  458. this._loginButton.Size = new System.Drawing.Size(72, 25);
  459. this._loginButton.TabIndex = 29;
  460. this._loginButton.Click += new System.EventHandler(this.OnOK);
  461. // 
  462. // _cancelButton
  463. // 
  464. this._cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  465. this._cancelButton.ImageIndex = 0;
  466. this._cancelButton.FlatStyle = FlatStyle.System;
  467. this._cancelButton.ImeMode = System.Windows.Forms.ImeMode.NoControl;
  468. this._cancelButton.Location = new System.Drawing.Point(248, 384);
  469. this._cancelButton.Name = "_cancelButton";
  470. this._cancelButton.RightToLeft = System.Windows.Forms.RightToLeft.No;
  471. this._cancelButton.Size = new System.Drawing.Size(72, 25);
  472. this._cancelButton.TabIndex = 30;
  473. // 
  474. // LoginDialog
  475. // 
  476. this.AcceptButton = this._loginButton;
  477. this.Anchor = System.Windows.Forms.AnchorStyles.None;
  478. this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
  479. this.CancelButton = this._cancelButton;
  480. this.ClientSize = new System.Drawing.Size(330, 415);
  481. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  482.   this._terminalGroup,
  483.   this._sshGroup,
  484.   this._hostBox,
  485.   this._methodBox,
  486.   this._portBox,
  487.   this._cancelButton,
  488.   this._loginButton,
  489.   this._methodLabel,
  490.   this._portLabel,
  491.   this._hostLabel});
  492. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  493. this.MaximizeBox = false;
  494. this.MinimizeBox = false;
  495. this.Name = "LoginDialog";
  496. this.ShowInTaskbar = false;
  497. //this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  498.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  499. this._sshGroup.ResumeLayout(false);
  500. this._terminalGroup.ResumeLayout(false);
  501. this.ResumeLayout(false);
  502. }
  503. #endregion
  504. private void InitializeText() {
  505. this._hostLabel.Text = GApp.Strings.GetString("Form.LoginDialog._hostLabel");
  506. this._portLabel.Text = GApp.Strings.GetString("Form.LoginDialog._portLabel");
  507. this._methodLabel.Text = GApp.Strings.GetString("Form.LoginDialog._methodLabel");
  508. this._sshGroup.Text = GApp.Strings.GetString("Form.LoginDialog._sshGroup");
  509. this._privateKeyLabel.Text = GApp.Strings.GetString("Form.LoginDialog._privateKeyLabel");
  510. this._authenticationLabel.Text = GApp.Strings.GetString("Form.LoginDialog._authenticationLabel");
  511. this._passphraseLabel.Text = GApp.Strings.GetString("Form.LoginDialog._passphraseLabel");
  512. this._usernameLabel.Text = GApp.Strings.GetString("Form.LoginDialog._usernameLabel");
  513. this._terminalGroup.Text = GApp.Strings.GetString("Form.LoginDialog._terminalGroup");
  514. this._localEchoLabel.Text = GApp.Strings.GetString("Form.LoginDialog._localEchoLabel");
  515. this._newLineLabel.Text = GApp.Strings.GetString("Form.LoginDialog._newLineLabel");
  516. this._logFileLabel.Text = GApp.Strings.GetString("Form.LoginDialog._logFileLabel");
  517. this._encodingLabel.Text = GApp.Strings.GetString("Form.LoginDialog._encodingLabel");
  518. this._logTypeLabel.Text = GApp.Strings.GetString("Form.LoginDialog._logTypeLabel");
  519. this._terminalTypeLabel.Text = GApp.Strings.GetString("Form.LoginDialog._terminalTypeLabel");
  520. this._loginButton.Text = GApp.Strings.GetString("Common.OK");
  521. this._cancelButton.Text = GApp.Strings.GetString("Common.Cancel");
  522. this.Text = GApp.Strings.GetString("Form.LoginDialog.Text");
  523. _authOptions.Items.AddRange(EnumDescAttributeT.For(typeof(AuthType)).DescriptionCollection());
  524. }
  525. private void AdjustConnectionUI(object sender, System.EventArgs e) {
  526. if(_initializing) return;
  527. if(_methodBox.Text=="Telnet") {
  528. _portBox.SelectedIndex = 0; //Telnet:23
  529. }
  530. else {
  531. _portBox.SelectedIndex = 1; //SSH:22
  532. if(_authOptions.SelectedIndex==-1) _authOptions.SelectedIndex=0;
  533. }
  534. EnableValidControls();
  535. }
  536. private void AdjustAuthenticationUI(object sender, System.EventArgs e) {
  537. EnableValidControls();
  538. }
  539. private void InitializeLoginParams() {
  540. StringCollection c = _history.Hosts;
  541. foreach(string h in c) _hostBox.Items.Add(h);
  542. if(_hostBox.Items.Count>0) _hostBox.SelectedIndex = 0;
  543. c = _history.Accounts;
  544. foreach(string a in c) _userNameBox.Items.Add(a);
  545. if(_userNameBox.Items.Count>0) _userNameBox.SelectedIndex = 0;
  546. int[] ic = _history.Ports;
  547. foreach(int p in ic) _portBox.Items.Add(PortDescription(p));
  548. if(_hostBox.Items.Count > 0) {
  549. TCPTerminalParam last = _history.SearchByHost((string)_hostBox.Items[0]);
  550. if(last!=null) ApplyParam(last);
  551. }
  552. c = _history.LogPaths;
  553. foreach(string p in c) _logFileBox.Items.Add(p);
  554. if(GApp.Options.DefaultLogType!=LogType.None) {
  555. _logTypeBox.SelectedIndex = (int)GApp.Options.DefaultLogType;
  556. string t = GUtil.CreateLogFileName(null);
  557. _logFileBox.Items.Add(t);
  558. _logFileBox.Text = t;
  559. }
  560. else
  561. _logTypeBox.SelectedIndex = 0;
  562. }
  563. private void EnableValidControls() {
  564. bool ssh = _methodBox.Text!="Telnet";
  565. bool pubkey = _authOptions.SelectedIndex==(int)AuthType.PublicKey;
  566. bool kbd = _authOptions.SelectedIndex==(int)AuthType.KeyboardInteractive;
  567. _userNameBox.Enabled = ssh;
  568. _authOptions.Enabled = ssh;
  569. _passphraseBox.Enabled = ssh && (pubkey || !kbd);
  570. _privateKeyFile.Enabled = ssh && pubkey;
  571. _privateKeySelect.Enabled = ssh && pubkey;
  572. bool e = ((LogType)EnumDescAttributeT.For(typeof(LogType)).FromDescription(_logTypeBox.Text, LogType.None)!=LogType.None);
  573. _logFileBox.Enabled = e;
  574. _selectLogButton.Enabled = e;
  575. }
  576. public void ApplyParam(TCPTerminalParam param) {
  577. _initializing = true;
  578. _methodBox.SelectedIndex = (int)param.Method;
  579. _portBox.SelectedIndex = _portBox.FindStringExact(PortDescription(param.Port));
  580. _methodBox.SelectedIndex = _methodBox.FindStringExact(param.Method.ToString());
  581. if(param.IsSSH) {
  582. SSHTerminalParam sp = (SSHTerminalParam)param;
  583. _userNameBox.SelectedIndex = _userNameBox.FindStringExact(sp.Account);
  584. _passphraseBox.Text = sp.Passphrase;
  585. if(sp.AuthType==AuthType.PublicKey)
  586. _privateKeyFile.Text = sp.IdentityFile;
  587. else
  588. _privateKeyFile.Text = "";
  589. _authOptions.SelectedIndex = (int)sp.AuthType;
  590. }
  591. _encodingBox.SelectedIndex = (int)param.EncodingProfile.Type;
  592. _newLineBox.SelectedIndex = (int)param.TransmitNL;
  593. _localEchoBox.SelectedIndex = param.LocalEcho? 1 : 0;
  594. _terminalTypeBox.SelectedIndex = (int)param.TerminalType;
  595. _initializing = false;
  596. EnableValidControls();
  597. }
  598. public ConnectionTag Result {
  599. get {
  600. return _result;
  601. }
  602. }
  603. private void OnHostIsSelected(object sender, System.EventArgs e) {
  604. if(_initializing) return;
  605. string host = _hostBox.Text;
  606. TCPTerminalParam param = _history.SearchByHost(host);
  607. Debug.Assert(param!=null);
  608. ApplyParam(param);
  609. }
  610. private static int ParsePort(string text) {
  611. //昿弌偺傗偮
  612. if(text.IndexOf("(22)")!=-1)
  613. return 22;
  614. if(text.IndexOf("(23)")!=-1)
  615. return 23;
  616. try {
  617. return Int32.Parse(text);
  618. }
  619. catch(FormatException) {
  620. throw new FormatException(String.Format(GApp.Strings.GetString("Message.LoginDialog.InvalidPort"), text));
  621. }
  622. }
  623. private static ConnectionMethod ParseMethod(string text) {
  624. if(text.IndexOf("SSH1")!=-1)
  625. return ConnectionMethod.SSH1;
  626. else if(text.IndexOf("SSH2")!=-1)
  627. return ConnectionMethod.SSH2;
  628. else if(text.IndexOf("Telnet")!=-1)
  629. return ConnectionMethod.Telnet;
  630. else
  631. throw new ArgumentException("unknown method "+text);
  632. }
  633. private static string PortDescription(int port) {
  634. if(port==22)
  635. return "SSH(22)";
  636. else if(port==23)
  637. return "Telnet(23)";
  638. else
  639. return port.ToString();
  640. }
  641. private void OnOpenPrivateKey(object sender, System.EventArgs e) {
  642. string fn = GCUtil.SelectPrivateKeyFileByDialog(this);
  643. if(fn!=null) _privateKeyFile.Text = fn;
  644. _privateKeySelect.Focus(); //偳偭偪偵偟偰傕師偺僼僅乕僇僗偼尞慖戰儃僞儞傊
  645. }
  646. public void OnOK(object sender, System.EventArgs e) {
  647. this.DialogResult = DialogResult.None;
  648. TCPTerminalParam param = ValidateContent();
  649. if(param==null) return;  //僷儔儊乕僞偵岆傝偑偁傟偽懄扙弌
  650. _loginButton.Enabled = false;
  651. _cancelButton.Enabled = false;
  652. this.Cursor = Cursors.WaitCursor;
  653. this.Text = GApp.Strings.GetString("Caption.LoginDialog.Connecting");
  654. _savedHWND = this.Handle;
  655. HostKeyCheckCallback checker = null;
  656. if(param.IsSSH)
  657. checker = new HostKeyCheckCallback(new HostKeyChecker(this, (SSHTerminalParam)param).CheckHostKeyCallback);
  658. _connector = CommunicationUtil.StartNewConnection(this, param, _passphraseBox.Text, checker);
  659. if(_connector==null) ClearConnectingState();
  660. }
  661. //擖椡撪梕偵岆傝偑偁傟偽偦傟傪寈崘偟偰null傪曉偡丅側偗傟偽昁梫側偲偙傠傪杽傔偨TCPTerminalParam傪曉偡
  662. private TCPTerminalParam ValidateContent() {
  663. string msg = null;
  664. TCPTerminalParam p = null;
  665. SSHTerminalParam sp = null;
  666. try {
  667. ConnectionMethod m = ParseMethod(_methodBox.Text);
  668. if(m==ConnectionMethod.Telnet)
  669. p = new TelnetTerminalParam("");
  670. else {
  671. p = sp = new SSHTerminalParam(ConnectionMethod.SSH2, "", "", "");
  672. sp.Method = m;
  673. sp.Account = _userNameBox.Text;
  674. }
  675. p.Host = _hostBox.Text;
  676. try {
  677. p.Port = ParsePort(_portBox.Text);
  678. }
  679. catch(FormatException ex) {
  680. msg = ex.Message;
  681. }
  682. if(_hostBox.Text.Length==0)
  683. msg = GApp.Strings.GetString("Message.LoginDialog.HostIsEmpty");
  684. p.LogType = (LogType)EnumDescAttributeT.For(typeof(LogType)).FromDescription(_logTypeBox.Text, LogType.None);
  685.                 if(p.LogType!=LogType.None) {
  686. p.LogPath = _logFileBox.Text;
  687. if(p.LogPath==GUtil.CreateLogFileName(null)) p.LogPath = GUtil.CreateLogFileName(_hostBox.Text);
  688. LogFileCheckResult r = GCUtil.CheckLogFileName(p.LogPath, this);
  689. if(r==LogFileCheckResult.Cancel || r==LogFileCheckResult.Error) return null;
  690. p.LogAppend = (r==LogFileCheckResult.Append);
  691. }
  692. if(p.IsSSH) {
  693. Debug.Assert(sp!=null);
  694. sp.AuthType = (AuthType)_authOptions.SelectedIndex;
  695. if(sp.AuthType==AuthType.PublicKey) {
  696. if(!File.Exists(_privateKeyFile.Text))
  697. msg = GApp.Strings.GetString("Message.LoginDialog.KeyFileNotExist");
  698. else
  699. sp.IdentityFile = _privateKeyFile.Text;
  700. }
  701. }
  702. p.EncodingProfile = EncodingProfile.Get((EncodingType)_encodingBox.SelectedIndex);
  703. p.LocalEcho = _localEchoBox.SelectedIndex==1;
  704. p.TransmitNL = (NewLine)EnumDescAttributeT.For(typeof(NewLine)).FromDescription(_newLineBox.Text, NewLine.CR);
  705. p.TerminalType = (TerminalType)_terminalTypeBox.SelectedIndex;
  706. if(msg!=null) {
  707. ShowError(msg);
  708. return null;
  709. }
  710. else
  711. return p;
  712. }
  713. catch(Exception ex) {
  714. GUtil.Warning(this, ex.Message);
  715. return null;
  716. }
  717. }
  718. protected override void OnActivated(EventArgs args) {
  719. if(_firstFlag) {
  720. _firstFlag = false;
  721. _hostBox.Focus();
  722. }
  723. }
  724. private void SelectLog(object sender, System.EventArgs e) {
  725. string fn = GCUtil.SelectLogFileByDialog(this);
  726. if(fn!=null) _logFileBox.Text = fn;
  727. }
  728. private void OnLogTypeChanged(object sender, System.EventArgs e) {
  729. if(_initializing) return;
  730. EnableValidControls();
  731. }
  732. protected override bool ProcessDialogKey(Keys key) {
  733. if(_connector!=null && key==(Keys.Control | Keys.C)) {
  734. _connector.Interrupt();
  735. ClearConnectingState();
  736. return true;
  737. }
  738. else
  739. return base.ProcessDialogKey(key);
  740. }
  741. private void ClearConnectingState() {
  742. _loginButton.Enabled = true;
  743. _cancelButton.Enabled = true;
  744. this.Cursor = Cursors.Default;
  745. this.Text = GApp.Strings.GetString("Form.LoginDialog.Text");
  746. _connector = null;
  747. }
  748. private void ShowError(string msg) {
  749. GUtil.Warning(this, msg, GApp.Strings.GetString("Caption.LoginDialog.ConnectionError"));
  750. }
  751. protected override void WndProc(ref Message msg) {
  752. base.WndProc(ref msg);
  753. if(msg.Msg==GConst.WMG_ASYNCCONNECT) {
  754. if(msg.LParam.ToInt32()==1) {
  755. this.DialogResult = DialogResult.OK;
  756. Close();
  757. }
  758. else {
  759. ClearConnectingState();
  760. ShowError(_errorMessage);
  761. }
  762. }
  763. }
  764. //ISocketWithTimeoutClient 偙傟傜偼偙偺僂傿儞僪僂偲偼暿偺僗儗僢僪偱幚峴偝傟傞偺偱怲廳偵
  765. public void SuccessfullyExit(object result) {
  766. _result = (ConnectionTag)result;
  767. Win32.SendMessage(_savedHWND, GConst.WMG_ASYNCCONNECT, IntPtr.Zero, new IntPtr(1));
  768. }
  769. public void ConnectionFailed(string message) {
  770. _errorMessage = message;
  771. Win32.SendMessage(_savedHWND, GConst.WMG_ASYNCCONNECT, IntPtr.Zero, IntPtr.Zero);
  772. }
  773. public void CancelTimer() {
  774. }
  775. public IWin32Window GetWindow() {
  776. return this;
  777. }
  778. }
  779. }