Skip to content

Commit 4127e67

Browse files
committed
create test commands in config
1 parent acb87a1 commit 4127e67

File tree

2 files changed

+149
-13
lines changed

2 files changed

+149
-13
lines changed

SourceServerManager/ViewModels/MainWindowViewModel.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public string StatusText
118118
public ICommand DuplicateSelectedServerCommand { get; }
119119
public ICommand DeleteSelectedServerCommand { get; }
120120
public ICommand RefreshSelectedServerCommand { get; }
121+
public ICommand TestRconConnectionCommand { get; }
122+
public ICommand TestFtpConnectionCommand { get; }
121123

122124
public MainWindowViewModel()
123125
{
@@ -162,6 +164,9 @@ public MainWindowViewModel()
162164
RefreshSelectedServerCommand = ReactiveCommand.Create(
163165
async () => { if (SelectedServer != null) await _statusService.UpdateServerInfoAsync(SelectedServer); });
164166

167+
TestRconConnectionCommand = ReactiveCommand.CreateFromTask(TestRconConnectionAsync);
168+
TestFtpConnectionCommand = ReactiveCommand.CreateFromTask(TestFtpConnectionAsync);
169+
165170
// Load saved servers
166171
Task.Run(LoadSavedServersAsync);
167172

@@ -757,6 +762,121 @@ private void AppendToFTPConsole(string text, bool isRcon = true)
757762
});
758763
}
759764

