Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit d5768b7

Browse files
committed
added "Test FTP connection" button in configs
1 parent ec7854c commit d5768b7

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

UI/Windows/ConfigWindow.xaml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
<TextBlock Name="PreBuildBlock" IsHitTestVisible="False" HorizontalAlignment="Left" VerticalAlignment="Center"/>
160160
<ComboBox Name="CMD_ItemC" Width="200" HorizontalAlignment="Right" Margin="0,0,0,5"/>
161161
</DockPanel>
162-
<TextBox Name="C_PreBuildCmd" FlowDirection="RightToLeft" Height="80" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" TextChanged="C_PreBuildCmd_TextChanged" GotFocus="BuildCommandsBoxes_OnFocus" />
162+
<TextBox Name="C_PreBuildCmd" Height="80" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" TextChanged="C_PreBuildCmd_TextChanged" GotFocus="BuildCommandsBoxes_OnFocus" />
163163
</StackPanel>
164164

165165
<StackPanel Margin="5">
@@ -183,14 +183,15 @@
183183
</Grid.Resources>
184184
<Grid Grid.Column="0">
185185
<StackPanel Margin="5,0,5,0">
186-
<TextBlock Name="FTPHostBlock" IsHitTestVisible="False" />
187-
<TextBox Name="C_FTPHost" TextChanged="C_FTPHost_TextChanged" />
188-
<TextBlock Name="FTPUserBlock" IsHitTestVisible="False" />
189-
<TextBox Name="C_FTPUser" TextChanged="C_FTPUser_TextChanged" />
190-
<TextBlock Name="FTPPWBlock" IsHitTestVisible="False" />
191-
<PasswordBox Name="C_FTPPW" PasswordChanged="C_FTPPW_TextChanged" />
192-
<TextBlock Name="FTPDirBlock" IsHitTestVisible="False" />
193-
<TextBox Name="C_FTPDir" TextChanged="C_FTPDir_TextChanged" />
186+
<TextBlock x:Name="FTPHostBlock" IsHitTestVisible="False" />
187+
<TextBox x:Name="C_FTPHost" TextChanged="C_FTPHost_TextChanged" />
188+
<TextBlock x:Name="FTPUserBlock" IsHitTestVisible="False" />
189+
<TextBox x:Name="C_FTPUser" TextChanged="C_FTPUser_TextChanged" />
190+
<TextBlock x:Name="FTPPWBlock" IsHitTestVisible="False" />
191+
<PasswordBox x:Name="C_FTPPW" PasswordChanged="C_FTPPW_TextChanged" />
192+
<TextBlock x:Name="FTPDirBlock" IsHitTestVisible="False" />
193+
<TextBox x:Name="C_FTPDir" TextChanged="C_FTPDir_TextChanged" />
194+
<Button x:Name="FTPTestConnectionButton" Content="Test Connection" Click="FTPTestConnectionButton_Click" HorizontalAlignment="Left" Margin="0,20,0,-10"/>
194195
</StackPanel>
195196
</Grid>
196197
<Grid Grid.Column="1" Margin="5,0,0,0">

UI/Windows/ConfigWindow.xaml.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Windows;
1010
using System.Windows.Controls;
1111
using System.Windows.Input;
12+
using System.Windows.Threading;
1213
using System.Xml;
1314
using MahApps.Metro;
1415
using MahApps.Metro.Controls.Dialogs;
@@ -500,6 +501,29 @@ private void C_FTPDir_TextChanged(object sender, TextChangedEventArgs e)
500501
Program.Configs[ConfigListBox.SelectedIndex].FTPDir = C_FTPDir.Text;
501502
}
502503

504+
private async void FTPTestConnectionButton_Click(object sender, RoutedEventArgs e)
505+
{
506+
var result = await this.ShowProgressAsync("Testing the FTP connection with the provided details...", "Please wait...", settings: Program.MainWindow.MetroDialogOptions);
507+
var ftp = new FTP(C_FTPHost.Text, C_FTPUser.Text, C_FTPPW.Password);
508+
result.SetIndeterminate();
509+
result.SetCancelable(true);
510+
result.Canceled += async delegate
511+
{
512+
await result?.CloseAsync();
513+
return;
514+
};
515+
if (await ftp.TestConnection())
516+
{
517+
await result?.CloseAsync();
518+
await this.ShowMessageAsync("Success", "Connection successful!", settings: Program.MainWindow.MetroDialogOptions);
519+
}
520+
else
521+
{
522+
await result?.CloseAsync();
523+
await this.ShowMessageAsync("Error", $"The connection could not be made! Message: {ftp.ErrorMessage}", settings: Program.MainWindow.MetroDialogOptions);
524+
}
525+
}
526+
503527
private void C_RConIP_TextChanged(object sender, RoutedEventArgs e)
504528
{
505529
if (!AllowChange)

Utils/Models/FTP.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.IO;
33
using System.Net;
4+
using System.Threading.Tasks;
45
using Renci.SshNet;
6+
using Renci.SshNet.Common;
57

68
namespace SPCode.Utils
79
{
@@ -11,6 +13,8 @@ public class FTP
1113
private readonly string _user;
1214
private readonly string _pass;
1315

16+
public string ErrorMessage;
17+
1418
public FTP(string host, string user, string password) { _host = host; _user = user; _pass = password; }
1519

1620
public void Upload(string remoteFile, string localFile)
@@ -39,5 +43,55 @@ public void Upload(string remoteFile, string localFile)
3943
client.UploadFile(requestUri, WebRequestMethods.Ftp.UploadFile, localFile);
4044
}
4145
}
46+
47+
/// <summary>
48+
/// Returns whether the specified credentials in the Configs Window can be used for a valid FTP/SFTP connection.
49+
/// </summary>
50+
public async Task<bool> TestConnection()
51+
{
52+
var requestUri = new UriBuilder(_host).Uri;
53+
var success = true;
54+
55+
if (requestUri.Scheme == "sftp")
56+
{
57+
var connectionInfo = new ConnectionInfo(requestUri.Host, requestUri.Port == -1 ? 22 : requestUri.Port, _user, new PasswordAuthenticationMethod(_user, _pass));
58+
using var sftp = new SftpClient(connectionInfo);
59+
sftp.OperationTimeout = TimeSpan.FromSeconds(5);
60+
try
61+
{
62+
await Task.Run(() => sftp.Connect());
63+
}
64+
catch (SshAuthenticationException)
65+
{
66+
success = false;
67+
ErrorMessage = "Invalid credentials!";
68+
}
69+
catch (Exception ex)
70+
{
71+
success = false;
72+
ErrorMessage = ex.Message;
73+
}
74+
finally
75+
{
76+
sftp.Disconnect();
77+
sftp.Dispose();
78+
}
79+
}
80+
else
81+
{
82+
var requestDir = WebRequest.Create(requestUri);
83+
requestDir.Credentials = new NetworkCredential(_user, _pass);
84+
requestDir.Timeout = 5000;
85+
try
86+
{
87+
var response = await requestDir.GetResponseAsync();
88+
}
89+
catch
90+
{
91+
success = false;
92+
}
93+
}
94+
return success;
95+
}
4296
}
4397
}

0 commit comments

Comments
 (0)