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

210 lines
6.2 KiB
C#
Raw Normal View History

2021-05-20 13:32:17 +03:00
using Microsoft.Win32;
using MvvmCross.Platforms.Wpf.Presenters.Attributes;
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;
2021-05-24 13:57:17 +03:00
using System.Diagnostics;
2021-06-18 15:53:46 +03:00
using System.IO;
2021-05-20 13:32:17 +03:00
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
2021-05-24 13:57:17 +03:00
using System.Windows.Documents;
2021-05-20 13:32:17 +03:00
using System.Windows.Threading;
namespace ProxySuper.WPF.Views
{
/// <summary>
/// XrayInstallerView.xaml 的交互逻辑
/// </summary>
2021-05-21 13:03:40 +03:00
[MvxWindowPresentation(Identifier = nameof(XrayInstallerView), Modal = false)]
2021-05-20 13:32:17 +03:00
public partial class XrayInstallerView : MvxWindow
{
public XrayInstallerView()
{
InitializeComponent();
}
public new XrayInstallerViewModel ViewModel
{
get
{
var t = base.ViewModel;
return (XrayInstallerViewModel)base.ViewModel;
}
}
public XrayProject Project { get; set; }
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;
}
private void SaveInstallLog(object sender, EventArgs e)
{
if (!Directory.Exists("Logs"))
{
Directory.CreateDirectory("Logs");
}
var fileName = Path.Combine("Logs", DateTime.Now.ToString("yyyy-MM-dd hh-mm") + ".xary.txt");
File.WriteAllText(fileName, ViewModel.CommandText);
2021-05-20 13:32:17 +03:00
}
private SshClient _sshClient;
private void OpenConnect()
{
WriteOutput("正在登陆服务器 ...");
var conneInfo = CreateConnectionInfo(ViewModel.Host);
_sshClient = new SshClient(conneInfo);
try
{
_sshClient.Connect();
}
catch (Exception ex)
{
WriteOutput("登陆失败!");
WriteOutput(ex.Message);
return;
}
WriteOutput("登陆服务器成功!");
ViewModel.Connected = true;
Project = new XrayProject(_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-20 13:32:17 +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));
}
if (host.Proxy.Type == LocalProxyType.None)
{
return new ConnectionInfo(host.Address, host.Port, host.UserName, auth);
}
else
{
return new ConnectionInfo(
host: host.Address,
port: host.Port,
username: host.UserName,
proxyType: (ProxyTypes)(int)host.Proxy.Type,
proxyHost: host.Proxy.Address,
proxyPort: host.Proxy.Port,
proxyUsername: host.Proxy.UserName,
proxyPassword: host.Proxy.Password,
authenticationMethods: auth);
}
}
2021-06-18 15:53:46 +03:00
#region
2021-05-24 13:57:17 +03:00
private void OpenLink(object sender, RoutedEventArgs e)
{
Hyperlink link = sender as Hyperlink;
Process.Start(new ProcessStartInfo(link.NavigateUri.AbsoluteUri));
}
2021-05-20 13:32:17 +03:00
private void Install(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(Project.Install);
}
private void UpdateXrayCore(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(Project.UpdateXrayCore);
}
private void UpdateXraySettings(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(Project.UpdateXraySettings);
}
private void InstallCert(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(Project.InstallCertToXray);
}
private void UninstallXray(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(Project.UninstallProxy);
}
private void UploadCert(object sender, RoutedEventArgs e)
{
var fileDialog = new OpenFileDialog();
fileDialog.Filter = "压缩文件|*.zip";
fileDialog.FileOk += DoUploadCert;
fileDialog.ShowDialog();
}
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);
}
});
}
private void DoUploadCert(object sender, CancelEventArgs e)
{
Task.Factory.StartNew(() =>
{
var file = sender as OpenFileDialog;
using (var stream = file.OpenFile())
{
Project.UploadCert(stream);
}
});
}
2021-06-18 15:53:46 +03:00
#endregion
2021-05-20 13:32:17 +03:00
}
}