1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-30 00:56:07 +03:00
ProxySU/ProxySuper.WPF/Views/NaiveProxy/NaiveProxyInstallerView.xaml.cs

189 lines
5.5 KiB
C#
Raw Normal View History

2021-05-25 13:28:37 +03:00
using Microsoft.Win32;
using MvvmCross.Platforms.Wpf.Views;
using ProxySuper.Core.Models.Hosts;
using ProxySuper.Core.Services;
using ProxySuper.Core.ViewModels;
using Renci.SshNet;
using System;
using System.ComponentModel;
using System.Diagnostics;
2021-06-18 15:53:46 +03:00
using System.IO;
2021-05-25 13:28:37 +03:00
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
namespace ProxySuper.WPF.Views
{
/// <summary>
/// NaiveProxyInstallerView.xaml 的交互逻辑
/// </summary>
public partial class NaiveProxyInstallerView : MvxWindow
{
public NaiveProxyInstallerView()
{
InitializeComponent();
}
public new NaiveProxyInstallerViewModel ViewModel
{
get
{
return (NaiveProxyInstallerViewModel)base.ViewModel;
}
}
public NaiveProxyProject Project { get; set; }
2021-07-08 13:37:32 +03:00
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
_sshClient.Disconnect();
_sshClient.Dispose();
}
2021-05-25 13:28:37 +03:00
private SshClient _sshClient;
private void OpenConnect()
{
WriteOutput("正在登陆服务器 ...");
var conneInfo = CreateConnectionInfo(ViewModel.Host);
2021-07-10 06:57:56 +03:00
conneInfo.Timeout = TimeSpan.FromSeconds(60);
2021-05-25 13:28:37 +03:00
_sshClient = new SshClient(conneInfo);
try
{
_sshClient.Connect();
}
catch (Exception ex)
{
WriteOutput("登陆失败!");
WriteOutput(ex.Message);
return;
}
WriteOutput("登陆服务器成功!");
ViewModel.Connected = true;
Project = new NaiveProxyProject(_sshClient, ViewModel.Settings, WriteOutput);
}
private void WriteOutput(string outShell)
{
if (!outShell.EndsWith("\n"))
{
outShell += "\n";
}
2021-06-18 15:53:46 +03:00
ViewModel.CommandText += outShell;
2021-05-25 13:28:37 +03:00
Dispatcher.Invoke(() =>
{
OutputTextBox.AppendText(outShell);
OutputTextBox.ScrollToEnd();
});
}
private ConnectionInfo CreateConnectionInfo(Host host)
{
AuthenticationMethod auth = null;
if (host.SecretType == LoginSecretType.Password)
{
auth = new PasswordAuthenticationMethod(host.UserName, host.Password);
}
else if (host.SecretType == LoginSecretType.PrivateKey)
{
auth = new PrivateKeyAuthenticationMethod(host.UserName, new PrivateKeyFile(host.PrivateKeyPath));
}
2021-08-16 13:25:43 +03:00
if (host.Proxy.Type == ProxyTypes.None)
2021-05-25 13:28:37 +03:00
{
return new ConnectionInfo(host.Address, host.Port, host.UserName, auth);
}
else
{
return new ConnectionInfo(
host: host.Address,
port: host.Port,
username: host.UserName,
2021-08-16 13:25:43 +03:00
proxyType: host.Proxy.Type,
2021-05-25 13:28:37 +03:00
proxyHost: host.Proxy.Address,
proxyPort: host.Proxy.Port,
proxyUsername: host.Proxy.UserName,
proxyPassword: host.Proxy.Password,
authenticationMethods: auth);
}
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
base.Loaded += (sender, arg) =>
{
Task.Factory.StartNew(OpenConnect);
};
2021-06-18 15:53:46 +03:00
base.Closed += SaveInstallLog;
2021-07-08 13:37:32 +03:00
base.Closed += Disconnect;
}
private void Disconnect(object sender, EventArgs e)
{
if (_sshClient != null)
{
_sshClient.Disconnect();
_sshClient.Dispose();
}
2021-05-25 13:28:37 +03:00
}
2021-06-18 15:53:46 +03:00
private void SaveInstallLog(object sender, EventArgs e)
{
if (!Directory.Exists("Logs"))
{
Directory.CreateDirectory("Logs");
}
var fileName = System.IO.Path.Combine("Logs", DateTime.Now.ToString("yyyy-MM-dd hh-mm") + ".naiveproxy.txt");
File.WriteAllText(fileName, ViewModel.CommandText);
}
2021-05-25 13:28:37 +03:00
private void OpenLink(object sender, RoutedEventArgs e)
{
Hyperlink link = sender as Hyperlink;
Process.Start(new ProcessStartInfo(link.NavigateUri.AbsoluteUri));
}
private void Install(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(Project.Install);
}
private void Uninstall(object sender, RoutedEventArgs e)
{
var result = MessageBox.Show("您确认要卸载代理吗?", "提示", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
Task.Factory.StartNew(Project.Uninstall);
}
}
private void UploadWeb(object sender, RoutedEventArgs e)
{
var fileDialog = new OpenFileDialog();
fileDialog.Filter = "压缩文件|*.zip";
fileDialog.FileOk += DoUploadWeb;
fileDialog.ShowDialog();
}
private void DoUploadWeb(object sender, CancelEventArgs e)
{
Task.Factory.StartNew(() =>
{
var file = sender as OpenFileDialog;
using (var stream = file.OpenFile())
{
Project.UploadWeb(stream);
}
});
}
}
}