123 lines
4.1 KiB
C#
123 lines
4.1 KiB
C#
using Microsoft.UI.Xaml;
|
|
using Microsoft.UI.Xaml.Controls;
|
|
using Renci.SshNet.Common;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Windows.ApplicationModel.DataTransfer;
|
|
using Windows.Storage;
|
|
using Windows.Storage.Pickers;
|
|
using WinRT.Interop;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
// To learn more about WinUI, the WinUI project structure,
|
|
// and more about our project templates, see: http://aka.ms/winui-project-info.
|
|
|
|
namespace wakka
|
|
{
|
|
/// <summary>
|
|
/// An empty page that can be used on its own or navigated to within a Frame.
|
|
/// </summary>
|
|
|
|
public sealed partial class StartupSshPage : Page
|
|
{
|
|
private static SshConnection Ssh { get; set; } = new SshConnection();
|
|
|
|
public StartupSshPage()
|
|
{
|
|
this.InitializeComponent();
|
|
string? userName = (string)App.LocalSettingsData.Values["username"];
|
|
if (userName == null || userName.Trim() == string.Empty)
|
|
{
|
|
sshConnectButton.IsEnabled = false;
|
|
}
|
|
else
|
|
{
|
|
usernameTextBox.Text = (string)App.LocalSettingsData.Values["username"];
|
|
}
|
|
if (Ssh.KeyExists())
|
|
{
|
|
sshKeySelectButton.Content = "SSH Key Set!";
|
|
//sshKeySelectButton.Click = Sele
|
|
}
|
|
}
|
|
|
|
private async void SelectPrivateKey(object sender, RoutedEventArgs e)
|
|
{
|
|
var picker = new FileOpenPicker();
|
|
picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
|
|
picker.FileTypeFilter.Add("*");
|
|
picker.ViewMode = PickerViewMode.List;
|
|
var currentWindow = (App.Current as App)?.m_window;
|
|
if (currentWindow != null)
|
|
{
|
|
InitializeWithWindow.Initialize(picker, WindowNative.GetWindowHandle(currentWindow));
|
|
StorageFile file = await picker.PickSingleFileAsync();
|
|
if (file != null)
|
|
{
|
|
var SourcePath = file.Path;
|
|
var DestinationPath = SshConnection.SshKeyPath;
|
|
await file.CopyAsync(App.LocalStroageData, "key", NameCollisionOption.ReplaceExisting);
|
|
sshKeySelectButton.Content = "SSH Key Set!";
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CreateSshKey(object sender, RoutedEventArgs e)
|
|
{
|
|
var publicKey = Ssh.CreateSshKey();
|
|
if (publicKey != null)
|
|
{
|
|
var dataPackage = new DataPackage();
|
|
dataPackage.SetText(publicKey);
|
|
Clipboard.SetContent(dataPackage);
|
|
sshKeyCreateButton.Content = "Public key copied!";
|
|
}
|
|
|
|
}
|
|
|
|
private void OnTextChanged(object sender, TextChangedEventArgs e)
|
|
{
|
|
App.LocalSettingsData.Values["username"] = usernameTextBox.Text;
|
|
if (usernameTextBox.Text.Trim() == string.Empty)
|
|
{
|
|
sshConnectButton.IsEnabled = false;
|
|
}
|
|
else
|
|
{
|
|
sshConnectButton.IsEnabled = true;
|
|
}
|
|
}
|
|
|
|
private async void DisplayConnectionErrorDialog ()
|
|
{
|
|
var dialog = new ContentDialog
|
|
{
|
|
Title = "SSH Authentication Error",
|
|
Content = "Username or SSH key are incorrect.",
|
|
CloseButtonText = "Close"
|
|
};
|
|
dialog.XamlRoot = this.XamlRoot;
|
|
await dialog.ShowAsync();
|
|
}
|
|
|
|
private void AttemptConnection(object sender, RoutedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
Ssh.InitializeConnection((string)App.LocalSettingsData.Values["username"]);
|
|
var mainWindow = (App.Current as App)?.m_window;
|
|
if (mainWindow != null)
|
|
{
|
|
mainWindow.Content = new NavigationPage();
|
|
}
|
|
|
|
}
|
|
catch (SshAuthenticationException)
|
|
{
|
|
sshKeySelectButton.Content = "Select New SSH Key";
|
|
DisplayConnectionErrorDialog();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|