mirror of
https://github.com/proxysu/ProxySU.git
synced 2025-02-16 14:43:14 +03:00
添加异步线程处理ssh连接
This commit is contained in:
parent
fe45b897c1
commit
387163367d
@ -32,8 +32,10 @@ namespace ProxySU
|
|||||||
}
|
}
|
||||||
//System.Diagnostics.Process exitProgram = System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id);
|
//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)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
//ProgressBarSetUpProcessing.IsIndeterminate = true;
|
||||||
|
#region 检测输入的内空是否有错,并读取内容
|
||||||
if (string.IsNullOrEmpty(TextBoxHost.Text) == true || string.IsNullOrEmpty(TextBoxPort.Text) == true || string.IsNullOrEmpty(TextBoxUserName.Text) == true)
|
if (string.IsNullOrEmpty(TextBoxHost.Text) == true || string.IsNullOrEmpty(TextBoxPort.Text) == true || string.IsNullOrEmpty(TextBoxUserName.Text) == true)
|
||||||
{
|
{
|
||||||
MessageBox.Show("主机地址、主机端口、用户名为必填项,不能为空");
|
MessageBox.Show("主机地址、主机端口、用户名为必填项,不能为空");
|
||||||
@ -97,10 +99,10 @@ namespace ProxySU
|
|||||||
|
|
||||||
//TextBlockSetUpProcessing.Text = "登录中";
|
//TextBlockSetUpProcessing.Text = "登录中";
|
||||||
//ProgressBarSetUpProcessing.IsIndeterminate = true;
|
//ProgressBarSetUpProcessing.IsIndeterminate = true;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
// try
|
||||||
try
|
//{
|
||||||
{
|
|
||||||
|
|
||||||
//var connectionInfo = new PasswordConnectionInfo(sshHostName, sshPort, sshUser, sshPassword);
|
//var connectionInfo = new PasswordConnectionInfo(sshHostName, sshPort, sshUser, sshPassword);
|
||||||
|
|
||||||
@ -133,47 +135,51 @@ namespace ProxySU
|
|||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
Task.Factory.StartNew(StartTaskSetUp);
|
|
||||||
//Thread thread = new Thread(StartSetUpRemoteHost(connectionInfo, TextBlockSetUpProcessing));
|
|
||||||
//Task task = new Task(StartSetUpRemoteHost(connectionInfo, TextBlockSetUpProcessing));
|
|
||||||
|
|
||||||
//thread.Start();
|
//using (var client = new SshClient(sshHostName, sshPort, sshUser, sshPassword))
|
||||||
//StartSetUpRemoteHost(connectionInfo, TextBlockSetUpProcessing);
|
//Action<ConnectionInfo, TextBlock> startSetUpAction = new Action<ConnectionInfo, TextBlock>(StartSetUpRemoteHost);
|
||||||
}
|
Task task = new Task(() => StartSetUpRemoteHost(connectionInfo, TextBlockSetUpProcessing, ProgressBarSetUpProcessing));
|
||||||
catch (Exception ex1)//例外处理
|
task.Start();
|
||||||
{
|
|
||||||
//MessageBox.Show(ex1.Message);
|
|
||||||
if (ex1.Message.Contains("连接尝试失败") == true)
|
|
||||||
{
|
//catch (Exception ex1)//例外处理
|
||||||
MessageBox.Show($"{ex1.Message}\n请检查主机地址及端口是否正确,如果通过代理,请检查代理是否正常工作");
|
#region 例外处理,未使用
|
||||||
}
|
//{
|
||||||
|
// //MessageBox.Show(ex1.Message);
|
||||||
|
// if (ex1.Message.Contains("连接尝试失败") == true)
|
||||||
|
// {
|
||||||
|
// MessageBox.Show($"{ex1.Message}\n请检查主机地址及端口是否正确,如果通过代理,请检查代理是否正常工作");
|
||||||
|
// }
|
||||||
|
|
||||||
else if (ex1.Message.Contains("denied (password)") == true)
|
// else if (ex1.Message.Contains("denied (password)") == true)
|
||||||
{
|
// {
|
||||||
MessageBox.Show($"{ex1.Message}\n密码错误或用户名错误");
|
// MessageBox.Show($"{ex1.Message}\n密码错误或用户名错误");
|
||||||
}
|
// }
|
||||||
else if (ex1.Message.Contains("Invalid private key file") == true)
|
// else if (ex1.Message.Contains("Invalid private key file") == true)
|
||||||
{
|
// {
|
||||||
MessageBox.Show($"{ex1.Message}\n所选密钥文件错误或者格式不对");
|
// MessageBox.Show($"{ex1.Message}\n所选密钥文件错误或者格式不对");
|
||||||
}
|
// }
|
||||||
else if (ex1.Message.Contains("denied (publickey)") == true)
|
// else if (ex1.Message.Contains("denied (publickey)") == true)
|
||||||
{
|
// {
|
||||||
MessageBox.Show($"{ex1.Message}\n使用密钥登录,密钥文件错误或用户名错误");
|
// MessageBox.Show($"{ex1.Message}\n使用密钥登录,密钥文件错误或用户名错误");
|
||||||
}
|
// }
|
||||||
else if (ex1.Message.Contains("目标计算机积极拒绝") == true)
|
// else if (ex1.Message.Contains("目标计算机积极拒绝") == true)
|
||||||
{
|
// {
|
||||||
MessageBox.Show($"{ex1.Message}\n主机地址错误,如果使用了代理,也可能是连接代理的端口错误");
|
// MessageBox.Show($"{ex1.Message}\n主机地址错误,如果使用了代理,也可能是连接代理的端口错误");
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
MessageBox.Show("未知错误");
|
// MessageBox.Show("未知错误");
|
||||||
}
|
// }
|
||||||
//TextBlockSetUpProcessing.Text = "主机登录失败";
|
// //TextBlockSetUpProcessing.Text = "主机登录失败";
|
||||||
//ProgressBarSetUpProcessing.IsIndeterminate = false;
|
// //ProgressBarSetUpProcessing.IsIndeterminate = false;
|
||||||
//ProgressBarSetUpProcessing.Value = 0;
|
// //ProgressBarSetUpProcessing.Value = 0;
|
||||||
}
|
//}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region 端口数字防错代码,密钥选择代码
|
||||||
private void Button_canel_Click(object sender, RoutedEventArgs e)
|
private void Button_canel_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
Application.Current.Shutdown();
|
Application.Current.Shutdown();
|
||||||
@ -205,6 +211,8 @@ namespace ProxySU
|
|||||||
TextBoxCertFilePath.Text = openFileDialog.FileName;
|
TextBoxCertFilePath.Text = openFileDialog.FileName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region 界面控件的有效无效控制代码块
|
#region 界面控件的有效无效控制代码块
|
||||||
private void RadioButtonNoProxy_Checked(object sender, RoutedEventArgs e)
|
private void RadioButtonNoProxy_Checked(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
@ -275,96 +283,114 @@ namespace ProxySU
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private void StartTaskSetUp()
|
|
||||||
{
|
|
||||||
Task task = new Task(() => StartSetUpRemoteHost(ConnectionInfo connectionInfo, TextBlock textBlockName), this.first);
|
|
||||||
//Task task2 = new Task((tb) => Begin(this.second), this.first);
|
|
||||||
//Task task3 = new Task((tb) => Begin(this.Three), this.first);
|
|
||||||
task.Start();
|
|
||||||
task.Wait();
|
|
||||||
//task2.Start();
|
|
||||||
//task2.Wait();
|
|
||||||
//task3.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
//登录远程主机布署程序
|
//登录远程主机布署程序
|
||||||
private void StartSetUpRemoteHost(ConnectionInfo connectionInfo,TextBlock textBlockName)
|
private void StartSetUpRemoteHost(ConnectionInfo connectionInfo,TextBlock textBlockName, ProgressBar progressBar)
|
||||||
{
|
{
|
||||||
string currentStatus = "正在登录远程主机......";
|
string currentStatus = "正在登录远程主机......";
|
||||||
Action<TextBlock, String> updateAction = new Action<TextBlock, string>(UpdateTextBlockSetUpProcessing);
|
Action<TextBlock, ProgressBar, string> updateAction = new Action<TextBlock, ProgressBar, string>(UpdateTextBlock);
|
||||||
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, currentStatus);
|
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
|
||||||
|
|
||||||
#region 主机指纹,暂未启用
|
|
||||||
//byte[] expectedFingerPrint = new byte[] {
|
|
||||||
// 0x66, 0x31, 0xaf, 0x00, 0x54, 0xb9, 0x87, 0x31,
|
|
||||||
// 0xff, 0x58, 0x1c, 0x31, 0xb1, 0xa2, 0x4c, 0x6b
|
|
||||||
// };
|
|
||||||
#endregion
|
|
||||||
//using (var client = new SshClient(sshHostName, sshPort, sshUser, sshPassword))
|
|
||||||
using (var client = new SshClient(connectionInfo))
|
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
#region ssh登录验证主机指纹代码块,暂未启用
|
#region 主机指纹,暂未启用
|
||||||
// client.HostKeyReceived += (sender, e) =>
|
//byte[] expectedFingerPrint = new byte[] {
|
||||||
// {
|
// 0x66, 0x31, 0xaf, 0x00, 0x54, 0xb9, 0x87, 0x31,
|
||||||
// if (expectedFingerPrint.Length == e.FingerPrint.Length)
|
// 0xff, 0x58, 0x1c, 0x31, 0xb1, 0xa2, 0x4c, 0x6b
|
||||||
// {
|
// };
|
||||||
// for (var i = 0; i < expectedFingerPrint.Length; i++)
|
|
||||||
// {
|
|
||||||
// if (expectedFingerPrint[i] != e.FingerPrint[i])
|
|
||||||
// {
|
|
||||||
// e.CanTrust = false;
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// e.CanTrust = false;
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
#endregion
|
#endregion
|
||||||
|
using (var client = new SshClient(connectionInfo))
|
||||||
client.Connect();
|
|
||||||
if (client.IsConnected == true)
|
|
||||||
{
|
{
|
||||||
currentStatus = "主机已登录";
|
#region ssh登录验证主机指纹代码块,暂未启用
|
||||||
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, currentStatus);
|
// client.HostKeyReceived += (sender, e) =>
|
||||||
//ProgressBarSetUpProcessing.IsIndeterminate = false;
|
// {
|
||||||
//ProgressBarSetUpProcessing.Value = 100;
|
// if (expectedFingerPrint.Length == e.FingerPrint.Length)
|
||||||
|
// {
|
||||||
|
// for (var i = 0; i < expectedFingerPrint.Length; i++)
|
||||||
|
// {
|
||||||
|
// if (expectedFingerPrint[i] != e.FingerPrint[i])
|
||||||
|
// {
|
||||||
|
// e.CanTrust = false;
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// e.CanTrust = false;
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
client.Connect();
|
||||||
|
if (client.IsConnected == true)
|
||||||
|
{
|
||||||
|
currentStatus = "主机已登录";
|
||||||
|
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
client.RunCommand("echo 1111 >> test.json");
|
||||||
|
MessageBox.Show(client.ConnectionInfo.ServerVersion.ToString());
|
||||||
|
//MessageBox.Show(client);
|
||||||
|
client.Disconnect();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex1)//例外处理
|
||||||
|
#region 例外处理
|
||||||
|
{
|
||||||
|
//MessageBox.Show(ex1.Message);
|
||||||
|
if (ex1.Message.Contains("连接尝试失败") == true)
|
||||||
|
{
|
||||||
|
MessageBox.Show($"{ex1.Message}\n请检查主机地址及端口是否正确,如果通过代理,请检查代理是否正常工作");
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (ex1.Message.Contains("denied (password)") == true)
|
||||||
|
{
|
||||||
|
MessageBox.Show($"{ex1.Message}\n密码错误或用户名错误");
|
||||||
|
}
|
||||||
|
else if (ex1.Message.Contains("Invalid private key file") == true)
|
||||||
|
{
|
||||||
|
MessageBox.Show($"{ex1.Message}\n所选密钥文件错误或者格式不对");
|
||||||
|
}
|
||||||
|
else if (ex1.Message.Contains("denied (publickey)") == true)
|
||||||
|
{
|
||||||
|
MessageBox.Show($"{ex1.Message}\n使用密钥登录,密钥文件错误或用户名错误");
|
||||||
|
}
|
||||||
|
else if (ex1.Message.Contains("目标计算机积极拒绝") == true)
|
||||||
|
{
|
||||||
|
MessageBox.Show($"{ex1.Message}\n主机地址错误,如果使用了代理,也可能是连接代理的端口错误");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
currentStatus = "主机登录失败";
|
MessageBox.Show("未知错误");
|
||||||
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, currentStatus);
|
|
||||||
//ProgressBarSetUpProcessing.IsIndeterminate = false;
|
|
||||||
//ProgressBarSetUpProcessing.Value = 0;
|
|
||||||
}
|
}
|
||||||
client.RunCommand("echo 1111 >> test.json");
|
currentStatus = "主机登录失败";
|
||||||
MessageBox.Show(client.ConnectionInfo.ServerVersion.ToString());
|
textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
|
||||||
//MessageBox.Show(client);
|
|
||||||
client.Disconnect();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
//private void Begin(TextBlock textBlockName)
|
|
||||||
//{
|
|
||||||
// int i = 100000000;
|
|
||||||
// while (i > 0)
|
|
||||||
// {
|
|
||||||
// i--;
|
|
||||||
// }
|
|
||||||
// Random random = new Random();
|
|
||||||
// String Num = random.Next(0, 100).ToString();
|
|
||||||
// Action<TextBlock, String> updateAction = new Action<TextBlock, string>(UpdateTextBlockSetUpProcessing);
|
|
||||||
// TextBlockSetUpProcessing.Dispatcher.BeginInvoke(updateAction, textBlockName, Num);
|
|
||||||
//}
|
|
||||||
//更新UI显示内容
|
//更新UI显示内容
|
||||||
private void UpdateTextBlockSetUpProcessing(TextBlock textBlockName, string currentStatus)
|
private void UpdateTextBlock(TextBlock textBlockName, ProgressBar progressBar, string currentStatus)
|
||||||
{
|
{
|
||||||
textBlockName.Text = currentStatus;
|
textBlockName.Text = currentStatus;
|
||||||
|
if (currentStatus.Contains("正在登录远程主机") == true)
|
||||||
|
{
|
||||||
|
progressBar.IsIndeterminate = true;
|
||||||
|
}
|
||||||
|
else if (currentStatus.Contains("主机已登录") == true)
|
||||||
|
{
|
||||||
|
progressBar.IsIndeterminate = false;
|
||||||
|
progressBar.Value = 100;
|
||||||
|
}
|
||||||
|
else if (currentStatus.Contains("主机登录失败") == true)
|
||||||
|
{
|
||||||
|
progressBar.IsIndeterminate = false;
|
||||||
|
progressBar.Value = 0;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user