|
1 | 1 | using System; |
2 | | -using System.Diagnostics; |
3 | | -using System.IO; |
4 | 2 |
|
5 | 3 | namespace Tnfsd.NET |
6 | 4 | { |
7 | 5 | public static class FirewallManager |
8 | 6 | { |
9 | | - public static void AddFirewallException(string exePath, string displayName) |
| 7 | + public static void AddFirewallException(string exePath, string ruleName) |
10 | 8 | { |
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); |
13 | 11 |
|
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 |
21 | 18 |
|
22 | | - if (VerifyFirewallRuleExists(exePath)) |
| 19 | + // Add to authorized apps if not already present |
| 20 | + var apps = fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications; |
| 21 | + try |
23 | 22 | { |
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); |
25 | 24 | } |
26 | | - else |
| 25 | + catch |
27 | 26 | { |
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 |
29 | 28 | } |
30 | 29 | } |
31 | 30 |
|
32 | 31 | public static void RemoveFirewallException(string exePath) |
33 | 32 | { |
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; |
43 | 36 |
|
44 | | - if (!VerifyFirewallRuleExists(exePath)) |
| 37 | + try |
45 | 38 | { |
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); |
47 | 40 | } |
48 | | - else |
| 41 | + catch |
49 | 42 | { |
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 |
51 | 44 | } |
52 | 45 | } |
53 | 46 |
|
54 | | - private static bool VerifyFirewallRuleExists(string exePath) |
| 47 | + public static bool FirewallExceptionExists(string ruleName) |
55 | 48 | { |
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; |
68 | 52 |
|
69 | | - return output.Contains(exePath, StringComparison.OrdinalIgnoreCase); |
70 | | - } |
71 | | - catch |
| 53 | + foreach (dynamic app in apps) |
72 | 54 | { |
73 | | - return false; |
| 55 | + if (app.Name != null && |
| 56 | + app.Name.Equals(ruleName, StringComparison.OrdinalIgnoreCase)) |
| 57 | + { |
| 58 | + return true; |
| 59 | + } |
74 | 60 | } |
75 | | - } |
76 | 61 |
|
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; |
88 | 63 | } |
89 | 64 | } |
90 | 65 | } |
| 66 | + |
0 commit comments