Skip to content

Commit f522532

Browse files
author
Rich Stephens
committed
Full functionality create/delete task scheduler task, create/delete firewall exception
1 parent 7cd269b commit f522532

File tree

11 files changed

+599
-538
lines changed

11 files changed

+599
-538
lines changed

Tnfsd.NET/AppSettings.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.IO;
2+
using Microsoft.Extensions.Configuration;
3+
4+
namespace Tnfsd.NET
5+
{
6+
public static class AppSettings
7+
{
8+
private static IConfigurationRoot _config;
9+
10+
static AppSettings()
11+
{
12+
var builder = new ConfigurationBuilder()
13+
.SetBasePath(Directory.GetCurrentDirectory())
14+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
15+
16+
_config = builder.Build();
17+
}
18+
19+
public static string TNFSDownloadURL => _config["TNFSDownloadURL"];
20+
}
21+
}

Tnfsd.NET/FirewallHelper.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

Tnfsd.NET/FirewallManager.cs

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,66 @@
11
using System;
2-
using System.Diagnostics;
3-
using System.IO;
42

53
namespace Tnfsd.NET
64
{
75
public static class FirewallManager
86
{
9-
public static void AddFirewallException(string exePath, string displayName)
7+
public static void AddFirewallException(string exePath, string ruleName)
108
{
11-
if (string.IsNullOrWhiteSpace(exePath) || !File.Exists(exePath))
12-
throw new FileNotFoundException("Executable not found for firewall exception.");
9+
Type fwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr");
10+
dynamic fwMgr = Activator.CreateInstance(fwMgrType);
1311

14-
// Don't do anything if the firewall rule already exists.
15-
if (VerifyFirewallRuleExists(exePath))
16-
{
17-
return;
18-
}
19-
20-
RunNetshCommand($"advfirewall firewall add rule name=\"{displayName}\" dir=in action=allow program=\"{exePath}\" enable=yes profile=private,public");
12+
dynamic app = Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"));
13+
app.Name = ruleName;
14+
app.ProcessImageFileName = exePath;
15+
app.Enabled = true;
16+
app.Scope = 0; // NET_FW_SCOPE_ALL
17+
app.IpVersion = 2; // NET_FW_IP_VERSION_ANY
2118

22-
if (VerifyFirewallRuleExists(exePath))
19+
// Add to authorized apps if not already present
20+
var apps = fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications;
21+
try
2322
{
24-
System.Windows.Forms.MessageBox.Show($"Firewall rule successfully added for {Path.GetFileName(exePath)}.", "Firewall Rule Added", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
23+
apps.Add(app);
2524
}
26-
else
25+
catch
2726
{
28-
System.Windows.Forms.MessageBox.Show($"Warning: Unable to verify the firewall rule for {Path.GetFileName(exePath)}.", "Verification Warning", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
27+
// If already exists, ignore
2928
}
3029
}
3130

3231
public static void RemoveFirewallException(string exePath)
3332
{
34-
if (string.IsNullOrWhiteSpace(exePath)) return;
35-
36-
// Don't do anything if the firewall rule is doesn't exist
37-
if (!VerifyFirewallRuleExists(exePath))
38-
{
39-
return;
40-
}
41-
42-
RunNetshCommand($"advfirewall firewall delete rule program=\"{exePath}\"");
33+
Type fwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr");
34+
dynamic fwMgr = Activator.CreateInstance(fwMgrType);
35+
var apps = fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications;
4336

44-
if (!VerifyFirewallRuleExists(exePath))
37+
try
4538
{
46-
System.Windows.Forms.MessageBox.Show($"Firewall rule successfully removed for {Path.GetFileName(exePath)}.", "Firewall Rule Removed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
39+
apps.Remove(exePath);
4740
}
48-
else
41+
catch
4942
{
50-
System.Windows.Forms.MessageBox.Show($"Warning: Firewall rule may still exist for {Path.GetFileName(exePath)}.", "Verification Warning", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
43+
// Ignore if not found
5144
}
5245
}
5346

54-
private static bool VerifyFirewallRuleExists(string exePath)
47+
public static bool FirewallExceptionExists(string ruleName)
5548
{
56-
try
57-
{
58-
Process process = new Process();
59-
process.StartInfo.FileName = "netsh";
60-
process.StartInfo.Arguments = $"advfirewall firewall show rule name=all | findstr /I \"{exePath}\"";
61-
process.StartInfo.RedirectStandardOutput = true;
62-
process.StartInfo.RedirectStandardError = true;
63-
process.StartInfo.UseShellExecute = false;
64-
process.StartInfo.CreateNoWindow = true;
65-
process.Start();
66-
string output = process.StandardOutput.ReadToEnd();
67-
process.WaitForExit();
49+
Type fwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr");
50+
dynamic fwMgr = Activator.CreateInstance(fwMgrType);
51+
var apps = fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications;
6852

69-
return output.Contains(exePath, StringComparison.OrdinalIgnoreCase);
70-
}
71-
catch
53+
foreach (dynamic app in apps)
7254
{
73-
return false;
55+
if (app.Name != null &&
56+
app.Name.Equals(ruleName, StringComparison.OrdinalIgnoreCase))
57+
{
58+
return true;
59+
}
7460
}
75-
}
7661

77-
private static void RunNetshCommand(string arguments)
78-
{
79-
Process process = new Process();
80-
process.StartInfo.FileName = "netsh";
81-
process.StartInfo.Arguments = arguments;
82-
process.StartInfo.RedirectStandardOutput = true;
83-
process.StartInfo.RedirectStandardError = true;
84-
process.StartInfo.UseShellExecute = false;
85-
process.StartInfo.CreateNoWindow = true;
86-
process.Start();
87-
process.WaitForExit();
62+
return false;
8863
}
8964
}
9065
}
66+

0 commit comments

Comments
 (0)