3using System.Threading.Tasks;
5using Microsoft.Extensions.Logging;
30 CancellationToken cancellationToken)
32 logger.LogInformation(
"Adding Windows Firewall exception for {path}...", exePath);
33 var arguments = $
"advfirewall firewall add rule name=\"{exceptionName}\" program=\"{exePath}\" protocol=tcp dir=in enable=yes action=allow";
34 await
using var netshProcess = await processExecutor.
LaunchProcess(
36 Environment.CurrentDirectory,
39 readStandardHandles:
true,
40 noShellExecute:
true);
43 netshProcess.AdjustPriority(
false);
46 using (cancellationToken.Register(() => netshProcess.Terminate()))
47 exitCode = (await netshProcess.Lifetime).Value;
48 cancellationToken.ThrowIfCancellationRequested();
51 "netsh.exe output:{newLine}{output}",
53 await netshProcess.GetCombinedOutput(cancellationToken));
Helper class for interacting with the Windows Firewall.
static async ValueTask< int > AddFirewallException(IProcessExecutor processExecutor, ILogger logger, string exceptionName, string exePath, bool lowPriority, CancellationToken cancellationToken)
Add an executable exception to the Windows firewall.
ValueTask< IProcess > LaunchProcess(string fileName, string workingDirectory, string arguments, CancellationToken cancellationToken, IReadOnlyDictionary< string, string >? environment=null, string? fileRedirect=null, bool readStandardHandles=false, bool noShellExecute=false)
Launch a IProcess.