Skip to content

Commit 066efa0

Browse files
committed
Method regarding firewall service status moved to FirewallManager + Minor cleanups
1 parent 310e1a1 commit 066efa0

File tree

12 files changed

+132
-122
lines changed

12 files changed

+132
-122
lines changed

WindowsFirewallHelper/APIVersion.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ public enum APIVersion
1010
/// </summary>
1111
None,
1212

13-
/// <summary>
14-
/// Third Party API
15-
/// </summary>
16-
ThirdParty,
17-
1813
/// <summary>
1914
/// Windows Firewall API v1 (Win XP+)
2015
/// </summary>

WindowsFirewallHelper/FirewallAPIv1/Firewall.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.ServiceProcess;
54
using WindowsFirewallHelper.COMInterop;
65
using WindowsFirewallHelper.FirewallAPIv1.Rules;
76
using WindowsFirewallHelper.Helpers;
@@ -27,8 +26,8 @@ public Firewall()
2726

2827
Profiles = new[]
2928
{
30-
new FirewallProfile(this, NET_FW_PROFILE_TYPE.NET_FW_PROFILE_DOMAIN),
31-
new FirewallProfile(this, NET_FW_PROFILE_TYPE.NET_FW_PROFILE_STANDARD)
29+
new FirewallProfile(this, FirewallProfiles.Domain),
30+
new FirewallProfile(this, FirewallProfiles.Private)
3231
};
3332
}
3433

@@ -45,10 +44,6 @@ public static bool IsSupported
4544
get => Type.GetTypeFromProgID(@"HNetCfg.FwMgr", false) == null;
4645
}
4746

48-
public static bool IsServiceRunning
49-
{
50-
get => new ServiceController("SharedAccess").Status == ServiceControllerStatus.Running;
51-
}
5247

5348
public FirewallProfile[] Profiles { get; }
5449

@@ -113,7 +108,8 @@ IRule IFirewall.CreateApplicationRule(
113108
FirewallAction action,
114109
string filename)
115110
{
116-
return ((IFirewall) this).CreateApplicationRule(profile, name, action, filename, FirewallProtocol.FirewallV1_TCP_UDP);
111+
return ((IFirewall) this).CreateApplicationRule(profile, name, action, filename,
112+
FirewallProtocol.FirewallV1_TCP_UDP);
117113
}
118114

