From ff88fb50075e99caed61a389a0209bae0c398c1b Mon Sep 17 00:00:00 2001 From: Salvador Cipolla Date: Wed, 17 Dec 2025 20:51:14 -0300 Subject: [PATCH] Create firewall exceptions for newly downloaded FSO executables --- Knossos.NET/Classes/FsoBuild.cs | 16 +++++++ Knossos.NET/Classes/KnUtils.cs | 46 +++++++++++++++++++ .../Templates/Tasks/InstallBuild.cs | 15 ++++++ 3 files changed, 77 insertions(+) diff --git a/Knossos.NET/Classes/FsoBuild.cs b/Knossos.NET/Classes/FsoBuild.cs index ab28fdff..d2f4fd9c 100644 --- a/Knossos.NET/Classes/FsoBuild.cs +++ b/Knossos.NET/Classes/FsoBuild.cs @@ -445,6 +445,22 @@ public async Task RunFSO(FsoExecType executableType, string cmdline, return null; } + /// + /// Gets a all valid executables for the OS/Cpu arch + /// + /// List or null + public List? GetExecutables() + { + if (directExec != null) + { + return null; + } + + var validExecs = executables.Where(b => b.isValid); + + return validExecs?.ToList(); + } + /// /// Return the fsofile executable fullpath /// diff --git a/Knossos.NET/Classes/KnUtils.cs b/Knossos.NET/Classes/KnUtils.cs index a97a435a..b5ac6772 100644 --- a/Knossos.NET/Classes/KnUtils.cs +++ b/Knossos.NET/Classes/KnUtils.cs @@ -1252,5 +1252,51 @@ public static void CreateDesktopShortcut(string shortcutName, string destFileFul Log.Add(Log.LogSeverity.Error, "KnUtils.CreateDesktopShortcut()", ex); } } + + /// + /// Add windows firewall exception for a executable + /// + /// + /// + /// true/false + public static bool AddFirewallException(string programPath, string ruleName) + { + if (!isWindows) + { + Log.Add(Log.LogSeverity.Error, "KnUtils.AddFirewallExceptions()", "This function is only supported on Windows."); + return false; + } + try + { + var startInfo = new ProcessStartInfo + { + FileName = "netsh", + Arguments = $"advfirewall firewall add rule name=\"{ruleName}\" dir=in action=allow program=\"{programPath.Replace("/", "\\")}\" enable=yes", + UseShellExecute = true, + Verb = "runas", + WindowStyle = ProcessWindowStyle.Hidden + }; + + using (var process = Process.Start(startInfo)) + { + process?.WaitForExit(); + if (process?.ExitCode == 0) + { + Log.Add(Log.LogSeverity.Information, "KnUtils.AddFirewallExceptions()", "Added new Firewall Rules for : " + ruleName); + return true; + } + else + { + Log.Add(Log.LogSeverity.Error, "KnUtils.AddFirewallExceptions()", "Failed to add Firewall Rules for : " + ruleName + ". Error code was: " + process?.ExitCode); + return false; + } + } + } + catch (Exception ex) + { + Log.Add(Log.LogSeverity.Error, "KnUtils.AddFirewallExceptions()", ex); + return false; + } + } } } diff --git a/Knossos.NET/ViewModels/Templates/Tasks/InstallBuild.cs b/Knossos.NET/ViewModels/Templates/Tasks/InstallBuild.cs index 0462e205..ed316712 100644 --- a/Knossos.NET/ViewModels/Templates/Tasks/InstallBuild.cs +++ b/Knossos.NET/ViewModels/Templates/Tasks/InstallBuild.cs @@ -436,6 +436,21 @@ public partial class TaskItemViewModel : ViewModelBase IsCompleted = true; CancelButtonVisible = false; + //Add firewall rules (windows) + if (KnUtils.IsWindows) + { + var executables = newBuild.GetExecutables(); + if (executables != null && executables.Any()) + { + foreach (var executable in executables) + { + var fp = newBuild.GetExecutablePath(executable); + if (fp != null) + _ = Task.Factory.StartNew(() => KnUtils.AddFirewallException(fp, "Knossos " + newBuild.ToString())); + } + } + } + //Re-run Dependencies checks MainWindowViewModel.Instance?.RunModStatusChecks();