1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-27 07:36:06 +03:00
ProxySU/ProxySU_Core/Views/TerminalWindow.xaml.cs

236 lines
6.6 KiB
C#
Raw Normal View History

2021-02-25 04:59:06 +03:00
using MahApps.Metro.Controls.Dialogs;
2021-03-09 12:52:09 +03:00
using Microsoft.Win32;
2021-03-04 11:25:36 +03:00
using ProxySU_Core.Models;
2021-03-17 06:58:56 +03:00
using ProxySU_Core.Models.Developers;
2021-02-25 04:59:06 +03:00
using ProxySU_Core.ViewModels;
2021-02-28 13:22:21 +03:00
using ProxySU_Core.Views;
2021-02-25 04:59:06 +03:00
using Renci.SshNet;
using System;
using System.Collections.Generic;
2021-03-09 12:52:09 +03:00
using System.ComponentModel;
using System.Diagnostics;
2021-02-25 04:59:06 +03:00
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ProxySU_Core
{
/// <summary>
/// TerminalWindow.xaml 的交互逻辑
/// </summary>
public partial class TerminalWindow
{
2021-03-04 13:25:52 +03:00
private Record Record { get; set; }
2021-02-25 04:59:06 +03:00
private readonly Terminal _vm;
private SshClient _sshClient;
2021-03-09 12:52:09 +03:00
XrayProject project;
2021-03-04 13:25:52 +03:00
public TerminalWindow(Record record)
2021-02-25 04:59:06 +03:00
{
InitializeComponent();
2021-03-04 11:25:36 +03:00
ResizeMode = ResizeMode.NoResize;
WindowStartupLocation = WindowStartupLocation.CenterScreen;
2021-02-25 04:59:06 +03:00
2021-03-04 13:25:52 +03:00
this.Record = record;
_vm = new Terminal(record.Host);
2021-02-25 04:59:06 +03:00
DataContext = _vm;
Task.Factory.StartNew(() =>
{
try
{
OpenConnect(_vm.Host);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
});
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
2021-03-09 12:52:09 +03:00
_vm.HasConnected = false;
2021-02-25 04:59:06 +03:00
if (_sshClient != null)
_sshClient.Disconnect();
if (_sshClient != null)
_sshClient.Dispose();
}
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);
}
}
private void OpenConnect(Host host)
{
2021-03-26 06:28:41 +03:00
WriteOutput("正在登陆服务器 ...");
2021-02-25 04:59:06 +03:00
var conneInfo = CreateConnectionInfo(host);
_sshClient = new SshClient(conneInfo);
2021-03-26 06:28:41 +03:00
try
{
_sshClient.Connect();
}
catch (Exception ex)
{
2021-03-27 17:18:56 +03:00
WriteOutput("登陆失败!");
2021-03-26 06:28:41 +03:00
WriteOutput(ex.Message);
2021-03-27 17:18:56 +03:00
return;
2021-03-26 06:28:41 +03:00
}
WriteOutput("登陆服务器成功!");
2021-03-09 12:52:09 +03:00
_vm.HasConnected = true;
2021-03-11 07:05:50 +03:00
project = new XrayProject(_sshClient, Record.Settings, WriteOutput);
2021-02-25 04:59:06 +03:00
}
2021-03-11 07:05:50 +03:00
private void WriteOutput(string outShell)
2021-02-25 04:59:06 +03:00
{
2021-03-11 07:05:50 +03:00
if (!outShell.EndsWith("\n"))
{
outShell += "\n";
}
2021-03-04 13:25:52 +03:00
Dispatcher.Invoke(() =>
{
2021-03-11 07:05:50 +03:00
OutputTextBox.AppendText(outShell);
2021-03-04 13:25:52 +03:00
OutputTextBox.ScrollToEnd();
});
2021-02-25 04:59:06 +03:00
}
private void Install(object sender, RoutedEventArgs e)
{
2021-03-04 13:25:52 +03:00
Task.Factory.StartNew(() =>
{
project.Install();
});
2021-02-25 04:59:06 +03:00
}
2021-03-15 12:58:20 +03:00
private void UpdateXrayCore(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
project.UpdateXrayCore();
});
}
private void UpdateXraySettings(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
project.UpdateXraySettings();
});
}
2021-03-09 12:52:09 +03:00
private void InstallCert(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
project.InstallCert();
});
}
2021-03-26 06:28:41 +03:00
private void UninstallXray(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
project.Uninstall();
});
}
2021-03-15 12:58:20 +03:00
private void UploadCert(object sender, RoutedEventArgs e)
{
var fileDialog = new OpenFileDialog();
fileDialog.Filter = "压缩文件|*.zip";
fileDialog.FileOk += DoUploadCert;
fileDialog.ShowDialog();
}
2021-03-09 12:52:09 +03:00
private void UploadWeb(object sender, RoutedEventArgs e)
{
var fileDialog = new OpenFileDialog();
2021-03-15 12:58:20 +03:00
fileDialog.Filter = "压缩文件|*.zip";
fileDialog.FileOk += DoUploadWeb;
2021-03-09 12:52:09 +03:00
fileDialog.ShowDialog();
}
2021-03-09 14:02:14 +03:00
private void ReinstallCaddy(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
project.ReinstallCaddy();
});
}
2021-03-15 12:58:20 +03:00
private void DoUploadWeb(object sender, CancelEventArgs e)
2021-03-11 09:31:18 +03:00
{
Task.Factory.StartNew(() =>
{
2021-03-15 12:58:20 +03:00
var file = sender as OpenFileDialog;
using (var stream = file.OpenFile())
{
project.UploadWeb(stream);
}
2021-03-11 09:31:18 +03:00
});
}
2021-03-15 12:58:20 +03:00
private void DoUploadCert(object sender, CancelEventArgs e)
2021-03-09 12:52:09 +03:00
{
Task.Factory.StartNew(() =>
{
var file = sender as OpenFileDialog;
using (var stream = file.OpenFile())
{
2021-03-15 12:58:20 +03:00
project.UploadCert(stream);
2021-03-09 12:52:09 +03:00
}
});
}
private void OpenLink(object sender, RoutedEventArgs e)
{
Hyperlink link = sender as Hyperlink;
Process.Start(new ProcessStartInfo(link.NavigateUri.AbsoluteUri));
}
2021-02-25 04:59:06 +03:00
}
}