From 417a523bcaf12927f2a61490978322ff1b9263e4 Mon Sep 17 00:00:00 2001 From: Micah Morrison Date: Fri, 17 Jan 2025 16:47:42 -0500 Subject: [PATCH] For #177: Show client names in status window --- .../Controls/ServerStatusWindow.xaml | 45 +++++++++++------ .../Models/ServerStatusPrerequisite.cs | 50 ++++++++++++++++++- 2 files changed, 78 insertions(+), 17 deletions(-) diff --git a/WgServerforWindows/Controls/ServerStatusWindow.xaml b/WgServerforWindows/Controls/ServerStatusWindow.xaml index e449acd..b3d81fa 100644 --- a/WgServerforWindows/Controls/ServerStatusWindow.xaml +++ b/WgServerforWindows/Controls/ServerStatusWindow.xaml @@ -1,22 +1,35 @@ - + - - + + - - + + diff --git a/WgServerforWindows/Models/ServerStatusPrerequisite.cs b/WgServerforWindows/Models/ServerStatusPrerequisite.cs index 873dfeb..9b7866c 100644 --- a/WgServerforWindows/Models/ServerStatusPrerequisite.cs +++ b/WgServerforWindows/Models/ServerStatusPrerequisite.cs @@ -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 { @@ -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 clients = new(); + try + { + // First, load all of the clients, so we can associate peer IDs. + ClientConfigurationList clientConfigurations = new ClientConfigurationList(); + List clientConfigurationsFromFile = new List(); + foreach (string clientConfigurationFile in Directory.GetFiles(ClientConfigurationsPrerequisite.ClientDataDirectory, "*.conf")) + { + clientConfigurationsFromFile.Add(new ClientConfiguration(clientConfigurations).Load(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 { @@ -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 }