Skip to content

Commit e9731c4

Browse files
committed
FirewallProduct introduced
1 parent 066efa0 commit e9731c4

File tree

5 files changed

+241
-0
lines changed

5 files changed

+241
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public enum NET_FW_RULE_CATEGORY
2+
{
3+
NET_FW_RULE_CATEGORY_BOOT = 0,
4+
NET_FW_RULE_CATEGORY_STEALTH = 1,
5+
NET_FW_RULE_CATEGORY_FIREWALL = 2,
6+
NET_FW_RULE_CATEGORY_CONSEC = 3,
7+
NET_FW_RULE_CATEGORY_MAX = 4
8+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using WindowsFirewallHelper.COMInterop;
6+
using WindowsFirewallHelper.Helpers;
7+
8+
namespace WindowsFirewallHelper
9+
{
10+
public class FirewallProduct
11+
{
12+
public FirewallProduct(string name)
13+
{
14+
if (IsSupported)
15+
{
16+
throw new NotSupportedException();
17+
}
18+
19+
UnderlyingObject = (INetFwProduct) Activator.CreateInstance(
20+
Type.GetTypeFromProgID("HNetCfg.FwProduct")
21+
);
22+
Name = name;
23+
}
24+
25+
internal FirewallProduct(INetFwProduct product)
26+
{
27+
UnderlyingObject = product;
28+
}
29+
30+
public string FriendlyName
31+
{
32+
get => NativeHelper.ResolveStringResource(Name);
33+
}
34+
35+
public static bool IsSupported
36+
{
37+
get => Type.GetTypeFromProgID("HNetCfg.FwProduct") != null;
38+
}
39+
40+
public string Name
41+
{
42+
get => UnderlyingObject.DisplayName;
43+
set => UnderlyingObject.DisplayName = value;
44+
}
45+
46+
public FirewallRuleCategory[] RuleCategories
47+
{
48+
get
49+
{
50+
var arrayObject = UnderlyingObject.RuleCategories;
51+
52+
if (arrayObject is IEnumerable array)
53+
{
54+
var ruleCategories = new List<FirewallRuleCategory>();
55+
56+
foreach (var ruleCategoryObject in array)
57+
{
58+
var ruleCategoryInt = (int) ruleCategoryObject;
59+
ruleCategories.Add((FirewallRuleCategory) ruleCategoryInt);
60+
}
61+
62+
return ruleCategories.ToArray();
63+
}
64+
65+
return new FirewallRuleCategory[0];
66+
}
67+
set
68+
{
69+
var array = value.Select(category => (int) category).Cast<object>().ToArray();
70+
UnderlyingObject.RuleCategories = array;
71+
}
72+
}
73+
74+
public string SignedExecutableFilename
75+
{
76+
get => UnderlyingObject.PathToSignedProductExe;
77+
}
78+
79+
/// <summary>
80+
/// Returns the list of all registered third party firewalls management instances
81+
/// </summary>
82+
public static ICollection<FirewallProduct> ThirdPartyFirewalls
83+
{
84+
get => new FirewallProductsCollection(GetProducts());
85+
}
86+
87+
private INetFwProduct UnderlyingObject { get; }
88+
89+
private static INetFwProducts GetProducts()
90+
{
91+
if (!IsSupported)
92+
{
93+
throw new NotSupportedException();
94+
}
95+
96+
return (INetFwProducts) Activator.CreateInstance(
97+
Type.GetTypeFromProgID("HNetCfg.FwProducts")
98+
);
99+
}
100+
101+
/// <summary>
102+
/// Register an instance of a third party firewall management class
103+
/// </summary>
104+
public FirewallProductRegistrationHandle Register()
105+
{
106+
return new FirewallProductRegistrationHandle(GetProducts().Register(GetCOMObject()));
107+
}
108+
109+
110+
internal INetFwProduct GetCOMObject()
111+
{
112+
return UnderlyingObject;
113+
}
114+
}
115+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace WindowsFirewallHelper
5+
{
6+
public class FirewallProductRegistrationHandle : IDisposable
7+
{
8+
private object _handle;
9+
10+
public FirewallProductRegistrationHandle(object handle)
11+
{
12+
if (handle == null || !handle.GetType().IsCOMObject)
13+
{
14+
throw new ArgumentException("Handle is empty or invalid.", nameof(handle));
15+
}
16+
17+
_handle = handle;
18+
}
19+
20+
/// <inheritdoc />
21+
public void Dispose()
22+
{
23+
Release();
24+
GC.SuppressFinalize(this);
25+
}
26+
27+
public void Release()
28+
{
29+
lock (this)
30+
{
31+
if (_handle != null)
32+
{
33+
Marshal.ReleaseComObject(_handle);
34+
_handle = null;
35+
}
36+
}
37+
}
38+
39+
/// <inheritdoc />
40+
~FirewallProductRegistrationHandle()
41+
{
42+
Release();
43+
}
44+
}
45+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Runtime.InteropServices.ComTypes;
3+
using WindowsFirewallHelper.COMInterop;
4+
using WindowsFirewallHelper.Helpers;
5+
6+
namespace WindowsFirewallHelper
7+
{
8+
internal class FirewallProductsCollection : COMCollection<INetFwProducts, INetFwProduct, int, FirewallProduct>
9+
{
10+
/// <inheritdoc />
11+
public FirewallProductsCollection(INetFwProducts nativeEnumerable) : base(nativeEnumerable)
12+
{
13+
}
14+
15+
/// <inheritdoc />
16+
public override bool IsReadOnly { get; } = true;
17+
18+
/// <inheritdoc />
19+
protected override INetFwProduct ConvertManagedToNative(FirewallProduct managed)
20+
{
21+
return managed.GetCOMObject();
22+
}
23+
24+
/// <inheritdoc />
25+
protected override FirewallProduct ConvertNativeToManaged(INetFwProduct native)
26+
{
27+
return new FirewallProduct(native);
28+
}
29+
30+
/// <inheritdoc />
31+
protected override int GetCollectionKey(FirewallProduct managed)
32+
{
33+
throw new InvalidOperationException();
34+
}
35+
36+
/// <inheritdoc />
37+
protected override IEnumVARIANT GetEnumVariant()
38+
{
39+
return NativeEnumerable.GetEnumeratorVariant();
40+
}
41+
42+
/// <inheritdoc />
43+
protected override void InternalAdd(INetFwProduct native)
44+
{
45+
throw new InvalidOperationException();
46+
}
47+
48+
/// <inheritdoc />
49+
protected override int InternalCount()
50+
{
51+
return NativeEnumerable.Count;
52+
}
53+
54+
/// <inheritdoc />
55+
protected override INetFwProduct InternalItem(int key)
56+
{
57+
return NativeEnumerable.Item(key);
58+
}
59+
60+
/// <inheritdoc />
61+
protected override void InternalRemove(int key)
62+
{
63+
throw new InvalidOperationException();
64+
}
65+
}
66+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public enum FirewallRuleCategory
2+
{
3+
Boot = NET_FW_RULE_CATEGORY.NET_FW_RULE_CATEGORY_BOOT,
4+
Stealth = NET_FW_RULE_CATEGORY.NET_FW_RULE_CATEGORY_STEALTH,
5+
Firewall = NET_FW_RULE_CATEGORY.NET_FW_RULE_CATEGORY_FIREWALL,
6+
ConnectionSecurity = NET_FW_RULE_CATEGORY.NET_FW_RULE_CATEGORY_CONSEC
7+
}

0 commit comments

Comments
 (0)