Let me start about Why...
Why
Since most people agree that the most effective way of blocking Ip's is by using a solution like iptables. So with that in mind why would i wanna block such a huge list directly in Apache. As a maintainer of servers and after investigating "unwanted traffic" trying to do all kinds of things that the online service isnt made for i started looking for a solution to stop that once and for all....
So in the past 2 years i identified all/most possible networks that produce "unwanted traffic". These networks have no use on the online service at all and are bassicly bad for the environment when u look at all the energy and resources that they consume!
So in my attempt to remove all the power from these services i made a list of all worldwide hosting addresses and bussines connections that are outside of the targetted area from the online service (which means all business connections unless they are inside 2 targetted countries and all hosting addresses).
This resulted in almost 300k subnets which after combining the subnets resulted in almost 290k subnets left that i wanna block.
When using this list in a solution like iptables it would also block updating services so to prevent this it's better to use apache since it will then only affect apache and nothing else on this server, protecting an entire server is not inside the scope of this question.
Possible solutions
Adding the subnets to .htaccess (Don't rlly wanna go there since this will make apache reload the list on every request)
Using
RewriteEngine on
RewriteMap hosts-deny "txt:/path/to/hosts.deny"
RewriteCond "${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND}" "!=NOT-FOUND" [OR]
RewriteCond "${hosts-deny:%{REMOTE_HOST}|NOT-FOUND}" "!=NOT-FOUND"
RewriteRule "^" "-" [F]
Works very well when using single addresses but converting 300k subnets to individual addresses doens't look very appealing to me, so unless this can be transformed to work with subnets this isn't going to work either.
Using expressions
RewriteCond expr "-R '12.345.678.90/28'"
This works untill i try to add more then 1 subnet, i think it goes wrong with RequireAll
, RequireAny
, RequireNone
or the location it is placed.
Hopefully someone can shed a light on this and guide me towards the right way of implementing my solution.