tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
WindowsFirewallHelper.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4
5using Microsoft.Extensions.Logging;
6
8{
13 {
24 public static async ValueTask<int> AddFirewallException(
25 IProcessExecutor processExecutor,
26 ILogger logger,
27 string exceptionName,
28 string exePath,
29 bool lowPriority,
30 CancellationToken cancellationToken)
31 {
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(
35 "netsh.exe",
36 Environment.CurrentDirectory,
37 arguments,
38 cancellationToken,
39 readStandardHandles: true,
40 noShellExecute: true);
41
42 if (lowPriority)
43 netshProcess.AdjustPriority(false);
44
45 int exitCode;
46 using (cancellationToken.Register(() => netshProcess.Terminate()))
47 exitCode = (await netshProcess.Lifetime).Value;
48 cancellationToken.ThrowIfCancellationRequested();
49
50 logger.LogDebug(
51 "netsh.exe output:{newLine}{output}",
52 Environment.NewLine,
53 await netshProcess.GetCombinedOutput(cancellationToken));
54
55 return exitCode;
56 }
57 }
58}
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.