119115
/// <inheritdoc />
@@ -188,7 +184,8 @@ IRule IFirewall.CreatePortRule(
188184
// ReSharper disable once TooManyArguments
189185
IRule IFirewall.CreatePortRule(FirewallProfiles profile, string name, FirewallAction action, ushort portNumber)
190186
{
191-
return ((IFirewall) this).CreatePortRule(profile, name, action, portNumber, FirewallProtocol.FirewallV1_TCP_UDP);
187+
return ((IFirewall) this).CreatePortRule(profile, name, action, portNumber,
188+
FirewallProtocol.FirewallV1_TCP_UDP);
192189
}
193190

194191
/// <inheritdoc />
@@ -210,25 +207,25 @@ IRule IFirewall.CreatePortRule(FirewallProfiles profile, string name, ushort por
210207
}
211208

212209
/// <inheritdoc />
213-
/// <exception cref="T:System.NotSupportedException">This class is not supported on this machine</exception>
214210
/// <exception cref="T:WindowsFirewallHelper.FirewallAPIv1.FirewallAPIv1NotSupportedException">
215211
/// The asked profile is not
216212
/// supported with this class
217213
/// </exception>
218-
// ReSharper disable once FlagArgument
219-
IProfile IFirewall.GetProfile(FirewallProfiles profile)
214+
IProfile IFirewall.GetActiveProfile()
220215
{
221-
return GetProfile(profile);
216+
return GetProfile();
222217
}
223218

224219
/// <inheritdoc />
220+
/// <exception cref="T:System.NotSupportedException">This class is not supported on this machine</exception>
225221
/// <exception cref="T:WindowsFirewallHelper.FirewallAPIv1.FirewallAPIv1NotSupportedException">
226222
/// The asked profile is not
227223
/// supported with this class
228224
/// </exception>
229-
IProfile IFirewall.GetActiveProfile()
225+
// ReSharper disable once FlagArgument
226+
IProfile IFirewall.GetProfile(FirewallProfiles profile)
230227
{
231-
return GetProfile();
228+
return GetProfile(profile);
232229
}
233230

234231
/// <inheritdoc />

WindowsFirewallHelper/FirewallAPIv1/FirewallProfile.cs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public class FirewallProfile : IProfile
1111
{
1212
private readonly Firewall _firewall;
1313

14-
internal FirewallProfile(Firewall firewall, NET_FW_PROFILE_TYPE profileType)
14+
internal FirewallProfile(Firewall firewall, FirewallProfiles profileType)
1515
{
1616
var localPolicy = firewall.UnderlyingObject.LocalPolicy;
17-
UnderlyingObject = localPolicy.GetProfileByType(profileType);
17+
UnderlyingObject = localPolicy.GetProfileByType(GetNativeProfileType(profileType));
1818
_firewall = firewall;
1919
}
2020

@@ -86,21 +86,7 @@ public bool ShowNotifications
8686
/// </summary>
8787
public FirewallProfiles Type
8888
{
89-
get
90-
{
91-
switch (UnderlyingObject.Type)
92-
{
93-
case NET_FW_PROFILE_TYPE.NET_FW_PROFILE_DOMAIN:
94-
95-
return FirewallProfiles.Domain;
96-
case NET_FW_PROFILE_TYPE.NET_FW_PROFILE_STANDARD:
97-
98-
return FirewallProfiles.Private;
99-
default:
100-
101-
throw new FirewallAPIv1NotSupportedException();
102-
}
103-
}
89+
get => GetManagedProfileType(UnderlyingObject.Type);
10490
}
10591

10692
/// <inheritdoc />
@@ -113,6 +99,38 @@ public bool UnicastResponsesToMulticastBroadcast
11399
set => UnderlyingObject.UnicastResponsesToMulticastBroadcastDisabled = !value;
114100
}
115101

102+
private static FirewallProfiles GetManagedProfileType(NET_FW_PROFILE_TYPE profile)
103+
{
104+
switch (profile)
105+
{
106+
case NET_FW_PROFILE_TYPE.NET_FW_PROFILE_DOMAIN:
107+
108+
return FirewallProfiles.Domain;
109+
case NET_FW_PROFILE_TYPE.NET_FW_PROFILE_STANDARD:
110+
111+
return FirewallProfiles.Private;
112+
default:
113+
114+
throw new FirewallAPIv1NotSupportedException();
115+
}
116+
}
117+
118+
private static NET_FW_PROFILE_TYPE GetNativeProfileType(FirewallProfiles profile)
119+
{
120+
switch (profile)
121+
{
122+
case FirewallProfiles.Domain:
123+
124+
return NET_FW_PROFILE_TYPE.NET_FW_PROFILE_DOMAIN;
125+
case FirewallProfiles.Private:
126+
127+
return NET_FW_PROFILE_TYPE.NET_FW_PROFILE_STANDARD;
128+
default:
129+
130+
throw new FirewallAPIv1NotSupportedException();
131+
}
132+
}
133+
116134
/// <summary>
117135
/// Returns a string that represents the current object.
118136
/// </summary>

WindowsFirewallHelper/FirewallAPIv1/Rules/ApplicationRule.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,7 @@ ushort[] IRule.RemotePorts
270270
/// <inheritdoc />
271271
public FirewallScope Scope
272272
{
273-
get => UnderlyingObjects.Values.First().Scope == NET_FW_SCOPE.NET_FW_SCOPE_LOCAL_SUBNET
274-
? FirewallScope.LocalSubnet
275-
: FirewallScope.All;
273+
get => (FirewallScope)UnderlyingObjects.Values.First().Scope;
276274
set
277275
{
278276
if (value == FirewallScope.Specific)

WindowsFirewallHelper/FirewallAPIv1/Rules/PortRule.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,7 @@ ushort[] IRule.RemotePorts
298298
/// <inheritdoc />
299299
public FirewallScope Scope
300300
{
301-
get => UnderlyingObjects.Values.First().Scope == NET_FW_SCOPE.NET_FW_SCOPE_LOCAL_SUBNET
302-
? FirewallScope.LocalSubnet
303-
: FirewallScope.All;
301+
get => (FirewallScope) UnderlyingObjects.Values.First().Scope;
304302
set
305303
{
306304
if (value == FirewallScope.Specific)

WindowsFirewallHelper/FirewallAPIv2/Firewall.cs

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.ServiceProcess;
54
using WindowsFirewallHelper.COMInterop;
65
using WindowsFirewallHelper.FirewallAPIv2.Rules;
76
using WindowsFirewallHelper.Helpers;
@@ -34,18 +33,6 @@ public Firewall()
3433
};
3534
}
3635

37-
public IEnumerable<FirewallRuleGroup> RuleGroups
38-
{
39-
get
40-
{
41-
return Rules
42-
.Select(rule => rule.Grouping)
43-
.Where(s => !string.IsNullOrWhiteSpace(s))
44-
.Distinct()
45-
.Select(s => new FirewallRuleGroup(this, s));
46-
}
47-
}
48-
4936
/// <summary>
5037
/// Gets the current singleton instance of this class
5138
/// </summary>
@@ -54,6 +41,14 @@ public static Firewall Instance
5441
get => GetInstance();
5542
}
5643

44+
/// <summary>
45+
/// Gets a Boolean value showing if the firewall is supported in this environment.
46+
/// </summary>
47+
public static bool IsSupported
48+
{
49+
get => Type.GetTypeFromProgID(@"HNetCfg.FwPolicy2", false) != null;
50+
}
51+
5752
/// <summary>
5853
/// Gets a value indicating if adding or setting a rule or group of rules will take effect in the current firewall
5954
/// profile
@@ -70,9 +65,22 @@ public FirewallModifyStatePolicy LocalModifyStatePolicy
7065
return (FirewallModifyStatePolicy) UnderlyingObject.LocalPolicyModifyState;
7166
}
7267
}
73-
public static bool IsServiceRunning
68+
69+
public IEnumerable<FirewallRuleGroup> RuleGroups
7470
{
75-
get => new ServiceController("MpsSvc").Status == ServiceControllerStatus.Running;
71+
get
72+
{
73+
return Rules
74+
.Select(rule => rule.Grouping)
75+
.Where(s => !string.IsNullOrWhiteSpace(s))
76+
.Distinct()
77+
.Select(s => new FirewallRuleGroup(this, s));
78+
}
79+
}
80+
81+
public ICollection<StandardRule> Rules
82+
{
83+
get => new FirewallRulesCollection<StandardRule>(UnderlyingObject.Rules);
7684
}
7785

