From b8fd635357a623a49e2ba10e7cfc0aa1e2114591 Mon Sep 17 00:00:00 2001 From: ProxySU Date: Mon, 9 Mar 2020 08:01:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E5=AE=8C=E5=96=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxySU/MainWindow.xaml.cs | 85 +++++++++++++++++++++++++++++++------- ProxySU/ProxySU.csproj | 3 -- 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/ProxySU/MainWindow.xaml.cs b/ProxySU/MainWindow.xaml.cs index 9dcb60d..bc0a453 100644 --- a/ProxySU/MainWindow.xaml.cs +++ b/ProxySU/MainWindow.xaml.cs @@ -28,17 +28,35 @@ namespace ProxySU RadioButtonNoProxy.IsChecked = true; RadioButtonProxyNoLogin.IsChecked = true; } - + //System.Diagnostics.Process exitProgram = System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id); private void Button_Login_Click(object sender, RoutedEventArgs e) { //byte[] expectedFingerPrint = new byte[] { // 0x66, 0x31, 0xaf, 0x00, 0x54, 0xb9, 0x87, 0x31, // 0xff, 0x58, 0x1c, 0x31, 0xb1, 0xa2, 0x4c, 0x6b // }; + if (string.IsNullOrEmpty(TextBoxHost.Text) == true || string.IsNullOrEmpty(TextBoxPort.Text) == true || string.IsNullOrEmpty(TextBoxUserName.Text) == true) + { + MessageBox.Show("主机地址、主机端口、用户名为必填项,不能为空"); + //exitProgram.Kill(); + return; + } string sshHostName = TextBoxHost.Text.ToString(); int sshPort = int.Parse(TextBoxPort.Text); string sshUser = TextBoxUserName.Text.ToString(); + if (RadioButtonPasswordLogin.IsChecked == true && string.IsNullOrEmpty(PasswordBoxHostPassword.Password) == true) + { + MessageBox.Show("登录密码为必填项,不能为空"); + //exitProgram.Kill(); + return; + } string sshPassword = PasswordBoxHostPassword.Password.ToString(); + if (RadioButtonCertLogin.IsChecked == true && string.IsNullOrEmpty(TextBoxCertFilePath.Text) == true) + { + MessageBox.Show("密钥文件为必填项,不能为空"); + //exitProgram.Kill(); + return; + } string sshPrivateKey = TextBoxCertFilePath.Text.ToString(); ProxyTypes proxyTypes = new ProxyTypes();//默认为None //MessageBox.Show(proxyTypes.ToString()); @@ -61,15 +79,29 @@ namespace ProxySU } //MessageBox.Show(proxyTypes.ToString()); + if (RadioButtonNoProxy.IsChecked==false&&(string.IsNullOrEmpty(TextBoxProxyHost.Text)==true||string.IsNullOrEmpty(TextBoxProxyPort.Text)==true)) + { + MessageBox.Show("如果选择了代理,则代理地址与端口不能为空"); + //exitProgram.Kill(); + return; + } string sshProxyHost = TextBoxProxyHost.Text.ToString(); int sshProxyPort = int.Parse(TextBoxProxyPort.Text.ToString()); + if (RadiobuttonProxyYesLogin.IsChecked == true && (string.IsNullOrEmpty(TextBoxProxyUserName.Text) == true || string.IsNullOrEmpty(PasswordBoxProxyPassword.Password) == true)) + { + MessageBox.Show("如果代理需要登录,则代理登录的用户名与密码不能为空"); + //exitProgram.Kill(); + return; + } string sshProxyUser = TextBoxProxyUserName.Text.ToString(); string sshProxyPassword = PasswordBoxProxyPassword.Password.ToString(); + //判断相关值是否为空 + //var connectionInfo = new PasswordConnectionInfo(sshHostName, sshPort, sshUser, sshPassword); - var connectionInfo = new ConnectionInfo( + var connectionInfo = new ConnectionInfo( sshHostName, sshPort, sshUser, @@ -78,11 +110,29 @@ namespace ProxySU sshProxyPort, sshProxyUser, sshProxyPassword, - new PasswordAuthenticationMethod(sshUser, sshPassword), + new PasswordAuthenticationMethod(sshUser, sshPassword) + //new PrivateKeyAuthenticationMethod(sshUser, new PrivateKeyFile(sshPrivateKey)) + ); + + if (RadioButtonCertLogin.IsChecked == true) + { + connectionInfo = new ConnectionInfo( + sshHostName, + sshPort, + sshUser, + proxyTypes, + sshProxyHost, + sshProxyPort, + sshProxyUser, + sshProxyPassword, + //new PasswordAuthenticationMethod(sshUser, sshPassword) new PrivateKeyAuthenticationMethod(sshUser, new PrivateKeyFile(sshPrivateKey)) ); - + + } + using (var client = new SshClient(connectionInfo)) + #region //using (var client = new SshClient(sshHostName, sshPort, sshUser, sshPassword)) { // client.HostKeyReceived += (sender, e) => @@ -103,10 +153,20 @@ namespace ProxySU // e.CanTrust = false; // } // }; - client.Connect(); - client.RunCommand("echo 1111 >> test.json"); - MessageBox.Show(client.ConnectionInfo.ServerVersion.ToString()); - client.Disconnect(); + #endregion + try + { + client.Connect(); + client.RunCommand("echo 1111 >> test.json"); + MessageBox.Show(client.ConnectionInfo.ServerVersion.ToString()); + //MessageBox.Show(client); + client.Disconnect(); + } + catch (Exception ex1 ) + { + MessageBox.Show(ex1.Message); + } + } } @@ -129,12 +189,6 @@ namespace ProxySU } } - //private void RadioButtonHttp_Checked(object sender, RoutedEventArgs e) - //{ - // MessageBox.Show("PrxoyHttp"); - //} - - private void ButtonOpenFileDialog_Click(object sender, RoutedEventArgs e) { var openFileDialog = new Microsoft.Win32.OpenFileDialog() @@ -147,7 +201,7 @@ namespace ProxySU TextBoxCertFilePath.Text = openFileDialog.FileName; } } - + #region private void RadioButtonNoProxy_Checked(object sender, RoutedEventArgs e) { TextBlockProxyHost.IsEnabled = false; @@ -215,5 +269,6 @@ namespace ProxySU TextBoxProxyUserName.IsEnabled = true; PasswordBoxProxyPassword.IsEnabled = true; } + #endregion } } diff --git a/ProxySU/ProxySU.csproj b/ProxySU/ProxySU.csproj index 5ce2a1b..0160279 100644 --- a/ProxySU/ProxySU.csproj +++ b/ProxySU/ProxySU.csproj @@ -117,9 +117,6 @@ false - - -