Skip to content

Commit

Permalink
For #177: Show client names in status window
Browse files Browse the repository at this point in the history
  • Loading branch information
micahmo committed Jan 17, 2025
1 parent 65e412e commit 417a523
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 17 deletions.
45 changes: 29 additions & 16 deletions WgServerforWindows/Controls/ServerStatusWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
<Window x:Class="WgServerforWindows.Controls.ServerStatusWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WgServerforWindows.Controls"
xmlns:properties="clr-namespace:WgServerforWindows.Properties"
xmlns:models="clr-namespace:WgServerforWindows.Models"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=models:ServerStatusPrerequisite}"
Title="{x:Static properties:Resources.ServerStatus}" Height="400" Width="450">
<Window
x:Class="WgServerforWindows.Controls.ServerStatusWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WgServerforWindows.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="clr-namespace:WgServerforWindows.Models"
xmlns:properties="clr-namespace:WgServerforWindows.Properties"
Title="{x:Static properties:Resources.ServerStatus}"
Width="500"
Height="400"
d:DataContext="{d:DesignInstance Type=models:ServerStatusPrerequisite}"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<CheckBox Grid.Row="0" Margin="10" Content="{x:Static properties:Resources.UpdateLive}" IsChecked="{Binding UpdateLive}"/>
<TextBox Grid.Row="1" Margin="10" Text="{Binding ServerStatus, Mode=OneWay}" FontFamily="Consolas" IsReadOnly="True"
ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"/>
<CheckBox
Grid.Row="0"
Margin="10"
Content="{x:Static properties:Resources.UpdateLive}"
IsChecked="{Binding UpdateLive}" />
<TextBox
Grid.Row="1"
Margin="10"
FontFamily="Consolas"
IsReadOnly="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Text="{Binding ServerStatus, Mode=OneWay}" />
</Grid>
</Window>
50 changes: 49 additions & 1 deletion WgServerforWindows/Models/ServerStatusPrerequisite.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Threading;
using SharpConfig;
using WgAPI;
using WgAPI.Commands;
using WgServerforWindows.Cli.Options;
using WgServerforWindows.Controls;
using WgServerforWindows.Properties;
using System.Collections.Generic;
using System.Linq;

namespace WgServerforWindows.Models
{
Expand Down Expand Up @@ -63,7 +68,49 @@ public void Show()

#region Public properties

public string ServerStatus => new WireGuardExe().ExecuteCommand(new ShowCommand(ServerConfigurationPrerequisite.WireGuardServerInterfaceName));
public string ServerStatus
{
get
{
Dictionary<string, string> clients = new();
try
{
// First, load all of the clients, so we can associate peer IDs.
ClientConfigurationList clientConfigurations = new ClientConfigurationList();
List<ClientConfiguration> clientConfigurationsFromFile = new List<ClientConfiguration>();
foreach (string clientConfigurationFile in Directory.GetFiles(ClientConfigurationsPrerequisite.ClientDataDirectory, "*.conf"))
{
clientConfigurationsFromFile.Add(new ClientConfiguration(clientConfigurations).Load<ClientConfiguration>(Configuration.LoadFromFile(clientConfigurationFile)));
}

clients = clientConfigurationsFromFile.ToDictionary(c => c.PublicKeyProperty.Value, c => c.Name);
}
catch
{
// Ignore if there's any problem with this
}

// Get the output of the status command
string statusOutput = new WireGuardExe().ExecuteCommand(new ShowCommand(ServerConfigurationPrerequisite.WireGuardServerInterfaceName));

// Iterate through the output and correlate peer IDs to names
StringBuilder result = new StringBuilder();
foreach (string line in statusOutput.Split(Environment.NewLine))
{
Match match = _peerRegex.Match(line);
if (match.Success && clients.ContainsKey(match.Groups[1].Value))
{
result.AppendLine(_peerRegex.Replace(line, $"peer: {clients[match.Groups[1].Value]} ({match.Groups[1].Value})"));
}
else
{
result.AppendLine(line);
}
}

return result.ToString();
}
}

public bool UpdateLive
{
Expand All @@ -77,6 +124,7 @@ public bool UpdateLive
#region Private fields

private readonly DispatcherTimer _updateTimer;
private readonly Regex _peerRegex = new Regex(@"peer:\s([^\s]+)", RegexOptions.Compiled);

#endregion
}
Expand Down

0 comments on commit 417a523

Please sign in to comment.