|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set +x |
| 4 | +set -e |
| 5 | + |
| 6 | +DEFAULT_ADRESS_FILE=ips |
| 7 | +echo "INTERNAL_PORT=${INTERNAL_PORT:=9701}" |
| 8 | +echo "CLI_PORT=${CLI_PORT:=9702}" |
| 9 | +echo "CHAIN=${CHAIN:=DOCKER-USER}" |
| 10 | +echo "MAX_CONN=${MAX_CONN:=500}" |
| 11 | + |
| 12 | +usage() { |
| 13 | + echo |
| 14 | + echo "Usage:" |
| 15 | + echo -n "INTERFACE=[your_network_interface] IP_FILE=[path_to_ip_addresses_file, defaults to $DEFAULT_ADRESS_FILE] " |
| 16 | + echo -n "INTERNAL_PORT[default 9701] CLI_PORT=[default 9702] CHAIN[default DOCKER-USER] MAX_CONN[default 500]" |
| 17 | + echo "$0" |
| 18 | + echo |
| 19 | + echo "This script will add rules to your ip tables chain CHAIN to allow incoming connections on port INTERNAL_PORT" |
| 20 | + echo "only from ips listed in the IP_FILE. It will also restrict the number of connections to port CLI_PORT to MAX_CONN." |
| 21 | + echo |
| 22 | + echo "The ip adresses file should contain the list of nodes" |
| 23 | + echo "in your network. One ip address per line." |
| 24 | + echo "The network interface should be the physical one used for incoming connections from the internet" |
| 25 | + echo |
| 26 | + echo "This script needs to be run as root/via sudo." |
| 27 | + echo |
| 28 | +} |
| 29 | + |
| 30 | +# skip existing rules to avoid duplicates |
| 31 | +add_new_rule() { |
| 32 | + RULE="$@" |
| 33 | + |
| 34 | + if iptables -C $RULE 2>/dev/null 1>&2; then |
| 35 | + echo "[skip] $RULE already exists" |
| 36 | + elif [[ "$RULE" == *"DROP"* ]] || [[ "$RULE" == *"RETURN"* ]]; then |
| 37 | + iptables -A $RULE |
| 38 | + echo "[ok] $RULE added to the end of the chain" |
| 39 | + else |
| 40 | + iptables -I $RULE |
| 41 | + echo "[ok] $RULE added to the beginning of the chain" |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +make_last_rule() { |
| 46 | + RULE="$@" |
| 47 | + while iptables -C $RULE 2>/dev/null 1>&2; do |
| 48 | + iptables -D $RULE |
| 49 | + echo "[ok] $RULE deleted" |
| 50 | + done |
| 51 | + iptables -A $RULE |
| 52 | + echo "[ok] $RULE added to the end of the chain" |
| 53 | +} |
| 54 | + |
| 55 | +# -h --help --whatever |
| 56 | +if ! [ -z "$*" ]; then |
| 57 | + usage |
| 58 | + exit 0 |
| 59 | +fi |
| 60 | + |
| 61 | +echo "INTERFACE=${INTERFACE:=ens18}" |
| 62 | + |
| 63 | +# check if INTERFACE is set to an inet facing interface |
| 64 | +if ! ip a | grep inet | grep "$INTERFACE" >/dev/null; then |
| 65 | + echo "interface '$INTERFACE' does not seem to be an internet facing interface" |
| 66 | + usage |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | + |
| 70 | +echo "IP_FILE=${IP_FILE:=$DEFAULT_ADRESS_FILE}" |
| 71 | + |
| 72 | +if ! [ -f "$IP_FILE" ]; then |
| 73 | + echo "file '$IP_FILE' not found" |
| 74 | + usage |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +# 9701 whitelist approach: drop all others INCOMING (-i) connections |
| 79 | +add_new_rule $CHAIN -p tcp -i $INTERFACE --dport $INTERNAL_PORT -j DROP |
| 80 | + |
| 81 | +# 9701 create IP whitelist from file |
| 82 | +while read IP; do |
| 83 | + if [[ "$IP" != "#"* ]] && [[ "$IP" != "" ]]; then |
| 84 | + add_new_rule $CHAIN -p tcp --dport $INTERNAL_PORT -s $IP -j ACCEPT |
| 85 | + fi |
| 86 | +done <"$IP_FILE" |
| 87 | + |
| 88 | +# 9702 connlimit |
| 89 | +add_new_rule $CHAIN -p tcp --syn --dport $CLI_PORT -m connlimit --connlimit-above $MAX_CONN -j REJECT |
| 90 | + |
| 91 | +# make sure, RETURN ist the last rule |
| 92 | +make_last_rule $CHAIN -j RETURN |
0 commit comments