@@ -93,7 +93,7 @@ static int Main(string[] args)
9393 switch ( args [ 0 ] . ToLower ( ) )
9494 {
9595 case "/apply" :
96- return ApplyRules ( ) ;
96+ return ApplyRules ( IPVersion . Any ) ;
9797 case "/clear" :
9898 return ClearRules ( ) ;
9999 case "/config" :
@@ -106,6 +106,9 @@ static int Main(string[] args)
106106 {
107107 switch ( args [ 0 ] . ToLower ( ) )
108108 {
109+ case "/apply" :
110+ IPVersion v = ParseIPVersion ( args . Skip ( 1 ) . FirstOrDefault ( ) ) ;
111+ return ApplyRules ( v ) ;
109112 case "/remove" :
110113 return RemoveCacheItem ( args . Skip ( 1 ) ) ;
111114 case "/add" :
@@ -119,6 +122,32 @@ static int Main(string[] args)
119122 return ERR . ARGS ;
120123 }
121124
125+ /// <summary>
126+ /// Parses the string "v4" or "v6" into an <see cref="IPVersion"/>
127+ /// </summary>
128+ /// <param name="Arg">IP version string</param>
129+ /// <returns>
130+ /// <see cref="IPVersion"/> value.
131+ /// <see cref="IPVersion.Any"/> if no argument supplied or empty.
132+ /// <see cref="IPVersion.None"/> if argument supplied and invalid.
133+ /// </returns>
134+ private static IPVersion ParseIPVersion ( string Arg )
135+ {
136+ if ( string . IsNullOrEmpty ( Arg ) )
137+ {
138+ return IPVersion . Any ;
139+ }
140+ if ( Arg . ToLower ( ) == "v4" )
141+ {
142+ return IPVersion . V4 ;
143+ }
144+ if ( Arg . ToLower ( ) == "v6" )
145+ {
146+ return IPVersion . V6 ;
147+ }
148+ return IPVersion . None ;
149+ }
150+
122151 /// <summary>
123152 /// Clears firewall rules
124153 /// </summary>
@@ -133,7 +162,7 @@ private static int ClearRules()
133162 /// <summary>
134163 /// Applies firewall rules
135164 /// </summary>
136- private static int ApplyRules ( )
165+ private static int ApplyRules ( IPVersion Version )
137166 {
138167 Log ( "Applying firewall Rules..." ) ;
139168 try
@@ -142,14 +171,19 @@ private static int ApplyRules()
142171 . Select ( m => new RangeSet ( )
143172 {
144173 Direction = m . Direction ,
145- Ranges = Cache . GetAddresses ( m ) . Select ( n => new CIDR ( n , true ) ) . ToArray ( )
174+ Ranges = Cache
175+ . GetAddresses ( m )
176+ . Select ( n => new CIDR ( n , true ) )
177+ . Where ( n => Version . HasFlag ( n . Type ) )
178+ . ToArray ( )
146179 } )
180+ . Where ( m => m . Ranges . Length > 0 )
147181 . ToArray ( ) ;
148182 Debug ( "Clearing existing firewall rules..." ) ;
149- Firewall . ClearRules ( ) ;
183+ Debug ( "Removed {0} rules..." , Firewall . ClearRules ( ) ) ;
150184 Debug ( "Adding new rules..." ) ;
151185 Firewall . BlockRanges ( FWRanges ) ;
152- Log ( "Blocked {0} ranges" , FWRanges . SelectMany ( m => m . Ranges ) . Count ( ) ) ;
186+ Log ( "Blocked {0} ranges in {1} rules " , FWRanges . SelectMany ( m => m . Ranges ) . Count ( ) , FWRanges . Length ) ;
153187 }
154188 catch ( Exception ex )
155189 {
@@ -382,7 +416,7 @@ private static bool GetCache()
382416
383417 private static void ShowHelp ( )
384418 {
385- Console . Error . WriteLine ( @"AnyBlock.exe [/v] [/clear | /config | /add dir name | /remove name | /apply | /list | /export <format>]
419+ Console . Error . WriteLine ( @"AnyBlock.exe [/v] [/clear | /config | /add dir name | /remove name | /apply [v{4|6}] | /list | /export <format>]
386420Blocks IP ranges in the Windows firewall
387421
388422Shows a graphical configuration window if no arguments are specified.
@@ -424,11 +458,14 @@ To remove TOR exit nodes you would use the arguments /remove tor tor Exit
424458You can only remove one entry at a time.
425459To remove all entries, simply delete the 'settings.json' file
426460
427- /apply
461+ /apply [v{4|6}]
428462Applying the List will remove all blocked IPs that are no longer in the
429463current list of addresses.
430464To get most out of this command, schedule this as a task to be run every
43146524 hours.
466+ You can optionally specify to only add IPv4 or IPv6 addresses.
467+ This will reduce the number of rules drastically
468+ if you're only reachable via one protocol.
432469
433470/clear
434471Removes all rules from the firewall without deleting them from the settings.
0 commit comments