blockinst.sh: added option for nginx

This commit is contained in:
Gitea 2019-02-22 05:02:31 -05:00
parent ea5b3bea5f
commit d9266972a9
3 changed files with 54 additions and 17 deletions

View file

@ -1,8 +0,0 @@
Copyright (c) <year> <owner> . All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software must display the following acknowledgement:
This product includes software developed by the organization .
4. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

63
blockinst.sh Normal file → Executable file
View file

@ -1,28 +1,73 @@
#!/usr/bin/env bash
### Takes a list of instances (one per line), resolves their IPs,
### and creates a file with the IP as the name in a specified dir.
### Be sure to toss the relevant config in your CaddyFile
###
### Takes a list of instances (one per line), resolves their IPs,
### and creates a blocklist your server understands
###
### Caddy example:
### ipfilter / {
### rule block
### prefix_dir blocks # folder relative to working dir
### blockpage www/blocked.html # optional block page
### }
###
### Nginx example: (not necessary if running debian or a debian derivative)
### http {
### ...
### include blockips.conf;
### ...
### }
###
# blocklist location
filename="$HOME/.config/blocked.txt"
# Which web server are you using? [nginx|caddy]
srv="nginx"
# Config locations. Ignore any you don't need
confCaddy="$HOME/.config/caddy/blocks"
confNginx="/etc/nginx/conf.d/blocks.conf"
# The DNS server to use for the domain lookups
# A local dnsmasq server is fine
dnsServer="208.67.222.222"
### ----------------------------------------------------
### Don't edit anything below unless you know what you're doing
### -----------------------------------------------------
# clear blocks if using nginx
if [ "$srv" == "nginx" ]; then
sudo rm $confNginx
sudo touch $confNginx
fi
# read blocklist
while read domain; do
rawip=$(dig +short $domain @192.168.2.2 | grep '^[.0-9]*$')
if [ -z "$rawip" ]
then
echo "$domain: missing"
# dns lookup
rawip=$(dig +short $domain @$dnsServer | grep '^[.0-9]*$')
# just say the domain isn't found of lookup fails
if [ -z "$rawip" ]; then
echo "$domain: not found"
else
# read each ip in dig's output
for ip in $rawip; do
echo "$domain: $ip"
echo "$domain" > $HOME/.config/caddy/blocks/$ip
# create block files for caddy
if [ "$srv" == "caddy" ]; then
echo "$domain" > $confCaddy/$ip
# create block file for nginx
elif [ "$srv" == "nginx" ]; then
echo "deny $ip;" | sudo tee -a $confNginx > /dev/null
# configured web server not supported
else
echo "Server unsupported or mis-spelt! Check the config and try again."
echo "[ caddy | nginx ]"
fi
done
fi
done < $filename

0
debloat.sh Normal file → Executable file
View file