1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-25 14:46:08 +03:00

学习完善异常处理

This commit is contained in:
ProxySU 2020-03-09 08:01:43 +08:00
parent dceea20538
commit b8fd635357
2 changed files with 70 additions and 18 deletions

View File

@ -28,17 +28,35 @@ namespace ProxySU
RadioButtonNoProxy.IsChecked = true; RadioButtonNoProxy.IsChecked = true;
RadioButtonProxyNoLogin.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) private void Button_Login_Click(object sender, RoutedEventArgs e)
{ {
//byte[] expectedFingerPrint = new byte[] { //byte[] expectedFingerPrint = new byte[] {
// 0x66, 0x31, 0xaf, 0x00, 0x54, 0xb9, 0x87, 0x31, // 0x66, 0x31, 0xaf, 0x00, 0x54, 0xb9, 0x87, 0x31,
// 0xff, 0x58, 0x1c, 0x31, 0xb1, 0xa2, 0x4c, 0x6b // 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(); string sshHostName = TextBoxHost.Text.ToString();
int sshPort = int.Parse(TextBoxPort.Text); int sshPort = int.Parse(TextBoxPort.Text);
string sshUser = TextBoxUserName.Text.ToString(); string sshUser = TextBoxUserName.Text.ToString();
if (RadioButtonPasswordLogin.IsChecked == true && string.IsNullOrEmpty(PasswordBoxHostPassword.Password) == true)
{
MessageBox.Show("登录密码为必填项,不能为空");
//exitProgram.Kill();
return;
}
string sshPassword = PasswordBoxHostPassword.Password.ToString(); string sshPassword = PasswordBoxHostPassword.Password.ToString();
if (RadioButtonCertLogin.IsChecked == true && string.IsNullOrEmpty(TextBoxCertFilePath.Text) == true)
{
MessageBox.Show("密钥文件为必填项,不能为空");
//exitProgram.Kill();
return;
}
string sshPrivateKey = TextBoxCertFilePath.Text.ToString(); string sshPrivateKey = TextBoxCertFilePath.Text.ToString();
ProxyTypes proxyTypes = new ProxyTypes();//默认为None ProxyTypes proxyTypes = new ProxyTypes();//默认为None
//MessageBox.Show(proxyTypes.ToString()); //MessageBox.Show(proxyTypes.ToString());
@ -61,15 +79,29 @@ namespace ProxySU
} }
//MessageBox.Show(proxyTypes.ToString()); //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(); string sshProxyHost = TextBoxProxyHost.Text.ToString();
int sshProxyPort = int.Parse(TextBoxProxyPort.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 sshProxyUser = TextBoxProxyUserName.Text.ToString();
string sshProxyPassword = PasswordBoxProxyPassword.Password.ToString(); string sshProxyPassword = PasswordBoxProxyPassword.Password.ToString();
//判断相关值是否为空
//var connectionInfo = new PasswordConnectionInfo(sshHostName, sshPort, sshUser, sshPassword); //var connectionInfo = new PasswordConnectionInfo(sshHostName, sshPort, sshUser, sshPassword);
var connectionInfo = new ConnectionInfo( var connectionInfo = new ConnectionInfo(
sshHostName, sshHostName,
sshPort, sshPort,
sshUser, sshUser,
@ -78,11 +110,29 @@ namespace ProxySU
sshProxyPort, sshProxyPort,
sshProxyUser, sshProxyUser,
sshProxyPassword, 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)) new PrivateKeyAuthenticationMethod(sshUser, new PrivateKeyFile(sshPrivateKey))
); );
}
using (var client = new SshClient(connectionInfo)) using (var client = new SshClient(connectionInfo))
#region
//using (var client = new SshClient(sshHostName, sshPort, sshUser, sshPassword)) //using (var client = new SshClient(sshHostName, sshPort, sshUser, sshPassword))
{ {
// client.HostKeyReceived += (sender, e) => // client.HostKeyReceived += (sender, e) =>
@ -103,10 +153,20 @@ namespace ProxySU
// e.CanTrust = false; // e.CanTrust = false;
// } // }
// }; // };
client.Connect(); #endregion
client.RunCommand("echo 1111 >> test.json"); try
MessageBox.Show(client.ConnectionInfo.ServerVersion.ToString()); {
client.Disconnect(); 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) private void ButtonOpenFileDialog_Click(object sender, RoutedEventArgs e)
{ {
var openFileDialog = new Microsoft.Win32.OpenFileDialog() var openFileDialog = new Microsoft.Win32.OpenFileDialog()
@ -147,7 +201,7 @@ namespace ProxySU
TextBoxCertFilePath.Text = openFileDialog.FileName; TextBoxCertFilePath.Text = openFileDialog.FileName;
} }
} }
#region
private void RadioButtonNoProxy_Checked(object sender, RoutedEventArgs e) private void RadioButtonNoProxy_Checked(object sender, RoutedEventArgs e)
{ {
TextBlockProxyHost.IsEnabled = false; TextBlockProxyHost.IsEnabled = false;
@ -215,5 +269,6 @@ namespace ProxySU
TextBoxProxyUserName.IsEnabled = true; TextBoxProxyUserName.IsEnabled = true;
PasswordBoxProxyPassword.IsEnabled = true; PasswordBoxProxyPassword.IsEnabled = true;
} }
#endregion
} }
} }

View File

@ -117,9 +117,6 @@
<Install>false</Install> <Install>false</Install>
</BootstrapperPackage> </BootstrapperPackage>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Resource Include="ProxySU2.ico" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Resource Include="ProxySU.ico" /> <Resource Include="ProxySU.ico" />
</ItemGroup> </ItemGroup>