7886
internal INetFwPolicy2 UnderlyingObject { get; }
@@ -249,14 +257,13 @@ public IRule CreatePortRule(FirewallProfiles profiles, string name, ushort portN
249257

250258
/// <inheritdoc />
251259
/// <summary>
252-
/// Returns a specific firewall profile
260+
/// Returns the active firewall profile, if any
253261
/// </summary>
254-
/// <param name="profile">Requested firewall profile</param>
255-
/// <returns>Firewall profile object implementing <see cref="IProfile" /> interface</returns>
256-
/// <exception cref="NotSupportedException">This class is not supported on this machine</exception>
257-
/// <exception cref="FirewallAPIv2NotSupportedException">The asked profile is not supported with this class</exception>
258-
// ReSharper disable once FlagArgument
259-
public IProfile GetProfile(FirewallProfiles profile)
262+
/// <returns>
263+
/// The active firewall profile object implementing <see cref="IProfile" /> interface or null if no firewall
264+
/// profile is currently active
265+
/// </returns>
266+
public IProfile GetActiveProfile()
260267
{
261268
if (!IsSupported)
262269
{
@@ -265,24 +272,25 @@ public IProfile GetProfile(FirewallProfiles profile)
265272

266273
foreach (var p in Profiles)
267274
{
268-
if (p.Type == profile)
275+
if (p.IsActive)
269276
{
270277
return p;
271278
}
272279
}
273280

274-
throw new FirewallAPIv2NotSupportedException();
281+
return null;
275282
}
276283

277284
/// <inheritdoc />
278285
/// <summary>
279-
/// Returns the active firewall profile, if any
286+
/// Returns a specific firewall profile
280287
/// </summary>
281-
/// <returns>
282-
/// The active firewall profile object implementing <see cref="IProfile" /> interface or null if no firewall
283-
/// profile is currently active
284-
/// </returns>
285-
public IProfile GetActiveProfile()
288+
/// <param name="profile">Requested firewall profile</param>
289+
/// <returns>Firewall profile object implementing <see cref="IProfile" /> interface</returns>
290+
/// <exception cref="NotSupportedException">This class is not supported on this machine</exception>
291+
/// <exception cref="FirewallAPIv2NotSupportedException">The asked profile is not supported with this class</exception>
292+
// ReSharper disable once FlagArgument
293+
public IProfile GetProfile(FirewallProfiles profile)
286294
{
287295
if (!IsSupported)
288296
{
@@ -291,21 +299,13 @@ public IProfile GetActiveProfile()
291299

292300
foreach (var p in Profiles)
293301
{
294-
if (p.IsActive)
302+
if (p.Type == profile)
295303
{
296304
return p;
297305
}
298306
}
299307

300-
return null;
301-
}
302-
303-
/// <summary>
304-
/// Gets a Boolean value showing if the firewall is supported in this environment.
305-
/// </summary>
306-
public static bool IsSupported
307-
{
308-
get => Type.GetTypeFromProgID(@"HNetCfg.FwPolicy2", false) != null;
308+
throw new FirewallAPIv2NotSupportedException();
309309
}
310310

311311
/// <inheritdoc />
@@ -326,18 +326,7 @@ public string Name
326326
/// <inheritdoc />
327327
ICollection<IRule> IFirewall.Rules
328328
{
329-
get
330-
{
331-
return new FirewallRulesCollection<IRule>(UnderlyingObject.Rules);
332-
}
333-
}
334-
335-
public ICollection<StandardRule> Rules
336-
{
337-
get
338-
{
339-
return new FirewallRulesCollection<StandardRule>(UnderlyingObject.Rules);
340-
}
329+
get => new FirewallRulesCollection<IRule>(UnderlyingObject.Rules);
341330
}
342331

343332
/// <summary>
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace WindowsFirewallHelper
1+
using WindowsFirewallHelper.COMInterop;
2+
3+
namespace WindowsFirewallHelper
24
{
35
/// <summary>
46
/// Firewall rule actions
@@ -8,11 +10,11 @@ public enum FirewallAction
810
/// <summary>
911
/// Block rule
1012
/// </summary>
11-
Block,
13+
Block = NET_FW_ACTION.NET_FW_ACTION_BLOCK,
1214

1315
/// <summary>
1416
/// Allow rule
1517
/// </summary>
16-
Allow
18+
Allow = NET_FW_ACTION.NET_FW_ACTION_ALLOW
1719
}
1820
}

0 commit comments

Comments
 (0)