765+
private async Task TestRconConnectionAsync()
766+
{
767+
if (SelectedServer == null)
768+
{
769+
UpdateStatus("Error: No server selected", false);
770+
return;
771+
}
772+
773+
// Validate RCON fields
774+
if (string.IsNullOrWhiteSpace(SelectedServer.IpAddress))
775+
{
776+
UpdateStatus("Error: IP Address is required for RCON test", false);
777+
return;
778+
}
779+
780+
if (SelectedServer.RconPort <= 0 || SelectedServer.RconPort > 65535)
781+
{
782+
UpdateStatus("Error: RCON Port must be between 1 and 65535", false);
783+
return;
784+
}
785+
786+
if (string.IsNullOrWhiteSpace(SelectedServer.RconPassword))
787+
{
788+
UpdateStatus("Error: RCON Password is required for RCON test", false);
789+
return;
790+
}
791+
792+
try
793+
{
794+
UpdateStatus($"Testing RCON connection to {SelectedServer.IpAddress}:{SelectedServer.RconPort}...");
795+
796+
// Test with a simple status command
797+
var response = await _rconService.ExecuteCommandAsync(SelectedServer, "status");
798+
799+
if (!string.IsNullOrEmpty(response) && !response.Contains("Unable to connect"))
800+
{
801+
UpdateStatus($"✅ RCON connection successful to {SelectedServer.DisplayName}", true, 5000);
802+
}
803+
else
804+
{
805+
UpdateStatus($"❌ RCON connection failed: Invalid response received", false);
806+
}
807+
}
808+
catch (Exception ex)
809+
{
810+
UpdateStatus($"❌ RCON connection failed: {ex.Message}", false);
811+
}
812+
}
813+
814+
private async Task TestFtpConnectionAsync()
815+
{
816+
if (SelectedServer == null)
817+
{
818+
UpdateStatus("Error: No server selected", false);
819+
return;
820+
}
821+
822+
// Validate FTP fields
823+
if (string.IsNullOrWhiteSpace(SelectedServer.FtpHost))
824+
{
825+
UpdateStatus("Error: FTP Host is required for FTP test", false);
826+
return;
827+
}
828+
829+
if (SelectedServer.FtpPort <= 0 || SelectedServer.FtpPort > 65535)
830+
{
831+
UpdateStatus("Error: FTP Port must be between 1 and 65535", false);
832+
return;
833+
}
834+
835+
if (string.IsNullOrWhiteSpace(SelectedServer.FtpUsername))
836+
{
837+
UpdateStatus("Error: FTP Username is required for FTP test", false);
838+
return;
839+
}
840+
841+
if (string.IsNullOrWhiteSpace(SelectedServer.FtpPassword))
842+
{
843+
UpdateStatus("Error: FTP Password is required for FTP test", false);
844+
return;
845+
}
846+
847+
try
848+
{
849+
string protocol = SelectedServer.FtpProtocol == FileTransferProtocol.SFTP ? "SFTP" : "FTP";
850+
UpdateStatus($"Testing {protocol} connection to {SelectedServer.FtpHost}:{SelectedServer.FtpPort}...");
851+
852+
string result;
853+
if (SelectedServer.FtpProtocol == FileTransferProtocol.SFTP)
854+
{
855+
// Test SFTP connection by listing the root directory
856+
result = await _sftpService.ListDirectoryAsync(SelectedServer, "/");
857+
}
858+
else
859+
{
860+
// Test FTP connection by listing the root directory
861+
result = await _ftpService.ListDirectoryAsync(SelectedServer, "/");
862+
}
863+
864+
if (!string.IsNullOrEmpty(result) && !result.Contains("Error") && !result.Contains("Failed"))
865+
{
866+
UpdateStatus($"✅ {protocol} connection successful to {SelectedServer.DisplayName}", true, 5000);
867+
}
868+
else
869+
{
870+
UpdateStatus($"❌ {protocol} connection failed: Unable to list directory", false);
871+
}
872+
}
873+
catch (Exception ex)
874+
{
875+
string protocol = SelectedServer.FtpProtocol == FileTransferProtocol.SFTP ? "SFTP" : "FTP";
876+
UpdateStatus($"❌ {protocol} connection failed: {ex.Message}", false);
877+
}
878+
}
879+
760880
protected override void Dispose(bool disposing)
761881
{
762882
if (disposing)

SourceServerManager/Views/MainWindow.axaml

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
</Grid>
182182

183183
<!-- Server config fields - without Name field -->
184-
<Grid ColumnDefinitions="Auto,*" RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto"
184+
<Grid ColumnDefinitions="Auto,*" RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto"
185185
IsVisible="{Binding SelectedServer}" Margin="0,15,0,0">
186186
<!-- Connection settings -->
187187
<TextBlock Grid.Row="0" Grid.Column="0" Text="IP Address:" Margin="0,5,5,5"/>
@@ -199,38 +199,54 @@
199199
PasswordChar="*" Margin="0,5"
200200
IsReadOnly="{Binding !IsEditingServer}"/>
201201

202+
<!-- RCON Test Button -->
203+
<Button Grid.Row="4" Grid.Column="1" Content="Test RCON Connection"
204+
Command="{Binding TestRconConnectionCommand}"
205+
IsVisible="{Binding SelectedServer}"
206+
HorizontalAlignment="Right"
207+
Margin="0,5,0,10"
208+
Padding="10,3"/>
209+
202210
<!-- FTP settings -->
203-
<TextBlock Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Text="FTP Settings"
211+
<TextBlock Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" Text="FTP Settings"
204212
FontWeight="Bold" Margin="0,15,0,5"/>
205213

206-
<TextBlock Grid.Row="5" Grid.Column="0" Text="Protocol:" Margin="0,5,5,5"/>
207-
<ComboBox Grid.Row="5" Grid.Column="1"
214+
<TextBlock Grid.Row="6" Grid.Column="0" Text="Protocol:" Margin="0,5,5,5"/>
215+
<ComboBox Grid.Row="6" Grid.Column="1"
208216
SelectedIndex="{Binding SelectedServer.FtpProtocol}"
209217
IsEnabled="{Binding IsEditingServer}"
210218
Margin="0,5">
211219
<ComboBoxItem>FTP</ComboBoxItem>
212220
<ComboBoxItem>SFTP</ComboBoxItem>
213221
</ComboBox>
214222

215-
<TextBlock Grid.Row="6" Grid.Column="0" Text="FTP Host:" Margin="0,5,5,5"/>
216-
<TextBox Grid.Row="6" Grid.Column="1" Text="{Binding SelectedServer.FtpHost}" Margin="0,5"
223+
<TextBlock Grid.Row="7" Grid.Column="0" Text="FTP Host:" Margin="0,5,5,5"/>
224+
<TextBox Grid.Row="7" Grid.Column="1" Text="{Binding SelectedServer.FtpHost}" Margin="0,5"
217225
IsReadOnly="{Binding !IsEditingServer}"/>
218226

219-
<TextBlock Grid.Row="7" Grid.Column="0" Text="FTP Port:" Margin="0,5,5,5"/>
220-
<TextBox Grid.Row="7" Grid.Column="1" Text="{Binding SelectedServer.FtpPort}" Margin="0,5"
227+
<TextBlock Grid.Row="8" Grid.Column="0" Text="FTP Port:" Margin="0,5,5,5"/>
228+
<TextBox Grid.Row="8" Grid.Column="1" Text="{Binding SelectedServer.FtpPort}" Margin="0,5"
221229
IsReadOnly="{Binding !IsEditingServer}"/>
222230

223-
<TextBlock Grid.Row="8" Grid.Column="0" Text="FTP Username:" Margin="0,5,5,5"/>
224-
<TextBox Grid.Row="8" Grid.Column="1" Text="{Binding SelectedServer.FtpUsername}" Margin="0,5"
231+
<TextBlock Grid.Row="9" Grid.Column="0" Text="FTP Username:" Margin="0,5,5,5"/>
232+
<TextBox Grid.Row="9" Grid.Column="1" Text="{Binding SelectedServer.FtpUsername}" Margin="0,5"
225233
IsReadOnly="{Binding !IsEditingServer}"/>
226234

227-
<TextBlock Grid.Row="9" Grid.Column="0" Text="FTP Password:" Margin="0,5,5,5"/>
228-
<TextBox Grid.Row="9" Grid.Column="1" Text="{Binding SelectedServer.FtpPassword}"
235+
<TextBlock Grid.Row="10" Grid.Column="0" Text="FTP Password:" Margin="0,5,5,5"/>
236+
<TextBox Grid.Row="10" Grid.Column="1" Text="{Binding SelectedServer.FtpPassword}"
229237
PasswordChar="*" Margin="0,5"
230238
IsReadOnly="{Binding !IsEditingServer}"/>
231239

240+
<!-- FTP Test Button -->
241+
<Button Grid.Row="11" Grid.Column="1" Content="Test FTP Connection"
242+
Command="{Binding TestFtpConnectionCommand}"
243+
IsVisible="{Binding SelectedServer}"
244+
HorizontalAlignment="Right"
245+
Margin="0,5,0,10"
246+
Padding="10,3"/>
247+
232248
<!-- Action buttons (conditional display based on edit mode) -->
233-
<StackPanel Grid.Row="10" Grid.Column="0" Grid.ColumnSpan="2"
249+
<StackPanel Grid.Row="12" Grid.Column="0" Grid.ColumnSpan="2"
234250
Orientation="Horizontal" HorizontalAlignment="Right"
235251
Margin="0,15,0,0">
236252
<!-- Buttons visible when NOT in edit mode -->

0 commit comments

Comments
 (0)