@@ -16,18 +16,26 @@ namespace WindowsFirewallHelper
1616 public class FirewallLegacy : IFirewall
1717 {
1818 /// <summary>
19- /// Creates a new instance of this class on the current thread and leaves the cross thread control to the user of the
19+ /// Creates a new instance of this class on the current thread for the local machine and leaves the cross thread control to the user of the
2020 /// class.
2121 /// </summary>
22- public FirewallLegacy ( )
22+ public FirewallLegacy ( ) : this ( new COMTypeResolver ( ) )
2323 {
24- if ( ! IsSupported )
24+ }
25+
26+ /// <summary>
27+ /// Creates a new instance of this class on the current thread for a remote machine and leaves the cross thread control to the user of the
28+ /// class.
29+ /// </summary>
30+ public FirewallLegacy ( COMTypeResolver typeResolver )
31+ {
32+ if ( ! IsSupported ( typeResolver ) )
2533 {
2634 throw new NotSupportedException ( "This type is not supported in this environment." ) ;
2735 }
2836
29- UnderlyingObject = ComHelper . CreateInstance < INetFwMgr > ( ) ;
30-
37+ TypeResolver = typeResolver ;
38+ UnderlyingObject = TypeResolver . CreateInstance < INetFwMgr > ( ) ;
3139 Profiles = new ReadOnlyCollection < FirewallLegacyProfile > (
3240 new [ ]
3341 {
@@ -40,10 +48,13 @@ public FirewallLegacy()
4048 /// <inheritdoc />
4149 public IFirewall Reload ( )
4250 {
43- UnderlyingObject = ComHelper . CreateInstance < INetFwMgr > ( ) ;
51+ UnderlyingObject = TypeResolver . CreateInstance < INetFwMgr > ( ) ;
4452 return this ;
4553 }
4654
55+ /// <inheritdoc />
56+ public COMTypeResolver TypeResolver { get ; }
57+
4758 /// <summary>
4859 /// Gets the current singleton instance of this class
4960 /// </summary>
@@ -53,11 +64,19 @@ public static FirewallLegacy Instance
5364 }
5465
5566 /// <summary>
56- /// Gets a Boolean value showing if the firewall is supported in this environment.
67+ /// Gets a Boolean value showing if the firewall is supported locally
68+ /// </summary>
69+ public static bool IsLocallySupported
70+ {
71+ get => IsSupported ( new COMTypeResolver ( ) ) ;
72+ }
73+
74+ /// <summary>
75+ /// Gets a Boolean value showing if the firewall is supported remotely
5776 /// </summary>
58- public static bool IsSupported
77+ public static bool IsSupported ( COMTypeResolver typeResolver )
5978 {
60- get => ComHelper . IsSupported < INetFwMgr > ( ) ;
79+ return typeResolver . IsSupported < INetFwMgr > ( ) ;
6180 }
6281
6382 /// <summary>
@@ -70,7 +89,7 @@ public static bool IsSupported
7089 /// </summary>
7190 public IFirewallLegacyRulesCollection Rules
7291 {
73- get => new FirewallLegacyRulesCollection ( Profiles . ToArray ( ) ) ;
92+ get => new FirewallLegacyRulesCollection ( Profiles . ToArray ( ) , this ) ;
7493 }
7594
7695 internal INetFwMgr UnderlyingObject { get ; private set ; }
@@ -85,11 +104,6 @@ IFirewallRule IFirewall.CreateApplicationRule(
85104 string filename ,
86105 FirewallProtocol protocol )
87106 {
88- if ( ! IsSupported )
89- {
90- throw new NotSupportedException ( ) ;
91- }
92-
93107 if ( ! protocol . Equals ( FirewallProtocol . Any ) )
94108 {
95109 throw new FirewallLegacyNotSupportedException (
@@ -170,18 +184,13 @@ IFirewallRule IFirewall.CreatePortRule(
170184 FirewallProtocol protocol
171185 )
172186 {
173- if ( ! IsSupported )
174- {
175- throw new NotSupportedException ( ) ;
176- }
177-
178187 if ( action != FirewallAction . Allow )
179188 {
180189 throw new FirewallLegacyNotSupportedException (
181190 "Windows Firewall Legacy only accepts allow exception rules." ) ;
182191 }
183192
184- return new FirewallLegacyPortRule ( name , portNumber , profiles ) { Protocol = protocol } ;
193+ return new FirewallLegacyPortRule ( name , portNumber , profiles , TypeResolver ) { Protocol = protocol } ;
185194 }
186195
187196 /// <inheritdoc />
@@ -282,12 +291,7 @@ public FirewallLegacyApplicationRule CreateApplicationRule(
282291 string filename
283292 )
284293 {
285- if ( ! IsSupported )
286- {
287- throw new NotSupportedException ( ) ;
288- }
289-
290- return new FirewallLegacyApplicationRule ( name , filename , profiles ) ;
294+ return new FirewallLegacyApplicationRule ( name , filename , profiles , TypeResolver ) ;
291295 }
292296
293297 /// <summary>
@@ -301,19 +305,14 @@ public FirewallLegacyApplicationRule CreateApplicationRule(
301305 string filename
302306 )
303307 {
304- if ( ! IsSupported )
305- {
306- throw new NotSupportedException ( ) ;
307- }
308-
309308 var activeProfile = GetActiveProfile ( ) ;
310309
311310 if ( activeProfile == null )
312311 {
313312 throw new InvalidOperationException ( "No firewall profile is currently active." ) ;
314313 }
315314
316- return new FirewallLegacyApplicationRule ( name , filename , activeProfile . Type ) ;
315+ return new FirewallLegacyApplicationRule ( name , filename , activeProfile . Type , TypeResolver ) ;
317316 }
318317
319318 /// <summary>
@@ -325,12 +324,7 @@ string filename
325324 /// <returns>Returns the newly created <see cref="FirewallLegacyPortRule" /> instance</returns>
326325 public FirewallLegacyPortRule CreatePortRule ( FirewallProfiles profiles , string name , ushort portNumber )
327326 {
328- if ( ! IsSupported )
329- {
330- throw new NotSupportedException ( ) ;
331- }
332-
333- return new FirewallLegacyPortRule ( name , portNumber , profiles ) { Protocol = FirewallProtocol . TCP } ;
327+ return new FirewallLegacyPortRule ( name , portNumber , profiles , TypeResolver ) { Protocol = FirewallProtocol . TCP } ;
334328 }
335329
336330 /// <summary>
@@ -342,19 +336,14 @@ public FirewallLegacyPortRule CreatePortRule(FirewallProfiles profiles, string n
342336 /// <returns>Returns the newly created <see cref="FirewallLegacyPortRule" /> instance</returns>
343337 public FirewallLegacyPortRule CreatePortRule ( string name , ushort portNumber )
344338 {
345- if ( ! IsSupported )
346- {
347- throw new NotSupportedException ( ) ;
348- }
349-
350339 var activeProfile = GetActiveProfile ( ) ;
351340
352341 if ( activeProfile == null )
353342 {
354343 throw new InvalidOperationException ( "No firewall profile is currently active." ) ;
355344 }
356345
357- return new FirewallLegacyPortRule ( name , portNumber , activeProfile . Type ) { Protocol = FirewallProtocol . TCP } ;
346+ return new FirewallLegacyPortRule ( name , portNumber , activeProfile . Type , TypeResolver ) { Protocol = FirewallProtocol . TCP } ;
358347 }
359348
360349 /// <summary>
0 commit comments